An async thread-safe singleton for the Azure OpenAI Chat (completions) client
$ dotnet add package Soenneker.Azure.OpenAI.Client.Chat
Soenneker.OpenAI.Client.ChatThis library provides an implementation for interacting with Azure's OpenAI service. It allows you to configure and utilize a ChatClient to perform various tasks using OpenAI's models.
For the direct from OpenAI version of this: Soenneker.OpenAI.Client.Chat
dotnet add package Soenneker.Azure.OpenAI.Client.Chat
Register:
builder.services.AddAzureOpenAIChatClientAsSingleton();
IConfiguration values:
"Azure:OpenAI:Chat:Deployment"
"Azure:OpenAI:ApiKey"
"Azure:OpenAI:Uri"
public class OpenAIService
{
private readonly IAzureOpenAIChatClient _chatClient;
public OpenAIService(IAzureOpenAIChatClient chatClient)
{
_chatClient = chatClient;
}
public async ValueTask<string> Chat(string prompt, CancellationToken cancellationToken = default)
{
var client = await _chatClient.Get(cancellationToken);
ChatCompletion completion = await client.CompleteChatAsync(prompt);
}
}