π Deep Dive: FastAPI Application Architecture
π§ Why FastAPI for RAG?
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FASTAPI BENEFITS β
β β
β β
Async support β Efficient I/O for LLM calls β
β β
Automatic docs β Swagger UI at /docs β
β β
Pydantic β Type validation and serialization β
β β
Streaming β SSE for real-time responses β
β β
Production-ready β Used by Netflix, Uber, Microsoft β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Architecture Overview
src/api/
βββ main.py # Application entry point
βββ models.py # Pydantic request/response models
βββ __init__.py
βββ routes/
βββ __init__.py
βββ health.py # Health check endpoints
βββ query.py # Query/RAG endpoints
βββ ingest.py # Document ingestion endpoints
π― main.py - Application Entry Point
FastAPI App Configuration
app = FastAPI(
title="DocuMind AI",
description="RAG-Powered Documentation Assistant API",
version="1.0.0",
docs_url="/docs",
redoc_url="/redoc",
lifespan=lifespan,
)
Lifespan Context Manager
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("Starting DocuMind AI API...")
yield
logger.info("Shutting down...")
CORS Configuration
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
π models.py - Pydantic Schemas
QueryRequest
class QueryRequest(BaseModel):
question: str = Field(..., min_length=1, max_length=2000)
collection_name: str = Field(default="documents")
top_k: int = Field(default=5, ge=1, le=20)
include_sources: bool = Field(default=True)
QueryResponse
class QueryResponse(BaseModel):
answer: str
question: str
sources: Optional[List[SourceDocument]]
num_sources: Optional[int]
IngestResponse
class IngestResponse(BaseModel):
success: bool
message: str
documents_processed: int
chunks_created: int
collection_name: str
π£οΈ Routes
Health Routes (/health)
| Endpoint |
Method |
Purpose |
/health |
GET |
Basic health check |
/health/detailed |
GET |
Component-level health |
/ready |
GET |
Kubernetes readiness |
/live |
GET |
Kubernetes liveness |
Query Routes (/api/query)
| Endpoint |
Method |
Purpose |
/api/query |
POST |
Standard RAG query |
/api/query/stream |
POST |
Streaming response |
Ingest Routes (/api/ingest)
| Endpoint |
Method |
Purpose |
/api/ingest |
POST |
Upload files |
/api/ingest/text |
POST |
Ingest raw text |
/api/ingest/{collection} |
DELETE |
Delete collection |
π Query Flow
POST /api/query
{
"question": "What is the refund policy?",
"collection_name": "documents",
"top_k": 5,
"include_sources": true
}
β
get_pipeline(collection_name, top_k)
β
pipeline.query(question, include_sources)
β
{
"answer": "Based on the documentation...",
"question": "What is the refund policy?",
"sources": [...],
"num_sources": 3
}
π€ Ingest Flow
POST /api/ingest
[files: policy.pdf, guide.md]
β
Validate file extensions (.pdf, .md, .txt)
β
Save to temp files
β
MultiFormatDocumentLoader.load_file()
β
DocumentChunker.chunk_documents()
β
VectorStore.add_documents()
β
{
"success": true,
"documents_processed": 2,
"chunks_created": 15
}
π Running the API
python -m src.api.main
uvicorn src.api.main:app --reload --host 0.0.0.0 --port 8000
Access Points:
π Summary
| File |
Purpose |
main.py |
App config, CORS, routers |
models.py |
Pydantic schemas |
routes/health.py |
Health checks |
routes/query.py |
RAG query endpoints |
routes/ingest.py |
File upload endpoints |