-
-
Notifications
You must be signed in to change notification settings - Fork 333
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
Native SDK support for Structured Outputs #508
Comments
I like dry-rb and use it daily, but I think it should be bare-bones and let the developer create abstractions on top of it using dry-rb, Sorbet, Active Record etc. At least make passing and returning dry-rb objects optional. Example: client = OpenAI::Client.new
completion = client.chat(
parameters: {
model: "gpt-4o-2024-08-06",
messages: [
{ role: "system", content: "You are a helpful math tutor." },
{ role: "user", content: "solve 8x + 31 = 2" },
],
response_format: {
type: "json_schema",
json_schema: {
name: "math_response",
strict: true,
schema: {
type: "object",
properties: {
steps: {
type: "array",
items: {
type: "object",
properties: {
explanation: {
type: "string",
},
output: {
type: "string",
},
},
required: ["explanation", "output"],
additionalProperties: false,
},
},
final_answer: {
type: "string",
},
},
required: ["steps", "final_answer"],
additionalProperties: false,
},
},
},
}
) And if |
The grok-ruby gem has an optional dry-schema integration: https://github.com/drnic/groq-ruby#using-dry-schema-with-json-mode |
And the Node official library has support for Zod. So, making an argument against my previous comment, maybe it should support dry's |
Here's a ruby script implementing StructuredOutputs::Schema that provides the functionality OpenAI added to their python SDK for super-simple structured output definitions. Replicates their cookbook example perfectly. Key thing is this simplicity: class MathReasoning < StructuredOutputs::Schema
def initialize
super do
define :step do
string :explanation
string :output
end
array :steps, items: ref(:step)
string :final_answer
end
end
end
schema = MathReasoning.new
result = client.parse(
model: "gpt-4o-2024-08-06",
messages: [
{ role: "system", content: "You are a helpful math tutor. Guide the user through the solution step by step." },
{ role: "user", content: "how can I solve 8x + 7 = -23" }
],
response_format: schema
) To get this: {
"steps": [
{
"expression": "8x + 7 = -23",
"explanation": "This is your starting equation."
},
{
"expression": "8x = -23 - 7",
"explanation": "Subtract 7 from both sides to isolate the term with 'x' on the left side."
},
{
"expression": "8x = -30",
"explanation": "Simplify the right side by calculating -23 - 7, which is -30."
},
{
"expression": "x = -30 / 8",
"explanation": "Divide both sides by 8 to solve for 'x'."
},
{
"expression": "x = -3.75",
"explanation": "Simplify the division to get the final value of 'x'. Alternatively, divide both sides of the equation 30 by 2 to simplify it down to 15, then divide again to get 15 divided by 4, which is -3.75."
}
],
"final_answer": "x = -3.75"
} |
The |
Strong endorse with @bastos approach |
Hoping this gets included soon! |
I support this. is there a bounty? |
I adapted @jeremedia's script to work with rails. I renamed
|
The Problem
We all hate trying to coax ChatGPT into adhering to a JSON schema. OpenAI decided to make that easier for us.
The flow:
It would be really nice to have native support within this ruby gem!
Prior Art
In python, a nice example with math Q&A
Potential Solve
Not sure what the ideal
pydantic
replacement would be, but perhapsdry-struct
? DTO declarations could look like this:(Not sure if the gem should handle any schema validations, since that's purportedly OpenAI's job, but there's
dry-validations
if so.)The rest of OpenAI's math tutor example might look like
The text was updated successfully, but these errors were encountered: