-1

When receiving a response from OpenAI's text-davinci-003 model, I was able to extract the text from the response with the following PHP code:

$response = $response->choices[0]->text;

Here was the Da Vinci response code:

{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "\n\nThis is indeed a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 7,
    "total_tokens": 12
  }
}

I am now trying to alter my code to work with the recently released gpt-3.5-turbo model which returns the response slightly differently:

{
  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}

My question is, how can I alter the code:

$response = $response->choices[0]->text;

...so that it can grab the content of the response message?

1 Answer 1

2

Try this:

$response = $response->choices[0]->message->content;

Working example

If you run test.php 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);
?>
6
  • On another note, I had:
    – sw123456
    Mar 2 at 10:17
  • $post_fields = [ "model" => "text-davinci-003", "prompt" => $query, "max_tokens" => 500, "temperature" => 0.8 ];
    – sw123456
    Mar 2 at 10:17
  • ...when sending a query to da vinci. But the new model requires a 'message' (not prompt) in the form: "messages": [{"role": "user", "content": "Hello!"}]
    – sw123456
    Mar 2 at 10:18
  • How can I alter the "prompt" => $query line to "messages" => so I can pass in role and content?
    – sw123456
    Mar 2 at 10:19
  • 1
    The solution for the comments above: OpenAI ChatGPT (gpt-3.5-turbo) API: Why am I getting NULL response?
    – Rok Benko
    Mar 2 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.