openai / openai-dotnet

The official .NET library for the OpenAI API
https://www.nuget.org/packages/OpenAI
MIT License
1.47k stars 145 forks source link

Lack of documentation for DI and thread-safety for .NET #264

Open emilmuller opened 2 weeks ago

emilmuller commented 2 weeks ago

Confirm this is a feature request for the .NET library and not the underlying OpenAI API

Describe the feature or improvement you are requesting

There seems to be a lack of documentation regarding DI and thread-safety, which makes it difficult to migrate to version 2 with confidence. Even though the code below mentions Azure, it is a non-Azure-OpenAI issue as well.

Before, using Azure.AI.OpenAI 1.0.0-beta.xx, I had this in my Program.cs:

builder.Services.AddAzureClients(x => { x.AddOpenAIClient( ...

This seems to have been deprecated. At least, I cannot find out how to set up dependency injection like that.

If I wanted to use OpenAIClient, should I just add a singleton? Is it thread-safe? I cannot find any mention of this. Something like this?

builder.Services.AddSingleton(new OpenAIClient(...

Now, how do I generate embeddings? The obvious way, if I already have an OpenAIClient, would be something like:

openAIClient.GetEmbeddingClient("123").GenerateEmbeddingsAsync(...

But that would create a new instance of EmbeddingClient each time. Could I have EmbeddingClient as a singleton then?

builder.Services.AddSingleton(services => services.GetService<OpenAIClient>().GetEmbeddingClient("123"));

Or is that not thread-safe to use?

Additional context

No response

joseharriaga commented 1 week ago

Thank you for reaching out, @emilmuller ! All the clients (including the OpenAIClient and the EmbeddingClient) are thread-safe. What you have above works.

vigneshmsft commented 6 days ago

thanks, I was after this question too, the Azure OpenAI SDK mentions it is thread-safe here. I want to use this library outside of Azure SDK and wasn't sure if ChatClient is thread-safe and its safe to register a single instance. I can see the OpenAIClient is a factory class to create new instances of other clients but is it safe to reuse a single ChatClient instance across different threads in parallel for different chat completions?