From 5953be63cbed6595ccfca4c632f1404190739808 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Wed, 9 Jul 2025 22:44:24 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9cline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/OpenAi/ChatCompletionsInput.cs | 37 +++++++++++++++++-- .../Services/OpenApiService.cs | 14 ++++--- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/OpenAi/ChatCompletionsInput.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/OpenAi/ChatCompletionsInput.cs index c75e604f..d5fb3f0c 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/OpenAi/ChatCompletionsInput.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application.Contracts/Dtos/OpenAi/ChatCompletionsInput.cs @@ -1,10 +1,13 @@ -namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAiDto; +using System.Collections; +using System.Text.Json; + +namespace Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAiDto; public class ChatCompletionsInput { public List Messages { get; set; } - public bool Stream { get; set; } + public bool? Stream { get; set; } public string? Prompt { get; set; } public string Model { get; set; } @@ -16,6 +19,32 @@ public class ChatCompletionsInput public class OpenAiMessage { - public string Role { get; set; } - public string Content { get; set; } + public string? Role { get; set; } + public object? Content { get; set; } + + public string ConvertContent() + { + if (Content is string content) + { + return content; + } + + if (Content is JsonElement jsonElement && jsonElement.ValueKind == JsonValueKind.Array) + { + var contentItems = jsonElement.Deserialize>(); + var currentContentItem = contentItems.FirstOrDefault(); + if (currentContentItem.type == "text") + { + return currentContentItem.text; + } + } + + throw new UserFriendlyException("当前格式暂不支持"); + } +} + +public class ContentItem +{ + public string? type { get; set; } + public string? text { get; set; } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/OpenApiService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/OpenApiService.cs index d7e64fdf..5a46a73e 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/OpenApiService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/OpenApiService.cs @@ -36,27 +36,31 @@ public class OpenApiService : ApplicationService /// /// [HttpPost("openApi/v1/chat/completions")] - public async Task ChatCompletionsAsync(ChatCompletionsInput input, CancellationToken cancellationToken) + public async Task ChatCompletionsAsync([FromBody]ChatCompletionsInput input, CancellationToken cancellationToken) { //前面都是校验,后面才是真正的调用 var httpContext = this._httpContextAccessor.HttpContext; - + var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext)); var history = new List(); foreach (var aiChatContextDto in input.Messages) { if (aiChatContextDto.Role == "assistant") { - history.Add(ChatMessage.CreateAssistantMessage(aiChatContextDto.Content)); + history.Add(ChatMessage.CreateAssistantMessage(aiChatContextDto.ConvertContent())); } else if (aiChatContextDto.Role == "user") { - history.Add(ChatMessage.CreateUserMessage(aiChatContextDto.Content)); + history.Add(ChatMessage.CreateUserMessage(aiChatContextDto.ConvertContent())); + } + else if (aiChatContextDto.Role == "system") + { + history.Add(ChatMessage.CreateSystemMessage(aiChatContextDto.ConvertContent())); } } //是否使用流式传输 - if (input.Stream) + if (input.Stream == true) { //ai网关代理httpcontext await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input.Model,