2026-03-27 16:01:28 +08:00
|
|
|
|
"""LLM JSON 输出校验(memory 富化)。"""
|
|
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
2026-04-30 16:22:55 +08:00
|
|
|
|
from typing import Any
|
2026-03-27 16:01:28 +08:00
|
|
|
|
|
|
|
|
|
|
from pydantic import BaseModel, Field, field_validator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ExtractedFactItem(BaseModel):
|
|
|
|
|
|
fact_type: str = "event"
|
|
|
|
|
|
subject: str | None = None
|
|
|
|
|
|
predicate: str | None = None
|
|
|
|
|
|
object_json: Any = None
|
|
|
|
|
|
confidence: float = Field(default=0.75, ge=0.0, le=1.0)
|
|
|
|
|
|
source_chunk_id: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
@field_validator("fact_type", mode="before")
|
|
|
|
|
|
@classmethod
|
|
|
|
|
|
def _coerce_fact_type(cls, v: object) -> str:
|
|
|
|
|
|
ft = str(v or "event").strip() or "event"
|
|
|
|
|
|
if ft not in ("person", "event", "relation", "place", "milestone"):
|
|
|
|
|
|
return "event"
|
|
|
|
|
|
return ft
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FactsExtractionPayload(BaseModel):
|
|
|
|
|
|
facts: list[ExtractedFactItem] = Field(default_factory=list)
|
|
|
|
|
|
|
|
|
|
|
|
|
feat(eval): memoir A/B chapter judging and eval-web parity with dialogue
- Judge baseline excerpt and library chapter separately; build_memoir_compare_summary for gate, nine-dim and leaf deltas.
- Memoir SSE chapter payload: baseline_judge, compare_summary, baseline_judge_error.
- MemoirJudgeOutput: loose score coercion and post-validate clamp; memoir judge prompt caps from settings.
- app-eval-web: two-column MemoirScoreCard layout, MemoirCompareSummary, chapter blocks and CSS.
- Add memoir_compare_summary, log_events, celery_log_context, memoir_pipeline_progress; tests and migration 0014.
- Misc: memory/evidence and enrichment paths, task/orchestrator updates, internal-eval docs, env examples.
2026-04-10 10:23:43 +08:00
|
|
|
|
class EnrichmentPayload(BaseModel):
|
|
|
|
|
|
"""单轮记忆富化:会话摘要 + 结构化事实(ingest 后一次 LLM 调用)。"""
|
|
|
|
|
|
|
|
|
|
|
|
summary: str = ""
|
|
|
|
|
|
facts: list[ExtractedFactItem] = Field(default_factory=list)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-03-27 16:01:28 +08:00
|
|
|
|
def facts_payload_to_dicts(payload: FactsExtractionPayload) -> list[dict]:
|
|
|
|
|
|
out: list[dict] = []
|
|
|
|
|
|
for item in payload.facts:
|
|
|
|
|
|
d = item.model_dump()
|
|
|
|
|
|
scid = d.get("source_chunk_id")
|
|
|
|
|
|
if scid is not None and not isinstance(scid, str):
|
|
|
|
|
|
d["source_chunk_id"] = str(scid)
|
|
|
|
|
|
out.append(d)
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
feat(eval): memoir A/B chapter judging and eval-web parity with dialogue
- Judge baseline excerpt and library chapter separately; build_memoir_compare_summary for gate, nine-dim and leaf deltas.
- Memoir SSE chapter payload: baseline_judge, compare_summary, baseline_judge_error.
- MemoirJudgeOutput: loose score coercion and post-validate clamp; memoir judge prompt caps from settings.
- app-eval-web: two-column MemoirScoreCard layout, MemoirCompareSummary, chapter blocks and CSS.
- Add memoir_compare_summary, log_events, celery_log_context, memoir_pipeline_progress; tests and migration 0014.
- Misc: memory/evidence and enrichment paths, task/orchestrator updates, internal-eval docs, env examples.
2026-04-10 10:23:43 +08:00
|
|
|
|
def enrichment_payload_to_fact_dicts(payload: EnrichmentPayload) -> list[dict]:
|
|
|
|
|
|
"""将 EnrichmentPayload.facts 转为与 extract_facts 一致的字典列表。"""
|
|
|
|
|
|
return facts_payload_to_dicts(FactsExtractionPayload(facts=list(payload.facts)))
|