-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_api.py
More file actions
66 lines (47 loc) · 1.63 KB
/
Copy pathagent_api.py
File metadata and controls
66 lines (47 loc) · 1.63 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
64
65
66
"""
Agent API Server with Web UI.
Wraps the SmartLocationAgent in a FastAPI web server,
serves the HTML frontend, and exposes a /query endpoint.
"""
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from pathlib import Path
from agent.agent import SmartLocationAgent
app = FastAPI(title="Smart Location Intelligence Agent", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
agent = SmartLocationAgent()
BASE_DIR = Path(__file__).parent
TEMPLATE_PATH = BASE_DIR / "templates" / "index.html"
class QueryRequest(BaseModel):
query: str
class QueryResponse(BaseModel):
query: str
response: str
success: bool
error: str | None = None
@app.get("/", response_class=HTMLResponse)
def serve_ui():
return TEMPLATE_PATH.read_text()
@app.get("/health")
def health():
return {"status": "ok", "service": "agent"}
@app.post("/query", response_model=QueryResponse)
async def query_agent(request: QueryRequest):
try:
response = agent.run(request.query)
return QueryResponse(query=request.query, response=response, success=True)
except Exception as e:
return QueryResponse(query=request.query, response="", success=False, error=str(e))
if __name__ == "__main__":
import uvicorn
port = int(os.environ.get("PORT", 8000))
uvicorn.run("agent_api:app", host="0.0.0.0", port=port, reload=False)