Skip to content

Commit

Permalink
feat: speak more
Browse files Browse the repository at this point in the history
  • Loading branch information
lss233 committed Dec 14, 2022
1 parent ee98381 commit 88cf074
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
18 changes: 10 additions & 8 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ async def handle_message(id: str, message: str, timeout_task: asyncio.Task) -> s
return config.response.rollback_success
else:
return config.response.rollback_fail
try:
resp = await session.get_chat_response(message)
logger.debug(f"{id} - {resp}")
timeout_task.cancel()
return resp["message"]
except Exception as e:
# session.reset_conversation()
await bot.refresh_session()
resp, e = await session.get_chat_response(message)
logger.debug(f"{id} - {resp}")
timeout_task.cancel()
if e:
logger.exception(e)
timeout_task.cancel()
refresh_task = bot.refresh_session()
if refresh_task:
await refresh_task
if resp:
return resp["message"]
if e:
return config.response.error_format.format(exc=e)

async def send_task(target: Union[Friend, Group], app: Ariadne, source: Source):
Expand Down
20 changes: 14 additions & 6 deletions chatbot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from revChatGPT.revChatGPT import AsyncChatbot, generate_uuid
from charset_normalizer import from_bytes
from typing import Awaitable, Any, Dict
from typing import Awaitable, Any, Dict, Tuple
from config import Config
from loguru import logger
import json
Expand Down Expand Up @@ -63,15 +63,23 @@ def rollback_conversation(self) -> bool:
self.conversation_id = self.prev_conversation_id.pop()
self.parent_id = self.prev_parent_id.pop()
return True
async def get_chat_response(self, message, output="text") -> Dict[str, Any]:
async def get_chat_response(self, message) -> Tuple[Dict[str, Any], Exception]:
self.prev_conversation_id.append(self.conversation_id)
self.prev_parent_id.append(self.parent_id)
bot.conversation_id = self.conversation_id
bot.parent_id = self.parent_id
resp = await bot.get_chat_response(message, output=output)
self.conversation_id = resp["conversation_id"]
self.parent_id = resp["parent_id"]
return resp
final_resp = None
exception = None
try:
async for resp in await bot.get_chat_response(message, output="stream"):
if final_resp is None:
logger.debug("已收到回应,正在接收中……")
self.conversation_id = resp["conversation_id"]
self.parent_id = resp["parent_id"]
final_resp = resp
except Exception as e:
exception = e
return final_resp, exception
sessions = {}


Expand Down

0 comments on commit 88cf074

Please sign in to comment.