头图

这里简单用了 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()

这样就能够快乐玩耍啦!🎉


02
1 声望1 粉丝