fix: 🐛 minor API bug fix

Delete the chat session from class
This commit is contained in:
Grey_D
2023-03-21 13:16:14 +08:00
parent de9010d25e
commit 2166113902
6 changed files with 131 additions and 44 deletions

0
utils/__init__.py Normal file
View File

View File

@@ -13,6 +13,18 @@ from config.chatgpt_config import ChatGPTConfig
logger = loguru.logger
# A sample ChatGPTConfig class has the following structure. All fields can be obtained from the browser's cookie.
# In particular, cf_clearance、__Secure-next-auth.session-token、_puid are required.
# @dataclasses.dataclass
# class ChatGPTConfig:
# model: str = "text-davinci-002-render-sha"
# _puid: str = ""
# cf_clearance: str = ""
# session_token: str = ""
# error_wait_time: float = 20
# is_debugging: bool = False
class ChatGPT:
def __init__(self, config: ChatGPTConfig):
@@ -42,13 +54,11 @@ class ChatGPT:
return "Bearer " + authorization
def get_latest_message_id(self, conversation_id):
# 获取会话窗口最新消息id连续对话必须
url = f"https://chat.openai.com/backend-api/conversation/{conversation_id}"
r = requests.get(url, headers=self.headers, proxies=self.proxies)
return r.json()["current_node"]
def send_new_message(self, message):
# 发送新会话窗口消息返回会话id
logger.info(f"send_new_message")
url = "https://chat.openai.com/backend-api/conversation"
message_id = str(uuid1())
@@ -67,7 +77,6 @@ class ChatGPT:
r = requests.post(url, headers=self.headers, json=data, proxies=self.proxies)
if r.status_code != 200:
# 发送消息阻塞时等待20秒从新发送
logger.error(r.json()["detail"])
time.sleep(self.config.error_wait_time)
return self.send_new_message(message)
@@ -89,10 +98,8 @@ class ChatGPT:
return text, conversation_id
def send_message(self, message, conversation_id):
# 指定会话窗口发送连续对话消息
logger.info(f"send_message")
url = "https://chat.openai.com/backend-api/conversation"
# 获取会话窗口最新消息id
if conversation_id not in self.latest_message_id_dict:
logger.info(f"conversation_id: {conversation_id}")
message_id = self.get_latest_message_id(conversation_id)
@@ -115,7 +122,6 @@ class ChatGPT:
}
r = requests.post(url, headers=self.headers, json=data, proxies=self.proxies)
if r.status_code != 200:
# 发送消息阻塞时等待20秒从新发送
logger.warning(r.json()["detail"])
time.sleep(self.config.error_wait_time)
return self.send_message(message, conversation_id)
@@ -158,6 +164,11 @@ class ChatGPT:
"is_visible": False,
}
r = requests.patch(url, headers=self.headers, json=data, proxies=self.proxies)
# delete conversation id locally
if conversation_id in self.latest_message_id_dict:
del self.latest_message_id_dict[conversation_id]
if r.status_code == 200:
return True
else:

View File

@@ -20,20 +20,21 @@ class ChatGPTBrowser:
The ChatGPT Wrapper based on browser (playwright).
It keeps the same interface as ChatGPT.
"""
def __init__(self, model=None):
config = Config()
if model is not None:
config.set('chat.model', model)
config.set("chat.model", model)
self.bot = ChatGPT(config)
def get_authorization(self):
# TODO: get authorization from browser
return
return
def get_latest_message_id(self, conversation_id):
# TODO: get latest message id from browser
return
return
def get_conversation_history(self, limit=20, offset=0):
# Get the conversation id in the history
return self.bot.get_history(limit, offset)
@@ -52,16 +53,15 @@ class ChatGPTBrowser:
def extract_code_fragments(self, text):
code_fragments = re.findall(r"```(.*?)```", text, re.DOTALL)
return code_fragments
def delete_conversation(self, conversation_id=None):
# delete conversation with its uuid
if conversation_id is not None:
self.bot.delete_conversation(conversation_id)
if __name__ == "__main__":
chatgptBrowser_session = ChatGPTBrowser()
text, conversation_id = chatgptBrowser_session.send_new_message(
"I am a new tester for RESTful APIs."
)