import logging from fastapi import FastAPI, HTTPException from fastapi.responses import JSONResponse from langgraph.errors import GraphRecursionError from langchain_groq import ChatGroq from apps.models import QueryInput from apps.agent.graph import Agent from apps.agent.constant import GROQ_API_KEY, MODEL_GROQ, CONFIG logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) llm = ChatGroq(model=MODEL_GROQ, api_key=GROQ_API_KEY, temperature=0.1) agent = Agent(llm=llm) app = FastAPI( title="Agent API", description="API to interact with the RAG agent.", version="0.1.0", docs_url="/docs", redoc_url="/redoc", openapi_url="/openapi.json", ) @app.get("/", summary="API Health Check", tags=["Health"]) async def health_check(): """Endpoint for checking the API status.""" return {"status": "API is running"} @app.post("/query-agent", summary="Query the RAG Agent", tags=["Agent"]) async def query_rag_agent(query: QueryInput): """ """ try: output = agent.graph.invoke({"messages": ("user", query.query)}, CONFIG) response = output["messages"][-1].content logger.info(f"Processed query successfully: {query.query}") return JSONResponse( content={"response": response}, media_type="application/json", status_code=200 ) except GraphRecursionError: logger.error("Graph recursion limit reached; query processing failed.") raise HTTPException( status_code=500, detail="Recursion limit reached. Could not generate response despite 25 attempts." )