fix: 记录使用量与错误信息时保留原始模型ID

在模型别名(yi-)转换场景下,统一使用 sourceModelId 记录消息、用量统计及异常信息,避免因模型ID被覆盖导致统计与日志不准确。
This commit is contained in:
ccnetcore
2025-12-28 01:04:58 +08:00
parent e5b81c08f3
commit bdaa53bac8

View File

@@ -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<IAnthropicChatCompletionService>(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)