From 44b2ade9bc9e9436f27eb95a517889d8a9fe1bf2 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Wed, 2 Jul 2025 00:28:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/AiChatService.cs | 39 ++++++++++++++----- .../AiChat/ChatErrorException.cs | 31 +++++++++++++++ 2 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/ChatErrorException.cs diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs index 9952820a..98912870 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiChatService.cs @@ -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 _aiModelRepository; private readonly AiBlacklistManager _aiBlacklistManager; private readonly UsageStatisticsManager _usageStatisticsManager; + private readonly ILogger _logger; public AiChatService(IHttpContextAccessor httpContextAccessor, AiMessageManager aiMessageManager, AiBlacklistManager aiBlacklistManager, - ISqlSugarRepository aiModelRepository, UsageStatisticsManager usageStatisticsManager) + ISqlSugarRepository aiModelRepository, UsageStatisticsManager usageStatisticsManager, + ILogger logger) { this._httpContextAccessor = httpContextAccessor; _aiMessageManager = aiMessageManager; _aiBlacklistManager = aiBlacklistManager; _aiModelRepository = aiModelRepository; _usageStatisticsManager = usageStatisticsManager; + _logger = logger; } @@ -97,7 +101,7 @@ public class AiChatService : ApplicationService if (CurrentUser.IsAuthenticated) { await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId()); - if (!CurrentUser.Roles.Contains("YiXinAi-Vip")&&CurrentUser.UserName!="cc") + if (!CurrentUser.Roles.Contains("YiXinAi-Vip") && CurrentUser.UserName != "cc") { throw new UserFriendlyException("该模型需要VIP用户才能使用,请购买VIP后重新登录重试"); } @@ -165,24 +169,39 @@ public class AiChatService : ApplicationService }, cancellationToken); - await foreach (var data in completeChatResponse) + //IAsyncEnumerable 只能在最外层捕获异常(如果你有其他办法的话...) + try { - if (data.IsFinish) + await foreach (var data in completeChatResponse) { - tokenUsage = data.TokenUsage; - } + if (data.IsFinish) + { + tokenUsage = data.TokenUsage; + } - var model = MapToMessage(input.Model, data.Content); + var model = MapToMessage(input.Model, data.Content); + var message = JsonConvert.SerializeObject(model, new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }); + backupSystemContent.Append(data.Content); + // 将消息加入队列而不是直接写入 + 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(data.Content); - // 将消息加入队列而不是直接写入 + backupSystemContent.Append(errorContent); messageQueue.Enqueue($"data: {message}\n"); } - //断开连接 messageQueue.Enqueue("data: [DONE]\n"); // 标记完成并发送结束标记 diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/ChatErrorException.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/ChatErrorException.cs new file mode 100644 index 00000000..97cf38fa --- /dev/null +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/ChatErrorException.cs @@ -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; + } +} \ No newline at end of file