fix: 记录使用量与错误信息时保留原始模型ID
在模型别名(yi-)转换场景下,统一使用 sourceModelId 记录消息、用量统计及异常信息,避免因模型ID被覆盖导致统计与日志不准确。
This commit is contained in:
@@ -515,6 +515,8 @@ public class AiGateWayManager : DomainService
|
|||||||
// 设置响应头,声明是 json
|
// 设置响应头,声明是 json
|
||||||
//response.ContentType = "application/json; charset=UTF-8";
|
//response.ContentType = "application/json; charset=UTF-8";
|
||||||
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
|
var modelDescribe = await GetModelAsync(ModelApiTypeEnum.Claude, request.Model);
|
||||||
|
|
||||||
|
var sourceModelId = request.Model;
|
||||||
if (!string.IsNullOrEmpty(request.Model) &&
|
if (!string.IsNullOrEmpty(request.Model) &&
|
||||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
@@ -533,7 +535,7 @@ public class AiGateWayManager : DomainService
|
|||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = "不予存储",
|
Content = "不予存储",
|
||||||
ModelId = request.Model,
|
ModelId = sourceModelId,
|
||||||
TokenUsage = data.TokenUsage,
|
TokenUsage = data.TokenUsage,
|
||||||
}, tokenId);
|
}, tokenId);
|
||||||
|
|
||||||
@@ -541,11 +543,11 @@ public class AiGateWayManager : DomainService
|
|||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = "不予存储",
|
Content = "不予存储",
|
||||||
ModelId = request.Model,
|
ModelId = sourceModelId,
|
||||||
TokenUsage = data.TokenUsage
|
TokenUsage = data.TokenUsage
|
||||||
}, tokenId);
|
}, tokenId);
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId.Value, request.Model, data.TokenUsage, tokenId);
|
await _usageStatisticsManager.SetUsageAsync(userId.Value, sourceModelId, data.TokenUsage, tokenId);
|
||||||
|
|
||||||
// 扣减尊享token包用量
|
// 扣减尊享token包用量
|
||||||
var totalTokens = data.TokenUsage.TotalTokens ?? 0;
|
var totalTokens = data.TokenUsage.TotalTokens ?? 0;
|
||||||
@@ -588,6 +590,7 @@ public class AiGateWayManager : DomainService
|
|||||||
var chatService =
|
var chatService =
|
||||||
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
|
||||||
|
|
||||||
|
var sourceModelId = request.Model;
|
||||||
if (!string.IsNullOrEmpty(request.Model) &&
|
if (!string.IsNullOrEmpty(request.Model) &&
|
||||||
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
request.Model.StartsWith("yi-", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
@@ -616,7 +619,7 @@ public class AiGateWayManager : DomainService
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
_logger.LogError(e, $"Ai对话异常");
|
_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);
|
throw new UserFriendlyException(errorContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,7 +627,7 @@ public class AiGateWayManager : DomainService
|
|||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = "不予存储",
|
Content = "不予存储",
|
||||||
ModelId = request.Model,
|
ModelId = sourceModelId,
|
||||||
TokenUsage = tokenUsage,
|
TokenUsage = tokenUsage,
|
||||||
}, tokenId);
|
}, tokenId);
|
||||||
|
|
||||||
@@ -632,11 +635,11 @@ public class AiGateWayManager : DomainService
|
|||||||
new MessageInputDto
|
new MessageInputDto
|
||||||
{
|
{
|
||||||
Content = "不予存储",
|
Content = "不予存储",
|
||||||
ModelId = request.Model,
|
ModelId = sourceModelId,
|
||||||
TokenUsage = tokenUsage
|
TokenUsage = tokenUsage
|
||||||
}, tokenId);
|
}, tokenId);
|
||||||
|
|
||||||
await _usageStatisticsManager.SetUsageAsync(userId, request.Model, tokenUsage, tokenId);
|
await _usageStatisticsManager.SetUsageAsync(userId, sourceModelId, tokenUsage, tokenId);
|
||||||
|
|
||||||
// 扣减尊享token包用量
|
// 扣减尊享token包用量
|
||||||
if (userId.HasValue && tokenUsage is not null)
|
if (userId.HasValue && tokenUsage is not null)
|
||||||
|
|||||||
Reference in New Issue
Block a user