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

I’m getting the same error as you. But I still haven’t found a way to solve it. How did you solve it?

Hey man, can you explain what did you do to solve this issue?

I’m also having the same issue for quite some time now and I haven’t managed to find a workaround till now.

Maybe it’s a bit late.
I was facing this issue too, after so many hours I find out I had to change the CURLOPT_SSL_VERIFYPEER value to false in OpenAi class.

1 Like

@armando.nura90

I had to change the CURLOPT_SSL_VERIFYPEER value to false in OpenAi class.

Hi, could you please explain how to do it? Even a small hint is helpful.

I would suggest to use openai-php/client instead.

composer require openai-php/client

And then in your script:

<?php

include 'vendor/autoload.php';

1 Like

@jochenschultz
Thanks for your suggestion!
I would like to use it but our project does not support PHP8.1, so I have to find others.

Nah, why? Make it a microservice. You might want to start multiple requests in parallel anyways.

1 Like