将群画像缓存策略调整为按天更新
This commit is contained in:
@@ -195,10 +195,10 @@ debug = true
|
||||
# 1. 这里读取最近 5 份群摘要,再聚合成稳定主题/近期重点/未决问题;
|
||||
# 2. 自动回复消费时优先走这些结构字段,减少 markdown 大段文本的理解损耗;
|
||||
# 3. item_limit 控制每类字段带给模型的条数,避免群背景过重。
|
||||
# 4. cache_ttl_sec 让群画像结果落库复用,在短时间内且源数据没变时直接读快照,避免每条消息重复聚合。
|
||||
# 4. cache_ttl_sec 现在按“群画像日更”来理解:默认一天内直接复用,只在群总结更新或过了窗口后才重建。
|
||||
summary_history_limit = 5
|
||||
summary_item_limit = 4
|
||||
cache_ttl_sec = 600
|
||||
cache_ttl_sec = 86400
|
||||
|
||||
[group_profiles.default]
|
||||
mode = "social"
|
||||
|
||||
@@ -39,19 +39,17 @@ class GroupMemoryService:
|
||||
self.summary_history_limit = max(int(self.config.get("summary_history_limit", 5) or 5), 1)
|
||||
self.summary_item_limit = max(int(self.config.get("summary_item_limit", 4) or 4), 1)
|
||||
# 群画像快照缓存:
|
||||
# 1. 自动回复是高频路径,群画像如果每条消息都重新聚合,会重复扫群总结和近期消息;
|
||||
# 2. 这里引入数据库快照,只要在 TTL 内且源数据没有变化,就直接复用;
|
||||
# 3. 这样快照既能跨进程/重启保留,又能把每条消息的聚合成本压下来。
|
||||
self.cache_ttl_sec = max(int(self.config.get("cache_ttl_sec", 600) or 600), 0)
|
||||
# 1. 群画像更像“日级背景”,不需要跟着每条群消息实时抖动;
|
||||
# 2. 因此这里把缓存设计成“按天/按总结刷新”,而不是“消息一变就立刻失效”;
|
||||
# 3. 这样既能明显减轻高频群的聚合成本,也更符合群画像本身应当稳定的产品语义。
|
||||
self.cache_ttl_sec = max(int(self.config.get("cache_ttl_sec", 86400) or 86400), 0)
|
||||
|
||||
def build_group_memory_profile(self, room_id: str, group_name: str = "") -> Dict:
|
||||
source_summary_latest_at = self._get_latest_summary_time(room_id)
|
||||
source_message_latest_at = self._get_latest_group_message_time(room_id)
|
||||
cached_profile = self._load_cached_profile_if_fresh(
|
||||
room_id=room_id,
|
||||
group_name=group_name,
|
||||
source_summary_latest_at=source_summary_latest_at,
|
||||
source_message_latest_at=source_message_latest_at,
|
||||
)
|
||||
if cached_profile:
|
||||
return cached_profile
|
||||
@@ -138,7 +136,8 @@ class GroupMemoryService:
|
||||
group_name=group_name,
|
||||
profile=profile,
|
||||
source_summary_latest_at=source_summary_latest_at,
|
||||
source_message_latest_at=source_message_latest_at,
|
||||
# 这里仍记录最近群消息时间做观测字段,但它不再参与快照失效判断。
|
||||
source_message_latest_at=self._get_latest_group_message_time(room_id),
|
||||
source_summary_count=len(summary_records),
|
||||
source_message_sample_count=len(recent_messages),
|
||||
)
|
||||
@@ -218,7 +217,6 @@ class GroupMemoryService:
|
||||
room_id: str,
|
||||
group_name: str,
|
||||
source_summary_latest_at: str,
|
||||
source_message_latest_at: str,
|
||||
) -> Optional[Dict]:
|
||||
if self.cache_ttl_sec <= 0:
|
||||
return None
|
||||
@@ -226,10 +224,10 @@ class GroupMemoryService:
|
||||
if not snapshot:
|
||||
return None
|
||||
|
||||
# 快照新鲜度判断分两层:
|
||||
# 1. 先看快照是否还在 TTL 内,避免长期无限复用旧画像;
|
||||
# 2. 再看“最近群总结更新时间 / 最近群消息时间”是否和上次构建时一致,
|
||||
# 只有源数据没变,才允许直接复用。
|
||||
# 快照新鲜度判断改成“日级背景”思路:
|
||||
# 1. 先看快照是否还在 TTL 内,默认按 24 小时窗口复用;
|
||||
# 2. 再看最近群总结是否更新,如果总结有新内容,立即触发重建;
|
||||
# 3. 普通群消息不会让群画像失效,避免高频消息把缓存形同虚设。
|
||||
last_generated_at = self._parse_dt(snapshot.get("last_generated_at"))
|
||||
if not last_generated_at:
|
||||
return None
|
||||
@@ -239,8 +237,6 @@ class GroupMemoryService:
|
||||
|
||||
if str(snapshot.get("source_summary_latest_at", "") or "") != str(source_summary_latest_at or ""):
|
||||
return None
|
||||
if str(snapshot.get("source_message_latest_at", "") or "") != str(source_message_latest_at or ""):
|
||||
return None
|
||||
|
||||
profile = dict(snapshot.get("profile", {}) or {})
|
||||
if not profile:
|
||||
|
||||
Reference in New Issue
Block a user