feat(chat): 引入跨群聊上下文共享功能
该功能允许在不同但相关的群聊之间共享对话上下文,从而提供更连贯和情境感知的回复。 主要实现方式: - 在配置文件中引入 `cross_context` 部分,允许用户定义“共享组”,将多个群聊ID(原始ID)归入一组。 - 新增 `_build_cross_context_block` 方法,用于构建并注入到Prompt中。 - 支持两种上下文获取模式: - `normal` 模式:获取共享组内其他群聊的最新消息。 - `s4u` 模式:获取当前发言用户在共享组内其他群聊的近期发言记录。 - 更新了Prompt模板以包含新的 `cross_context_block`。 - 提供了相应的配置模板和版本号更新。
This commit is contained in:
@@ -13,7 +13,7 @@ from src.config.api_ada_configs import TaskConfig
|
||||
from src.individuality.individuality import get_individuality
|
||||
from src.llm_models.utils_model import LLMRequest
|
||||
from src.chat.message_receive.message import UserInfo, Seg, MessageRecv, MessageSending
|
||||
from src.chat.message_receive.chat_stream import ChatStream
|
||||
from src.chat.message_receive.chat_stream import ChatStream, get_chat_manager
|
||||
from src.chat.message_receive.uni_message_sender import HeartFCSender
|
||||
from src.chat.utils.timer_calculator import Timer # <--- Import Timer
|
||||
from src.chat.utils.utils import get_chat_type_and_target_info
|
||||
@@ -22,6 +22,7 @@ from src.chat.utils.chat_message_builder import (
|
||||
build_readable_messages,
|
||||
get_raw_msg_before_timestamp_with_chat,
|
||||
replace_user_references_sync,
|
||||
build_readable_messages_with_id,
|
||||
)
|
||||
from src.chat.express.expression_selector import expression_selector
|
||||
from src.chat.memory_system.memory_activator import MemoryActivator
|
||||
@@ -84,6 +85,7 @@ def init_prompt():
|
||||
{relation_info_block}
|
||||
{extra_info_block}
|
||||
|
||||
{cross_context_block}
|
||||
|
||||
{identity}
|
||||
|
||||
@@ -154,6 +156,7 @@ If you need to use the search tool, please directly call the function "lpmm_sear
|
||||
{relation_info_block}
|
||||
{extra_info_block}
|
||||
|
||||
{cross_context_block}
|
||||
{identity}
|
||||
如果有人说你是人机,你可以用一种阴阳怪气的口吻来回应
|
||||
{schedule_block}
|
||||
@@ -236,6 +239,76 @@ class DefaultReplyer:
|
||||
|
||||
self.tool_executor = ToolExecutor(chat_id=self.chat_stream.stream_id, enable_cache=False)
|
||||
|
||||
async def _build_cross_context_block(self, current_chat_id: str, target_user_info: Optional[Dict[str, Any]]) -> str:
|
||||
"""构建跨群聊上下文"""
|
||||
if not global_config.cross_context.enable:
|
||||
return ""
|
||||
|
||||
# 找到当前群聊所在的共享组
|
||||
target_group = None
|
||||
current_stream = get_chat_manager().get_stream(current_chat_id)
|
||||
if not current_stream or not current_stream.group_info:
|
||||
return ""
|
||||
current_chat_raw_id = current_stream.group_info.group_id
|
||||
|
||||
for group in global_config.cross_context.groups:
|
||||
if str(current_chat_raw_id) in group.chat_ids:
|
||||
target_group = group
|
||||
break
|
||||
|
||||
if not target_group:
|
||||
return ""
|
||||
|
||||
# 根据prompt_mode选择策略
|
||||
prompt_mode = global_config.personality.prompt_mode
|
||||
other_chat_raw_ids = [chat_id for chat_id in target_group.chat_ids if chat_id != str(current_chat_raw_id)]
|
||||
|
||||
cross_context_messages = []
|
||||
|
||||
if prompt_mode == "normal":
|
||||
# normal模式:获取其他群聊的最近N条消息
|
||||
for chat_raw_id in other_chat_raw_ids:
|
||||
stream_id = get_chat_manager().get_stream_id(current_stream.platform, chat_raw_id, is_group=True)
|
||||
if not stream_id: continue
|
||||
|
||||
messages = get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=stream_id,
|
||||
timestamp=time.time(),
|
||||
limit=5 # 可配置
|
||||
)
|
||||
if messages:
|
||||
chat_name = get_chat_manager().get_stream_name(stream_id) or stream_id
|
||||
formatted_messages, _ = build_readable_messages_with_id(messages, timestamp_mode="relative")
|
||||
cross_context_messages.append(f"[以下是来自“{chat_name}”的近期消息]\n{formatted_messages}")
|
||||
|
||||
elif prompt_mode == "s4u":
|
||||
# s4u模式:获取当前发言用户在其他群聊的消息
|
||||
if target_user_info:
|
||||
user_id = target_user_info.get("user_id")
|
||||
|
||||
if user_id:
|
||||
for chat_raw_id in other_chat_raw_ids:
|
||||
stream_id = get_chat_manager().get_stream_id(current_stream.platform, chat_raw_id, is_group=True)
|
||||
if not stream_id: continue
|
||||
|
||||
messages = get_raw_msg_before_timestamp_with_chat(
|
||||
chat_id=stream_id,
|
||||
timestamp=time.time(),
|
||||
limit=20 # 获取更多消息以供筛选
|
||||
)
|
||||
user_messages = [msg for msg in messages if msg.get('user_id') == user_id][-5:] # 筛选并取最近5条
|
||||
|
||||
if user_messages:
|
||||
chat_name = get_chat_manager().get_stream_name(stream_id) or stream_id
|
||||
user_name = target_user_info.get("person_name") or target_user_info.get("user_nickname") or user_id
|
||||
formatted_messages, _ = build_readable_messages_with_id(user_messages, timestamp_mode="relative")
|
||||
cross_context_messages.append(f"[以下是“{user_name}”在“{chat_name}”的近期发言]\n{formatted_messages}")
|
||||
|
||||
if not cross_context_messages:
|
||||
return ""
|
||||
|
||||
return "# 跨群上下文参考\n" + "\n\n".join(cross_context_messages) + "\n"
|
||||
|
||||
def _select_weighted_models_config(self) -> Tuple[TaskConfig, float]:
|
||||
"""使用加权随机选择来挑选一个模型配置"""
|
||||
configs = self.model_set
|
||||
|
||||
Reference in New Issue
Block a user