RAG API Reference¶
Complete API reference for RapidAI's RAG system.
Classes¶
RAG¶
Main orchestrator for retrieval-augmented generation.
class RAG:
def __init__(
self,
embedding: Optional[BaseEmbedding] = None,
vectordb: Optional[BaseVectorDB] = None,
chunker: Optional[BaseChunker] = None,
config: Optional[RAGConfig] = None,
)
Parameters:
| Parameter | Type | Default | Description |
|---|---|---|---|
embedding |
BaseEmbedding |
Auto-created | Embedding provider |
vectordb |
BaseVectorDB |
Auto-created | Vector database |
chunker |
BaseChunker |
Auto-created | Text chunker |
config |
RAGConfig |
None |
Configuration (loads from env if None) |
Methods:
async initialize()¶
Initialize vector database collection.
async add_document(source)¶
Add a document to the RAG system.
Parameters:
source(str | Path | Document) - File path or Document object
Returns: List[DocumentChunk] - Created chunks
async add_documents(sources)¶
Add multiple documents.
Parameters:
sources(List[str | Path | Document]) - List of file paths or Document objects
Returns: List[List[DocumentChunk]] - List of chunk lists
async retrieve(query, top_k, filter_metadata)¶
Retrieve relevant chunks for a query.
Parameters:
query(str) - Search querytop_k(Optional[int]) - Number of results (default from config)filter_metadata(Optional[Dict[str, Any]]) - Metadata filters
Returns: RetrievalResult - Result with text and sources
async query(query, llm, system_prompt, top_k)¶
Full RAG query: retrieve + generate.
Parameters:
query(str) - User queryllm(BaseLLM) - LLM instance for generationsystem_prompt(Optional[str]) - System prompttop_k(Optional[int]) - Number of chunks to retrieve
Returns: str - Generated response
Embedding¶
Factory function for creating embedding providers.
Parameters:
provider(str) - Provider name ("sentence-transformers", "openai", "mock")**kwargs- Additional parameters for provider
Returns: BaseEmbedding - Embedding instance
Example:
# Sentence transformers
embedding = Embedding()
# OpenAI
embedding = Embedding(provider="openai", model="text-embedding-3-large")
DocumentLoader¶
Factory function for creating document loaders.
Auto-detects file type from extension and returns appropriate loader.
Supported extensions: .pdf, .docx, .txt, .md, .markdown, .html, .htm
Example:
Chunker¶
Factory function for creating chunkers.
Parameters:
strategy(str) - Chunking strategy ("recursive", "sentence")**kwargs- Additional parameters for chunker
Strategies:
recursive- Hierarchical splitting with multiple separatorssentence- Sentence-based splitting
Example:
# Recursive
chunker = Chunker()
# Sentence-based
chunker = Chunker(strategy="sentence", chunk_size=512, chunk_overlap=2)
VectorDB¶
Factory function for creating vector databases.
Parameters:
backend(str) - Backend name ("chromadb", "mock")**kwargs- Additional parameters for backend
Example:
Decorators¶
@rag()¶
Decorator for adding RAG to routes.
def rag(
sources: Optional[List[Union[str, Path]]] = None,
top_k: int = 5,
collection_name: Optional[str] = None,
auto_initialize: bool = True,
) -> Callable
Parameters:
sources- Documents to load at startuptop_k- Number of chunks to retrievecollection_name- Vector DB collection nameauto_initialize- Auto-initialize RAG system
Example:
@app.route("/ask", methods=["POST"])
@rag(sources=["docs/manual.pdf"], top_k=5)
async def ask(query: str, rag_context):
# rag_context is automatically injected
pass
Configuration¶
RAGConfig¶
class RAGConfig(BaseSettings):
embedding: EmbeddingConfig
chunking: ChunkingConfig
vectordb: VectorDBConfig
top_k: int = 5
enable_caching: bool = True
cache_ttl: int = 3600
Environment Variables:
RAPIDAI_RAG_TOP_K- Default top_k value
EmbeddingConfig¶
class EmbeddingConfig(BaseSettings):
provider: str = "sentence-transformers"
model: str = "all-MiniLM-L6-v2"
batch_size: int = 32
api_key: Optional[str] = None
Environment Variables:
RAPIDAI_EMBEDDING_PROVIDER- Embedding providerRAPIDAI_EMBEDDING_MODEL- Model nameRAPIDAI_EMBEDDING_BATCH_SIZE- Batch sizeRAPIDAI_EMBEDDING_API_KEY- API key (for OpenAI)
ChunkingConfig¶
class ChunkingConfig(BaseSettings):
strategy: str = "recursive"
chunk_size: int = 512
chunk_overlap: int = 50
separator: str = "\n\n"
Environment Variables:
RAPIDAI_CHUNKING_STRATEGY- Chunking strategyRAPIDAI_CHUNKING_CHUNK_SIZE- Chunk sizeRAPIDAI_CHUNKING_CHUNK_OVERLAP- Chunk overlapRAPIDAI_CHUNKING_SEPARATOR- Separator
VectorDBConfig¶
class VectorDBConfig(BaseSettings):
backend: str = "chromadb"
persist_directory: str = "./chroma_data"
collection_name: str = "rapidai_docs"
Environment Variables:
RAPIDAI_VECTORDB_BACKEND- Vector DB backendRAPIDAI_VECTORDB_PERSIST_DIRECTORY- Persist directoryRAPIDAI_VECTORDB_COLLECTION_NAME- Collection name
Types¶
All RAG-related types are defined in rapidai.types:
Document¶
@dataclass
class Document:
content: str
metadata: Dict[str, Any]
chunks: Optional[List[DocumentChunk]] = None
DocumentChunk¶
@dataclass
class DocumentChunk:
content: str
metadata: Dict[str, Any]
embedding: Optional[List[float]] = None
RetrievalResult¶
@dataclass
class RetrievalResult:
text: str
sources: List[DocumentChunk]
score: Optional[float] = None
Base Classes¶
For implementing custom components:
BaseEmbedding¶
class BaseEmbedding(ABC):
@abstractmethod
async def embed_text(self, text: str) -> List[float]:
pass
@abstractmethod
async def embed_batch(self, texts: List[str]) -> List[List[float]]:
pass
@property
@abstractmethod
def dimension(self) -> int:
pass
BaseDocumentLoader¶
class BaseDocumentLoader(ABC):
@abstractmethod
async def load(self, source: Union[str, Path]) -> Document:
pass
async def load_batch(self, sources: List[Union[str, Path]]) -> List[Document]:
pass
BaseChunker¶
class BaseChunker(ABC):
@abstractmethod
def chunk(self, document: Document) -> List[DocumentChunk]:
pass
BaseVectorDB¶
class BaseVectorDB(ABC):
@abstractmethod
async def create_collection(self, name: str, dimension: int, **kwargs) -> None:
pass
@abstractmethod
async def add_chunks(self, collection: str, chunks: List[DocumentChunk]) -> None:
pass
@abstractmethod
async def search(
self,
collection: str,
query_embedding: List[float],
top_k: int = 5,
filter_metadata: Optional[Dict[str, Any]] = None,
) -> List[DocumentChunk]:
pass
@abstractmethod
async def delete_collection(self, name: str) -> None:
pass