Files
life-echo/api/app/features/memory/retrieval_trace.py
2026-04-30 16:22:55 +08:00

35 lines
1.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""将 memory 检索 bundle 压成可入库的轻量 trace仅稳定 id限长"""
from __future__ import annotations
from typing import Any
def _capped_ids(items: list[Any] | None, *, cap: int = 100) -> list[str]:
out: list[str] = []
for x in items or []:
if len(out) >= cap:
break
if isinstance(x, dict) and x.get("id"):
out.append(str(x["id"]))
return out
def chat_memory_retrieval_trace_from_bundle(
bundle: dict,
*,
top_k: int,
query_len: int,
) -> dict:
"""供 conversation_messages.memory_retrieval_trace_json 使用。"""
return {
"schema_version": 1,
"source": "chat_memory_retrieval",
"top_k": top_k,
"query_len": query_len,
"chunk_ids": _capped_ids(bundle.get("relevant_chunks")),
"fact_ids": _capped_ids(bundle.get("relevant_facts")),
"summary_ids": _capped_ids(bundle.get("relevant_summaries")),
"story_ids": _capped_ids(bundle.get("relevant_stories")),
}