乐趣区

关于chatgpt:OpenAI-ChatGPT-API-尝新

这里简略用了 Open AI 最新的 ChatGPT API gpt-3.5-turbo,并且加上了分割上下文性能

  1. 注册 OpenAI 账号
    https://platform.openai.com/

$$\quad$$

  1. 获取 OpenAI API Key

$$\quad$$

  1. 将下面的 Key 更换掉上面代码中的 openai.api_key

装置 open ai 依赖

pip install openai

运行上面代码

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
# -- coding: utf-8 --
import openai

# please replace the key here with your own key
openai.api_key = "your key"


def chatgpt():
    messages = []
    while True:
        input_ = input("please input your questions:")
        messages.append({"role": "user", "content": input_})
        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=messages
        )

        # for decoding in Chinese
        answer = response['choices'][0]['message']['content'].encode('utf-8').decode('utf-8')
        messages.append({"role": response['choices'][0]['message']['role'], "content": answer})

        print(answer)

chatgpt()

这样就可能高兴游玩啦!🎉

退出移动版