openai-php / client

⚡️ OpenAI PHP is a supercharged community-maintained PHP API client that allows you to interact with OpenAI API.
MIT License
4.75k stars 491 forks source link

json_schema/Structured Outputs #464

Open rhughes8469 opened 1 month ago

rhughes8469 commented 1 month ago

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

ericthornton88 commented 4 weeks 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

jrmgx commented 3 weeks ago

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

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

monsieurbab commented 3 weeks ago

+1 to add support for this feature

thijndehaas commented 3 weeks ago

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
            ],
        ]
    ]
];
gehrisandro commented 3 weeks ago

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!

rhughes8469 commented 2 weeks ago

Thanks guys! I can also confirm it works exactly as expected.