feat(cross_context): 支持为跨上下文聊天指定消息数量

允许用户在配置文件中为每个聊天流(群聊或私聊)单独设置要检索的历史消息数量(limit)。如果未指定,则默认为5条。

- 更新了 `build_cross_context_normal` 和 `build_cross_context_s4u` 方法以解析和使用这个新的 `limit` 参数。
- 在S4U(Search for User)模式下,获取的消息数量调整为 `limit` 的4倍,以确保有足够的消息可供筛选。
- 更新了配置文件模板 `bot_config_template.toml` 以反映此项新功能,并提供了示例。
This commit is contained in:
minecraft1024a
2025-10-11 19:02:09 +08:00
parent 604b5bc4ac
commit b3956c2fa6
2 changed files with 21 additions and 12 deletions

View File

@@ -50,7 +50,8 @@ async def build_cross_context_normal(chat_stream: ChatStream, other_chat_infos:
构建跨群聊/私聊上下文 (Normal模式)
"""
cross_context_messages = []
for chat_type, chat_raw_id in other_chat_infos:
for chat_info in other_chat_infos:
chat_type, chat_raw_id, limit = chat_info[0], chat_info[1], int(chat_info[2]) if len(chat_info) > 2 else 5
is_group = chat_type == "group"
stream_id = get_chat_manager().get_stream_id(chat_stream.platform, chat_raw_id, is_group=is_group)
if not stream_id:
@@ -60,7 +61,7 @@ async def build_cross_context_normal(chat_stream: ChatStream, other_chat_infos:
messages = await get_raw_msg_before_timestamp_with_chat(
chat_id=stream_id,
timestamp=time.time(),
limit=5, # 可配置
limit=limit,
)
if messages:
chat_name = await get_chat_manager().get_stream_name(stream_id) or chat_raw_id
@@ -89,19 +90,25 @@ async def build_cross_context_s4u(
user_id = target_user_info.get("user_id")
if user_id:
for chat_type, chat_raw_id in other_chat_infos:
for chat_info in other_chat_infos:
chat_type, chat_raw_id, limit = (
chat_info[0],
chat_info[1],
int(chat_info[2]) if len(chat_info) > 2 else 5,
)
is_group = chat_type == "group"
stream_id = get_chat_manager().get_stream_id(chat_stream.platform, chat_raw_id, is_group=is_group)
if not stream_id:
continue
try:
# S4U模式下我们获取更多消息以供筛选
messages = await get_raw_msg_before_timestamp_with_chat(
chat_id=stream_id,
timestamp=time.time(),
limit=20, # 获取更多消息以供筛选
limit=limit * 4, # 获取4倍limit的消息以供筛选
)
user_messages = [msg for msg in messages if msg.get("user_id") == user_id][-5:]
user_messages = [msg for msg in messages if msg.get("user_id") == user_id][-limit:]
if user_messages:
chat_name = await get_chat_manager().get_stream_name(stream_id) or chat_raw_id
@@ -144,7 +151,8 @@ async def get_chat_history_by_group_name(group_name: str) -> str:
chat_manager = get_chat_manager()
cross_context_messages = []
for chat_type, chat_raw_id in chat_infos:
for chat_info in chat_infos:
chat_type, chat_raw_id, limit = chat_info[0], chat_info[1], int(chat_info[2]) if len(chat_info) > 2 else 5
is_group = chat_type == "group"
found_stream = None
@@ -168,7 +176,7 @@ async def get_chat_history_by_group_name(group_name: str) -> str:
messages = await get_raw_msg_before_timestamp_with_chat(
chat_id=stream_id,
timestamp=time.time(),
limit=5, # 可配置
limit=limit,
)
if messages:
chat_name = await get_chat_manager().get_stream_name(stream_id) or chat_raw_id

View File

@@ -1,5 +1,5 @@
[inner]
version = "7.2.6"
version = "7.2.7"
#----以下是给开发人员阅读的如果你只是部署了MoFox-Bot不需要阅读----
#如果你想要修改配置文件请递增version的值
@@ -523,14 +523,15 @@ insomnia_trigger_delay_minutes = [15, 45]
enable = true
# 在这里定义您的“共享组”
# 只有在同一个组内的聊天才会共享上下文
# 格式chat_ids = [["type", "id"], ["type", "id"], ...]
# 格式chat_ids = [["type", "id", limit (可选)], ["type", "id", limit (可选)], ...]
# type 可选 "group" 或 "private"
# limit 是一个可选的整数用于指定从该聊天流中获取的消息数量如果未指定默认为5
[[cross_context.groups]]
name = "项目A技术讨论组"
chat_ids = [
["group", "169850076"], # 假设这是“开发群”的ID
["group", "1025509724"], # 假设这是“产品群”的ID
["private", "123456789"] # 假设这是某个用户的私聊
["group", "169850076", 10], # 假设这是“开发群”的ID, 从这个群里拿10条消息
["group", "1025509724", 5], # 假设这是“产品群”的ID拿5条
["private", "123456789"] # 假设这是某个用户的私聊使用默认值5
]
# 定义QQ空间互通组
# 同一个组的chat_id会共享上下文用于生成更相关的说说