feat: 完成错误信息输出
This commit is contained in:
@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
@@ -31,16 +32,19 @@ public class AiChatService : ApplicationService
|
||||
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
|
||||
private readonly AiBlacklistManager _aiBlacklistManager;
|
||||
private readonly UsageStatisticsManager _usageStatisticsManager;
|
||||
private readonly ILogger<AiChatService> _logger;
|
||||
|
||||
public AiChatService(IHttpContextAccessor httpContextAccessor,
|
||||
AiMessageManager aiMessageManager, AiBlacklistManager aiBlacklistManager,
|
||||
ISqlSugarRepository<AiModelEntity> aiModelRepository, UsageStatisticsManager usageStatisticsManager)
|
||||
ISqlSugarRepository<AiModelEntity> aiModelRepository, UsageStatisticsManager usageStatisticsManager,
|
||||
ILogger<AiChatService> logger)
|
||||
{
|
||||
this._httpContextAccessor = httpContextAccessor;
|
||||
_aiMessageManager = aiMessageManager;
|
||||
_aiBlacklistManager = aiBlacklistManager;
|
||||
_aiModelRepository = aiModelRepository;
|
||||
_usageStatisticsManager = usageStatisticsManager;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
@@ -165,6 +169,9 @@ public class AiChatService : ApplicationService
|
||||
}, cancellationToken);
|
||||
|
||||
|
||||
//IAsyncEnumerable 只能在最外层捕获异常(如果你有其他办法的话...)
|
||||
try
|
||||
{
|
||||
await foreach (var data in completeChatResponse)
|
||||
{
|
||||
if (data.IsFinish)
|
||||
@@ -181,7 +188,19 @@ public class AiChatService : ApplicationService
|
||||
// 将消息加入队列而不是直接写入
|
||||
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");
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user