File size: 1,545 Bytes
686a009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
758b24d
 
 
 
 
 
 
 
686a009
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from agent.agentic_workflow import GraphBuilder
from fastapi.responses import JSONResponse
from dotenv import load_dotenv
from pydantic import BaseModel
import datetime
from typing import List

load_dotenv()

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # Adjust for prod
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class QueryRequest(BaseModel):
    question: str

@app.get("/")
async def health_check():
    return {
        "status": "healthy",
        "timestamp": datetime.datetime.now().isoformat(),
        "service": "ai-trip-planner-api"
    }

@app.post("/query")
async def query_travel_agent(query: QueryRequest):
    try:
        graph_agent = GraphBuilder(model_provider="groq")

        # Collect streamed output
        streamed_response: List[str] = []

        async def capture_stream():
            async for event in graph_agent.graph.astream(
                {"messages": [query.question]},
            ):
                if "messages" in event:
                    for msg in event["messages"]:
                        streamed_response.append(msg.content)

        # Ensure the graph is built
        graph_agent.build_graph()
        await capture_stream()

        return {"answer": streamed_response[-1] if streamed_response else "No response received."}

    except Exception as e:
        return JSONResponse(status_code=500, content={"error": str(e)})