Files
Yi.Framework/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/AiChat/Impl/AzureChatService.cs
2025-06-21 01:08:14 +08:00

40 lines
1.2 KiB
C#

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;
}
}
}
}