[编程开发] [转]ChatGPT PHP Demo源码

[复制链接]
查看387 | 回复4 | 2023-1-7 16:46:45 | 显示全部楼层 |阅读模式
请自行申请OpenAI Key
申请地址:https://beta.openai.com/account/api-keys
  1. $text="问答内容"; //例:致富论坛网址是多少?
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/completions");
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  5. curl_setopt($ch, CURLOPT_POSTFIELDS, '{
  6.     "model": "text-davinci-003",
  7.     "prompt": "'.$text.'",
  8.     "max_tokens": 2048
  9. }');
  10. curl_setopt($ch, CURLOPT_POST, 1);

  11. // Set the API key as an HTTP header
  12. $headers = array();
  13. $headers[] = "Content-Type: application/json";
  14. $headers[] = "Authorization:  Bearer 你的KEY";
  15. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

  16. // Send the request and parse the response
  17. $response = curl_exec($ch);
  18. $response_data = json_decode($response, true);
  19. if (curl_errno($ch)) {
  20.     // If there was an error executing the cURL request, print it out
  21.     echo 'Error: ' . curl_error($ch);
  22. } else {
  23.      $result = array(
  24.         'code'=> 200,
  25.         'msg'=>"获取成功",
  26.         'data'=>array(
  27.             'html'=> trim($response_data['choices'][0]['text']),
  28.             'title'=>$text
  29.         ),
  30.     );
  31.     echo json_encode($result,320);
  32.     exit();
  33. }
  34. curl_close($ch);
复制代码
Dingo | 2023-1-7 18:18:48 来自手机 | 显示全部楼层
新号的免费额度是18美刀,token消耗和问题及答复长短好像有关
回复

使用道具 举报

Xidorn | 2023-1-7 18:23:50 | 显示全部楼层
Dingo 发表于 2023-1-7 18:18
新号的免费额度是18美刀,token消耗和问题及答复长短好像有关

是的,我移植到网站上,获取6k条内容,就把免费额度消耗完了。

点评

接码5毛一个号,换6000条内容也还行  详情 回复 发表于 2023-1-7 18:26
回复

使用道具 举报

Dingo | 2023-1-7 18:26:07 来自手机 | 显示全部楼层
Xidorn 发表于 2023-1-7 18:23
是的,我移植到网站上,获取6k条内容,就把免费额度消耗完了。

接码5毛一个号,换6000条内容也还行
回复

使用道具 举报

Xidorn | 2023-1-7 19:07:45 来自手机 | 显示全部楼层
Dingo 发表于 2023-1-7 18:26
接码5毛一个号,换6000条内容也还行
那可以
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则