mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-28 00:14:21 +00:00
feat(dprompt.py): 解耦完成
This commit is contained in:
+60
-44
@@ -7,6 +7,7 @@ import pkg.openai.session
|
||||
import pkg.utils.context
|
||||
import config
|
||||
|
||||
|
||||
@command(
|
||||
"reset",
|
||||
"重置当前会话",
|
||||
@@ -14,19 +15,23 @@ import config
|
||||
[],
|
||||
False
|
||||
)
|
||||
def cmd_reset(cmd: str, params: list, session_name: str,
|
||||
def cmd_reset(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""重置会话"""
|
||||
reply = []
|
||||
|
||||
|
||||
if len(params) == 0:
|
||||
pkg.openai.session.get_session(session_name).reset(explicit=True)
|
||||
reply = ["[bot]会话已重置"]
|
||||
else:
|
||||
pkg.openai.session.get_session(session_name).reset(explicit=True, use_prompt=params[0])
|
||||
reply = ["[bot]会话已重置,使用场景预设:{}".format(params[0])]
|
||||
|
||||
try:
|
||||
import pkg.openai.dprompt as dprompt
|
||||
pkg.openai.session.get_session(session_name).reset(explicit=True, use_prompt=params[0])
|
||||
reply = ["[bot]会话已重置,使用场景预设:{}".format(dprompt.mode_inst().get_full_name(params[0]))]
|
||||
except Exception as e:
|
||||
reply = ["[bot]会话重置失败,错误信息:{}".format(e)]
|
||||
|
||||
return reply
|
||||
|
||||
|
||||
@@ -37,9 +42,9 @@ def cmd_reset(cmd: str, params: list, session_name: str,
|
||||
[],
|
||||
False
|
||||
)
|
||||
def cmd_last(cmd: str, params: list, session_name: str,
|
||||
def cmd_last(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""切换到前一次会话"""
|
||||
reply = []
|
||||
result = pkg.openai.session.get_session(session_name).last_session()
|
||||
@@ -52,6 +57,7 @@ def cmd_last(cmd: str, params: list, session_name: str,
|
||||
|
||||
return reply
|
||||
|
||||
|
||||
@command(
|
||||
"next",
|
||||
"切换到后一次会话",
|
||||
@@ -59,12 +65,12 @@ def cmd_last(cmd: str, params: list, session_name: str,
|
||||
[],
|
||||
False
|
||||
)
|
||||
def cmd_next(cmd: str, params: list, session_name: str,
|
||||
def cmd_next(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: int, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""切换到后一次会话"""
|
||||
reply = []
|
||||
|
||||
|
||||
result = pkg.openai.session.get_session(session_name).next_session()
|
||||
if result is None:
|
||||
reply = ["[bot]没有后一次的对话"]
|
||||
@@ -84,13 +90,13 @@ def cmd_next(cmd: str, params: list, session_name: str,
|
||||
False
|
||||
)
|
||||
def cmd_prompt(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""获取当前会话的前文"""
|
||||
reply = []
|
||||
|
||||
|
||||
msgs = ""
|
||||
session:list = pkg.openai.session.get_session(session_name).prompt
|
||||
session: list = pkg.openai.session.get_session(session_name).prompt
|
||||
for msg in session:
|
||||
if len(params) != 0 and params[0] in ['-all', '-a']:
|
||||
msgs = msgs + "{}: {}\n\n".format(msg['role'], msg['content'])
|
||||
@@ -111,11 +117,11 @@ def cmd_prompt(cmd: str, params: list, session_name: str,
|
||||
False
|
||||
)
|
||||
def cmd_list(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""列出当前会话的所有历史记录"""
|
||||
reply = []
|
||||
|
||||
|
||||
pkg.openai.session.get_session(session_name).persistence()
|
||||
page = 0
|
||||
|
||||
@@ -143,12 +149,12 @@ def cmd_list(cmd: str, params: list, session_name: str,
|
||||
pkg.openai.session.get_session(session_name).persistence()
|
||||
if len(msg) >= 2:
|
||||
reply_str += "#{} 创建:{} {}\n".format(i + page * 10,
|
||||
datetime_obj.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
msg[0]['content'])
|
||||
datetime_obj.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
msg[0]['content'])
|
||||
else:
|
||||
reply_str += "#{} 创建:{} {}\n".format(i + page * 10,
|
||||
datetime_obj.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"无内容")
|
||||
datetime_obj.strftime("%Y-%m-%d %H:%M:%S"),
|
||||
"无内容")
|
||||
if results[i]['create_timestamp'] == pkg.openai.session.get_session(
|
||||
session_name).create_timestamp:
|
||||
current = i + page * 10
|
||||
@@ -172,19 +178,19 @@ def cmd_list(cmd: str, params: list, session_name: str,
|
||||
False
|
||||
)
|
||||
def cmd_resend(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""重新获取上一次问题的回复"""
|
||||
reply = []
|
||||
|
||||
|
||||
session = pkg.openai.session.get_session(session_name)
|
||||
to_send = session.undo()
|
||||
|
||||
mgr = pkg.utils.context.get_qqbot_manager()
|
||||
|
||||
reply = pkg.qqbot.message.process_normal_message(to_send, mgr, config,
|
||||
launcher_type, launcher_id, sender_id)
|
||||
|
||||
launcher_type, launcher_id, sender_id)
|
||||
|
||||
return reply
|
||||
|
||||
|
||||
@@ -196,11 +202,11 @@ def cmd_resend(cmd: str, params: list, session_name: str,
|
||||
False
|
||||
)
|
||||
def cmd_del(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""删除当前会话的历史记录"""
|
||||
reply = []
|
||||
|
||||
|
||||
if len(params) == 0:
|
||||
reply = ["[bot]参数不足, 格式: !del <序号>\n可以通过!list查看序号"]
|
||||
else:
|
||||
@@ -226,28 +232,37 @@ def cmd_del(cmd: str, params: list, session_name: str,
|
||||
)
|
||||
def cmd_default(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""操作情景预设"""
|
||||
reply = []
|
||||
|
||||
if len(params) == 0:
|
||||
# 输出目前所有情景预设
|
||||
import pkg.openai.dprompt as dprompt
|
||||
reply_str = "[bot]当前所有情景预设:\n\n"
|
||||
for key, value in dprompt.get_prompt_dict().items():
|
||||
reply_str += " - {}: {}\n".format(key, value)
|
||||
reply_str = "[bot]当前所有情景预设({}模式):\n\n".format(config.preset_mode)
|
||||
|
||||
reply_str += "\n当前默认情景预设:{}\n".format(dprompt.get_current())
|
||||
reply_str += "请使用!default <情景预设>来设置默认情景预设"
|
||||
prompts = dprompt.mode_inst().list()
|
||||
|
||||
for key in prompts:
|
||||
pro = prompts[key]
|
||||
reply_str += "名称: {}".format(key)
|
||||
|
||||
for r in pro:
|
||||
reply_str += "\n - [{}]: {}".format(r['role'], r['content'])
|
||||
|
||||
reply_str += "\n\n"
|
||||
|
||||
reply_str += "\n当前默认情景预设:{}\n".format(dprompt.mode_inst().get_using_name())
|
||||
reply_str += "请使用 !default <情景预设名称> 来设置默认情景预设"
|
||||
reply = [reply_str]
|
||||
elif len(params) > 0 and is_admin:
|
||||
# 设置默认情景
|
||||
import pkg.openai.dprompt as dprompt
|
||||
try:
|
||||
dprompt.set_current(params[0])
|
||||
reply = ["[bot]已设置默认情景预设为:{}".format(dprompt.get_current())]
|
||||
except KeyError:
|
||||
reply = ["[bot]err: 未找到情景预设:{}".format(params[0])]
|
||||
full_name = dprompt.mode_inst().set_using_name(params[0])
|
||||
reply = ["[bot]已设置默认情景预设为:{}".format(full_name)]
|
||||
except Exception as e:
|
||||
reply = ["[bot]err: {}".format(e)]
|
||||
else:
|
||||
reply = ["[bot]err: 仅管理员可设置默认情景预设"]
|
||||
|
||||
@@ -262,13 +277,14 @@ def cmd_default(cmd: str, params: list, session_name: str,
|
||||
True
|
||||
)
|
||||
def cmd_delhst(cmd: str, params: list, session_name: str,
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
text_message: str, launcher_type: str, launcher_id: int,
|
||||
sender_id: int, is_admin: bool) -> list:
|
||||
"""删除指定会话的所有历史记录"""
|
||||
reply = []
|
||||
|
||||
|
||||
if len(params) == 0:
|
||||
reply = ["[bot]err:请输入要删除的会话名: group_<群号> 或者 person_<QQ号>, 或使用 !delhst all 删除所有会话的历史记录"]
|
||||
reply = [
|
||||
"[bot]err:请输入要删除的会话名: group_<群号> 或者 person_<QQ号>, 或使用 !delhst all 删除所有会话的历史记录"]
|
||||
else:
|
||||
if params[0] == "all":
|
||||
pkg.utils.context.get_database_manager().delete_all_session_history()
|
||||
|
||||
Reference in New Issue
Block a user