feat:完成ai网关搭建
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using Azure;
|
||||
using Azure.AI.OpenAI;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OpenAI.Chat;
|
||||
using Yi.Framework.AiHub.Application.Contracts.Options;
|
||||
|
||||
namespace Yi.Framework.AiHub.Domain.AiChat.Impl;
|
||||
|
||||
public class AzureChatService : IChatService
|
||||
{
|
||||
private readonly AiChatModelOptions _options;
|
||||
|
||||
public AzureChatService(IOptions<AiGateWayOptions> options)
|
||||
{
|
||||
this._options = options.Value.Chats[nameof(AzureChatService)];
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<string> CompleteChatAsync(string modelId, List<ChatMessage> messages)
|
||||
{
|
||||
var endpoint = new Uri(_options.Endpoint);
|
||||
|
||||
var deploymentName = modelId;
|
||||
var apiKey = _options.ApiKey;
|
||||
|
||||
AzureOpenAIClient azureClient = new(
|
||||
endpoint,
|
||||
new AzureKeyCredential(apiKey));
|
||||
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
|
||||
|
||||
var response = chatClient.CompleteChatStreamingAsync(messages);
|
||||
|
||||
await foreach (StreamingChatCompletionUpdate update in response)
|
||||
{
|
||||
foreach (ChatMessageContentPart updatePart in update.ContentUpdate)
|
||||
{
|
||||
yield return updatePart.Text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user