feat: 完成ai网关搭建

This commit is contained in:
ccnetcore
2025-06-21 01:41:05 +08:00
parent 3b74dfd49a
commit 29985e2118
4 changed files with 18 additions and 10 deletions

View File

@@ -50,8 +50,8 @@ public class AiService : ApplicationService
/// 发送消息
/// </summary>
/// <param name="input"></param>
/// <param name="cancelToken"></param>
public async Task PostSendAsync(SendMessageInput input, CancellationToken cancelToken)
/// <param name="cancellationToken"></param>
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<AiGateWayManager>();
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); // 确保立即推送数据
}

View File

@@ -4,5 +4,5 @@ namespace Yi.Framework.AiHub.Domain.AiChat;
public interface IChatService
{
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages);
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages,CancellationToken cancellationToken);
}

View File

@@ -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<string> CompleteChatAsync(string modelId, List<ChatMessage> messages)
public async IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> 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)
{

View File

@@ -18,16 +18,18 @@ public class AiGateWayManager : DomainService
this._options = options.Value;
}
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages)
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages,
CancellationToken cancellationToken)
{
foreach (var chat in _options.Chats)
{
if (chat.Value.ModelIds.Contains(modelId))
{
var chatService = LazyServiceProvider.GetRequiredKeyedService<IChatService>(chat.Key);
return chatService.CompleteChatAsync(modelId, messages);
return chatService.CompleteChatAsync(modelId, messages, cancellationToken);
}
}
throw new UserFriendlyException($"当前暂不支持该模型-【{modelId}】");
}
}