Created
January 19, 2026 20:32
-
-
Save lovemycodesnippets/06ca78ee527a07dd220069c4000dbfaa to your computer and use it in GitHub Desktop.
How to Build Production-Ready AI Agents with RAG and FastAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # app.py | |
| import os, asyncio | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from rag import build_index | |
| from agent import agent_run | |
| app = FastAPI(title="Agentic AI API") | |
| # Build a tiny demo index (swap with your corpus) | |
| PAGES = [("handbook", "All employees must follow the Q2 compliance checklist."), | |
| ("policy", "Customer PII must never be returned in chat responses.")] | |
| INDEX = build_index(PAGES) | |
| class AskRequest(BaseModel): | |
| query: str | |
| @app.post("/ask") | |
| async def ask(req: AskRequest): | |
| try: | |
| result = await asyncio.wait_for(agent_run(req.query, INDEX), timeout=25) | |
| return result | |
| except asyncio.TimeoutError: | |
| raise HTTPException(status_code=504, detail="Agent timed out") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment