From 30ff248bee7dbcb455de7f72ab32de491557adeb Mon Sep 17 00:00:00 2001 From: Bigsk Date: Tue, 16 May 2023 22:13:01 +0800 Subject: [PATCH] Added ChatGPT Bot --- chatgpt_bot/chatgpt_bot.py | 36 ++++++++++++++++++++++++++++++++++++ chatgpt_bot/requirements.txt | 1 + 2 files changed, 37 insertions(+) create mode 100644 chatgpt_bot/chatgpt_bot.py create mode 100644 chatgpt_bot/requirements.txt diff --git a/chatgpt_bot/chatgpt_bot.py b/chatgpt_bot/chatgpt_bot.py new file mode 100644 index 0000000..7644f0d --- /dev/null +++ b/chatgpt_bot/chatgpt_bot.py @@ -0,0 +1,36 @@ +import requests + +API_KEY = "YOUR API KEY" + +PROXIES = { + "http": "http://127.0.0.1:4780", + "https": "http://127.0.0.1:4780" +} + +MODEL = "gpt-3.5-turbo" + +history = [{"role": "system", "content": "你好"}] + +def chat(): + data = { + "messages": history, + "model": MODEL + } + + response = requests.post( + "https://api.openai.com/v1/chat/completions", + headers={"Authorization": f"Bearer {API_KEY}"}, + json=data, + proxies=PROXIES + ) + + message = response.json()["choices"][0]["message"]["content"].strip() + history.append(response.json()["choices"][0]["message"]) + + return message + +while True: + message = chat() + print("Bot:", message) + msg = input("You: ") + history.append({"role": "user", "content": msg}) diff --git a/chatgpt_bot/requirements.txt b/chatgpt_bot/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/chatgpt_bot/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file