From 5934056fe62e0a28ff48b957af197674af9903f2 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Sun, 12 Oct 2025 14:38:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DAnthropic=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3TokenUsage=E5=BA=8F=E5=88=97=E5=8C=96=E5=8F=8AHttpClie?= =?UTF-8?q?nt=E5=88=9B=E5=BB=BA=E6=96=B9=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/Anthropic/AnthropicChatCompletionDto.cs | 2 ++ .../ThorClaude/Chats/AnthropicChatCompletionsService.cs | 2 +- .../Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs | 6 +++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain.Shared/Dtos/Anthropic/AnthropicChatCompletionDto.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain.Shared/Dtos/Anthropic/AnthropicChatCompletionDto.cs index ee2e993a..f505bc32 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain.Shared/Dtos/Anthropic/AnthropicChatCompletionDto.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain.Shared/Dtos/Anthropic/AnthropicChatCompletionDto.cs @@ -19,6 +19,7 @@ public class AnthropicStreamDto [JsonPropertyName("error")] public AnthropicStreamErrorDto? Error { get; set; } + [JsonIgnore] public ThorUsageResponse TokenUsage => new ThorUsageResponse { PromptTokens = Usage?.InputTokens + Usage?.CacheCreationInputTokens + Usage?.CacheReadInputTokens, @@ -94,6 +95,7 @@ public class AnthropicChatCompletionDto public AnthropicCompletionDtoUsage Usage { get; set; } + [JsonIgnore] public ThorUsageResponse TokenUsage => new ThorUsageResponse { PromptTokens = Usage?.InputTokens + Usage?.CacheCreationInputTokens + Usage?.CacheReadInputTokens, diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiGateWay/Impl/ThorClaude/Chats/AnthropicChatCompletionsService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiGateWay/Impl/ThorClaude/Chats/AnthropicChatCompletionsService.cs index 47746cdb..7573bfec 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiGateWay/Impl/ThorClaude/Chats/AnthropicChatCompletionsService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiGateWay/Impl/ThorClaude/Chats/AnthropicChatCompletionsService.cs @@ -96,7 +96,7 @@ public class AnthropicChatCompletionsService(IHttpClientFactory httpClientFactor options.Endpoint = "https://api.anthropic.com/"; } - var client = HttpClientFactory.GetHttpClient(options.Endpoint); + var client = httpClientFactory.CreateClient(); var headers = new Dictionary { 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 d753cf17..8905f2c0 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 @@ -546,7 +546,11 @@ public class AiGateWayManager : DomainService { await foreach (var responseResult in completeChatResponse) { - tokenUsage = responseResult.Item2?.TokenUsage; + //message_start是为了保底机制 + if (responseResult.Item1.Contains("message_delta")||responseResult.Item1.Contains("message_start")) + { + tokenUsage = responseResult.Item2?.TokenUsage; + } backupSystemContent.Append(responseResult.Item2?.Delta?.Text); await WriteAsEventStreamDataAsync(httpContext, responseResult.Item1, responseResult.Item2, cancellationToken);