Upgrading to gpt-3.5-turbo gone wrong

Hello sorry… I worked with text-davinci-003 and it works well, but trying to change model to gpt-3.5-turbo Is not working, I am using an API call, I don’t know if is should change the settings of my parameters

Did you read the chat completion API docs on the OpenAI reference site?

Everything you need is there, @ejosephrogo, including all the API params details and sample code and responses.

OpenAI API Chat

:slight_smile:

@ejosephrogo I am using gpt-3.5-turbo, and it’s working. How are you sending the API call?

@ruby_coder yes i read all that… But one thing is that my method is not there even if it’s similar, am using php codes… But that is not the problem, the problem is the text-davinci-003 is working fine, but not gpt-4, all I needed to do was to change the model from text-davinci-003 to gpt-4 model right? But if I do that no text response get returned, although the call was made, but if I change to the gpt-3 model it starts working

@jeong this is the code I use through php

$apiKey = “Api key”;

// Get the user’s message from the POST data
$userMessage = $_POST[“message”];

// Set up the cURL request
$ch = curl_init(“https://api.openai.com/v1/completions”);
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $apiKey,
“Content-Type: application/json”
),
CURLOPT_POSTFIELDS => json_encode(array(
“model” => “text-davinci-003”,
“prompt” => $userMessage,
“max_tokens” => 500,
“n” => 1,
“stop” => null,
“temperature” => 0.6,
“presence_penalty” => 0.0,
“best_of” => 1,
“stream” => false,
“stop” => “”,
“echo” => false,
“temperature” => 0.5
))
));

// Execute the cURL request and get the response
$response = curl_exec($ch);

// Check if the cURL request returned an error
if ($response === false) {
//echo $userMessage;
$error = curl_error($ch);
echo $error." - Error";
// Output the error message
error_log('cURL error: ’ . curl_error($ch));
} else {
// Decode the response from the API
$response_data = json_decode($response, true);
if (isset($response_data[‘choices’][0][‘text’])) {
echo $response_data[‘choices’][0][‘text’];
} else {
echo ‘Error: No answer returned, please try again’;
}
}

// Close the cURL handle
curl_close($ch);

Under normal circumstances all I needed to do was to change the text-davinci-003 model id to gpt-3.5-turbo, but when I do the Error: no answer returned… Get showed, but if I change it back then it works well

You need to make quite a few changes.

Change the URL to the new chat endpoint
Change the model to the chat model
Get rid of n, best_of, echo (and possibly a couple of others)

You also have the stop parameter in your code twice.

The prompt needs to be changed to messages as well (This is the biggest change you need to make)

Once you have done all of that, you will also need to change the code that processes the response.

There are many changes, and @ruby_coder has provided the URL you need to look at (repeated below for completeness)

1 Like

Hi @ejosephrogo

The above API endpoint is wrong for turbo

You need to study and use the chat completion

:slight_smile:

If you had of read the API docs, @ejosephrogo, you would easily see your API endpoint is wrong as are your API params.

Go to the main OpenAI API reference page:

In your browser search bar, paste this:

Create chat completion

Then read the API does for the chat completion and it’s easy.

It is important to read the docs.

HTH

:slight_smile:

@raymonddavey I know am sounding dumb right now but I only understood the openai api implementation the way I did it above, I did go through the URL @ruby_coder provided for me, but I don’t do python, and only python and node.js was explained fully which I don’t do, please could you just rewrite the code I provided above to work? I changed the endpoint and removed some perimeters and it still gives the same result

Hey @ejosephrogo

The link I sent you and the API docs have examples in curl not just python, etc.

You seem frustrated and when frustrated, it is easy to make mistakes.

Where did you get your PHP code (above) from you are having trouble modifying?

So where is that code you modified?

When you say “you changed the code and it does not work”, that has no meaning to developers. We need to see the code. We need to see the exact error message you are getting.

Post your code using Markdown wrapped with triple back-ticks like so (and make sure you format you code so it is easy to read).

```
#. your  code here
```

:slight_smile:

@ruby_coder you are right, just tired, here is the code.


$apiKey = "api_key";

// Get the user's message from the POST data
$userMessage = $_POST["message"];


// Set up the cURL request
$ch = curl_init("https://api.openai.com/v1/chat/completions");
curl_setopt_array($ch, array(
  CURLOPT_POST => true,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer " . $apiKey,
    "Content-Type: application/json"
  ),
  CURLOPT_POSTFIELDS => json_encode(array(
    "model" => "gpt-3.5-turbo",
    "prompt" => $userMessage,
    "max_tokens" => 500,
    "stop" => null,
    "temperature" => 0.6,
    "presence_penalty" => 0.0,
    "stream" => false,
    "stop" => "",
    "temperature" => 0.5
  ))
));

// Execute the cURL request and get the response
$response = curl_exec($ch);

// Check if the cURL request returned an error
if ($response === false) {
  //echo $userMessage;
  $error = curl_error($ch);
  echo $error." - Error";
  // Output the error message
  error_log('cURL error: ' . curl_error($ch));
} else {
  // Decode the response from the API
  $response_data = json_decode($response, true);
    if (isset($response_data['choices'][0]['text'])) {
        echo $response_data['choices'][0]['text'];
    } else {
        echo 'Error: No answer returned, please try again';
    }
}

// Close the cURL handle
curl_close($ch);

The above is not correct @ejosephrogo

If you review the API docs, look at the params.

Why are you using prompt as a param?

Is prompt a param in the API docs for the chat completion ?

You keep saying “I read the docs” but you are making very trivial mistakes @ejosephrogo because you are not reading the docs.

It is right there, in black and white what are the required params for the chat completion.

:slight_smile:

@ruby_coder yah I just took a look at the last link you sent and noticed that I have to use this instead

  "messages" => [{"role": "user", "content": $userResponse}],

Is that right? I did correct it, now and working on how to get the response text

Yes, and you need to look at an example completion response because the response is also different.

This is also in the docs, BTW.

It’s all there @ejosephrogo There are no secrets being kept from you. Just read the docs and follow it. That is what all of us “very seasoned” developers did when the chat completion API method was first released. On the day it was released, we read the docs on the new API end point and we implemented what was in the docs.

Everything. you need is there in the create chat completion section in the docs.

Post back with your next attempt at coding this, @ejosephrogo

:slight_smile:

See Also:

Note:

I have checked your original PHP code, @ejosephrogo and my guess is that you used ChatGPT to write this PHP code for you, initially. Is that correct?

The reason I asked is that I can reproduce your original error by asking ChatGPT to write PHP code for the OpenAI completion endpoint using the gpt-3.5-turbo model and ChatGPT provides similar “hallucinated nonsense” PHP code which makes no sense to anyone who has read the API docs.

:slight_smile:

1 Like

@ruby_coder @raymonddavey hanks alot… It works now