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

@@ -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}】");
}
}