增加常驻群长期记忆与成员轻画像输入

This commit is contained in:
liuwei
2026-04-24 15:44:03 +08:00
parent d0480691c3
commit 0ea7b61951
3 changed files with 79 additions and 6 deletions

View File

@@ -59,6 +59,7 @@ class ContextBuilder:
"trigger_type": trigger.get("trigger_type", "none"),
"reply_mode": reply_mode,
"flow_state": flow_state,
"member_profile_brief_prompt": self._build_member_profile_brief_prompt(member_context or {}),
"memory_prompt": self._build_member_memory_prompt(member_context, member_memory_focus or []),
"at_member_profile_prompt": self._build_at_member_profile_prompt(
member_context=member_context or {},
@@ -69,6 +70,7 @@ class ContextBuilder:
"vector_memory_prompt": self._build_vector_memory_prompt(vector_memories),
"social_memory_prompt": self._build_social_memory_prompt(social_memory or {}),
"group_facts_prompt": self._build_group_facts_prompt(group_facts or {}),
"group_long_memory_prompt": self._build_group_long_memory_prompt(group_profile or {}),
"group_profile_prompt": self._build_group_profile_prompt(group_profile or {}),
"quote_prompt": self._build_quote_prompt(quote_context or {}),
"image_prompt": self._build_image_prompt(image_context or {}),
@@ -207,6 +209,35 @@ class ContextBuilder:
text = re.sub(r"[^\u4e00-\u9fffA-Za-z0-9_]", "", text)
return text[:8]
@staticmethod
def _build_member_profile_brief_prompt(member_context: Dict) -> str:
# 这份摘要是“常驻给模型看的轻画像”:
# 1. 不要求当前一定是 @ 或强定向,因为用户希望每次回答都能带上对这个人的基本认识;
# 2. 这里只保留少量稳定信息,避免画像太重把当前问题压住;
# 3. 更细的成员记忆、近期相关记忆,仍走后面的按需增强链路。
if not member_context:
return ""
meta = member_context.get("meta", {}) or {}
summary = str(member_context.get("summary_text", "") or "").strip()
interaction_style = str(member_context.get("interaction_style", "") or "").strip()
response_hint = str(member_context.get("response_style_hint", "") or "").strip()
topics = ContextBuilder._stringify_items(member_context.get("topics_of_interest", []) or [], 3)
recent_focus = ContextBuilder._stringify_items(member_context.get("recent_focus", []) or [], 2)
skills = ContextBuilder._stringify_items(meta.get("skill_profile", []) or [], 2)
reply_prefs = ContextBuilder._stringify_items(meta.get("long_term_reply_preferences", []) or [], 2)
lines = [
"当前发言人轻画像:",
f"成员摘要:{summary}" if summary else "",
f"互动风格:{interaction_style}" if interaction_style else "",
f"偏好回复方式:{response_hint}" if response_hint else "",
f"长期兴趣:{topics}" if topics else "",
f"近期关注:{recent_focus}" if recent_focus else "",
f"技能侧重点:{skills}" if skills else "",
f"回复偏好:{reply_prefs}" if reply_prefs else "",
"这些信息只用于帮助理解提问方式和回答切口,不要像在背档案。",
]
return "\n".join([line for line in lines if line])
@staticmethod
def _build_member_memory_prompt(member_context: Dict, focus_lines: List[str] | None = None) -> str:
if not member_context:
@@ -320,6 +351,25 @@ class ContextBuilder:
def _build_group_facts_prompt(group_facts: Dict) -> str:
return str((group_facts or {}).get("prompt", "") or "").strip()
@staticmethod
def _build_group_long_memory_prompt(group_profile: Dict) -> str:
# 这份摘要是“群长期背景常驻层”:
# 1. 每次都给一小段,帮助模型知道这个群长期在聊什么、什么风格更合适;
# 2. 不把完整群画像整段塞进去,避免大量通用风格描述把 token 吃满;
# 3. 更细的群事实、群关系仍走相关性增强链路。
if not group_profile:
return ""
summary = ContextBuilder._compact_group_summary(str(group_profile.get("group_memory_summary", "") or ""), max_chars=220, max_sentences=4)
focus = ", ".join(group_profile.get("knowledge_focus", [])[:4])
memory_style = ContextBuilder._build_style_summary(group_profile.get("group_memory_style", {}))
lines = [
"群长期背景:",
f"长期摘要:{summary}" if summary else "",
f"常聊方向:{focus}" if focus else "",
f"历史社交风格:{memory_style}" if memory_style else "",
]
return "\n".join([line for line in lines if line])
@staticmethod
def _build_group_profile_prompt(group_profile: Dict) -> str:
if not group_profile: