File size: 3,260 Bytes
e8c5cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""FastAPI app: retrieve -> rerank -> Claude, with citation-click logging.

    uvicorn src.api.app:app --reload
    RESEARCHOS_CONFIG=configs/app.yaml uvicorn src.api.app:app
"""

from __future__ import annotations

import os
from contextlib import asynccontextmanager
from typing import Any

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel, Field

from src.api.db import FeedbackLog
from src.api.pipeline import ResearchPipeline
from src.config import load_config

CONFIG_PATH = os.environ.get("RESEARCHOS_CONFIG", "configs/app.yaml")

state: dict[str, Any] = {}


@asynccontextmanager
async def lifespan(_app: FastAPI):
    """Build indices and load models once, at startup."""
    cfg = load_config(CONFIG_PATH)
    state["cfg"] = cfg
    state["pipeline"] = ResearchPipeline(cfg)
    state["log"] = FeedbackLog(cfg["paths"]["feedback_db"])
    yield
    state.clear()


app = FastAPI(
    title="ResearchOS",
    description="Scientific literature retrieval with a fine-tuned cross-encoder reranker.",
    version="1.0.0",
    lifespan=lifespan,
)


class AskRequest(BaseModel):
    query: str = Field(min_length=3, max_length=1000)
    top_k: int = Field(default=5, ge=1, le=20)
    generate: bool = True


class ClickRequest(BaseModel):
    query_id: int
    doc_id: str


@app.get("/health")
def health() -> dict[str, Any]:
    """Liveness plus what the pipeline is actually running."""
    pipeline: ResearchPipeline | None = state.get("pipeline")
    return {
        "status": "ok" if pipeline else "loading",
        "documents": len(pipeline.corpus) if pipeline else 0,
        "reranker_backend": pipeline.backend if pipeline else None,
        "reranker_model": state["cfg"]["reranker"]["model"] if "cfg" in state else None,
    }


@app.post("/search")
def search(request: AskRequest) -> dict[str, Any]:
    """Retrieval and reranking only - no generation."""
    pipeline: ResearchPipeline = state["pipeline"]
    passages = pipeline.retrieve(request.query)[: request.top_k]
    return {"query": request.query, "results": passages}


@app.post("/ask")
def ask(request: AskRequest) -> dict[str, Any]:
    """Full pipeline. Logs the query and the citations shown."""
    pipeline: ResearchPipeline = state["pipeline"]
    log: FeedbackLog = state["log"]

    result = pipeline.answer(request.query, top_k_final=request.top_k, generate=request.generate)
    query_id = log.log_query(
        query=request.query,
        answer=result["answer"],
        latency_ms=result["timing_ms"]["total"],
        backend=result["backend"],
        citations=result["citations"],
    )
    return {"query_id": query_id, **result}


@app.post("/feedback/click")
def feedback_click(request: ClickRequest) -> dict[str, Any]:
    """Record that a user opened a cited passage. This is the retraining signal."""
    log: FeedbackLog = state["log"]
    try:
        log.log_click(request.query_id, request.doc_id)
    except Exception as exc:  # noqa: BLE001 - surface storage failures to the caller
        raise HTTPException(status_code=400, detail=str(exc)) from exc
    return {"logged": True, **log.stats()}


@app.get("/stats")
def stats() -> dict[str, int]:
    """Counts from the feedback log."""
    return state["log"].stats()