ruby2210/rag-chatbot
0
1"""2Ingestion endpoints for the RAG Chatbot application.3Implements the API endpoints for content ingestion and embedding.4"""5from fastapi import APIRouter, HTTPException6from pydantic import BaseModel, Field7from typing import Optional8from ..services.ingestion_service import ingestion_service9from ..utils.config import settings10from ..utils.logging import get_logger11 12 13logger = get_logger(__name__)14router = APIRouter()15 16 17class IngestRequest(BaseModel):18 """19 Request model for ingestion endpoint.20 """21 source_path: Optional[str] = Field(22 None,23 description="Path to markdown files; defaults to settings.BOOK_SOURCE_PATH"24 )25 26 27class IngestResponse(BaseModel):28 """29 Response model for ingestion endpoint.30 """31 status: str = Field(..., description="Processing status: 'completed', 'failed', or 'in_progress'")32 files_processed: int = Field(..., description="Number of files processed")33 chunks_created: int = Field(..., description="Number of content chunks created for embedding")34 message: str = Field(..., description="Human-readable status message")35 36 37@router.post("/embeddings/ingest", response_model=IngestResponse)38async def ingest_embeddings_endpoint(request: IngestRequest):39 """40 Ingest book content from markdown files and create embeddings for RAG retrieval.41 """42 try:43 # Use the provided source path or default to the configured path44 source_path = request.source_path or settings.BOOK_SOURCE_PATH45 46 # Perform the ingestion process47 result = ingestion_service.ingest_book_content(source_path)48 49 return IngestResponse(50 status=result.get("status", "failed"),51 files_processed=result.get("files_processed", 0),52 chunks_created=result.get("chunks_created", 0),53 message=result.get("message", "Ingestion completed")54 )55 56 except Exception as e:57 logger.error(f"Error in ingestion endpoint: {e}")58 raise HTTPException(status_code=500, detail=f"Error during ingestion: {str(e)}")