|
| 1 | +"""HTTP service that exposes the Crypt agent to the web frontend. |
| 2 | +
|
| 3 | +Endpoints: |
| 4 | + GET /health -> readiness, whether an LLM key is present, corpus size |
| 5 | + POST /search {query} -> hybrid retrieval results with coordinates (no key) |
| 6 | + POST /ask {question} -> grounded, cited answer + the cited places (needs key) |
| 7 | +
|
| 8 | +The retriever (and its models) load once at startup and are shared across |
| 9 | +requests. Run with: ``uvicorn crypt_agent.api:app --port 8088``. |
| 10 | +""" |
| 11 | + |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +from functools import lru_cache |
| 15 | + |
| 16 | +from fastapi import FastAPI, HTTPException |
| 17 | +from fastapi.middleware.cors import CORSMiddleware |
| 18 | +from pydantic import BaseModel |
| 19 | + |
| 20 | +from .agent import CryptAgent |
| 21 | +from .config import load_settings |
| 22 | +from .corpus import Place |
| 23 | +from .llm import LLM, LLMUnavailable |
| 24 | +from .retrieval import Retriever |
| 25 | + |
| 26 | +app = FastAPI(title="Crypt Agent", version="0.1.0") |
| 27 | +app.add_middleware( |
| 28 | + CORSMiddleware, |
| 29 | + allow_origins=["*"], # dev: the Next.js frontend is a separate origin |
| 30 | + allow_methods=["*"], |
| 31 | + allow_headers=["*"], |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +@lru_cache(maxsize=1) |
| 36 | +def _retriever() -> Retriever: |
| 37 | + return Retriever() |
| 38 | + |
| 39 | + |
| 40 | +@lru_cache(maxsize=1) |
| 41 | +def _agent() -> CryptAgent: |
| 42 | + return CryptAgent(retriever=_retriever(), llm=LLM()) |
| 43 | + |
| 44 | + |
| 45 | +def _place_dto(place: Place) -> dict: |
| 46 | + return { |
| 47 | + "id": place.id, |
| 48 | + "name": place.name, |
| 49 | + "city": place.city, |
| 50 | + "state": place.state, |
| 51 | + "structure_type": place.structure_type, |
| 52 | + "lat": place.lat, |
| 53 | + "lng": place.lng, |
| 54 | + "description": place.description, |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +class SearchRequest(BaseModel): |
| 59 | + query: str |
| 60 | + state: str | None = None |
| 61 | + structure_type: str | None = None |
| 62 | + k: int = 6 |
| 63 | + |
| 64 | + |
| 65 | +class AskRequest(BaseModel): |
| 66 | + question: str |
| 67 | + max_steps: int = 5 |
| 68 | + |
| 69 | + |
| 70 | +@app.get("/health") |
| 71 | +def health() -> dict: |
| 72 | + settings = load_settings() |
| 73 | + active_model = settings.model if settings.provider == "anthropic" else settings.ollama_model |
| 74 | + return { |
| 75 | + "ok": True, |
| 76 | + "provider": settings.provider, |
| 77 | + "has_llm": settings.has_llm, |
| 78 | + "model": active_model, |
| 79 | + "corpus_size": len(_retriever().places), |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | +@app.post("/search") |
| 84 | +def search(req: SearchRequest) -> dict: |
| 85 | + filters: dict = {} |
| 86 | + if req.state: |
| 87 | + filters["state"] = req.state |
| 88 | + if req.structure_type: |
| 89 | + filters["structure_type"] = req.structure_type |
| 90 | + hits = _retriever().search(req.query, k=req.k, filters=filters or None) |
| 91 | + return { |
| 92 | + "results": [ |
| 93 | + {**_place_dto(h.place), "rerank_score": h.rerank_score} for h in hits |
| 94 | + ] |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | +@app.post("/ask") |
| 99 | +def ask(req: AskRequest) -> dict: |
| 100 | + try: |
| 101 | + result = _agent().answer(req.question, max_steps=req.max_steps) |
| 102 | + except LLMUnavailable as exc: |
| 103 | + raise HTTPException(status_code=503, detail=str(exc)) |
| 104 | + places = _retriever().places |
| 105 | + cited = [_place_dto(places[i]) for i in result.cited_ids if 0 <= i < len(places)] |
| 106 | + return { |
| 107 | + "question": result.question, |
| 108 | + "answer": result.answer, |
| 109 | + "grounded": result.grounded, |
| 110 | + "steps": result.steps, |
| 111 | + "cited": cited, |
| 112 | + } |
0 commit comments