欢迎光临散文网 会员登陆 & 注册

python代码

2023-06-13 13:45 作者:岑今6666  | 我要投稿

#.env内容为API_KEY=sk-写自己的

import openai

from dotenv import dotenv_values


class ChatBox:

    def __init__(self):

        config = dotenv_values(".env")

        openai.api_key = config["API_KEY"]

        self.model = "text-davinci-003"

        self.chat_history = []


    def add_chat(self, message):

        response = openai.Completion.create(

            model=self.model,

            prompt=message,

            max_tokens=200

        )

        answer = response.choices[0].text.strip()

        self.chat_history.append((message, answer))

        return answer


    def get_chat_history(self):

        return self.chat_history


    def clear_chat_history(self):

        self.chat_history = []

chatbox = ChatBox()


while True:

    user_input = input("User: ")

    if user_input.lower() == "exit":

        break

    answer = chatbox.add_chat(user_input)

    print("ChatGPT:", answer)


python代码的评论 (共 条)

分享到微博请遵守国家法律