Closed rhughes8469 closed 2 months ago
I did not try this with structured outputs, but this worked for me when setting the response format to json
'model' => 'gpt-4o',
'response_format' => [
'type' => 'json_object'
],
Cross reference to this post
Does this package support the new response_format = 'json_schema' option (Structured Outputs).
Would you be able to send the schema in via the $client->chat()->create method as an array?
As today, it does not.
The OpenAI announce is quite new, and this library has not been updated yet. See OpenAI blog post for details: https://openai.com/index/introducing-structured-outputs-in-the-api/
Note that type: json_object
is a different thing, here we are talking about the new type: json_schema
+1 to add support for this feature
Update: the message should be added correctly, see the answer of @gehrisandro
This can be resolved by passing the json schema as parameter:
'model' => 'gpt-4o',
'response_format' => [
'type' => 'json_schema',
'json_schema' => $jsonSchema,
]
For example:
[
'model' => 'gpt-4o',
'message' => 'what is 1 + 1?',
'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
],
]
]
];
You can already use it, with the current version. The only missing part is the refusal
parameter in the CreateResponseMessage
.
I am going to add support for this in the next days.
Here is an example, how you can use it:
$response = $client->chat()->create([
'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
]
]
]
]);
Hope this helps!
Thanks guys! I can also confirm it works exactly as expected.
I can confirm it works, but when switching to my azure backend it does not work. Maybe Azure OpenAi does not support the current API yet?
Does this feature work when using the assistants API? I'm trying to set the response_format property in the $openAi->threads()->createAndRun([[) call
@OskarStark azure has support for it now https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/structured-outputs?tabs=rest
Yes also for gpt4omini
Does this package support the new response_format = 'json_schema' option (Structured Outputs).
Would you be able to send the schema in via the $client->chat()->create method as an array?
https://platform.openai.com/docs/guides/structured-outputs