diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiService.cs index 8661bf4f..1ba9eb9d 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/AiService.cs @@ -50,8 +50,8 @@ public class AiService : ApplicationService /// 发送消息 /// /// - /// - public async Task PostSendAsync(SendMessageInput input, CancellationToken cancelToken) + /// + public async Task PostSendAsync(SendMessageInput input, CancellationToken cancellationToken) { var httpContext = this._httpContextAccessor.HttpContext; var response = httpContext.Response; @@ -75,7 +75,7 @@ public class AiService : ApplicationService } var gateWay = LazyServiceProvider.GetRequiredService(); - var completeChatResponse = gateWay.CompleteChatAsync(input.Model, history); + var completeChatResponse = gateWay.CompleteChatAsync(input.Model, history,cancellationToken); await using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true); await foreach (var data in completeChatResponse) { @@ -86,8 +86,12 @@ public class AiService : ApplicationService }); await writer.WriteLineAsync($"data: {message}\n"); - await writer.FlushAsync(cancelToken); // 确保立即推送数据 + await writer.FlushAsync(cancellationToken); // 确保立即推送数据 } + + //断开连接 + await writer.WriteLineAsync($"data: done\n"); + await writer.FlushAsync(cancellationToken); // 确保立即推送数据 } diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/IChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/IChatService.cs index 04bfc081..76ca58c6 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/IChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/IChatService.cs @@ -4,5 +4,5 @@ namespace Yi.Framework.AiHub.Domain.AiChat; public interface IChatService { - public IAsyncEnumerable CompleteChatAsync(string modelId, List messages); + public IAsyncEnumerable CompleteChatAsync(string modelId, List messages,CancellationToken cancellationToken); } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs index 8ce00bab..ae9b8321 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs @@ -1,4 +1,5 @@ -using Azure; +using System.Runtime.CompilerServices; +using Azure; using Azure.AI.OpenAI; using Microsoft.Extensions.Options; using OpenAI.Chat; @@ -15,7 +16,8 @@ public class AzureChatService : IChatService this._options = options.Value.Chats[nameof(AzureChatService)]; } - public async IAsyncEnumerable CompleteChatAsync(string modelId, List messages) + public async IAsyncEnumerable CompleteChatAsync(string modelId, List messages, + [EnumeratorCancellation] CancellationToken cancellationToken) { var endpoint = new Uri(_options.Endpoint); @@ -27,7 +29,7 @@ public class AzureChatService : IChatService new AzureKeyCredential(apiKey)); ChatClient chatClient = azureClient.GetChatClient(deploymentName); - var response = chatClient.CompleteChatStreamingAsync(messages); + var response = chatClient.CompleteChatStreamingAsync(messages, cancellationToken: cancellationToken); await foreach (StreamingChatCompletionUpdate update in response) { 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 36ce17d9..5d926496 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 @@ -18,16 +18,18 @@ public class AiGateWayManager : DomainService this._options = options.Value; } - public IAsyncEnumerable CompleteChatAsync(string modelId, List messages) + public IAsyncEnumerable CompleteChatAsync(string modelId, List messages, + CancellationToken cancellationToken) { foreach (var chat in _options.Chats) { if (chat.Value.ModelIds.Contains(modelId)) { var chatService = LazyServiceProvider.GetRequiredKeyedService(chat.Key); - return chatService.CompleteChatAsync(modelId, messages); + return chatService.CompleteChatAsync(modelId, messages, cancellationToken); } } + throw new UserFriendlyException($"当前暂不支持该模型-【{modelId}】"); } } \ No newline at end of file