0

I am creating a PHP script to access Open Ai's API, to ask a query and get a response.

I am getting the following error:

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)

...but I thought I was providing the API key in the first variable?

Here is my code:

$api_key = "sk-U3B.........7MiL";

$query = "How are you?";

$url = "https://api.openai.com/v1/engines/davinci/jobs";

// Set up the API request headers
$headers = array(
    "Content-Type: application/json",
    "Authorization: Bearer " . $api_key
);

// Set up the API request body
$data = array(
    "prompt" => $query,
    "max_tokens" => 100,
    "temperature" => 0.5
);

// Use WordPress's built-in HTTP API to send the API request
$response = wp_remote_post( $url, array(
    'headers' => $headers,
    'body' => json_encode( $data )
) );

// Check if the API request was successful
if ( is_wp_error( $response ) ) {
    // If the API request failed, display an error message
    echo "Error communicating with OpenAI API: " . $response->get_error_message();
} else {
    // If the API request was successful, extract the response text
    $response_body = json_decode( $response['body'] );
    //$response_text = $response_body->choices[0]->text;
    var_dump($response_body);
    // Display the response text on the web page
    echo $response_body;
Rok Benko
  • 14,265
  • 2
  • 24
  • 49
sw123456
  • 3,339
  • 1
  • 24
  • 42
  • Not sure, but the examples in the user notes of `wp_remote_post` use the headers array differently (key=>value). See examples: https://developer.wordpress.org/reference/functions/wp_remote_post/#user-contributed-notes – Honk der Hase Feb 09 '23 at 17:07

1 Answers1

2

All Engines endpoints are deprecated.

Deprecated

This is the correct Completions endpoint:

https://api.openai.com/v1/completions

Working example

If you run test.php the OpenAI API will return the following completion:

string(23) "

This is indeed a test"

test.php

<?php
    $ch = curl_init();

    $url = 'https://api.openai.com/v1/completions';

    $api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';

    $post_fields = '{
        "model": "text-davinci-003",
        "prompt": "Say this is a test",
        "max_tokens": 7,
        "temperature": 0
    }';

    $header  = [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key
    ];

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error: ' . curl_error($ch);
    }
    curl_close($ch);

    $response = json_decode($result);
    var_dump($response->choices[0]->text);
?>
Rok Benko
  • 14,265
  • 2
  • 24
  • 49
  • Thanks so much for your help. I changed the endpoint and updated $url variable, but unfortunately the issues is continuing. I definitely have not used up the credit though as I've only just begun trying to use the API. So you know what else I might be doing wrong? – sw123456 Feb 09 '23 at 17:15
  • Does [this](https://stackoverflow.com/questions/75210324/openai-api-error-you-need-to-provide-your-api-key-in-an-authorization-header) help you? – Rok Benko Feb 09 '23 at 17:18
  • Sadly not, I've tried to implement some changes explained in the link but not helped unfortunately. You don't be any chance have a sample script which will enable the simple asking a query and getting a response, in php? Thank you – sw123456 Feb 09 '23 at 17:28
  • You're asking for a simple PHP example? – Rok Benko Feb 09 '23 at 17:35
  • This PHP script is part of a wordpress plugin and live on a server. Could the way the password is supplied be an issue? – sw123456 Feb 09 '23 at 17:35
  • Simple PHP example...yes please if possible? – sw123456 Feb 09 '23 at 17:43
  • 1
    Will edit my answer tomorrow. :) – Rok Benko Feb 09 '23 at 18:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251780/discussion-between-sw123456-and-cervus-camelopardalis). – sw123456 Feb 10 '23 at 09:10