前言

想用chatGPT的api,但是免费额度在4月1日都过期了,不能白嫖,翻了一下github,找到了这个项目:https://github.com/acheong08/ChatGPT(暂时不建议大家使用,看了issue说会封号,等我过段时间来现身说法)

记录一下坑点和常用代码

官方中文文档:https://github.com/acheong08/ChatGPT/blob/main/docs/README_zh.md

官方中文Wiki:https://github.com/CoolPlayLin/ChatGPT-Wiki/tree/master/docs/ChatGPT

注意点

一定要用符合版本的Python,不然可能会下载到历史版本,比如我之前用python3.7.9就一直只能下载3.0.2版本

python3.10 -m pip install --upgrade revChatGPT

使用

会话

from revChatGPT.V1 import Chatbot

chatbot = Chatbot(config={
  "access_token": "https://chat.openai.com/api/auth/session # - accessToken"
})

conversation_id = None    # 会话ID,让所有回答在一个会话中
while True:
    try:
        prompt = input("\n\nquestion: ")    # 问题
        prev_text = ""  # 回答
        print("answer: ")
        # 交互过程,轮询获取结果,prev_text是最终的回答变量
        for data in chatbot.ask(prompt, conversation_id=conversation_id):
            message = data["message"][len(prev_text) :]
            # 连续输出内容
            print(message, end="", flush=True)
            prev_text = data["message"]
            conversation_id = data["conversation_id"]
            parent_id = data["parent_id"]
            model = data["model"]
            finish_details = data["finish_details"]
            recipient = data["recipient"]
        # print(f"\n\nanswer: {prev_text}") # 最终结果
    except KeyboardInterrupt:
        print("KeyboardInterrupt,删除所有会话")
        break
    except Exception as ex:
        print(f"Error: {ex}")

# 删除所有会话
chatbot.clear_conversations()

其他

https://github.com/CoolPlayLin/ChatGPT-Wiki/blob/master/docs/ChatGPT/V1.md

直接在revChatGPT.V1.Chatbot中看函数即可,都有说明,比较清晰

Copyright © d4m1ts 2023 all right reserved,powered by Gitbook该文章修订时间: 2023-04-20 14:11:22

results matching ""

    No results matching ""