- 从 story 路由 prompt/校验中移除 new_story_title,改由叙事管线在正文足够长时生成 - 新增 story_title_min_body_chars;短正文使用章节类别占位标题 - CATEGORY_TO_CHAT_STAGE 对齐访谈 state.slots 的 stage 键 - 删除相对口述长度的叙事回退,仅保留 merge JSON 极端缩水类 fallback - evidence_format:解析 object_json 并优化事实条目标点符号 - 更新 narrative / experience 相关单测
124 lines
4.4 KiB
Python
124 lines
4.4 KiB
Python
"""
|
||
将 MemoryService.retrieve / evidence bundle 格式化为 prompt 用短文本(叙事与访谈共用)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import re
|
||
|
||
|
||
def _normalize_evidence_line(s: str) -> str:
|
||
return re.sub(r"\s+", " ", (s or "").strip().lower())
|
||
|
||
|
||
def dedupe_evidence_chunk_rows(chunks: list) -> list:
|
||
"""
|
||
对 relevant_chunks 做稳定去重:按归一化后长度降序 + 原下标,单遍包含判定;
|
||
复杂度 O(n log n);输出按原顺序中保留条目的相对顺序稳定。
|
||
"""
|
||
extracted: list[tuple[int, str, object]] = []
|
||
for i, c in enumerate(chunks):
|
||
content = (
|
||
c.get("content", "") if isinstance(c, dict) else getattr(c, "content", "")
|
||
)
|
||
t = (content or "").strip()
|
||
if not t:
|
||
continue
|
||
extracted.append((i, t, c))
|
||
if len(extracted) <= 1:
|
||
return [x[2] for x in extracted]
|
||
extracted.sort(
|
||
key=lambda x: (-len(_normalize_evidence_line(x[1])), x[0]),
|
||
)
|
||
kept_norms: list[str] = []
|
||
kept: list[tuple[int, object]] = []
|
||
for orig_idx, text, c in extracted:
|
||
n = _normalize_evidence_line(text)
|
||
dup = False
|
||
for kn in kept_norms:
|
||
if len(n) <= len(kn) and n in kn:
|
||
dup = True
|
||
break
|
||
if not dup:
|
||
kept_norms.append(n)
|
||
kept.append((orig_idx, c))
|
||
kept.sort(key=lambda x: x[0])
|
||
return [x[1] for x in kept]
|
||
|
||
|
||
def _flatten_object_json(obj_raw: object) -> str:
|
||
"""Extract readable text from fact object_json (may be dict, JSON string, or plain str)."""
|
||
if isinstance(obj_raw, dict):
|
||
return str(obj_raw.get("value", "")) or ", ".join(
|
||
f"{k}={v}" for k, v in obj_raw.items() if v
|
||
)
|
||
if isinstance(obj_raw, str):
|
||
s = obj_raw.strip()
|
||
if s.startswith("{"):
|
||
try:
|
||
parsed = json.loads(s)
|
||
if isinstance(parsed, dict):
|
||
return str(parsed.get("value", s)) or s
|
||
except (json.JSONDecodeError, TypeError):
|
||
pass
|
||
return s
|
||
return str(obj_raw) if obj_raw else ""
|
||
|
||
|
||
def format_evidence_chunks_for_prompt(evidence: dict) -> str:
|
||
"""将 retrieve_evidence / retrieve_evidence_sync 结果格式化为简短文本,供叙事与访谈 prompt 使用。
|
||
|
||
包含 chunks、摘要(若有)、confirmed facts、timeline、故事摘要(若有)。
|
||
"""
|
||
chunks = evidence.get("relevant_chunks") or []
|
||
chunks = dedupe_evidence_chunk_rows(chunks[:10])
|
||
summaries = evidence.get("relevant_summaries") or []
|
||
facts = evidence.get("relevant_facts") or []
|
||
timeline = evidence.get("timeline_hints") or []
|
||
stories = evidence.get("relevant_stories") or []
|
||
parts: list[str] = []
|
||
for c in chunks:
|
||
content = (
|
||
c.get("content", "") if isinstance(c, dict) else getattr(c, "content", "")
|
||
)
|
||
if content:
|
||
parts.append(content.strip())
|
||
for s in summaries[:3]:
|
||
if isinstance(s, dict):
|
||
st = (s.get("content") or "").strip()
|
||
stype = (s.get("summary_type") or "").strip()
|
||
if st:
|
||
label = f"[摘要:{stype}]" if stype else "[摘要]"
|
||
parts.append(f"{label} {st}")
|
||
for f in facts[:5]:
|
||
if isinstance(f, dict):
|
||
subj = f.get("subject", "")
|
||
pred = f.get("predicate", "")
|
||
obj_raw = f.get("object_json", "")
|
||
obj = _flatten_object_json(obj_raw)
|
||
if subj or pred:
|
||
if obj:
|
||
parts.append(f"{subj}:{pred}({obj})")
|
||
else:
|
||
parts.append(f"{subj}:{pred}")
|
||
else:
|
||
parts.append(f"{getattr(f, 'subject', '')}:{getattr(f, 'predicate', '')}")
|
||
for t in timeline[:5]:
|
||
if isinstance(t, dict):
|
||
title = (t.get("title") or "").strip()
|
||
year = t.get("event_year")
|
||
desc = (t.get("description") or "").strip()
|
||
line = " ".join(
|
||
x for x in (str(year) if year is not None else "", title, desc) if x
|
||
)
|
||
if line:
|
||
parts.append(line)
|
||
for st in stories[:3]:
|
||
if isinstance(st, dict):
|
||
title = (st.get("title") or "").strip()
|
||
summ = (st.get("summary") or "").strip()
|
||
if title or summ:
|
||
parts.append(" ".join(x for x in (title, summ) if x))
|
||
return "\n\n".join(parts) if parts else ""
|