58 lines
1.5 KiB
Python

"""冻结装载侧的卡片召回:走本地 muse.db 向量 blob 暴力扫,不调模型。"""
from __future__ import annotations
import sys
from pathlib import Path
from typing import Any, Callable, Sequence
def _project_root() -> Path:
return next(
parent
for parent in (Path(__file__).resolve().parent, *Path(__file__).resolve().parents)
if (parent / "AGENTS.md").is_file() and (parent / ".git").exists()
)
def recall_cards(
intent_vector: Sequence[float],
*,
kind: str | None = None,
work_id: int | None = None,
top: int = 5,
sqlite_path: str | Path | None = None,
) -> list[dict[str, Any]]:
"""按查询向量从本地库召回卡片。embedder 由调用方注入,本函数不调模型。"""
root = _project_root()
if str(root) not in sys.path:
sys.path.insert(0, str(root))
from muse.store import default_db_path, search_card_vectors
return search_card_vectors(
intent_vector,
kind=kind,
work_id=work_id,
top=top,
path=sqlite_path or default_db_path(),
)
def recall_by_intent(
intent: str,
*,
embedder: Callable[[str], list[float]],
kind: str | None = None,
work_id: int | None = None,
top: int = 5,
sqlite_path: str | Path | None = None,
) -> list[dict[str, Any]]:
return recall_cards(
embedder(intent),
kind=kind,
work_id=work_id,
top=top,
sqlite_path=sqlite_path,
)