fix: 修复Anthropic接口TokenUsage序列化及HttpClient创建方式问题
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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<string, string>
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user