openai / openai-dotnet

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

Generating embedding with EmbeddingClient causes throw reference exception for 2.0.0-beta.6 #84

Closed DavidLuong98 closed 6 days ago

DavidLuong98 commented 1 week ago

There is a crash when trying to generate an embedding, both synchronous and async methods. This only occurs for 2.0.0-beta.6. Downgrading to 2.0.0-beta.5 is a workaround.

Minimal code repro:

using OpenAI.Embeddings;

EmbeddingClient client = new(model: "text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
string description = "Foo";
Embedding embedding = client.GenerateEmbedding(description);
ReadOnlyMemory<float> vector = embedding.Vector;
EmbeddingGenerationOptions options = new() { Dimensions = 512 };

Embedding embedding = client.GenerateEmbedding(description, options);

Stack:

System.NullReferenceException: Object reference not set to an instance of an object.
   at OpenAI.OpenAIClient.<>c__DisplayClass21_0.<CreateAddCustomHeadersPolicy>b__0(PipelineMessage message)
   at OpenAI.GenericActionPipelinePolicy.ProcessAsync(PipelineMessage message, IReadOnlyList`1 pipeline, Int32 currentIndex)
   at OpenAI.GenericActionPipelinePolicy.ProcessAsync(PipelineMessage message, IReadOnlyList`1 pipeline, Int32 currentIndex)
   at System.ClientModel.Primitives.ClientPipeline.SendAsync(PipelineMessage message)
   at OpenAI.ClientPipelineExtensions.ProcessMessageAsync(ClientPipeline pipeline, PipelineMessage message, RequestOptions options)
   at OpenAI.Embeddings.EmbeddingClient.GenerateEmbeddingsAsync(BinaryContent content, RequestOptions options)
   at OpenAI.Embeddings.EmbeddingClient.GenerateEmbeddingAsync(String input, EmbeddingGenerationOptions options, CancellationToken cancellationToken)
joseharriaga commented 1 week ago

Thank you for reporting this issue, @DavidLuong98 ! I got a fix for it here: https://github.com/openai/openai-dotnet/pull/85

trrwilson commented 6 days ago

@DavidLuong98, the fix @joseharriaga described is published now as 2.0.0-beta.7. Thank you again for reporting this!