-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
63 lines (45 loc) · 1.64 KB
/
app.py
File metadata and controls
63 lines (45 loc) · 1.64 KB
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
from __future__ import annotations
from functools import lru_cache
from pathlib import Path
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel, Field
from sql_agent.config import Settings
from sql_agent.factory import build_orchestrator
from sql_agent.orchestrator import SQLAgentOrchestrator
BASE_DIR = Path(__file__).resolve().parent
WEB_DIR = BASE_DIR / "web"
app = FastAPI(title="SQL Agent API", version="0.1.0")
app.mount("/assets", StaticFiles(directory=WEB_DIR / "assets"), name="assets")
class AskRequest(BaseModel):
question: str = Field(min_length=3, max_length=1000)
class AskResponse(BaseModel):
question: str
sql: str
rows: str
summary: str
@lru_cache(maxsize=1)
def get_orchestrator() -> SQLAgentOrchestrator:
settings = Settings.from_env()
return build_orchestrator(settings)
@app.get("/")
def index() -> FileResponse:
return FileResponse(WEB_DIR / "index.html")
@app.get("/health")
def health() -> dict[str, str]:
return {"status": "ok"}
@app.post("/api/ask", response_model=AskResponse)
def ask(request: AskRequest) -> AskResponse:
try:
result = get_orchestrator().run(request.question)
except ValueError as err:
raise HTTPException(status_code=400, detail=str(err)) from err
except Exception as err: # pragma: no cover
raise HTTPException(status_code=500, detail=f"Agent execution failed: {err}") from err
return AskResponse(
question=result.user_question,
sql=result.sql,
rows=result.rows,
summary=result.summary,
)