Spaces:
Build error
Build error
|
Download docs/explanations/api.md from AIenthusSP/RAG-Documind-AI: direct link, hf CLI and curl.
- Browser
- Download file 5.02 kB
-
https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/docs/explanations/api.md
- Command line
-
hf download hf://spaces/AIenthusSP/RAG-Documind-AI/docs/explanations/api.md
-
curl -L -o api.md https://huggingface.co/spaces/AIenthusSP/RAG-Documind-AI/resolve/main/docs/explanations/api.md
5.02 kB
| # π 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 | |
| ```python | |
| app = FastAPI( | |
| title="DocuMind AI", | |
| description="RAG-Powered Documentation Assistant API", | |
| version="1.0.0", | |
| docs_url="/docs", # Swagger UI | |
| redoc_url="/redoc", # ReDoc alternative | |
| lifespan=lifespan, # Startup/shutdown events | |
| ) | |
| ``` | |
| ### Lifespan Context Manager | |
| ```python | |
| @asynccontextmanager | |
| async def lifespan(app: FastAPI): | |
| # Startup | |
| logger.info("Starting DocuMind AI API...") | |
| yield | |
| # Shutdown | |
| logger.info("Shutting down...") | |
| ``` | |
| ### CORS Configuration | |
| ```python | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=settings.cors_origins, # ["localhost:3000", "localhost:8501"] | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| ``` | |
| --- | |
| ## π models.py - Pydantic Schemas | |
| ### QueryRequest | |
| ```python | |
| 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 | |
| ```python | |
| class QueryResponse(BaseModel): | |
| answer: str | |
| question: str | |
| sources: Optional[List[SourceDocument]] | |
| num_sources: Optional[int] | |
| ``` | |
| ### IngestResponse | |
| ```python | |
| 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 | |
| ```bash | |
| # Development | |
| python -m src.api.main | |
| # Or with uvicorn directly | |
| uvicorn src.api.main:app --reload --host 0.0.0.0 --port 8000 | |
| ``` | |
| ### Access Points: | |
| - **API**: http://localhost:8000 | |
| - **Swagger Docs**: http://localhost:8000/docs | |
| - **ReDoc**: http://localhost:8000/redoc | |
| --- | |
| ## π 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 | | |