Added ChatGPT Bot
This commit is contained in:
parent
536a648013
commit
30ff248bee
36
chatgpt_bot/chatgpt_bot.py
Normal file
36
chatgpt_bot/chatgpt_bot.py
Normal file
@ -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})
|
1
chatgpt_bot/requirements.txt
Normal file
1
chatgpt_bot/requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
requests
|
Loading…
Reference in New Issue
Block a user