1

I am trying to carry out API calls to the newly release gpt-3.5-turbo model and have the following code, which should send a query (via the $query variable) to the API and then extract the content of a responding message from the API.

But I am getting null responses on each call. Any ideas what I have done incorrectly?

$ch = curl_init();

$query = "What is the capital city of England?";

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

$api_key = 'sk-**************************************';

$post_fields = [
    "model" => "gpt-3.5-turbo",
    "messages" => ["role" => "user","content" => $query],
    "max_tokens" => 500,
    "temperature" => 0.8
];

$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, json_encode($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);

$response = $response->choices[0]->message[0]->content;
Rok Benko
  • 14,265
  • 2
  • 24
  • 49
sw123456
  • 3,339
  • 1
  • 24
  • 42

1 Answers1

5

The reason why you're getting NULL response is because the JSON body could not be parsed.

You get the following error: "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please send an email to support@openai.com and include any relevant code you'd like help with.)".

Change this...

$post_fields = [
    "model" => "gpt-3.5-turbo",
    "messages" => ["role" => "user","content" => $query],
    "max_tokens" => 12,
    "temperature" => 0
];

...to this.

$post_fields = array(
    "model" => "gpt-3.5-turbo",
    "messages" => array(
        array(
            "role" => "user",
            "content" => $query
        )
    ),
    "max_tokens" => 12,
    "temperature" => 0
);

Working example

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

string(40) "

The capital city of England is London."

test.php

<?php
    $ch = curl_init();

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

    $api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';

    $query = 'What is the capital city of England?';

    $post_fields = array(
        "model" => "gpt-3.5-turbo",
        "messages" => array(
            array(
                "role" => "user",
                "content" => $query
            )
        ),
        "max_tokens" => 12,
        "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, json_encode($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]->message->content);
?>
Rok Benko
  • 14,265
  • 2
  • 24
  • 49
  • Using above example I can see in some of the answers showing half. any character limit here ? @Rok Benko. – Vrajesh Patel May 19 '23 at 12:38
  • 1
    @VrajeshPatel This is expected if you copy-pasted the code above because if your question and answer **together** are too long, the response will be cut off (i.e., you hit the model's token limit). Remove the `max_tokens` parameter. See [OpenAI GPT-3 API: Why do I get only partial completion? Why is the completion cut off?](https://stackoverflow.com/questions/75648132/openai-gpt-3-api-why-do-i-get-only-partial-completion-why-is-the-completion-cu/75671537#75671537) – Rok Benko May 19 '23 at 12:47