feat: 新增claude接口转换支持
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay;
|
||||
|
||||
public interface IAnthropicChatCompletionService
|
||||
{
|
||||
/// <summary>
|
||||
/// 非流式对话补全
|
||||
/// </summary>
|
||||
/// <param name="request">对话补全请求参数对象</param>
|
||||
/// <param name="aiModelDescribe">平台参数对象</param>
|
||||
/// <param name="cancellationToken">取消令牌</param>
|
||||
/// <returns></returns>
|
||||
Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe aiModelDescribe,
|
||||
AnthropicInput request,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 流式对话补全
|
||||
/// </summary>
|
||||
/// <param name="request">对话补全请求参数对象</param>
|
||||
/// <param name="aiModelDescribe">平台参数对象</param>
|
||||
/// <param name="cancellationToken">取消令牌</param>
|
||||
/// <returns></returns>
|
||||
IAsyncEnumerable<(string, AnthropicStreamDto?)> StreamChatCompletionsAsync(AiModelDescribe aiModelDescribe,
|
||||
AnthropicInput request,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay;
|
||||
|
||||
public interface ISpecialCompatible
|
||||
{
|
||||
public void Compatible(ThorChatCompletionsRequest request);
|
||||
public void AnthropicCompatible(AnthropicInput request);
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorClaude.Chats;
|
||||
|
||||
public class AnthropicChatCompletionsService(IHttpClientFactory httpClientFactory,ILogger<AnthropicChatCompletionsService> logger)
|
||||
: IAnthropicChatCompletionService
|
||||
{
|
||||
public async Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe options, AnthropicInput input,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var openai =
|
||||
Activity.Current?.Source.StartActivity("Claudia 对话补全");
|
||||
|
||||
if (string.IsNullOrEmpty(options.Endpoint))
|
||||
{
|
||||
options.Endpoint = "https://api.anthropic.com/";
|
||||
}
|
||||
|
||||
var client = httpClientFactory.CreateClient();
|
||||
|
||||
var headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x-api-key", options.ApiKey },
|
||||
{ "authorization", "Bearer " + options.ApiKey },
|
||||
{ "anthropic-version", "2023-06-01" }
|
||||
};
|
||||
|
||||
|
||||
bool isThink = input.Model.EndsWith("-thinking");
|
||||
input.Model = input.Model.Replace("-thinking", string.Empty);
|
||||
|
||||
if (input.MaxTokens is < 2048)
|
||||
{
|
||||
input.MaxTokens = 2048;
|
||||
}
|
||||
|
||||
if (isThink && input.Thinking is null)
|
||||
{
|
||||
input.Thinking = new AnthropicThinkingInput()
|
||||
{
|
||||
Type = "enabled",
|
||||
BudgetTokens = 4000
|
||||
};
|
||||
}
|
||||
|
||||
if (input.Thinking is not null && input.Thinking.BudgetTokens > 0 && input.MaxTokens != null)
|
||||
{
|
||||
if (input.Thinking.BudgetTokens > input.MaxTokens)
|
||||
{
|
||||
input.Thinking.BudgetTokens = input.MaxTokens.Value - 1;
|
||||
if (input.Thinking.BudgetTokens > 63999)
|
||||
{
|
||||
input.Thinking.BudgetTokens = 63999;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var response =
|
||||
await client.PostJsonAsync(options.Endpoint.TrimEnd('/') + "/v1/messages", input, string.Empty, headers);
|
||||
|
||||
openai?.SetTag("Model", input.Model);
|
||||
openai?.SetTag("Response", response.StatusCode.ToString());
|
||||
|
||||
// 大于等于400的状态码都认为是异常
|
||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||
{
|
||||
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", options.Endpoint,
|
||||
response.StatusCode, error);
|
||||
|
||||
throw new Exception("OpenAI对话异常" + response.StatusCode.ToString());
|
||||
}
|
||||
|
||||
var value =
|
||||
await response.Content.ReadFromJsonAsync<AnthropicChatCompletionDto>(ThorJsonSerializer.DefaultOptions,
|
||||
cancellationToken: cancellationToken);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<(string, AnthropicStreamDto?)> StreamChatCompletionsAsync(AiModelDescribe options, AnthropicInput input,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var openai =
|
||||
Activity.Current?.Source.StartActivity("Claudia 对话补全");
|
||||
|
||||
if (string.IsNullOrEmpty(options.Endpoint))
|
||||
{
|
||||
options.Endpoint = "https://api.anthropic.com/";
|
||||
}
|
||||
|
||||
var client = HttpClientFactory.GetHttpClient(options.Endpoint);
|
||||
|
||||
var headers = new Dictionary<string, string>
|
||||
{
|
||||
{ "x-api-key", options.ApiKey },
|
||||
{ "authorization", options.ApiKey },
|
||||
{ "anthropic-version", "2023-06-01" }
|
||||
};
|
||||
|
||||
var isThinking = input.Model.EndsWith("thinking");
|
||||
input.Model = input.Model.Replace("-thinking", string.Empty);
|
||||
|
||||
var response = await client.HttpRequestRaw(options.Endpoint.TrimEnd('/') + "/v1/messages", input, string.Empty,
|
||||
headers);
|
||||
|
||||
openai?.SetTag("Model", input.Model);
|
||||
openai?.SetTag("Response", response.StatusCode.ToString());
|
||||
|
||||
// 大于等于400的状态码都认为是异常
|
||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||
{
|
||||
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", options.Endpoint,
|
||||
response.StatusCode, error);
|
||||
|
||||
throw new Exception("OpenAI对话异常" + response.StatusCode);
|
||||
}
|
||||
|
||||
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));
|
||||
|
||||
using StreamReader reader = new(await response.Content.ReadAsStreamAsync(cancellationToken));
|
||||
string? line = string.Empty;
|
||||
|
||||
string? data = null;
|
||||
string eventType = string.Empty;
|
||||
|
||||
while ((line = await reader.ReadLineAsync(cancellationToken).ConfigureAwait(false)) != null)
|
||||
{
|
||||
line += Environment.NewLine;
|
||||
|
||||
if (line.StartsWith('{'))
|
||||
{
|
||||
logger.LogInformation("OpenAI对话异常 , StatusCode: {StatusCode} Response: {Response}", response.StatusCode,
|
||||
line);
|
||||
|
||||
throw new Exception("OpenAI对话异常" + line);
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.StartsWith("event:"))
|
||||
{
|
||||
eventType = line;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!line.StartsWith(OpenAIConstant.Data)) continue;
|
||||
|
||||
data = line[OpenAIConstant.Data.Length..].Trim();
|
||||
|
||||
var result = JsonSerializer.Deserialize<AnthropicStreamDto>(data,
|
||||
ThorJsonSerializer.DefaultOptions);
|
||||
|
||||
yield return (eventType, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorCustomOpenAI.Chats;
|
||||
|
||||
/// <summary>
|
||||
/// OpenAI到Claude适配器服务
|
||||
/// 将Claude格式的请求转换为OpenAI格式,然后将OpenAI的响应转换为Claude格式
|
||||
/// </summary>
|
||||
public class CustomOpenAIAnthropicChatCompletionsService(
|
||||
IAbpLazyServiceProvider serviceProvider,
|
||||
ILogger<CustomOpenAIAnthropicChatCompletionsService> logger)
|
||||
: IAnthropicChatCompletionService
|
||||
{
|
||||
private IChatCompletionService GetChatCompletionService()
|
||||
{
|
||||
return serviceProvider.GetRequiredKeyedService<IChatCompletionService>(nameof(OpenAiChatCompletionsService));
|
||||
}
|
||||
|
||||
public async Task<AnthropicChatCompletionDto> ChatCompletionsAsync(AiModelDescribe aiModelDescribe,
|
||||
AnthropicInput request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 转换请求格式:Claude -> OpenAI
|
||||
var openAIRequest = AnthropicToOpenAi.ConvertAnthropicToOpenAi(request);
|
||||
|
||||
if (openAIRequest.Model.StartsWith("gpt-5"))
|
||||
{
|
||||
openAIRequest.MaxCompletionTokens = request.MaxTokens;
|
||||
openAIRequest.MaxTokens = null;
|
||||
}
|
||||
else if (openAIRequest.Model.StartsWith("o3-mini") || openAIRequest.Model.StartsWith("o4-mini"))
|
||||
{
|
||||
openAIRequest.MaxCompletionTokens = request.MaxTokens;
|
||||
openAIRequest.MaxTokens = null;
|
||||
openAIRequest.Temperature = null;
|
||||
}
|
||||
|
||||
// 调用OpenAI服务
|
||||
var openAIResponse =
|
||||
await GetChatCompletionService().CompleteChatAsync(aiModelDescribe,openAIRequest, cancellationToken);
|
||||
|
||||
// 转换响应格式:OpenAI -> Claude
|
||||
var claudeResponse = AnthropicToOpenAi.ConvertOpenAIToClaude(openAIResponse, request);
|
||||
|
||||
return claudeResponse;
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<(string, AnthropicStreamDto?)> StreamChatCompletionsAsync(AiModelDescribe aiModelDescribe,
|
||||
AnthropicInput request,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var openAIRequest = AnthropicToOpenAi.ConvertAnthropicToOpenAi(request);
|
||||
openAIRequest.Stream = true;
|
||||
|
||||
if (openAIRequest.Model.StartsWith("gpt-5"))
|
||||
{
|
||||
openAIRequest.MaxCompletionTokens = request.MaxTokens;
|
||||
openAIRequest.MaxTokens = null;
|
||||
}
|
||||
else if (openAIRequest.Model.StartsWith("o3-mini") || openAIRequest.Model.StartsWith("o4-mini"))
|
||||
{
|
||||
openAIRequest.MaxCompletionTokens = request.MaxTokens;
|
||||
openAIRequest.MaxTokens = null;
|
||||
openAIRequest.Temperature = null;
|
||||
}
|
||||
|
||||
var messageId = Guid.NewGuid().ToString();
|
||||
var hasStarted = false;
|
||||
var hasTextContentBlockStarted = false;
|
||||
var hasThinkingContentBlockStarted = false;
|
||||
var toolBlocksStarted = new Dictionary<int, bool>(); // 使用索引而不是ID
|
||||
var toolCallIds = new Dictionary<int, string>(); // 存储每个索引对应的ID
|
||||
var toolCallIndexToBlockIndex = new Dictionary<int, int>(); // 工具调用索引到块索引的映射
|
||||
var accumulatedUsage = new AnthropicCompletionDtoUsage();
|
||||
var isFinished = false;
|
||||
var currentContentBlockType = ""; // 跟踪当前内容块类型
|
||||
var currentBlockIndex = 0; // 跟踪当前块索引
|
||||
var lastContentBlockType = ""; // 跟踪最后一个内容块类型,用于确定停止原因
|
||||
|
||||
await foreach (var openAIResponse in GetChatCompletionService().CompleteChatStreamAsync(aiModelDescribe,openAIRequest,
|
||||
cancellationToken))
|
||||
{
|
||||
// 发送message_start事件
|
||||
if (!hasStarted && openAIResponse.Choices?.Count > 0 &&
|
||||
openAIResponse.Choices.Any(x => x.Delta.ToolCalls?.Count > 0) == false)
|
||||
{
|
||||
hasStarted = true;
|
||||
var messageStartEvent = AnthropicToOpenAi.CreateMessageStartEvent(messageId, request.Model);
|
||||
yield return ("message_start", messageStartEvent);
|
||||
}
|
||||
|
||||
// 更新使用情况统计
|
||||
if (openAIResponse.Usage != null)
|
||||
{
|
||||
// 使用最新的token计数(OpenAI通常在最后的响应中提供完整的统计)
|
||||
if (openAIResponse.Usage.PromptTokens.HasValue)
|
||||
{
|
||||
accumulatedUsage.InputTokens = openAIResponse.Usage.PromptTokens.Value;
|
||||
}
|
||||
|
||||
if (openAIResponse.Usage.CompletionTokens.HasValue)
|
||||
{
|
||||
accumulatedUsage.OutputTokens = (int)openAIResponse.Usage.CompletionTokens.Value;
|
||||
}
|
||||
|
||||
if (openAIResponse.Usage.PromptTokensDetails?.CachedTokens.HasValue == true)
|
||||
{
|
||||
accumulatedUsage.CacheReadInputTokens =
|
||||
openAIResponse.Usage.PromptTokensDetails.CachedTokens.Value;
|
||||
}
|
||||
|
||||
// 记录调试信息
|
||||
logger.LogDebug("OpenAI Usage更新: Input={InputTokens}, Output={OutputTokens}, CacheRead={CacheRead}",
|
||||
accumulatedUsage.InputTokens, accumulatedUsage.OutputTokens,
|
||||
accumulatedUsage.CacheReadInputTokens);
|
||||
}
|
||||
|
||||
if (openAIResponse.Choices is { Count: > 0 })
|
||||
{
|
||||
var choice = openAIResponse.Choices.First();
|
||||
|
||||
// 处理内容
|
||||
if (!string.IsNullOrEmpty(choice.Delta?.Content))
|
||||
{
|
||||
// 如果当前有其他类型的内容块在运行,先结束它们
|
||||
if (currentContentBlockType != "text" && !string.IsNullOrEmpty(currentContentBlockType))
|
||||
{
|
||||
var stopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
stopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop", stopEvent);
|
||||
currentBlockIndex++; // 切换内容块时增加索引
|
||||
currentContentBlockType = "";
|
||||
}
|
||||
|
||||
// 发送content_block_start事件(仅第一次)
|
||||
if (!hasTextContentBlockStarted || currentContentBlockType != "text")
|
||||
{
|
||||
hasTextContentBlockStarted = true;
|
||||
currentContentBlockType = "text";
|
||||
lastContentBlockType = "text";
|
||||
var contentBlockStartEvent = AnthropicToOpenAi.CreateContentBlockStartEvent();
|
||||
contentBlockStartEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_start",
|
||||
contentBlockStartEvent);
|
||||
}
|
||||
|
||||
// 发送content_block_delta事件
|
||||
var contentDeltaEvent = AnthropicToOpenAi.CreateContentBlockDeltaEvent(choice.Delta.Content);
|
||||
contentDeltaEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_delta",
|
||||
contentDeltaEvent);
|
||||
}
|
||||
|
||||
// 处理工具调用
|
||||
if (choice.Delta?.ToolCalls is { Count: > 0 })
|
||||
{
|
||||
foreach (var toolCall in choice.Delta.ToolCalls)
|
||||
{
|
||||
var toolCallIndex = toolCall.Index; // 使用索引来标识工具调用
|
||||
|
||||
// 发送tool_use content_block_start事件
|
||||
if (toolBlocksStarted.TryAdd(toolCallIndex, true))
|
||||
{
|
||||
// 如果当前有文本或thinking内容块在运行,先结束它们
|
||||
if (currentContentBlockType == "text" || currentContentBlockType == "thinking")
|
||||
{
|
||||
var stopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
stopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop", stopEvent);
|
||||
currentBlockIndex++; // 增加块索引
|
||||
}
|
||||
// 如果当前有其他工具调用在运行,也需要结束它们
|
||||
else if (currentContentBlockType == "tool_use")
|
||||
{
|
||||
var stopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
stopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop", stopEvent);
|
||||
currentBlockIndex++; // 增加块索引
|
||||
}
|
||||
|
||||
currentContentBlockType = "tool_use";
|
||||
lastContentBlockType = "tool_use";
|
||||
|
||||
// 为此工具调用分配一个新的块索引
|
||||
toolCallIndexToBlockIndex[toolCallIndex] = currentBlockIndex;
|
||||
|
||||
// 保存工具调用的ID(如果有的话)
|
||||
if (!string.IsNullOrEmpty(toolCall.Id))
|
||||
{
|
||||
toolCallIds[toolCallIndex] = toolCall.Id;
|
||||
}
|
||||
else if (!toolCallIds.ContainsKey(toolCallIndex))
|
||||
{
|
||||
// 如果没有ID且之前也没有保存过,生成一个新的ID
|
||||
toolCallIds[toolCallIndex] = Guid.NewGuid().ToString();
|
||||
}
|
||||
|
||||
var toolBlockStartEvent = AnthropicToOpenAi.CreateToolBlockStartEvent(
|
||||
toolCallIds[toolCallIndex],
|
||||
toolCall.Function?.Name);
|
||||
toolBlockStartEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_start",
|
||||
toolBlockStartEvent);
|
||||
}
|
||||
|
||||
// 如果有增量的参数,发送content_block_delta事件
|
||||
if (!string.IsNullOrEmpty(toolCall.Function?.Arguments))
|
||||
{
|
||||
var toolDeltaEvent =
|
||||
AnthropicToOpenAi.CreateToolBlockDeltaEvent(toolCall.Function.Arguments);
|
||||
// 使用该工具调用对应的块索引
|
||||
toolDeltaEvent.Index = toolCallIndexToBlockIndex[toolCallIndex];
|
||||
yield return ("content_block_delta",
|
||||
toolDeltaEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理推理内容
|
||||
if (!string.IsNullOrEmpty(choice.Delta?.ReasoningContent))
|
||||
{
|
||||
// 如果当前有其他类型的内容块在运行,先结束它们
|
||||
if (currentContentBlockType != "thinking" && !string.IsNullOrEmpty(currentContentBlockType))
|
||||
{
|
||||
var stopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
stopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop", stopEvent);
|
||||
currentBlockIndex++; // 增加块索引
|
||||
currentContentBlockType = "";
|
||||
}
|
||||
|
||||
// 对于推理内容,也需要发送对应的事件
|
||||
if (!hasThinkingContentBlockStarted || currentContentBlockType != "thinking")
|
||||
{
|
||||
hasThinkingContentBlockStarted = true;
|
||||
currentContentBlockType = "thinking";
|
||||
lastContentBlockType = "thinking";
|
||||
var thinkingBlockStartEvent = AnthropicToOpenAi.CreateThinkingBlockStartEvent();
|
||||
thinkingBlockStartEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_start",
|
||||
thinkingBlockStartEvent);
|
||||
}
|
||||
|
||||
var thinkingDeltaEvent =
|
||||
AnthropicToOpenAi.CreateThinkingBlockDeltaEvent(choice.Delta.ReasoningContent);
|
||||
thinkingDeltaEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_delta",
|
||||
thinkingDeltaEvent);
|
||||
}
|
||||
|
||||
// 处理结束
|
||||
if (!string.IsNullOrEmpty(choice.FinishReason) && !isFinished)
|
||||
{
|
||||
isFinished = true;
|
||||
|
||||
// 发送content_block_stop事件(如果有活跃的内容块)
|
||||
if (!string.IsNullOrEmpty(currentContentBlockType))
|
||||
{
|
||||
var contentBlockStopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
contentBlockStopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop",
|
||||
contentBlockStopEvent);
|
||||
}
|
||||
|
||||
// 发送message_delta事件
|
||||
var messageDeltaEvent = AnthropicToOpenAi.CreateMessageDeltaEvent(
|
||||
AnthropicToOpenAi.GetStopReasonByLastContentType(choice.FinishReason, lastContentBlockType),
|
||||
accumulatedUsage);
|
||||
|
||||
// 记录最终Usage统计
|
||||
logger.LogDebug(
|
||||
"流式响应结束,最终Usage: Input={InputTokens}, Output={OutputTokens}, CacheRead={CacheRead}",
|
||||
accumulatedUsage.InputTokens, accumulatedUsage.OutputTokens,
|
||||
accumulatedUsage.CacheReadInputTokens);
|
||||
|
||||
yield return ("message_delta",
|
||||
messageDeltaEvent);
|
||||
|
||||
// 发送message_stop事件
|
||||
var messageStopEvent = AnthropicToOpenAi.CreateMessageStopEvent();
|
||||
yield return ("message_stop",
|
||||
messageStopEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 确保流正确结束
|
||||
if (!isFinished)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(currentContentBlockType))
|
||||
{
|
||||
var contentBlockStopEvent = AnthropicToOpenAi.CreateContentBlockStopEvent();
|
||||
contentBlockStopEvent.Index = currentBlockIndex;
|
||||
yield return ("content_block_stop",
|
||||
contentBlockStopEvent);
|
||||
}
|
||||
|
||||
var messageDeltaEvent =
|
||||
AnthropicToOpenAi.CreateMessageDeltaEvent(
|
||||
AnthropicToOpenAi.GetStopReasonByLastContentType("end_turn", lastContentBlockType),
|
||||
accumulatedUsage);
|
||||
yield return ("message_delta", messageDeltaEvent);
|
||||
|
||||
var messageStopEvent = AnthropicToOpenAi.CreateMessageStopEvent();
|
||||
yield return ("message_stop",
|
||||
messageStopEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yi.Framework.AiHub.Domain.AiGateWay.Exceptions;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay.Impl.ThorCustomOpenAI.Chats;
|
||||
|
||||
public sealed class OpenAiChatCompletionsService(ILogger<OpenAiChatCompletionsService> logger,IHttpClientFactory httpClientFactory)
|
||||
: IChatCompletionService
|
||||
{
|
||||
public async IAsyncEnumerable<ThorChatCompletionsResponse> CompleteChatStreamAsync(AiModelDescribe options,
|
||||
ThorChatCompletionsRequest chatCompletionCreate,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using var openai =
|
||||
Activity.Current?.Source.StartActivity("OpenAI 对话流式补全");
|
||||
|
||||
var response = await httpClientFactory.CreateClient().HttpRequestRaw(
|
||||
options?.Endpoint.TrimEnd('/') + "/chat/completions",
|
||||
chatCompletionCreate, options.ApiKey);
|
||||
|
||||
openai?.SetTag("Model", chatCompletionCreate.Model);
|
||||
openai?.SetTag("Response", response.StatusCode.ToString());
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
throw new UnauthorizedAccessException();
|
||||
}
|
||||
|
||||
// 如果限流则抛出限流异常
|
||||
if (response.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
throw new ThorRateLimitException();
|
||||
}
|
||||
|
||||
// 大于等于400的状态码都认为是异常
|
||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||
{
|
||||
var error = await response.Content.ReadAsStringAsync();
|
||||
logger.LogError("OpenAI对话异常 , StatusCode: {StatusCode} 错误响应内容:{Content}", response.StatusCode,
|
||||
error);
|
||||
|
||||
throw new BusinessException("OpenAI对话异常:" + error, response.StatusCode.ToString());
|
||||
}
|
||||
|
||||
using var stream = new StreamReader(await response.Content.ReadAsStreamAsync(cancellationToken));
|
||||
|
||||
using StreamReader reader = new(await response.Content.ReadAsStreamAsync(cancellationToken));
|
||||
string? line = string.Empty;
|
||||
var first = true;
|
||||
var isThink = false;
|
||||
while ((line = await reader.ReadLineAsync().ConfigureAwait(false)) != null)
|
||||
{
|
||||
line += Environment.NewLine;
|
||||
|
||||
if (line.StartsWith('{'))
|
||||
{
|
||||
logger.LogInformation("OpenAI对话异常 , StatusCode: {StatusCode} Response: {Response}", response.StatusCode,
|
||||
line);
|
||||
|
||||
throw new BusinessException("OpenAI对话异常", line);
|
||||
}
|
||||
|
||||
if (line.StartsWith(OpenAIConstant.Data))
|
||||
line = line[OpenAIConstant.Data.Length..];
|
||||
|
||||
line = line.Trim();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(line)) continue;
|
||||
|
||||
if (line == OpenAIConstant.Done)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (line.StartsWith(':'))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
var result = JsonSerializer.Deserialize<ThorChatCompletionsResponse>(line,
|
||||
ThorJsonSerializer.DefaultOptions);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var content = result?.Choices?.FirstOrDefault()?.Delta;
|
||||
|
||||
if (first && content?.Content == OpenAIConstant.ThinkStart)
|
||||
{
|
||||
isThink = true;
|
||||
continue;
|
||||
// 需要将content的内容转换到其他字段
|
||||
}
|
||||
|
||||
if (isThink && content?.Content?.Contains(OpenAIConstant.ThinkEnd) == true)
|
||||
{
|
||||
isThink = false;
|
||||
// 需要将content的内容转换到其他字段
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isThink && result?.Choices != null)
|
||||
{
|
||||
// 需要将content的内容转换到其他字段
|
||||
foreach (var choice in result.Choices)
|
||||
{
|
||||
choice.Delta.ReasoningContent = choice.Delta.Content;
|
||||
choice.Delta.Content = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
first = false;
|
||||
|
||||
yield return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ThorChatCompletionsResponse> CompleteChatAsync(AiModelDescribe options,
|
||||
ThorChatCompletionsRequest chatCompletionCreate,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
using var openai =
|
||||
Activity.Current?.Source.StartActivity("OpenAI 对话补全");
|
||||
|
||||
var response = await HttpClientFactory.GetHttpClient(options.Endpoint).PostJsonAsync(
|
||||
options?.Endpoint.TrimEnd('/') + "/chat/completions",
|
||||
chatCompletionCreate, options.ApiKey).ConfigureAwait(false);
|
||||
|
||||
openai?.SetTag("Model", chatCompletionCreate.Model);
|
||||
openai?.SetTag("Response", response.StatusCode.ToString());
|
||||
|
||||
if (response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
throw new BusinessException("渠道未登录,请联系管理人员", "401");
|
||||
}
|
||||
|
||||
// 如果限流则抛出限流异常
|
||||
if (response.StatusCode == HttpStatusCode.TooManyRequests)
|
||||
{
|
||||
throw new ThorRateLimitException();
|
||||
}
|
||||
|
||||
// 大于等于400的状态码都认为是异常
|
||||
if (response.StatusCode >= HttpStatusCode.BadRequest)
|
||||
{
|
||||
var error = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
|
||||
logger.LogError("OpenAI对话异常 请求地址:{Address}, StatusCode: {StatusCode} Response: {Response}", options.Endpoint,
|
||||
response.StatusCode, error);
|
||||
|
||||
throw new BusinessException("OpenAI对话异常", response.StatusCode.ToString());
|
||||
}
|
||||
|
||||
var result =
|
||||
await response.Content.ReadFromJsonAsync<ThorChatCompletionsResponse>(
|
||||
cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay;
|
||||
@@ -20,4 +21,12 @@ public class SpecialCompatible : ISpecialCompatible,ISingletonDependency
|
||||
handle(request);
|
||||
}
|
||||
}
|
||||
|
||||
public void AnthropicCompatible(AnthropicInput request)
|
||||
{
|
||||
foreach (var handle in _options.Value.AnthropicHandles)
|
||||
{
|
||||
handle(request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.Anthropic;
|
||||
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiGateWay;
|
||||
|
||||
public class SpecialCompatibleOptions
|
||||
{
|
||||
public List<Action<ThorChatCompletionsRequest>> Handles { get; set; } = new();
|
||||
public List<Action<AnthropicInput>> AnthropicHandles { get; set; } = new();
|
||||
}
|
||||
Reference in New Issue
Block a user