Fallback to fixed response when ChatGPT is unavailable

This commit is contained in:
Changhua
2023-04-27 02:18:59 +08:00
parent d3547b85c3
commit c59de3cf14

View File

@@ -31,8 +31,7 @@ class Robot(Job):
self.chat = ChatGPT(chatgpt.get("key"), chatgpt.get("api"), chatgpt.get("proxy")) self.chat = ChatGPT(chatgpt.get("key"), chatgpt.get("api"), chatgpt.get("proxy"))
def toAt(self, msg: Wcf.WxMsg) -> bool: def toAt(self, msg: Wcf.WxMsg) -> bool:
""" """处理被 @ 消息
处理被 @ 消息,现在只固定回复: "你@我干嘛?"
:param msg: 微信消息结构 :param msg: 微信消息结构
:return: 处理状态,`True` 成功,`False` 失败 :return: 处理状态,`True` 成功,`False` 失败
""" """
@@ -68,11 +67,12 @@ class Robot(Job):
def toChitchat(self, msg: Wcf.WxMsg) -> bool: def toChitchat(self, msg: Wcf.WxMsg) -> bool:
"""闲聊,接入 ChatGPT """闲聊,接入 ChatGPT
""" """
if not self.chat: if not self.chat: # 没接 ChatGPT固定回复
return False rsp = "你@我干嘛?"
else: # 接了 ChatGPT智能回复
q = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "")
rsp = self.chat.get_answer(q, (msg.roomid if msg.from_group() else msg.sender))
q = re.sub(r"@.*?[\u2005|\s]", "", msg.content).replace(" ", "")
rsp = self.chat.get_answer(q, (msg.roomid if msg.from_group() else msg.sender))
if rsp: if rsp:
if msg.from_group(): if msg.from_group():
self.sendTextMsg(rsp, msg.roomid, msg.sender) self.sendTextMsg(rsp, msg.roomid, msg.sender)
@@ -81,7 +81,7 @@ class Robot(Job):
return True return True
else: else:
self.LOG.error(f"无法从ChatGPT获得答案") self.LOG.error(f"无法从 ChatGPT 获得答案")
return False return False
def processMsg(self, msg: Wcf.WxMsg) -> None: def processMsg(self, msg: Wcf.WxMsg) -> None: