Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json_object #126

Open
MwSpaceLLC opened this issue Nov 20, 2023 · 2 comments
Open

json_object #126

MwSpaceLLC opened this issue Nov 20, 2023 · 2 comments

Comments

@MwSpaceLLC
Copy link

Describe the feature or improvement you're requesting

Please, see at: https://platform.openai.com/docs/guides/text-generation/json-mode

            $chat = json_decode(
                $open_ai->completion([
                    'model' => 'gpt-4-1106-preview',
                    'prompt' => $prompt,
                    'temperature' => 0.7,
                    'max_tokens' => 1024,
                    "top_p" => 1,
                    'frequency_penalty' => 0,
                    'presence_penalty' => 0,
                    'response_format ' => [
                        'type' => "json_object"
                    ]
                ])
            );

Additional context

No response

@jamesmalin
Copy link

jamesmalin commented Nov 26, 2023

Yes, it works great! I've been using this in all of my projects. Was super easy to use out of the box with this package.

Although it's straight forward, might be worth adding an example to the README.md for others searching for the feature across PHP GitHub projects.

Created a pull request:
#128

Adding PR here:

$complete = $open_ai->chat([
    'model' => 'gpt-3.5-turbo-1106',
    'messages' => [
            [
                'role' => 'system',
                'content' => 'You are a zoologist.'
            ],
            [
                'role' => 'system',
                'content' => 'Respond with JSON. Use this structure: [{"animal":"","description":""}]'
            ],
            [
                'role' => 'user',
                'content' => 'Supply a list of the most common animals you would see in a zoo.'
            ],
    ],
    'temperature' => 1,
    'max_tokens' => 3500,
    'response_format' => [ 'type' => 'json_object' ]
]);

@christoph-werker
Copy link

Just a quick note for everyone whoe stumpled over this issue.

$open_ai->completion([…]); won't work due to error Unrecognized request argument supplied: response_format.

But $open_ai->->chat works:

$result = json_decode($open_ai->chat([
	'model' => 'gpt-3.5-turbo-1106',
	'messages' => [
		[
			'role' => 'system',
			'content' => 'Respond with JSON. Use the provided keys to answer the question.',
		],
		[
			'role' => 'user',
			'content' => $prompt
		],
	],
	'temperature' => 1,
	'max_tokens' => 3500,
	'response_format' => ['type' => 'json_object']
]), false, 512, JSON_THROW_ON_ERROR);

Have fun.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants