From bdaa53bac861f823710daeaa4eb1983d6393ee22 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Sun, 28 Dec 2025 01:04:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=AE=B0=E5=BD=95=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E9=87=8F=E4=B8=8E=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E6=97=B6?= =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=8E=9F=E5=A7=8B=E6=A8=A1=E5=9E=8BID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在模型别名(yi-)转换场景下,统一使用 sourceModelId 记录消息、用量统计及异常信息,避免因模型ID被覆盖导致统计与日志不准确。 --- .../Managers/AiGateWayManager.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs index d85ec770..971fef75 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs @@ -515,6 +515,8 @@ public class AiGateWayManager : DomainService // 设置响应头,声明是 json //response.ContentType = "application/json; charset=UTF-8"; var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model); + + var sourceModelId = request.Model; if (!string.IsNullOrEmpty(request.Model) && request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase)) { @@ -533,7 +535,7 @@ public class AiGateWayManager : DomainService new MessageInputDto { Content = "不予存储", - ModelId = request.Model, + ModelId = sourceModelId, TokenUsage = data.TokenUsage, }, tokenId); @@ -541,11 +543,11 @@ public class AiGateWayManager : DomainService new MessageInputDto { Content = "不予存储", - ModelId = request.Model, + ModelId = sourceModelId, TokenUsage = data.TokenUsage }, tokenId); - await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.TokenUsage, tokenId); + await _usageStatisticsManager.SetUsageAsync(userId.Value, sourceModelId, data.TokenUsage, tokenId); // 扣减尊享token包用量 var totalTokens = data.TokenUsage.TotalTokens ?? 0; @@ -588,6 +590,7 @@ public class AiGateWayManager : DomainService var chatService = LazyServiceProvider.GetRequiredKeyedService(modelDescribe.HandlerName); + var sourceModelId = request.Model; if (!string.IsNullOrEmpty(request.Model) && request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase)) { @@ -616,7 +619,7 @@ public class AiGateWayManager : DomainService catch (Exception e) { _logger.LogError(e, $"Ai对话异常"); - var errorContent = $"对话Ai异常,异常信息:\n当前Ai模型:{request.Model}\n异常信息:{e.Message}\n异常堆栈:{e}"; + var errorContent = $"对话Ai异常,异常信息:\n当前Ai模型:{sourceModelId}\n异常信息:{e.Message}\n异常堆栈:{e}"; throw new UserFriendlyException(errorContent); } @@ -624,7 +627,7 @@ public class AiGateWayManager : DomainService new MessageInputDto { Content = "不予存储", - ModelId = request.Model, + ModelId = sourceModelId, TokenUsage = tokenUsage, }, tokenId); @@ -632,11 +635,11 @@ public class AiGateWayManager : DomainService new MessageInputDto { Content = "不予存储", - ModelId = request.Model, + ModelId = sourceModelId, TokenUsage = tokenUsage }, tokenId); - await _usageStatisticsManager.SetUsageAsync(userId, request.Model, tokenUsage, tokenId); + await _usageStatisticsManager.SetUsageAsync(userId, sourceModelId, tokenUsage, tokenId); // 扣减尊享token包用量 if (userId.HasValue && tokenUsage is not null)