OpenAI ChatGPT (GPT-3.5) API:如何从响应中提取消息内容?

sw1*_*456 1 php openai-api chatgpt-api

当收到来自 OpenAI 模型的响应时text-davinci-003,我能够使用以下 PHP 代码从响应中提取文本:

$response = $response->choices[0]->text;
Run Code Online (Sandbox Code Playgroud)

这是text-davinci-003响应代码:

{
  "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
  }
}
Run Code Online (Sandbox Code Playgroud)

我现在尝试更改我的代码以使用最近发布的gpt-3.5-turbo模型,该模型返回的响应略有不同:

{
  "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
  }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,如何更改代码:

$response = $response->choices[0]->text;
Run Code Online (Sandbox Code Playgroud)

...以便它可以抓取响应消息的内容?

Rok*_*nko 9

Python:

\n

\xe2\x80\xa2 如果您有 OpenAI Python SDK < v1

\n
print(response['choices'][0]['message']['content'])\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa2 如果您有 OpenAI Python SDK v1

\n
print(response.choices[0].message.content)\n
Run Code Online (Sandbox Code Playgroud)\n

节点.js:

\n

\xe2\x80\xa2 如果您有 OpenAI Node.js SDK v3

\n
console.log(response.data.choices[0].message.content);\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa2 如果您有 OpenAI Node.js SDK v4

\n
console.log(response.choices[0].message.content);\n
Run Code Online (Sandbox Code Playgroud)\n

PHP:

\n
var_dump($response->choices[0]->message->content);\n
Run Code Online (Sandbox Code Playgroud)\n
\n

PHP 中的工作示例

\n

如果运行test.php,OpenAI API 将返回以下完成信息:

\n
\n

字符串(40)“

\n

英国的首都是伦敦。”

\n
\n

测试.php

\n
<?php\n    $ch = curl_init();\n\n    $url = 'https://api.openai.com/v1/chat/completions';\n\n    $api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';\n\n    $query = 'What is the capital city of England?';\n\n    $post_fields = array(\n        "model" => "gpt-3.5-turbo",\n        "messages" => array(\n            array(\n                "role" => "user",\n                "content" => $query\n            )\n        ),\n        "max_tokens" => 12,\n        "temperature" => 0\n    );\n\n    $header  = [\n        'Content-Type: application/json',\n        'Authorization: Bearer ' . $api_key\n    ];\n\n    curl_setopt($ch, CURLOPT_URL, $url);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n    curl_setopt($ch, CURLOPT_POST, 1);\n    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields));\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);\n\n    $result = curl_exec($ch);\n    if (curl_errno($ch)) {\n        echo 'Error: ' . curl_error($ch);\n    }\n    curl_close($ch);\n\n    $response = json_decode($result);\n    var_dump($response->choices[0]->message->content);\n?>\n
Run Code Online (Sandbox Code Playgroud)\n