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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user