@@ -91,5 +91,5 @@ class OpenAIBot(Bot, OpenAIImage): | |||||
except Exception as e: | except Exception as e: | ||||
# unknown exception | # unknown exception | ||||
logger.exception(e) | logger.exception(e) | ||||
Session.clear_session(user_id) | |||||
self.sessions.clear_session(user_id) | |||||
return 0,0, "请再问我一次吧" | return 0,0, "请再问我一次吧" |
@@ -40,6 +40,10 @@ class SessionManager(object): | |||||
self.session_args = session_args | self.session_args = session_args | ||||
def build_session(self, session_id, system_prompt=None): | def build_session(self, session_id, system_prompt=None): | ||||
''' | |||||
如果session_id不在sessions中,创建一个新的session并添加到sessions中 | |||||
如果system_prompt不会空,会更新session的system_prompt并重置session | |||||
''' | |||||
if session_id not in self.sessions: | if session_id not in self.sessions: | ||||
self.sessions[session_id] = self.sessioncls(session_id, system_prompt, **self.session_args) | self.sessions[session_id] = self.sessioncls(session_id, system_prompt, **self.session_args) | ||||
elif system_prompt is not None: # 如果有新的system_prompt,更新并重置session | elif system_prompt is not None: # 如果有新的system_prompt,更新并重置session | ||||
@@ -70,7 +74,8 @@ class SessionManager(object): | |||||
return session | return session | ||||
def clear_session(self, session_id): | def clear_session(self, session_id): | ||||
del(self.sessions[session_id]) | |||||
if session_id in self.sessions: | |||||
del(self.sessions[session_id]) | |||||
def clear_all_session(self): | def clear_all_session(self): | ||||
self.sessions.clear() | self.sessions.clear() |
@@ -52,7 +52,7 @@ class Dungeon(Plugin): | |||||
if e_context['context'].type != ContextType.TEXT: | if e_context['context'].type != ContextType.TEXT: | ||||
return | return | ||||
bottype = Bridge().get_bot_type("chat") | bottype = Bridge().get_bot_type("chat") | ||||
if bottype != const.CHATGPT: | |||||
if bottype not in (const.CHATGPT, const.OPEN_AI): | |||||
return | return | ||||
bot = Bridge().get_bot("chat") | bot = Bridge().get_bot("chat") | ||||
content = e_context['context'].content[:] | content = e_context['context'].content[:] | ||||
@@ -179,7 +179,7 @@ class Godcmd(Plugin): | |||||
elif cmd == "id": | elif cmd == "id": | ||||
ok, result = True, f"用户id=\n{user}" | ok, result = True, f"用户id=\n{user}" | ||||
elif cmd == "reset": | elif cmd == "reset": | ||||
if bottype == const.CHATGPT: | |||||
if bottype in (const.CHATGPT, const.OPEN_AI): | |||||
bot.sessions.clear_session(session_id) | bot.sessions.clear_session(session_id) | ||||
ok, result = True, "会话已重置" | ok, result = True, "会话已重置" | ||||
else: | else: | ||||
@@ -201,7 +201,7 @@ class Godcmd(Plugin): | |||||
load_config() | load_config() | ||||
ok, result = True, "配置已重载" | ok, result = True, "配置已重载" | ||||
elif cmd == "resetall": | elif cmd == "resetall": | ||||
if bottype == const.CHATGPT: | |||||
if bottype in (const.CHATGPT, const.OPEN_AI): | |||||
bot.sessions.clear_all_session() | bot.sessions.clear_all_session() | ||||
ok, result = True, "重置所有会话成功" | ok, result = True, "重置所有会话成功" | ||||
else: | else: | ||||
@@ -17,15 +17,15 @@ class RolePlay(): | |||||
self.sessionid = sessionid | self.sessionid = sessionid | ||||
self.wrapper = wrapper or "%s" # 用于包装用户输入 | self.wrapper = wrapper or "%s" # 用于包装用户输入 | ||||
self.desc = desc | self.desc = desc | ||||
self.bot.sessions.build_session(self.sessionid, system_prompt=self.desc) | |||||
def reset(self): | def reset(self): | ||||
self.bot.sessions.clear_session(self.sessionid) | self.bot.sessions.clear_session(self.sessionid) | ||||
def action(self, user_action): | def action(self, user_action): | ||||
session = self.bot.sessions.build_session(self.sessionid, self.desc) | |||||
if session[0]['role'] == 'system' and session[0]['content'] != self.desc: # 目前没有触发session过期事件,这里先简单判断,然后重置 | |||||
self.reset() | |||||
self.bot.sessions.build_session(self.sessionid, self.desc) | |||||
session = self.bot.sessions.build_session(self.sessionid) | |||||
if session.system_prompt != self.desc: # 目前没有触发session过期事件,这里先简单判断,然后重置 | |||||
session.set_system_prompt(self.desc) | |||||
prompt = self.wrapper % user_action | prompt = self.wrapper % user_action | ||||
return prompt | return prompt | ||||
@@ -74,7 +74,7 @@ class Role(Plugin): | |||||
if e_context['context'].type != ContextType.TEXT: | if e_context['context'].type != ContextType.TEXT: | ||||
return | return | ||||
bottype = Bridge().get_bot_type("chat") | bottype = Bridge().get_bot_type("chat") | ||||
if bottype != const.CHATGPT: | |||||
if bottype not in (const.CHATGPT, const.OPEN_AI): | |||||
return | return | ||||
bot = Bridge().get_bot("chat") | bot = Bridge().get_bot("chat") | ||||
content = e_context['context'].content[:] | content = e_context['context'].content[:] | ||||