-
Notifications
You must be signed in to change notification settings - Fork 8
/
chatGPT.py
87 lines (75 loc) · 2.7 KB
/
chatGPT.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import uuid
from os import getenv
from revChatGPT.typing import Error
from revChatGPT.V1 import Chatbot
from revChatGPT.V3 import Chatbot as ChatbotByApi
from util import getLogger
logger = getLogger("chatGPT")
class ChatGPT:
_instance = None
chatbot = None
api_key = getenv("ApiKey")
def __new__(cls, *args, **kw):
if cls._instance is None:
cls._instance = object.__new__(cls, *args, **kw)
return cls._instance
def __init__(self) -> None:
if self.chatbot == None:
if self.api_key:
self.chatbot = ChatbotByApi(api_key=self.api_key, proxy=getenv("Proxy"))
else:
self.chatbot = Chatbot(
config={
"proxy": getenv("Proxy"),
"access_token": getenv("AccessToken"),
}
)
def sendMessage(self, input):
try:
result = "No result!"
if self.api_key:
result = self.chatbot.ask(input)
else:
for data in self.chatbot.ask(
input,
conversation_id=getenv("ConversationId"),
parent_id=getenv("ParentId"),
):
result = data["message"]
except Error as e:
result = "Error happend!"
logger.error(e.message)
# log message
logger.info(f"Q::{input}")
logger.info(f"A::{result}")
print()
return result
def sendMessageWithStream(self, input):
try:
logger.info(f"Q::{input}")
prev_text = ""
for data in self.chatbot.ask(
input,
conversation_id=getenv("ConversationId"),
parent_id=getenv("ParentId"),
):
if self.api_key:
print(data, end="", flush=True)
else:
# print log message on terminal screen
message = data["message"][len(prev_text) :]
print(message, end="", flush=True)
prev_text = data["message"]
# custom event `chatgpt`
yield f"id: {uuid.uuid1()}\n".encode("utf-8")
yield "event: chatgpt\n".encode("utf-8")
# encode '\n'
yield f"data: {repr(data if self.api_key else data['message'])}\n\n".encode(
"utf-8"
)
print()
yield "event: [Done]\n".encode("utf-8")
except Error as e:
logger.error(e.message)
yield "event: fail\n".encode("utf-8")
yield f"data: {repr(e)}\n\n".encode("utf-8")