Getting 400 error with new endpoint url

since the endpoint url is changed now my code is giving me this error ,

failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

function search_api($question) {
$apiEndpoint = ‘API URL HERE/v1/chat/completions’; // Updated API endpoint
$apiKey = ‘MY_API_KEY_HERE’; // Replace with your OpenAI API key

$headers = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $apiKey,
);

$data = array(
    'model' => 'text-davinci-003', // Specify the desired model
    'prompt' => $question,
    'max_tokens' => 7,
    'temperature' => 0,
);

$options = array(
    'http' => array(
        'header' => implode("\r\n", $headers),
        'method' => 'POST',
        'content' => json_encode($data),
    ),
);

Can anyone please advise? Greatly appreciated.

What’s in the body of the error response? Should give you more information

Also if you are using text-davinci you can’t use the /chat/completions endpoint. See the API Reference

1 Like

There’s no other errors. The only way I got this is from adding a custom debug script I was told to add, drove me nuts for hours. I was using the older url for completions that’s deprecated now and saw you have to use that new url now and that’s just where I’m stuck at.

You heard wrong, you can still use old URL. And you need to if you want to use davinci model.

If you use /chat/completions you need to use one of the gpt-* models.
(Also, unless you really want text-davinci you should probably try the chat/completion with gpt-3.5-turbo

I was using this /v1/engines/text-davinci-002/completions, when I use the one you show, I still get 400 error.

Which one? Can you share your updated full code?

the davinci url in the screenshot

heres part of the code mainly the part that calls it

how im calling it is by fetching chatbot.php on html front-end

my code is fairly simple, i cant share i have a lot going on here really dont want to get out there, but the connection should be fairly simple, im not sure whats going on, my other project im using the chat/completions url fine with JS onpage but this is the first time i call it from PHP and its not working. with the same data, etc.

Thank you for your time.

The chat completions endpoint is for the gpt-3.5-xxx models, and requires the ChatML list format.
For a chat completion endpoint, you will be bounced with 400 if you aren’t passing a pythonic list as $question.

davinci is a completion engine and doesn’t use completion/chat.

1 Like

Thanks for that !

Fixed adjusting code to

$data = array(
‘model’ => ‘gpt-3.5-turbo’, // Specify the desired model
‘messages’ => array(
array(
‘role’ => ‘system’,
‘content’ => ‘You are a helpful assistant.’
),
array(
‘role’ => ‘user’,
‘content’ => $question
),
),
‘max_tokens’ => 60,
‘temperature’ => 0.6,
);