Skip to content

Commit

Permalink
Merge pull request #618 from betalgo/dev
Browse files Browse the repository at this point in the history
8.6.2
  • Loading branch information
kayhantolga authored Aug 26, 2024
2 parents fd3a4d9 + 85c642f commit 063d7a9
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 2 deletions.
1 change: 1 addition & 0 deletions OpenAI.Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
//await ChatCompletionTestHelper.RunChatFunctionCallTestAsStream(sdk);
//await ChatCompletionTestHelper.RunSimpleCompletionStreamWithUsageTest(sdk);
//await BatchTestHelper.RunBatchOperationsTest(sdk);
//await ChatCompletionTestHelper.RunChatWithJsonSchemaResponseFormat(sdk);

// Whisper
//await AudioTestHelper.RunSimpleAudioCreateTranscriptionTest(sdk);
Expand Down
73 changes: 73 additions & 0 deletions OpenAI.Playground/TestHelpers/ChatCompletionTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,4 +445,77 @@ public static async Task RunChatFunctionCallTestAsStream(IOpenAIService sdk)
throw;
}
}

//https://platform.openai.com/docs/guides/structured-outputs/how-to-use
public static async Task RunChatWithJsonSchemaResponseFormat(IOpenAIService sdk)
{
ConsoleExtensions.WriteLine("Chat Completion Testing is starting:", ConsoleColor.Cyan);
try
{
var completionResult = await sdk.ChatCompletion.CreateCompletion(new()
{
Messages = new List<ChatMessage>
{
ChatMessage.FromSystem("You are a helpful math tutor. Guide the user through the solution step by step."),
ChatMessage.FromUser("how can I solve 8x + 7 = -23"),
},
Model = "gpt-4o-2024-08-06",
ResponseFormat = new ResponseFormat()
{
Type = StaticValues.CompletionStatics.ResponseFormat.JsonSchema,
JsonSchema = new()
{
Name = "math_response",
Strict = true,
Schema = PropertyDefinition.DefineObject(
new Dictionary<string, PropertyDefinition>
{
{
"steps", PropertyDefinition.DefineArray(
PropertyDefinition.DefineObject(
new Dictionary<string, PropertyDefinition>
{
{ "explanation", PropertyDefinition.DefineString("The explanation of the step") },
{ "output", PropertyDefinition.DefineString("The output of the step") }
},
new List<string> { "explanation", "output" },
false,
"A step in the mathematical process",
null
)
)
},
{
"final_answer", PropertyDefinition.DefineString("The final answer of the mathematical process")
}
},
new List<string> { "steps", "final_answer" },
false,
"Response containing steps and final answer of a mathematical process",
null
)
}
}
});

if (completionResult.Successful)
{
Console.WriteLine(completionResult.Choices.First().Message.Content);
}
else
{
if (completionResult.Error == null)
{
throw new("Unknown Error");
}

Console.WriteLine($"{completionResult.Error.Code}: {completionResult.Error.Message}");
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class ChatCompletionCreateRequest : IModelValidate, IOpenAiModels.ITemper
public enum ResponseFormats
{
Text,
Json
Json,
JsonSchema
}

/// <summary>
Expand Down Expand Up @@ -227,6 +228,7 @@ public ResponseFormats? ChatResponseFormat
{
ResponseFormats.Json => StaticValues.CompletionStatics.ResponseFormat.Json,
ResponseFormats.Text => StaticValues.CompletionStatics.ResponseFormat.Text,
ResponseFormats.JsonSchema => StaticValues.CompletionStatics.ResponseFormat.JsonSchema,
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null)
}
};
Expand Down
20 changes: 20 additions & 0 deletions OpenAI.SDK/ObjectModels/RequestModels/ResponseFormat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;
using OpenAI.ObjectModels.SharedModels;

namespace OpenAI.ObjectModels.RequestModels;

Expand All @@ -17,6 +19,24 @@ public class ResponseFormat

[JsonPropertyName("type")]
public string? Type { get; set; }

[JsonPropertyName("json_schema")]
public JsonSchema JsonSchema { get; set; }
}

public class JsonSchema
{
[JsonPropertyName("description")]
public string? Description { get; set; }

[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("strict")]
public bool? Strict { get; set; }

[JsonPropertyName("schema")]
public PropertyDefinition? Schema { get; set; }
}

[JsonConverter(typeof(ResponseFormatOptionConverter))]
Expand Down
1 change: 1 addition & 0 deletions OpenAI.SDK/ObjectModels/StaticValueHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public static class CompletionStatics
{
public static class ResponseFormat
{
public static string JsonSchema => "json_schema";
public static string Json => "json_object";
public static string Text => "text";
}
Expand Down
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ Due to time constraints, not all methods have been thoroughly tested or fully do
Needless to say, I cannot accept responsibility for any damage caused by using the library.

## Changelog

### 8.6.2
- Added support for Structured Outputs, here is the link for samples: [Wiki, Structured Outputs ](https://github.com/betalgo/openai/wiki/Structured-Outputs)
### 8.6.1
- Updated Models with new GPT-4o mini model.
### 8.6.0
Expand Down

0 comments on commit 063d7a9

Please sign in to comment.