Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LogProbs serialization bug #55

Merged
merged 3 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion OpenAI.Playground/TestHelpers/CompletionTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static async Task RunSimpleCompletionTest(IOpenAIService sdk)
{
Prompt = "Once upon a time",
// PromptAsList = new []{"Once upon a time"},
MaxTokens = 5
MaxTokens = 5,
LogProbs = 1,
}, Models.Davinci);

if (completionResult.Successful)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OpenAI.GPT3.ObjectModels.RequestModels
/// <summary>
/// Create Completion Request Model
/// </summary>
public record CompletionCreateRequest : IModelValidate, IOpenAiModels.ITemperature, IOpenAiModels.IModel, IOpenAiModels.ILogProbs, IOpenAiModels.IUser
public record CompletionCreateRequest : IModelValidate, IOpenAiModels.ITemperature, IOpenAiModels.IModel, IOpenAiModels.ILogProbsRequest, IOpenAiModels.IUser
{
/// <summary>
/// The prompt(s) to generate completions for, encoded as a string, a list of strings, or a list of token lists.
Expand Down
4 changes: 2 additions & 2 deletions OpenAI.SDK/ObjectModels/SharedModels/ChoiceResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace OpenAI.GPT3.ObjectModels.SharedModels;

public record ChoiceResponse : IOpenAiModels.ILogProbs
public record ChoiceResponse : IOpenAiModels.ILogProbsResponse
{
[JsonPropertyName("text")] public string Text { get; set; }

[JsonPropertyName("index")] public int? Index { get; set; }

[JsonPropertyName("finish_reason")] public string FinishReason { get; set; }

[JsonPropertyName("logprobs")] public int? LogProbs { get; set; }
[JsonPropertyName("logprobs")] public LogProbsResponse LogProbs { get; set; }
}
7 changes: 6 additions & 1 deletion OpenAI.SDK/ObjectModels/SharedModels/IOpenAiModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ public interface IModel
string Model { get; set; }
}

public interface ILogProbs
public interface ILogProbsRequest
{
int? LogProbs { get; set; }
}

public interface ILogProbsResponse
{
LogProbsResponse LogProbs { get; set; }
}

public interface ITemperature
{
float? Temperature { get; set; }
Expand Down
16 changes: 16 additions & 0 deletions OpenAI.SDK/ObjectModels/SharedModels/LogProbsResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;

namespace OpenAI.GPT3.ObjectModels.SharedModels;

public record LogProbsResponse
{
[JsonPropertyName("tokens")] public List<string> Tokens { get; set; }

[JsonPropertyName("token_logprobs")] public List<double> TokenLogProbs { get; set; }

[JsonPropertyName("top_logprobs")] public List<Dictionary<string, double>> TopLogProbsRaw { get; set; }

public Dictionary<string, double> TopLogProbs => TopLogProbsRaw.SelectMany(r => r).ToDictionary(k => k.Key, k => k.Value);

[JsonPropertyName("text_offset")] public List<int> TextOffset { get; set; }
}