目的地-Destination 前程似锦、未来可期、寻得良人、共赴白头,祝你也祝我。
博主 目的地-Destination
沪ICP备16003146号-2沪公网安备 31010702004922号萌ICP备20238488号博主 昨天 17:25 在线自豪地使用 Typecho 建站搭配使用 🌻Sunny 主题当前在线 19 人
歌曲封面 未知作品

沪ICP备16003146号-2

沪公网安备 31010702004922号

萌ICP备20238488号

网站已运行 7 年 342 天 1 小时 7 分

Powered by Typecho & Sunny

20 online · 52 ms

Title

ChatGPT之GPT 3.5 Turbo的API调用接口的方法

Chrison

·

烂笔头

·

Article
⚠️ 本文最后更新于2023年03月18日,已经过了584天没有更新,若内容或图片失效,请留言反馈
代码非常简单;
保存为.php文件,然后浏览器访问该文件,并在地址后面拼接?text=你要问的问题即可
例如:https://blog.chrison.cn/chatgpt.php?text=写一份女士的自我介绍
iShot_2023-03-17_21.11.41.png
♾️ php 代码:
<?php
/* 
Chrison Leon - https://blog.chrison.cn
*/

$openai_api_key = 'sk-'; //这里填写你申请的key
$number_of_interactions_to_remember = 10;

$forget = isset($_GET['forget']) ? $_GET['forget'] : null;
$text = isset($_GET['text']) ? $_GET['text'] : null;


if($forget){
    $_SESSION['conversations'] = null;
}
if($text){
  if (!isset($_SESSION['conversations'])) {
    $_SESSION['conversations'] = array();
  }

  if (count($_SESSION['conversations']) >= $number_of_interactions_to_remember) {
    array_shift($_SESSION['conversations']);
  }


  $data = array(
    'model' => 'gpt-3.5-turbo',
    'messages' => array(
      array(
        'role' => 'system',
        'content' => 'You are a helpful assistant called Frank that gives short, friendly reponses.'
      )
      
    )
  );

  foreach ($_SESSION['conversations'] as $conversation) {
    foreach ($conversation as $message) {
      array_push($data['messages'], array(
        'role' => $message['role'],
        'content' => $message['content']
      ));
    }
  }

  array_push($data['messages'], array(
    'role' => 'user',
    'content' => $text
  ));

  $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://api.openai.com/v1/chat/completions',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => json_encode($data),
    CURLOPT_HTTPHEADER => array(
      'Authorization: Bearer ' . $openai_api_key,
      'Content-Type: application/json'
    ),
  ));

  $response = curl_exec($curl);
  $response = json_decode($response, true);
  curl_close($curl);
  $content = $response['choices'][0]['message']['content'];

  $new_conversation = array(
    array(
      'role' => 'user',
      'content' => $text
    ),
    array(
      'role' => 'assistant',
      'content' => $content
    )
  );

  array_push($_SESSION['conversations'], $new_conversation);
  echo $content;
}
?>
现在已有 334 次阅读,5 条评论,0 人点赞
Author:Chrison
作者
ChatGPT之GPT 3.5 Turbo的API调用接口的方法
当前文章累计共 2413 字,阅读大概需要 1 分钟。
狗子成长记
2023年9月29日 · 35评论
科普| 物联网卡最全介绍(一)
2023年6月18日 · 6评论
微信工具类小程序
2023年2月13日 · 0评论
Comment:共5条
发表
  1. 头像
    @
    mango
    这个支持上下文关联吗?我第一次发送?text=广州灯光节在哪举行,然后再发送?text=举办时间是什么时候,他竟然回答不知道我问的是什么活动
    · Chrome · 中国广东省广州市电信

    👍

    💖

    💯

    💦

    😄

    🪙

    👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
    1. 头像
      @

      博主

      Chrison
      @mango
      本身是支持的,我代码里没加,因为会消耗使用次数。
      需要的话,可以看看API文档。将之前的对话保留,并和新问题一起提交。
      role使用assistant即可。
      可以参考https://blog.csdn.net/Xcodd/article/details/129321501
      · 火狐浏览器 · 中国江苏省苏州市电信

      👍

      💖

      💯

      💦

      😄

      🪙

      👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
  2. 头像
    @

    好友

    Teacher Du
    服务器可以放在国内吗?
    · Chrome · 中国北京市移动

    👍

    💖

    💯

    💦

    😄

    🪙

    👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
    1. 头像
      @
      现在不可以了,必须放在海外服务器了,最近刚被墙的
      · 火狐浏览器 · 中国北京市移动

      👍

      💖

      💯

      💦

      😄

      🪙

      👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
    2. 头像
      @
      不行了。。之前国内接口是可以直接访问的。。。后来有一天,我发现国内不能访问了。。。
      · 火狐浏览器 · 中国江苏省苏州市电信

      👍

      💖

      💯

      💦

      😄

      🪙

      👍 0 💖 0 💯 0 💦 0 😄 0 🪙 0
搜 索 消 息 足 迹
你还不曾留言过..
你还不曾留下足迹..
博主 不再显示
博主