我正在尝试执行对新发布的gpt-3.5-turbo模型的API调用,并具有以下代码,该代码应该向API发送查询(通过$query变量),然后从API提取响应消息的内容。
但是我每次打电话都得到空的回应。你知道我做错了什么吗?

  1. $ch = curl_init();
  2. $query = "What is the capital city of England?";
  3. $url = 'https://api.openai.com/v1/chat/completions';
  4. $api_key = 'sk-**************************************';
  5. $post_fields = [
  6. "model" => "gpt-3.5-turbo",
  7. "messages" => ["role" => "user","content" => $query],
  8. "max_tokens" => 500,
  9. "temperature" => 0.8
  10. ];
  11. $header = [
  12. 'Content-Type: application/json',
  13. 'Authorization: Bearer ' . $api_key
  14. ];
  15. curl_setopt($ch, CURLOPT_URL, $url);
  16. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  17. curl_setopt($ch, CURLOPT_POST, 1);
  18. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields));
  19. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  20. $result = curl_exec($ch);
  21. if (curl_errno($ch)) {
  22. echo 'Error: ' . curl_error($ch);
  23. }
  24. curl_close($ch);
  25. $response = json_decode($result);
  26. $response = $response->choices[0]->message[0]->content;

 

您得到NULL响应的原因是JSON主体无法解析。
您会得到以下错误:"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.)"
.
改变这个...

  1. $post_fields = [
  2. "model" => "gpt-3.5-turbo",
  3. "messages" => ["role" => "user","content" => $query],
  4. "max_tokens" => 12,
  5. "temperature" => 0
  6. ];

......到这个。

  1. $post_fields = array(
  2. "model" => "gpt-3.5-turbo",
  3. "messages" => array(
  4. array(
  5. "role" => "user",
  6. "content" => $query
  7. )
  8. ),
  9. "max_tokens" => 12,
  10. "temperature" => 0
  11. );

工作示例

如果您在CMD中运行php test.php,OpenAI API将返回以下完成:
字符串(40)"
英国的首都是伦敦。"

    • 测试. php**
  1. <?php
  2. $ch = curl_init();
  3. $url = 'https://api.openai.com/v1/chat/completions';
  4. $api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';
  5. $query = 'What is the capital city of England?';
  6. $post_fields = array(
  7. "model" => "gpt-3.5-turbo",
  8. "messages" => array(
  9. array(
  10. "role" => "user",
  11. "content" => $query
  12. )
  13. ),
  14. "max_tokens" => 12,
  15. "temperature" => 0
  16. );
  17. $header = [
  18. 'Content-Type: application/json',
  19. 'Authorization: Bearer ' . $api_key
  20. ];
  21. curl_setopt($ch, CURLOPT_URL, $url);
  22. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  23. curl_setopt($ch, CURLOPT_POST, 1);
  24. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_fields));
  25. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  26. $result = curl_exec($ch);
  27. if (curl_errno($ch)) {
  28. echo 'Error: ' . curl_error($ch);
  29. }
  30. curl_close($ch);
  31. $response = json_decode($result);
  32. var_dump($response->choices[0]->message->content);
  33. ?>
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。