OpenAI-API-dotnet icon indicating copy to clipboard operation
OpenAI-API-dotnet copied to clipboard

Logprobs results in Newtonsoft SerializationException

Open isaacwalkercox opened this issue 4 years ago • 2 comments

Adding logProbs: n, to a CompletionRequest object, results in a serialization error "One or more errors occurred. (Error converting value {null} to type 'System.Double'. Path 'choices[0].logprobs.token_logprobs[0]', line 1, position 418.)"

I have tried using logProbs with both CreateCompletionAsync and CreateCompletionsAsync and I recieve the same error. I wonder whether the JSON being returned from OpenAI has changed slightly?

isaacwalkercox avatar Jan 24 '21 23:01 isaacwalkercox

@isaacwalkercox I have tried to reproduce your error and it seems to work for various cases (example):

	var completionRequest = new CompletionRequest
	{
		Prompt = "one two three",
		MaxTokens = 5,
		Temperature = 0,
		TopP = 1,
		PresencePenalty = 0,
		FrequencyPenalty = 0,
		Logprobs = 10,
		StopSequence = "###"
	};

	var result = await api.Completions.CreateCompletionAsync(completionRequest);

Indeed token_logprobs maps to an IList<double> which cannot accept null values, but I cannot figure out a case when the OpenAI returns a null prob value.

Can you provide the code that did not work for you?

Alexei000 avatar Jun 19 '21 07:06 Alexei000

I have managed to reproduce this by mistake with the following code:


private OpenAIAPI GetApi => new OpenAIAPI(engine: Engine.Ada);
var api = GetApi;

var completionReq = new CompletionRequest
{
   Prompt = "three four five",
   Temperature = 0,
   MaxTokens = 5,
   Echo = true,
   StopSequence = "eight"
};

var results = await api.Completions.CreateCompletionsAsync(completionReq);

By replacing List<double> should be replaced with List<double?>, it works as expected. If this solution is acceptable, I will create a PR with this minor fix.

Note: After a while, it no longer reproduces. It might be some throttling or similar that makes OpenAI not return those values sometimes.

Alexei000 avatar Jun 19 '21 16:06 Alexei000

i know this is old, i'm trying to check on some of these...

i ran your test code and seems to execute without error.

is this still an issue?

gotmike avatar Jan 27 '23 19:01 gotmike

@gotmike Yes. This issue was resolved. Thank you for double checking.

isaacwalkercox avatar Jan 28 '23 13:01 isaacwalkercox