OpenAI August 6, 2024 release in under 30 seconds!
The new validated JSON output mode is a game-changer, ensuring model responses match specified JSON schemas with 100% accuracy in tests.
Key Use-Case: You can specify reasoning_steps in your JSON schema.
"json_schema": {
"name": "reasoning_schema",
"strict": true,
"schema": {
"type": "object",
"properties": {
"reasoning_steps": {
"type": "array",
"items": {
"type": "string"
},
"description": "The reasoning steps leading to the final conclusion."
},
"answer": {
"type": "string",
"description": "The final answer, taking into account the reasoning steps."
}
},
"required": ["reasoning_steps", "answer"],
"additionalProperties": false
}
}
This enables the model to reason through problems before giving a final answer, greatly improving results. I'm super excited about this!
{
"reasoning_steps": [
"First step is to compare the numbers 9.11 and 9.9.",
"Both numbers have the same whole number part, which is 9.",
"To compare the decimal parts, convert them to the same number of decimal places.",
"9.11 has two decimal places: it is 9.11.",
"9.9 has one decimal place: it can be rewritten as 9.90.",
"Now, compare 9.11 and 9.90 by looking at the decimal parts.",
"Compare 11 with 90.",
"90 is greater than 11, so 9.90 is greater than 9.11."
],
"answer": "9.9 is bigger than 9.11."
}
Note: The first API response with a new schema may have additional latency, usually under 10 seconds but sometimes up to 1 minute. Dynamic JSON schemas might not be very practical here.
Tip: Use ZOD to define your JSON schemas for easier implementation.
import OpenAI from "openai";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";
const openai = new OpenAI();
const Step = z.object({
explanation: z.string(),
output: z.string(),
});
const MathResponse = z.object({
steps: z.array(Step),
final_answer: z.string(),
});
const completion = await openai.beta.chat.completions.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: zodResponseFormat(MathResponse, "math_response"),
});
const math_response = completion.choices[0].message.parsed;
console.log(math_response);
Improved Safety: Structured Outputs follow OpenAI's safety policies and can refuse unsafe requests. A new 'refusal' string in API responses lets developers detect refusals programmatically.
Newly enhanced use cases:
- Better generate dynamic UIs by specifying UI components in the JSON schema.
- Better extract structured data from unstructured inputs like meeting notes and legal files.
Get started quickly with native SDK support for Python & Node.js. Also, Helicone (YC W23) supports the new features and SDK out of the box!
Game changer? - https://analyticsindiamag.com/ai-origins-evolution/wait-did-openai-just-solve-jagged-intelligence/