Added suggestion questions functionality
Browse files1234517 unmodified lines2324252627282930313233341 unmodified line3637383940414210 unmodified lines535455565758596061627 unmodified lines7071727374752 unmodified lines78798081828384858687888990919293949596import osfrom contextlib import asynccontextmanagerfrom fastapi import FastAPI, HTTPException, UploadFile, File, Formfrom pydantic import BaseModel, Fieldfrom dotenv import load_dotenv17 unmodified linesconninfo=DB_URI,min_size=1,max_size=15,max_idle=300,max_lifetime=1800,reconnect_timeout=10,kwargs={"autocommit": True, "prepare_threshold": 0},check=AsyncConnectionPool.check_connection,open=False,) as pool:await pool.open(wait=True, timeout=15)checkpointer = AsyncPostgresSaver(pool)1 unmodified lineapp.state.pool = poolapp.state.agent = build_agent(checkpointer)yieldapp = FastAPI(title="Enterprise PDF RAG API",description="A production-grade backend powering an agentic LangChain RAG assistant.",10 unmodified linesstatus: strthread_id: strresponse: str@app.post("/chat",response_model=ChatResponse)async def chat_endpoint(request: ChatRequest):agent = app.state.agentconfig = {"configurable": {"thread_id": request.thread_id}}7 unmodified lineslogger.error("Agent processing failed: %s", e)logger.error(traceback.format_exc())raise HTTPException(status_code=500, detail="Agent Processing Error!")output_messages = result.get("messages", [])if not output_messages:raise HTTPException(status_code=500, detail="No messages returned from the agent.")2 unmodified linesstatus="success",thread_id=request.thread_id,response=output_messages[-1].content,)@app.post("/upload")async def upload_pdf(file: UploadFile = File(...),user_id: str = Form(..., min_length=1) ):if file.content_type != "application/pdf":raise HTTPException(status_code=400, detail="Only PDF files are supported.")file_bytes = await file.read()if not file_bytes:raise HTTPException(status_code=400, detail="Uploaded file is empty.")await asyncio.to_thread(upload_file,file_bytes,file.filename,user_id)return {"status": "success",12345617 unmodified lines2425262728293031323334351 unmodified line3738394041424310 unmodified lines5455565758596061626364657 unmodified lines737475767778792 unmodified lines828384858687888990919293949596979899100101import osfrom contextlib import asynccontextmanagerfrom typing import Listfrom fastapi import FastAPI, HTTPException, UploadFile, File, Formfrom pydantic import BaseModel, Fieldfrom dotenv import load_dotenv17 unmodified linesconninfo=DB_URI,min_size=1,max_size=15,max_idle=300,max_lifetime=1800,reconnect_timeout=10,kwargs={"autocommit": True, "prepare_threshold": 0},check=AsyncConnectionPool.check_connection,open=False,) as pool:await pool.open(wait=True, timeout=15)checkpointer = AsyncPostgresSaver(pool)1 unmodified lineapp.state.pool = poolapp.state.agent = build_agent(checkpointer)yieldapp = FastAPI(title="Enterprise PDF RAG API",description="A production-grade backend powering an agentic LangChain RAG assistant.",10 unmodified linesstatus: strthread_id: strresponse: strsuggested_questions: List[str] = Field(default_factory=list)is_fallback: bool = False@app.post("/chat", response_model=ChatResponse)async def chat_endpoint(request: ChatRequest):agent = app.state.agentconfig = {"configurable": {"thread_id": request.thread_id}}7 unmodified lineslogger.error("Agent processing failed: %s", e)logger.error(traceback.format_exc())raise HTTPException(status_code=500, detail="Agent Processing Error!")output_messages = result.get("messages", [])if not output_messages:raise HTTPException(status_code=500, detail="No messages returned from the agent.")2 unmodified linesstatus="success",thread_id=request.thread_id,response=output_messages[-1].content,suggested_questions=result.get("suggested_questions", []),is_fallback=result.get("is_fallback", False),)@app.post("/upload")async def upload_pdf(file: UploadFile = File(...), user_id: str = Form(..., min_length=1)):if file.content_type != "application/pdf":raise HTTPException(status_code=400, detail="Only PDF files are supported.")file_bytes = await file.read()if not file_bytes:raise HTTPException(status_code=400, detail="Uploaded file is empty.")await asyncio.to_thread(upload_file, file_bytes, file.filename, user_id)return {"status": "success",
src/main.py+15-10