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

3
  • 2
    "It was saying something about..." A proper, complete error message would help Commented Feb 21, 2023 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 Commented Feb 21, 2023 at 14:37
  • Does this answer your question? See "Working example", it might help you. Commented Feb 21, 2023 at 15:28

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.