using System.Runtime.CompilerServices; 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 options) { this._options = options.Value.Chats[nameof(AzureChatService)]; } public async IAsyncEnumerable CompleteChatAsync(string modelId, List messages, [EnumeratorCancellation] CancellationToken cancellationToken) { 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, cancellationToken: cancellationToken); await foreach (StreamingChatCompletionUpdate update in response) { foreach (ChatMessageContentPart updatePart in update.ContentUpdate) { yield return updatePart.Text; } } } }