forked from wxtcstt/MiChatGUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgptbot.py
31 lines (31 loc) · 1.11 KB
/
chatgptbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import openai
class ChatGPTBot():
def __init__(self, openai_key, api_base=None):
self.history = []
self.api_base = api_base
self.openai_key = openai_key
def clear(self):
self.history=[]
def ask(self, query):
try:
openai.api_key = self.openai_key
if self.api_base:
openai.api_base = self.api_base
ms = []
for h in self.history:
ms.append({"role": "user", "content": h[0]})
ms.append({"role": "assistant", "content": h[1]})
ms.append({"role": "user", "content": f"{query}"})
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=ms)
message = (
completion["choices"][0]
.get("message")
.get("content")
.encode("utf8")
.decode()
)
self.history.append([f"{query}", message])
self.history = self.history[-5:]
return message
except Exception as e:
return "-1"