Closed MastaBaba closed 10 months ago
The error message is pretty clear. Do you have API key set as an environment variable? If not you’re overwriting the variable.
$api_key = "{{key}}";
$yourApiKey = getenv($api_key);
$client = OpenAI::client($yourApiKey);
Hi @MastaBaba
I agree to @pb30. If you still think it is a problem with the package, do not hesitate to get back on this one.
Thanks both.
Apologies for my being dim. I am not familiar with calling environment variables in PHP code.
But, also, doesn't this mean that what is in 'Get Started' actually lacks somehow setting an environment variable?
The following code works:
$api_key = "{{key}}";
$client = OpenAI::client($api_key);
$result = $client->completions()->create([
'model' => 'text-davinci-003',
'prompt' => 'PHP is',
]);
echo $result['choices'][0]['text'];
Thanks
I use the following basic code:
This results in the following error:
Fatal error: Uncaught OpenAI\Exceptions\ErrorException: You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys. in {{folder}}/openai/vendor/openai-php/client/src/Transporters/HttpTransporter.php:131 Stack trace: #0 {{folder}}/openai/vendor/openai-php/client/src/Transporters/HttpTransporter.php(57): OpenAI\Transporters\HttpTransporter->throwIfJsonError(Array, '{\n "error": ...') #1 {{folder}}/openai/vendor/openai-php/client/src/Resources/Completions.php(33): OpenAI\Transporters\HttpTransporter->requestObject(Object(OpenAI\ValueObjects\Transporter\Payload)) #2 {{folder}}/openai/index2.php(17): OpenAI\Resources\Completions->create(Array) #3 {main} thrown in {{folder}}/openai/vendor/openai-php/client/src/Transporters/HttpTransporter.php on line 131
What am I doing wrong?
The key I use I created a good hour ago.