存档

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

2023-02-08  本文已影响0人  张清柏

什么是ChatGPT

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

ChatGPT可以做什么?

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


在这里插入图片描述
在这里插入图片描述

怎么得到ChatGPT

验证码的获取

在这里插入图片描述
// 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

怎么调用api接口

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}}
    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'];
        }
    }

上一篇下一篇

猜你喜欢

热点阅读