Fix !chat !new commands

This commit is contained in:
hibobmaster 2023-09-16 15:35:18 +08:00
parent 8512e3ea22
commit 180826534b
No known key found for this signature in database
3 changed files with 31 additions and 63 deletions

4
.gitignore vendored
View File

@ -168,3 +168,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear # 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. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/ .idea/
# Custom
sync_db
manage_db

View File

@ -162,11 +162,6 @@ class Bot:
task.cancel() task.cancel()
logger.info("Bot closed!") 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 # message_callback RoomMessageText event
async def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None: async def message_callback(self, room: MatrixRoom, event: RoomMessageText) -> None:
if self.room_id is None: if self.room_id is None:
@ -219,8 +214,6 @@ class Bot:
# chatgpt # chatgpt
n = self.chat_prog.match(content_body) n = self.chat_prog.match(content_body)
if n: if n:
if sender_id not in self.chatgpt_data:
self.chatgpt_session_init(sender_id)
prompt = n.group(1) prompt = n.group(1)
if self.openai_api_key is not None: if self.openai_api_key is not None:
try: try:
@ -535,36 +528,10 @@ class Bot:
): ):
try: try:
await self.client.room_typing(room_id, timeout=int(self.timeout) * 1000) await self.client.room_typing(room_id, timeout=int(self.timeout) * 1000)
if ( content = await self.chatbot.ask_async(
self.chatgpt_data[sender_id]["first_time"] prompt=prompt,
or "conversationId" not in self.chatgpt_data[sender_id] convo_id=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,
},
}
}
) )
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( await send_room_message(
self.client, self.client,
room_id, room_id,
@ -574,11 +541,8 @@ class Bot:
user_message=raw_user_message, user_message=raw_user_message,
) )
except Exception: except Exception:
await send_room_message( await self.send_general_error_message(
self.client, room_id, reply_to_event_id, sender_id, raw_user_message
room_id,
reply_message=GENERAL_ERROR_MESSAGE,
reply_to_event_id=reply_to_event_id,
) )
# !gpt command # !gpt command
@ -601,11 +565,8 @@ class Bot:
user_message=raw_user_message, user_message=raw_user_message,
) )
except Exception: except Exception:
await send_room_message( await self.send_general_error_message(
self.client, room_id, reply_to_event_id, sender_id, raw_user_message
room_id,
reply_message=GENERAL_ERROR_MESSAGE,
reply_to_event_id=reply_to_event_id,
) )
# !lc command # !lc command
@ -633,11 +594,8 @@ class Bot:
user_message=raw_user_message, user_message=raw_user_message,
) )
except Exception: except Exception:
await send_room_message( await self.send_general_error_message(
self.client, room_id, reply_to_event_id, sender_id, raw_user_message
room_id,
reply_message=GENERAL_ERROR_MESSAGE,
reply_to_event_id=reply_to_event_id,
) )
# !new command # !new command
@ -651,12 +609,12 @@ class Bot:
) -> None: ) -> None:
try: try:
if "chat" in new_command: if "chat" in new_command:
self.chatgpt_session_init(sender_id) self.chatbot.reset(convo_id=sender_id)
content = ( content = (
"New conversation created, please use !chat to start chatting!" "New conversation created, please use !chat to start chatting!"
) )
else: 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( await send_room_message(
self.client, self.client,
@ -667,11 +625,8 @@ class Bot:
user_message=raw_user_message, user_message=raw_user_message,
) )
except Exception: except Exception:
await send_room_message( await self.send_general_error_message(
self.client, room_id, reply_to_event_id, sender_id, raw_user_message
room_id,
reply_message=GENERAL_ERROR_MESSAGE,
reply_to_event_id=reply_to_event_id,
) )
# !pic command # !pic command
@ -700,12 +655,8 @@ class Bot:
help_info = ( help_info = (
"!gpt [prompt], generate a one time response without context conversation\n" "!gpt [prompt], generate a one time response without context conversation\n"
+ "!chat [prompt], chat with 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" + "!pic [prompt], Image generation by Microsoft Bing\n"
+ "!talk [content], talk using chatgpt web (pandora)\n" + "!new + chat, start a new conversation \n"
+ "!goon, continue the incomplete conversation (pandora)\n"
+ "!new + [chat,bing,talk,bard], start a new conversation \n"
+ "!lc [prompt], chat using langchain api\n" + "!lc [prompt], chat using langchain api\n"
+ "!help, help message" + "!help, help message"
) # noqa: E501 ) # noqa: E501
@ -719,6 +670,19 @@ class Bot:
reply_to_event_id=reply_to_event_id, 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 # bot login
async def login(self) -> None: async def login(self) -> None:
resp = await self.client.login(password=self.password, device_name=DEVICE_NAME) resp = await self.client.login(password=self.password, device_name=DEVICE_NAME)

BIN
sync_db

Binary file not shown.