Module 5 phase 4 step 1 (docs/kb/modules/05-faza4-plan.md, §4): first user-facing HTTP entry point to the KB. FastAPI wrapping kb_retrieval.cascade_query/flat_query — GET /search (query_text -> embed via Ollama@SOLARIA -> cascade/flat -> envelope join -> JSON with per-source links) and GET /healthz. Search API only, no answer synthesis (phase 5) and no server-side dist filtering — the 0.45/0.55 colour thresholds are a frontend concern (plan §7, a later step). Hard startup invariant (plan §2 decision 2): refuses to start unless the configured EMBED_MODEL is present in both document_chunk.model and document_summary.embedding_model. Note the latter: document_summary.model is the LLM that *wrote* the summary (claude-haiku-4-5/gemma3:12b), not the embedder — checked live against kb-postgres@PIHA before writing this, see app/startup.py's docstring. Verified end-to-end with a live docker run: the invariant crash-loops on a mismatched EMBED_MODEL and passes through to a real /search hit against the live corpus with a correct model. Repo-only: no deploy, no npm/OIDC/DNS wiring (plan §8, later step), no local embed fallback (plan §5, later step) — Ollama@SOLARIA is called directly and a failure surfaces as 503, not a crash. Also: scripts/deploy/deploy.sh's gate now builds each service via `docker compose build` instead of a raw `docker build <svc_dir>`, so a service whose docker-compose.yml declares a repo-root build context (needed here to COPY packages/kb-retrieval/, the packages/ Dockerfile convention already documented in CLAUDE.md) resolves the same way in the gate as it does at real deploy time (deploy-node.sh's `docker compose ... up --build`). No behavior change for existing single-context services — verified against llm-gateway's compose file. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
49 lines
1.6 KiB
Python
49 lines
1.6 KiB
Python
"""`/search` core -- module 5 phase 4 (docs/kb/modules/05-faza4-plan.md §4). Kept decoupled
|
|
from FastAPI so it can be unit-tested with fake `conn`/`session` objects, the same style as
|
|
`kb_retrieval`'s own tests, instead of needing a live DB/Ollama behind a TestClient.
|
|
|
|
Response shape (plan §4 exactly): `{"query", "mode", "sol_status", "results": [...]}`, each
|
|
result carrying `dist` un-filtered -- the 0.45/0.55 colour thresholds (plan §7) are a frontend
|
|
concern (Krok 4, out of this step's scope), never applied server-side.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import aiohttp
|
|
import asyncpg
|
|
|
|
from kb_retrieval.retrieval import cascade_query, flat_query
|
|
|
|
from app.db import fetch_envelopes
|
|
from app.links import build_result
|
|
|
|
|
|
async def run_search(
|
|
conn: asyncpg.Connection,
|
|
session: aiohttp.ClientSession,
|
|
ollama_url: str,
|
|
query_text: str,
|
|
mode: str,
|
|
embed_model: str,
|
|
summary_model: str,
|
|
) -> dict:
|
|
if mode == "flat":
|
|
retrieval = await flat_query(conn, session, ollama_url, query_text, embed_model=embed_model)
|
|
else:
|
|
retrieval = await cascade_query(
|
|
conn, session, ollama_url, query_text,
|
|
summary_model=summary_model, embed_model=embed_model,
|
|
)
|
|
|
|
chunks = retrieval["chunks"]
|
|
envelope_ids = sorted({c["envelope_id"] for c in chunks})
|
|
envelopes = await fetch_envelopes(conn, envelope_ids)
|
|
|
|
results = [build_result(chunk, envelopes.get(chunk["envelope_id"])) for chunk in chunks]
|
|
|
|
return {
|
|
"query": query_text,
|
|
"mode": mode,
|
|
"sol_status": "up", # reaching this point means the embed call above succeeded
|
|
"results": results,
|
|
}
|