40 lines
1.2 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|
|
} |