Spaces:
Running
Running
File size: 1,651 Bytes
2a51e7d |
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 |
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."
) |