0

I am having a problem with getting responses from OpenAI's API...

I have this function:


`function generate_response($input) {
    $openai_api_key = "MY TOKEN";
    $prompt = "Answer the following question: " . $input;
    $data = array(
        'prompt' => $prompt,
        'max_tokens' => 1000,
        'temperature' => 0.5,
        'model' => 'davinci',
        'stop' => ['\n'], 
    );

    $url = 'https://api.openai.com/v1/completions';

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

    $data_string = json_encode($data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $response = json_decode(curl_exec($ch));
    curl_close($ch);

    if (isset($response->error)) {
        return "Error: " . $response->error->message;
    }
    $text_parts = array();
    foreach ($response->choices[0]->text as $part) {
        $text_parts[] = $part;
    }
    $text = implode('', $text_parts);
    return $text;
}
$user_question = "What is the capital of France?";
$response = generate_response($user_question);
echo "<h1>$response</h1>";`

It was saying something about model and engine then I used model however, it is still not working as I had expected any sugestions?

I tried changing model, engine, URL link. I was hoping i would get sensable answer for my question

  • 2
    "_It was saying something about..._" A proper, complete error message would help – brombeer Feb 21 '23 at 14:35
  • It was saying: can not specify both model and engine" after that saying: they couldn't process my request but now "Gateway Time-out Error" is on my screen – Royal Coder Feb 21 '23 at 14:37
  • Does [this](https://stackoverflow.com/questions/75401992/openai-api-error-you-didnt-provide-an-api-key-you-need-to-provide-your-api-k) answer your question? See "Working example", it might help you. – Rok Benko Feb 21 '23 at 15:28

1 Answers1

1

They are having an outage: https://status.openai.com/

The function seems fine, but there are a few things to consider:

The stop parameter in the $data array is set to ['\n'], which might not work as intended. It should be set to ["\n"] (with double quotes) instead.

The function assumes that the OpenAI API will always return a valid response, but that's not always the case. It's a good idea to add more error handling, like checking the HTTP status code or adding a timeout.

The function is returning HTML code (<h1>$response</h1>) directly, which might not be suitable for all use cases. It's better to return plain text and let the calling code handle the formatting.

Other than these points, the function should work as expected.

TimarTimar
  • 49
  • 5
  • when I changed single quote, error message changed to: "Warning: Invalid argument supplied for foreach() in /var/XXXX/XXXX/XXX/api.php on line 63" But I cannot see invalid argument: foreach ($response->choices[0]->text as $part) { – Royal Coder Feb 21 '23 at 15:03