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
|
||||
|
||||
@@ -45,7 +45,8 @@ from src.config.official_configs import (
|
||||
AntiPromptInjectionConfig,
|
||||
PluginsConfig,
|
||||
WakeUpSystemConfig,
|
||||
MonthlyPlanSystemConfig
|
||||
MonthlyPlanSystemConfig,
|
||||
CrossContextConfig
|
||||
)
|
||||
|
||||
from .api_ada_configs import (
|
||||
@@ -392,6 +393,7 @@ class Config(ValidatedConfigBase):
|
||||
plugins: PluginsConfig = Field(default_factory=lambda: PluginsConfig(), description="插件配置")
|
||||
wakeup_system: WakeUpSystemConfig = Field(default_factory=lambda: WakeUpSystemConfig(), description="唤醒度系统配置")
|
||||
monthly_plan_system: MonthlyPlanSystemConfig = Field(default_factory=lambda: MonthlyPlanSystemConfig(), description="月层计划系统配置")
|
||||
cross_context: CrossContextConfig = Field(default_factory=lambda: CrossContextConfig(), description="跨群聊上下文共享配置")
|
||||
|
||||
|
||||
class APIAdapterConfig(ValidatedConfigBase):
|
||||
|
||||
@@ -685,3 +685,15 @@ class MonthlyPlanSystemConfig(ValidatedConfigBase):
|
||||
generation_threshold: int = Field(default=10, ge=0, description="启动时,如果当月计划少于此数量,则触发LLM生成")
|
||||
plans_per_generation: int = Field(default=5, ge=1, description="每次调用LLM期望生成的计划数量")
|
||||
deletion_probability_on_use: float = Field(default=0.5, ge=0.0, le=1.0, description="计划被使用后,被删除的概率")
|
||||
|
||||
|
||||
class ContextGroup(ValidatedConfigBase):
|
||||
"""上下文共享组配置"""
|
||||
name: str = Field(..., description="共享组的名称")
|
||||
chat_ids: List[str] = Field(..., description="属于该组的聊天ID列表")
|
||||
|
||||
|
||||
class CrossContextConfig(ValidatedConfigBase):
|
||||
"""跨群聊上下文共享配置"""
|
||||
enable: bool = Field(default=False, description="是否启用跨群聊上下文共享功能")
|
||||
groups: List[ContextGroup] = Field(default_factory=list, description="上下文共享组列表")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
[inner]
|
||||
version = "6.4.1"
|
||||
version = "6.4.2"
|
||||
|
||||
#----以下是给开发人员阅读的,如果你只是部署了麦麦,不需要阅读----
|
||||
#如果你想要修改配置文件,请递增version的值
|
||||
@@ -428,3 +428,24 @@ group_mention_increment = 2.0 #"群聊艾特增加的唤醒度"
|
||||
decay_rate = 0.2 #"每次衰减的唤醒度数值"
|
||||
decay_interval = 30.0 #"唤醒度衰减间隔(秒)"
|
||||
angry_duration = 300.0 #"愤怒状态持续时间(秒)"
|
||||
|
||||
[cross_context] # 跨群聊上下文共享配置
|
||||
# 这是总开关,用于一键启用或禁用此功能
|
||||
enable = false
|
||||
|
||||
# 在这里定义您的“共享组”
|
||||
# 只有在同一个组内的群聊才会共享上下文
|
||||
# 注意:这里的chat_ids需要填写群号,而不是哈希过的ID
|
||||
[[cross_context.groups]]
|
||||
name = "项目A技术讨论组"
|
||||
chat_ids = [
|
||||
"111111", # 假设这是“开发群”的ID
|
||||
"222222" # 假设这是“产品群”的ID
|
||||
]
|
||||
|
||||
[[cross_context.groups]]
|
||||
name = "日常闲聊组"
|
||||
chat_ids = [
|
||||
"333333", # 假设这是“吹水群”的ID
|
||||
"444444" # 假设这是“游戏群”的ID
|
||||
]
|
||||
Reference in New Issue
Block a user