An async thread-safe singleton for the OpenAI Chat (completions) client
$ dotnet add package Soenneker.OpenAI.Client.Chat
Soenneker.OpenAI.Client.ChatThis library provides an implementation for interacting with the OpenAI service. It allows you to configure and utilize a ChatClient to perform various tasks using OpenAI's models.
For the Azure version of this: Soenneker.Azure.OpenAI.Client.Chat
dotnet add package Soenneker.OpenAI.Client.Chat
Register:
builder.services.AddOpenAIChatClientAsSingleton();
IConfiguration values:
"OpenAI:ApiKey"
"OpenAI:Model"
public class OpenAIService
{
private readonly IOpenAIChatClient _chatClient;
public OpenAIService(IOpenAIChatClient 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);
}
}