openai-php / client

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

json_schema/Structured Outputs #464

Closed rhughes8469 closed 2 months ago

rhughes8469 commented 3 months 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 3 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

jrmgx commented 3 months 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 months ago

+1 to add support for this feature

thijndehaas commented 3 months 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 months 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 3 months ago

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

OskarStark commented 2 months ago

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?

astware commented 1 month ago

Does this feature work when using the assistants API? I'm trying to set the response_format property in the $openAi->threads()->createAndRun([[) call

chris-sev commented 1 month ago

@OskarStark azure has support for it now https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/structured-outputs?tabs=rest

image

OskarStark commented 1 month ago

Yes also for gpt4omini