I can't connect to openai with APIckey

Hi,
I have created a php script and I got the secret key (sk-***********) but I have the following error:

Warning : file_get_contents
engines/davinci-codex/completions: Failed to open stream: HTTP request failed!

Any help please ?

Hi! Welcome to the forums!

Unfortunately, davinci has been al but retired. For completions, you need to use 3.5 instruct

curl https://api.openai.com/v1/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
  "model": "gpt-3.5-turbo-instruct",
  "prompt": "Hello!",
  "temperature": 1,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}'

here’s what that would look like in php:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $OPENAI_API_KEY
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$data = array(
    'model' => 'gpt-3.5-turbo-instruct',
    'prompt' => 'Hello!',
    'temperature' => 1,
    'max_tokens' => 256,
    'top_p' => 1,
    'frequency_penalty' => 0,
    'presence_penalty' => 0
);
$jsonData = json_encode($data);

curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

$response = curl_exec($ch);

if ($response === false) {
    echo 'cURL error: ' . curl_error($ch);
}

curl_close($ch);

// Process $response as needed

?>

does that help?

1 Like

Thank you, I made de modifications.
I got this message : Error during the request to the OpenAI API.

What library are you using? If it’s a community library, it’s quite possible that it’s outdated. If you’re not using a library (but curl) this sounds like an error that you yourself created. can you try to find what the underlying error is?