feat: 完成ai网关搭建
This commit is contained in:
@@ -50,8 +50,8 @@ public class AiService : ApplicationService
|
|||||||
/// 发送消息
|
/// 发送消息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="input"></param>
|
/// <param name="input"></param>
|
||||||
/// <param name="cancelToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
public async Task PostSendAsync(SendMessageInput input, CancellationToken cancelToken)
|
public async Task PostSendAsync(SendMessageInput input, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var httpContext = this._httpContextAccessor.HttpContext;
|
var httpContext = this._httpContextAccessor.HttpContext;
|
||||||
var response = httpContext.Response;
|
var response = httpContext.Response;
|
||||||
@@ -75,7 +75,7 @@ public class AiService : ApplicationService
|
|||||||
}
|
}
|
||||||
|
|
||||||
var gateWay = LazyServiceProvider.GetRequiredService<AiGateWayManager>();
|
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 using var writer = new StreamWriter(response.Body, Encoding.UTF8, leaveOpen: true);
|
||||||
await foreach (var data in completeChatResponse)
|
await foreach (var data in completeChatResponse)
|
||||||
{
|
{
|
||||||
@@ -86,8 +86,12 @@ public class AiService : ApplicationService
|
|||||||
});
|
});
|
||||||
|
|
||||||
await writer.WriteLineAsync($"data: {message}\n");
|
await writer.WriteLineAsync($"data: {message}\n");
|
||||||
await writer.FlushAsync(cancelToken); // 确保立即推送数据
|
await writer.FlushAsync(cancellationToken); // 确保立即推送数据
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//断开连接
|
||||||
|
await writer.WriteLineAsync($"data: done\n");
|
||||||
|
await writer.FlushAsync(cancellationToken); // 确保立即推送数据
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,5 +4,5 @@ namespace Yi.Framework.AiHub.Domain.AiChat;
|
|||||||
|
|
||||||
public interface IChatService
|
public interface IChatService
|
||||||
{
|
{
|
||||||
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages);
|
public IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages,CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using Azure;
|
using System.Runtime.CompilerServices;
|
||||||
|
using Azure;
|
||||||
using Azure.AI.OpenAI;
|
using Azure.AI.OpenAI;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using OpenAI.Chat;
|
using OpenAI.Chat;
|
||||||
@@ -15,7 +16,8 @@ public class AzureChatService : IChatService
|
|||||||
this._options = options.Value.Chats[nameof(AzureChatService)];
|
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);
|
var endpoint = new Uri(_options.Endpoint);
|
||||||
|
|
||||||
@@ -27,7 +29,7 @@ public class AzureChatService : IChatService
|
|||||||
new AzureKeyCredential(apiKey));
|
new AzureKeyCredential(apiKey));
|
||||||
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
|
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
|
||||||
|
|
||||||
var response = chatClient.CompleteChatStreamingAsync(messages);
|
var response = chatClient.CompleteChatStreamingAsync(messages, cancellationToken: cancellationToken);
|
||||||
|
|
||||||
await foreach (StreamingChatCompletionUpdate update in response)
|
await foreach (StreamingChatCompletionUpdate update in response)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -18,16 +18,18 @@ public class AiGateWayManager : DomainService
|
|||||||
this._options = options.Value;
|
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)
|
foreach (var chat in _options.Chats)
|
||||||
{
|
{
|
||||||
if (chat.Value.ModelIds.Contains(modelId))
|
if (chat.Value.ModelIds.Contains(modelId))
|
||||||
{
|
{
|
||||||
var chatService = LazyServiceProvider.GetRequiredKeyedService<IChatService>(chat.Key);
|
var chatService = LazyServiceProvider.GetRequiredKeyedService<IChatService>(chat.Key);
|
||||||
return chatService.CompleteChatAsync(modelId, messages);
|
return chatService.CompleteChatAsync(modelId, messages, cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new UserFriendlyException($"当前暂不支持该模型-【{modelId}】");
|
throw new UserFriendlyException($"当前暂不支持该模型-【{modelId}】");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user