Open rhughes8469 opened 1 month 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.
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