-
Notifications
You must be signed in to change notification settings - Fork 180
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
Add Support for OpenAI new Structured Outputs. #160
Comments
To make it similar to the python and node sdks that use pydantic and zod respectively, could we use something like NJsonSchema to specify the response_format? |
I've tried to use it, it does response with a JSON string, but doesn't always response with a consist object form, |
It would be brilliant if there were the ability to use this feature like as the JsonSerializer works, for example: |
I added this feature to LangChain.NET generated OpenAI SDK(https://github.com/tryAGI/OpenAI) today, here's what it looks like, maybe it will help. It also includes trimming/NativeAOT support: using OpenAI;
using var api = new OpenAiApi("API_KEY");
MathReasoning? mathReasoning = await api.Chat.CreateChatCompletionAsAsync<MathReasoning>(
messages: ["How can I solve 8x + 7 = -23?"],
model: CreateChatCompletionRequestModel.Gpt4o20240806,
strict: true);
Console.WriteLine($"Final answer: {mathReasoning?.FinalAnswer}");
Console.WriteLine("Reasoning steps:");
foreach (MathReasoningStep step in mathReasoning?.Steps ?? [])
{
Console.WriteLine($" - Explanation: {step.Explanation}");
Console.WriteLine($" Output: {step.Output}");
} public class MathReasoning
{
public MathReasoningStep[] Steps { get; set; } = [];
public string FinalAnswer { get; set; } = string.Empty;
}
public class MathReasoningStep
{
public string Explanation { get; set; } = string.Empty;
public string Output { get; set; } = string.Empty;
}
Tests are available here: |
I too have finished Structured Outputs in og OpenAI-DotNet 8.2.0 |
@StephenHodgson works fine! But it can't deserialize |
Ideally you should be able to! |
Thank you for reaching out, @RogerBarreto ! Support for structured outputs has been added as part of this PR: 🔗 #180 It is now available on NuGet starting with version 2.0.0-beta.9: 🔗 https://www.nuget.org/packages/OpenAI/2.0.0-beta.9 Here we have an example on how to use it with chat completions: 🔗 Example07_StructuredOutputsAsync.cs |
Hi, I dont see a JSON Schema generator implementation as part of 2.0.0-beta.9 or 2.0.0-beta.10. Are there any plans to add it? |
Hi @andreylukin, @sagos95 and @StephenHodgson I've written a JSON Schema generator to produce OpenAi compatible JSON Schema.
|
FWIW, starting in .net 9 |
Add Support for Structured Outputs
As mentioned in the recent blog post from OpenAI.
Now is possible to specify the
response_format
asjson_schema
.Additional context
The text was updated successfully, but these errors were encountered: