Spaces:
Sleeping
Sleeping
File size: 2,256 Bytes
67b9f40 1626edd 67b9f40 01c3ff5 67b9f40 01c3ff5 67b9f40 01c3ff5 67b9f40 01c3ff5 67b9f40 1626edd 67b9f40 1626edd 01c3ff5 1626edd 67b9f40 01c3ff5 67b9f40 1626edd 01c3ff5 67b9f40 1626edd 01c3ff5 67b9f40 01c3ff5 1626edd 01c3ff5 1626edd ba978d7 1626edd 01c3ff5 67b9f40 01c3ff5 1626edd 67b9f40 01c3ff5 1626edd 67b9f40 1626edd |
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 |
# main.py
import os
import asyncio
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from fastapi.middleware.cors import CORSMiddleware
# Import agent handler
from agent import run_agent_query
app = FastAPI(title="CFO Bot - Agent API")
# ---------------------------------------------------------
# CORS (adjust for production)
# ---------------------------------------------------------
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # replace with your frontend domain
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# ---------------------------------------------------------
# Request / Response Models
# ---------------------------------------------------------
class QueryRequest(BaseModel):
query: str
thread_id: str # REQUIRED — not optional
class QueryResponse(BaseModel):
output: str
# ---------------------------------------------------------
# Health check
# ---------------------------------------------------------
@app.get("/")
def root():
return {"status": "CFO Bot API running", "version": "1.0.0"}
# ---------------------------------------------------------
# Query Route
# ---------------------------------------------------------
@app.post("/query", response_model=QueryResponse)
async def query_endpoint(req: QueryRequest):
# Validate query
if not req.query or not req.query.strip():
raise HTTPException(status_code=400, detail="Query cannot be empty.")
# Validate thread_id
if not req.thread_id or not req.thread_id.strip():
raise HTTPException(status_code=400, detail="thread_id is required.")
try:
# Run agent — NO DEFAULT thread_id
result = await run_agent_query(
req.query,
thread_id=req.thread_id
)
print(req.query)
except Exception as exc:
raise HTTPException(status_code=500, detail=f"Agent failed: {str(exc)}")
# Extract output properly
final_output = (
getattr(result, "final_output", None)
or (result.get("final_output") if isinstance(result, dict) else None)
)
if not final_output:
final_output = "No response generated."
return QueryResponse(output=final_output)
|