from fastapi import FastAPI, Header, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
app = FastAPI()
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["POST"], allow_headers=["*"])
API_KEY = "your-secret"
class Query(BaseModel):
version: int = 1
query: str
top_k: int = 5
@app.post("/query")
def query(q: Query, authorization: str = Header(None)):
if authorization != f"Bearer {API_KEY}":
raise HTTPException(401, "unauthorized")
# Your retrieval logic here — embed q.query, search your vector DB, return top_k
chunks = [{"text": "...", "source": "..."}]
return {"chunks": chunks[:q.top_k]}