From 180826534b9fbd6edbc40ac4862b10b43c183906 Mon Sep 17 00:00:00 2001 From: hibobmaster <32976627+hibobmaster@users.noreply.github.com> Date: Sat, 16 Sep 2023 15:35:18 +0800 Subject: [PATCH] Fix !chat !new commands --- .gitignore | 4 +++ src/bot.py | 90 ++++++++++++++++------------------------------------- sync_db | Bin 135168 -> 0 bytes 3 files changed, 31 insertions(+), 63 deletions(-) delete mode 100644 sync_db diff --git a/.gitignore b/.gitignore index 67b91f9..9f22577 100644 --- a/.gitignore +++ b/.gitignore @@ -168,3 +168,7 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ + +# Custom +sync_db +manage_db diff --git a/src/bot.py b/src/bot.py index de785ef..74f498e 100644 --- a/src/bot.py +++ b/src/bot.py @@ -162,11 +162,6 @@ class Bot: task.cancel() logger.info("Bot closed!") - def chatgpt_session_init(self, sender_id: str) -> None: - self.chatgpt_data[sender_id] = { - "first_time": True, - } - # message_callback RoomMessageText event async def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None: if self.room_id is None: @@ -219,8 +214,6 @@ class Bot: # chatgpt n = self.chat_prog.match(content_body) if n: - if sender_id not in self.chatgpt_data: - self.chatgpt_session_init(sender_id) prompt = n.group(1) if self.openai_api_key is not None: try: @@ -535,36 +528,10 @@ class Bot: ): try: await self.client.room_typing(room_id, timeout=int(self.timeout) * 1000) - if ( - self.chatgpt_data[sender_id]["first_time"] - or "conversationId" not in self.chatgpt_data[sender_id] - ): - self.chatgpt_data[sender_id]["first_time"] = False - payload = { - "message": prompt, - } - else: - payload = { - "message": prompt, - "conversationId": self.chatgpt_data[sender_id]["conversationId"], - "parentMessageId": self.chatgpt_data[sender_id]["parentMessageId"], - } - payload.update( - { - "clientOptions": { - "clientToUse": "chatgpt", - "openaiApiKey": self.openai_api_key, - "modelOptions": { - "temperature": self.temperature, - }, - } - } + content = await self.chatbot.ask_async( + prompt=prompt, + convo_id=sender_id, ) - resp = await self.gptbot.queryChatGPT(payload) - content = resp["response"] - self.chatgpt_data[sender_id]["conversationId"] = resp["conversationId"] - self.chatgpt_data[sender_id]["parentMessageId"] = resp["messageId"] - await send_room_message( self.client, room_id, @@ -574,11 +541,8 @@ class Bot: user_message=raw_user_message, ) except Exception: - await send_room_message( - self.client, - room_id, - reply_message=GENERAL_ERROR_MESSAGE, - reply_to_event_id=reply_to_event_id, + await self.send_general_error_message( + room_id, reply_to_event_id, sender_id, raw_user_message ) # !gpt command @@ -601,11 +565,8 @@ class Bot: user_message=raw_user_message, ) except Exception: - await send_room_message( - self.client, - room_id, - reply_message=GENERAL_ERROR_MESSAGE, - reply_to_event_id=reply_to_event_id, + await self.send_general_error_message( + room_id, reply_to_event_id, sender_id, raw_user_message ) # !lc command @@ -633,11 +594,8 @@ class Bot: user_message=raw_user_message, ) except Exception: - await send_room_message( - self.client, - room_id, - reply_message=GENERAL_ERROR_MESSAGE, - reply_to_event_id=reply_to_event_id, + await self.send_general_error_message( + room_id, reply_to_event_id, sender_id, raw_user_message ) # !new command @@ -651,12 +609,12 @@ class Bot: ) -> None: try: if "chat" in new_command: - self.chatgpt_session_init(sender_id) + self.chatbot.reset(convo_id=sender_id) content = ( "New conversation created, please use !chat to start chatting!" ) else: - content = "Unkown keyword, please use !help to see the usage!" + content = "Unkown keyword, please use !help to get available commands" await send_room_message( self.client, @@ -667,11 +625,8 @@ class Bot: user_message=raw_user_message, ) except Exception: - await send_room_message( - self.client, - room_id, - reply_message=GENERAL_ERROR_MESSAGE, - reply_to_event_id=reply_to_event_id, + await self.send_general_error_message( + room_id, reply_to_event_id, sender_id, raw_user_message ) # !pic command @@ -700,12 +655,8 @@ class Bot: help_info = ( "!gpt [prompt], generate a one time response without context conversation\n" + "!chat [prompt], chat with context conversation\n" - + "!bing [prompt], chat with context conversation powered by Bing AI\n" - + "!bard [prompt], chat with Google's Bard\n" + "!pic [prompt], Image generation by Microsoft Bing\n" - + "!talk [content], talk using chatgpt web (pandora)\n" - + "!goon, continue the incomplete conversation (pandora)\n" - + "!new + [chat,bing,talk,bard], start a new conversation \n" + + "!new + chat, start a new conversation \n" + "!lc [prompt], chat using langchain api\n" + "!help, help message" ) # noqa: E501 @@ -719,6 +670,19 @@ class Bot: reply_to_event_id=reply_to_event_id, ) + # send general error message + async def send_general_error_message( + self, room_id, reply_to_event_id, sender_id, user_message + ): + await send_room_message( + self.client, + room_id, + reply_message=GENERAL_ERROR_MESSAGE, + reply_to_event_id=reply_to_event_id, + sender_id=sender_id, + user_message=user_message, + ) + # bot login async def login(self) -> None: resp = await self.client.login(password=self.password, device_name=DEVICE_NAME) diff --git a/sync_db b/sync_db deleted file mode 100644 index 27d67defc88e90fa56c06fd83827507bee30e935..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 135168 zcmeI*$*<#DdKmDk9$vlI({s}9lxJ7JPQxj%ZH^K}8-dMv6e*Du2PHv)q$r9M2XQ6^ zBt~^R3$L>akX_b6w%KH#EWJxM`5Tf&kYN*6y`jeEbP#87;RyHMl1Pf;`JIo4=bVpq zueI0GE`D;Znj+|);17P~!NZ3S{^ZG%2M-=Tdhp=EkC*Q+A3t24eCx{>%fAm_d;T>~ zmwo&}`LDkHV&hN$`-^RU>+^rUW^=F@2mk>f00e*l5C8%|00;m9AOHk_01)^;An+UC z|NgSqhoAnp2g?_D00AHX1b_e#00KY&2mk>f00e*l5C8%noWQppe)GY@Z@e7;f9EeA zEMMRO1b_e#00KY&2mk>f00jOT3jEvOefaT%=l^7afB%y>C1P~AFzq1Vv3lo8Q*Q&a zr8nI@P8yE92y&TF7Cl~}acn%v3}xn)kQ#Gs<-3yR$CF2{vmnOC^A)jfn;h+$>k4DY zsY;zRAFqaC914*N&s!|N?{AmwWit9>P(-S+UG^0;jY@I%f#bsX8Vp+;a;|~nHWkEF zjJcCf1s|OdY=jbsW6zOsYr7rNj45&@+2LGX_x!xudu?6_1FgsD5!vHW!dO9vb0KSa z?u8pDLKx4mI-QXm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