feat: 完成倍率的配置化

This commit is contained in:
chenchun
2025-11-25 09:54:13 +08:00
parent 4c65b2398d
commit 688d93e5c1
7 changed files with 24 additions and 12 deletions

View File

@@ -56,4 +56,9 @@ public class AiModelDescribe
/// 模型额外信息 /// 模型额外信息
/// </summary> /// </summary>
public string? ModelExtraInfo { get; set; } public string? ModelExtraInfo { get; set; }
/// <summary>
/// 模型倍率
/// </summary>
public decimal Multiplier { get; set; }
} }

View File

@@ -34,7 +34,7 @@ public class AnthropicStreamDto
}; };
public void SupplementalMultiplier(double multiplier) public void SupplementalMultiplier(decimal multiplier)
{ {
if (this.Usage is not null) if (this.Usage is not null)
{ {
@@ -130,7 +130,7 @@ public class AnthropicChatCompletionDto
CompletionTokensDetails = null CompletionTokensDetails = null
}; };
public void SupplementalMultiplier(double multiplier) public void SupplementalMultiplier(decimal multiplier)
{ {
if (this.Usage is not null) if (this.Usage is not null)
{ {

View File

@@ -61,7 +61,7 @@ public record ThorChatCompletionsResponse
[JsonPropertyName("error")] [JsonPropertyName("error")]
public ThorError? Error { get; set; } public ThorError? Error { get; set; }
public void SupplementalMultiplier(double multiplier) public void SupplementalMultiplier(decimal multiplier)
{ {
if (this.Usage is not null) if (this.Usage is not null)
{ {

View File

@@ -14,8 +14,6 @@ public class AnthropicChatCompletionsService(
ILogger<AnthropicChatCompletionsService> logger) ILogger<AnthropicChatCompletionsService> logger)
: IAnthropicChatCompletionService : IAnthropicChatCompletionService
{ {
public const double ClaudeMultiplier = 1.3d;
public async Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe options, AnthropicInput input, public async Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe options, AnthropicInput input,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
@@ -86,8 +84,7 @@ public class AnthropicChatCompletionsService(
var value = var value =
await response.Content.ReadFromJsonAsync<AnthropicChatCompletionDto>(ThorJsonSerializer.DefaultOptions, await response.Content.ReadFromJsonAsync<AnthropicChatCompletionDto>(ThorJsonSerializer.DefaultOptions,
cancellationToken: cancellationToken); cancellationToken: cancellationToken);
value.SupplementalMultiplier(AnthropicChatCompletionsService.ClaudeMultiplier);
return value; return value;
} }
@@ -169,8 +166,7 @@ public class AnthropicChatCompletionsService(
var result = JsonSerializer.Deserialize<AnthropicStreamDto>(data, var result = JsonSerializer.Deserialize<AnthropicStreamDto>(data,
ThorJsonSerializer.DefaultOptions); ThorJsonSerializer.DefaultOptions);
result.SupplementalMultiplier(AnthropicChatCompletionsService.ClaudeMultiplier);
yield return (eventType, result); yield return (eventType, result);
} }
} }

View File

@@ -716,7 +716,7 @@ public sealed class ClaudiaChatCompletionsService(
output.Usage.PromptTokens = output.Usage.InputTokens; output.Usage.PromptTokens = output.Usage.InputTokens;
output.Usage.CompletionTokens = output.Usage.OutputTokens; output.Usage.CompletionTokens = output.Usage.OutputTokens;
output.Usage.TotalTokens = output.Usage.InputTokens + output.Usage.OutputTokens; output.Usage.TotalTokens = output.Usage.InputTokens + output.Usage.OutputTokens;
output.SupplementalMultiplier(AnthropicChatCompletionsService.ClaudeMultiplier);
yield return output; yield return output;
} }
} }
@@ -873,7 +873,6 @@ public sealed class ClaudiaChatCompletionsService(
} }
thor.Usage.TotalTokens = thor.Usage.InputTokens + thor.Usage.OutputTokens; thor.Usage.TotalTokens = thor.Usage.InputTokens + thor.Usage.OutputTokens;
thor.SupplementalMultiplier(AnthropicChatCompletionsService.ClaudeMultiplier);
return thor; return thor;
} }
} }

View File

@@ -60,4 +60,9 @@ public class AiModelEntity : Entity<Guid>, IOrderNum, ISoftDelete
/// 模型Api类型现支持同一个模型id多种接口格式 /// 模型Api类型现支持同一个模型id多种接口格式
/// </summary> /// </summary>
public ModelApiTypeEnum ModelApiType { get; set; } public ModelApiTypeEnum ModelApiType { get; set; }
/// <summary>
/// 模型倍率
/// </summary>
public decimal Multiplier { get; set; } = 1;
} }

View File

@@ -77,7 +77,8 @@ public class AiGateWayManager : DomainService
ModelName = model.Name, ModelName = model.Name,
Description = model.Description, Description = model.Description,
AppExtraUrl = app.ExtraUrl, AppExtraUrl = app.ExtraUrl,
ModelExtraInfo = model.ExtraInfo ModelExtraInfo = model.ExtraInfo,
Multiplier = model.Multiplier
}) })
.FirstAsync(); .FirstAsync();
if (aiModelDescribe is null) if (aiModelDescribe is null)
@@ -106,6 +107,7 @@ public class AiGateWayManager : DomainService
await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken)) await foreach (var result in chatService.CompleteChatStreamAsync(modelDescribe, request, cancellationToken))
{ {
result.SupplementalMultiplier(modelDescribe.Multiplier);
yield return result; yield return result;
} }
} }
@@ -134,6 +136,7 @@ public class AiGateWayManager : DomainService
var chatService = var chatService =
LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName); LazyServiceProvider.GetRequiredKeyedService<IChatCompletionService>(modelDescribe.HandlerName);
var data = await chatService.CompleteChatAsync(modelDescribe, request, cancellationToken); var data = await chatService.CompleteChatAsync(modelDescribe, request, cancellationToken);
data.SupplementalMultiplier(modelDescribe.Multiplier);
if (userId is not null) if (userId is not null)
{ {
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId, await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,
@@ -506,6 +509,7 @@ public class AiGateWayManager : DomainService
await foreach (var result in chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken)) await foreach (var result in chatService.StreamChatCompletionsAsync(modelDescribe, request, cancellationToken))
{ {
result.Item2.SupplementalMultiplier(modelDescribe.Multiplier);
yield return result; yield return result;
} }
} }
@@ -534,6 +538,9 @@ public class AiGateWayManager : DomainService
var chatService = var chatService =
LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName); LazyServiceProvider.GetRequiredKeyedService<IAnthropicChatCompletionService>(modelDescribe.HandlerName);
var data = await chatService.ChatCompletionsAsync(modelDescribe, request, cancellationToken); var data = await chatService.ChatCompletionsAsync(modelDescribe, request, cancellationToken);
data.SupplementalMultiplier(modelDescribe.Multiplier);
if (userId is not null) if (userId is not null)
{ {
await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId, await _aiMessageManager.CreateUserMessageAsync(userId.Value, sessionId,