phpcurlopenai-apichatgpt-api

OpenAI Chat Completions API: Why do I get NULL response?


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;

Solution

  • 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);
    ?>