ChatGPT为什么这么火爆?这是一篇从入门到玩坏的教程

2023-02-09 15:52:26 598 林溪

什么是ChatGPT

ChatGPT是由OpenAI开发的一个人工智能聊天机器人程序,于2022年11月推出。该程序使用基于GPT-3.5架构的大型语言模型並通过强化学习进行训练。

ChatGPT可以做什么?

你能想到的,它基本上都能和你聊上两句,比如写一个起个名字啊,写个论文啊,讲个鬼故事,写段代码啥的 我们先来用几张图片来看一下吧

怎么得到ChatGPT

  • 第一步肯定是注册了,注册地址是https://beta.openai.com/signup ,但是只知道注册地址你可能也注册不了,Not available,哈哈,中国地区不能用,

  • 那怎么办呢?挂个节点吧,GitHub上面有很多免费节点,注意找国外的。开启全局模式。

  • 开启代理之后,访问http://ip111.cn/ 看一下自己的节点是不是国外,我这里是德国的

  • 好了,基本条件已经满足了,为了避免浏览器缓存cdn造成不必要的麻烦,浏览器另开一个无痕浏览,这个时候再访问https://beta.openai.com/signup,发现可以注册了,可以使用邮箱,谷歌或者微软账户,我这里使用邮箱注册.

  • 建议使用国外邮箱或者qq邮箱(qq邮箱原来是Foxmail收购来的,也是国际通用的),注册完毕之后,会让你去邮箱激活

  • 去你的邮箱里面点击链接激活就可以了,如果没找到,看看是不是在垃圾箱里

  • 页面跳转后填写昵称

  • 点击continue,接下来难题来了,让输入验证码。必须得是国外的手机号

验证码的获取

  • 我们可以通过验证码接收平台获取验证码,https://sms-activate.org/ 注意这个是收费的,但是0.5美元就可以解决,支持支某宝支付

  • 先注册,登录注册略过

  • 登录之后点击充值,最低0.5美元,不到4块钱人民币,这里展示的是卢布,36.58卢布,价格也是按卢布结算

  • 点击左边栏 openapi,点击购物车即可完成购买

  • 右侧栏会出现你购买的手机号,有效时间20分钟,如果收不到验证码可以点击❎关闭激活,重新选一个

  • 输入手机号之后获取验证码

  • 稍微等会就可以接收到验证码了,输入即可激活成功

  • 此时关闭代理,访问https://chat.openai.com/,即可愉快聊天,可以设置为暗黑模式

  • 注意,登录的时候需要开启代理,登录成功之后需要关闭代理,否则会出现下面报错Error reference number: 1020

  • 输入框内输入聊天

  • 写个20个字的恐怖小说

在这里插入图片描述
  • 用php写一段快速排序,看样子,我们离失业不远了
  • 用js写一段21点的游戏
// Utility function to get a random number between min and max (inclusive)
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Function to play the game
function play21Points() {
  let totalPoints = 0;

  while (totalPoints < 21) {
    let newCard = getRandomInt(1, 11);
    totalPoints += newCard;
    console.log(`New card: ${newCard}. Total points: ${totalPoints}.`);

    if (totalPoints > 21) {
      console.log(`Busted with ${totalPoints} points!`);
      break;
    }
  }

  if (totalPoints === 21) {
    console.log(`You win with 21 points!`);
  }
}

// Call the function to play the game
play21Points();

  • 应该有训练模型,可以给它灌输思想,比如
  • 写个论文

调用ChatGPT的api,实现智能聊天

ChatGPT怎么接入api
  • ChatGPT目前还没有api接口,但是我们可以调用一个相关的模型,叫GPT-3 。 但是这种调用就是一问一答模式的 ,没有上下文的记忆。

怎么调用api接口

  • 登录咱们刚才注册的账号,登录地址https://platform.openai.com/
  • 查看documentation,在https://platform.openai.com/docs/api-reference/authentication地址里面会有一些例子
  • 看到了需要一个API key,点击https://platform.openai.com/account/api-keys去查看你的key,如果没有可以自己创建,创建后记得保存key到记事本
  • 因为是按字数收费的,所以我们尽量控制一下返回字段的长度,我这里用curl的方式举个例子
curl https://api.openai.com/v1/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer api_token" \
-d '{"model": "text-davinci-003", "prompt": "中国的首都是哪里", "temperature": 0, "max_tokens": 70}'
  • 返回结果
{"id":"cmpl-6htm4YoGJRfAIRHpXaicbKxZdzKJC","object":"text_completion","created":1675920520,"model":"text-davinci-003","choices":[{"text":"\n\n中国的首都是北京。","index":0,"logprobs":null,"finish_reason":"stop"}],"usage":{"prompt_tokens":15,"completion_tokens":17,"total_tokens":32}}
  • 相信看到这里,小伙伴们已经知道了api的使用方法
  • 本来我打算接入到微信公众号的,但是公众号回复消息不能超过5秒,失败率太高了,不过有兴趣的同学可以通过主动回复消息的方式来完成,我把主要代码贴下。
    public function chatgpt($str)
    {
        // Set up the cURL request
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://api.openai.com/v1/completions");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, '{
  "model": "text-davinci-003",
  "prompt": "' . $str . '",
  "max_tokens": 700
  }');
        curl_setopt($ch, CURLOPT_POST, 1);

// Set the API key as an HTTP header
        $headers = array();
        $headers[] = "Content-Type: application/json";
        $headers[] = "Authorization: Bearer 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";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Send the request and parse the response
        $response = curl_exec($ch);
        $response_data = json_decode($response, true);
//        var_dump($response_data);die;

        if (curl_errno($ch)) {
            // If there was an error executing the cURL request, print it out
            Log::info('Error: ' . curl_error($ch));
            curl_close($ch);
            return '';
        } else {

            if(isset($response_data['error'])&&!empty($response_data['error'])){
                return '';
            }

            // Otherwise, print the response from the GPT-3 API
            Log::info($response_data['choices'][0]['text']);
            curl_close($ch);
            return $response_data['choices'][0]['text'];
        }
    }