feat: 完成错误信息输出

This commit is contained in:
ccnetcore
2025-07-02 00:28:44 +08:00
parent 1200d02fbf
commit 44b2ade9bc
2 changed files with 60 additions and 10 deletions

View File

@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
@@ -31,16 +32,19 @@ public class AiChatService : ApplicationService
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository; private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
private readonly AiBlacklistManager _aiBlacklistManager; private readonly AiBlacklistManager _aiBlacklistManager;
private readonly UsageStatisticsManager _usageStatisticsManager; private readonly UsageStatisticsManager _usageStatisticsManager;
private readonly ILogger<AiChatService> _logger;
public AiChatService(IHttpContextAccessor httpContextAccessor, public AiChatService(IHttpContextAccessor httpContextAccessor,
AiMessageManager aiMessageManager, AiBlacklistManager aiBlacklistManager, AiMessageManager aiMessageManager, AiBlacklistManager aiBlacklistManager,
ISqlSugarRepository<AiModelEntity> aiModelRepository, UsageStatisticsManager usageStatisticsManager) ISqlSugarRepository<AiModelEntity> aiModelRepository, UsageStatisticsManager usageStatisticsManager,
ILogger<AiChatService> logger)
{ {
this._httpContextAccessor = httpContextAccessor; this._httpContextAccessor = httpContextAccessor;
_aiMessageManager = aiMessageManager; _aiMessageManager = aiMessageManager;
_aiBlacklistManager = aiBlacklistManager; _aiBlacklistManager = aiBlacklistManager;
_aiModelRepository = aiModelRepository; _aiModelRepository = aiModelRepository;
_usageStatisticsManager = usageStatisticsManager; _usageStatisticsManager = usageStatisticsManager;
_logger = logger;
} }
@@ -165,6 +169,9 @@ public class AiChatService : ApplicationService
}, cancellationToken); }, cancellationToken);
//IAsyncEnumerable 只能在最外层捕获异常(如果你有其他办法的话...
try
{
await foreach (var data in completeChatResponse) await foreach (var data in completeChatResponse)
{ {
if (data.IsFinish) if (data.IsFinish)
@@ -181,7 +188,19 @@ public class AiChatService : ApplicationService
// 将消息加入队列而不是直接写入 // 将消息加入队列而不是直接写入
messageQueue.Enqueue($"data: {message}\n"); messageQueue.Enqueue($"data: {message}\n");
} }
}
catch (Exception e)
{
_logger.LogError(e, $"Ai对话异常");
var errorContent = $"Ai对话异常异常信息\n{e.Message}";
var model = MapToMessage(input.Model, errorContent);
var message = JsonConvert.SerializeObject(model, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
backupSystemContent.Append(errorContent);
messageQueue.Enqueue($"data: {message}\n");
}
//断开连接 //断开连接
messageQueue.Enqueue("data: [DONE]\n"); messageQueue.Enqueue("data: [DONE]\n");

View File

@@ -0,0 +1,31 @@
using Microsoft.Extensions.Logging;
namespace Yi.Framework.AiHub.Domain.AiChat;
public class ChatErrorException : Exception
{
public string? Code { get; set; }
public string? Details { get; set; }
public LogLevel LogLevel { get; set; }
public ChatErrorException(
string? code = null,
string? message = null,
string? details = null,
Exception? innerException = null,
LogLevel logLevel = LogLevel.Warning)
: base(message, innerException)
{
this.Code = code;
this.Details = details;
this.LogLevel = logLevel;
}
public ChatErrorException WithData(string name, object value)
{
this.Data[(object)name] = value;
return this;
}
}