What am I doing wrong? (PHP, API OpenAI)

I’m using orhanerday’s API for PHP. I followed the past examples and I can’t get a return in my code.

Can anyone help?

<?php 
require_once 'C:/Program Files/VertrigoServ/Php/vendor/autoload.php';

use Orhanerday\OpenAi\OpenAi;
$open_ai = new OpenAi(getenv('OPENAI_API_KEY'));

$complete = $open_ai->complete([
   'engine' => 'davinci',
   'prompt' => 'Hello',
   'temperature' => 0.9,
   'max_tokens' => 150,
   'frequency_penalty' => 0,
   'presence_penalty' => 0.6,
]);

echo $complete;

?>

I’ve been reminiscing for a few months now about my computer science years, where I learned some C/C++, through the OpenAI Playground, specifically using text-davinci-02 for guidance on debugs, and code, etc.

So I did the same with your code, I put a simple prompt with “Please, read the code below and help me to understand why this is wrong:”, followed by a stop sequence “###”.

Follows below the results.

  • Ask #1: :left_speech_bubble:

  • Playground Answer (GPT-3|text-davinci-02) #1: :robot:

  • Ask #2: :left_speech_bubble:
    How can we solve this?

  • Playground Answer (GPT-3|text-davinci-02) #2: :robot:

We can solve this by adding a complete() method to the Orhanerday\OpenAi\OpenAi class:

<?php 
require_once 'C:/Program Files/VertrigoServ/Php/vendor/autoload.php';

use Orhanerday\OpenAi\OpenAi;

class OpenAi {

   public function complete($options) {
      // ...
   }

}

$open_ai = new OpenAi(getenv('OPENAI_API_KEY'));

$complete = $open_ai->complete([
   'engine' => 'davinci',
   'prompt' => 'Hello',
   'temperature' => 0.9,
   'max_tokens' => 150,
   'frequency_penalty' => 0,
   'presence_penalty' => 0.6,
]);

echo $complete;

?>

See if it helps and let us know!

Good luck =D

The “complete” class exists, it is imported in the command “use Orhanerday\OpenAi\OpenAi;”.
My OpenAI key is saved in the .env file in which it is retrieved by “getenv(‘OPENAI_API_KEY’)”

When I run this code my PHP page is blank, no error, but no response either.

On my API Key page on the OpenAI website it says that it has never been used.

I still use the account’s credit balance, could this have something to do with it?

@pedropinta
Hi,

The API returns a JSON response, so you need first to convert it into a PHP array using the json_decode() function, after which you can anything possible with arrays.

In your case, it should be as follows:

$open_ai = new OpenAi(getenv('OPENAI_API_KEY'));

$complete = $open_ai->complete([
   'engine' => 'davinci',
   'prompt' => 'Hello',
   'temperature' => 0.9,
   'max_tokens' => 150,
   'frequency_penalty' => 0,
   'presence_penalty' => 0.6,
]);

$response = json_decode($complete , true); // Converting it into an associative array
$text = $response['choices'][0]['text']; // The completion (generated text)

echo $text;

You can also do print_r($response) to output the full API response as an array.

Thank reply, but $response is null :pensive:

Check and play with different values for the engine, prompt, and the other parameters. Verify that the API key is correctly set as well.

I wasn’t looking at the error that Curl returned, I was able to do that now and I’m having this error:

Curl error: SSL certificate problem: unable to get local issuer certificate

Do I need a valid SSL certificate on my webhost to use the API?

I managed to solve the previous error and now everything went well, I was able to use my API for the first time. :smile:

Thanks for the help.

1 Like