Skip to content
GitHubDiscord

AI Rag (lexigram-ai-rag)

Retrieval-Augmented Generation (RAG) pipeline for the Lexigram Framework


RAG (Retrieval-Augmented Generation) pipeline for the Lexigram Framework. Provides a multi-stage, fully configurable pipeline covering ingestion, query processing, retrieval, context optimisation, synthesis, quality assurance, and post-processing — all wired through the DI container via RAGModule. Zero-config usage starts with sensible defaults.

Terminal window
uv add lexigram-ai-rag
# Optional extras
uv add "lexigram-ai-rag[pdf,web]"
from lexigram import Application
from lexigram.di.module import Module, module
from lexigram.ai.rag import RAGModule
from lexigram.ai.rag.config import RAGConfig
@module(imports=[
RAGModule.configure(
RAGConfig(
vector_store_type="pgvector",
collection_name="my_docs",
top_k=5,
enable_citations=True,
)
)
])
class AppModule(Module):
pass
app = Application(modules=[AppModule])
if __name__ == "__main__":
app.run()

Zero-config usage: Call RAGModule.configure() with no arguments to use defaults.

application.yaml
ai_rag:
vector_store_type: "pgvector"
collection_name: "my_docs"
top_k: 5
embedding_model: "text-embedding-ada-002"
enable_citations: true
Section titled “Option 2 — Profiles + Environment Variables (recommended)”
Terminal window
export LEX_AI_RAG__VECTOR_STORE_TYPE=pgvector
# Environment variables for each field
from lexigram.ai.rag.config import RAGConfig
from lexigram.ai.rag import RAGModule
config = RAGConfig(
vector_store_type="pgvector",
collection_name="my_docs",
top_k=5,
)
RAGModule.configure(config)
FieldDefaultEnv varDescription
vector_store_typepgvectorLEX_AI_RAG__VECTOR_STORE_TYPEBackend: pgvector, chroma, qdrant, mock
collection_namedefaultLEX_AI_RAG__COLLECTION_NAMECollection / index name
vector_dimension1536LEX_AI_RAG__VECTOR_DIMENSIONEmbedding dimension
top_k5LEX_AI_RAG__TOP_KDocuments to retrieve per query
similarity_threshold0.7LEX_AI_RAG__SIMILARITY_THRESHOLDMinimum similarity score to include
use_hybrid_searchTrueLEX_AI_RAG__USE_HYBRID_SEARCHCombine semantic + keyword search
embedding_provideropenaiLEX_AI_RAG__EMBEDDING_PROVIDEREmbedding provider
embedding_modelNoneLEX_AI_RAG__EMBEDDING_MODELEmbedding model
chunking_strategyrecursiveLEX_AI_RAG__CHUNKING_STRATEGYrecursive, semantic, or token
chunk_size512LEX_AI_RAG__CHUNK_SIZETokens per chunk
chunk_overlap50LEX_AI_RAG__CHUNK_OVERLAPToken overlap between chunks
enable_citationsTrueLEX_AI_RAG__ENABLE_CITATIONSInclude source citations in responses
citation_styleinlineLEX_AI_RAG__CITATION_STYLEinline, footnote, or numbered
enable_query_expansionTrueLEX_AI_RAG__ENABLE_QUERY_EXPANSIONExpand queries before retrieval
enable_hydeFalseLEX_AI_RAG__ENABLE_HYDEHypothetical Document Embeddings
synthesis_strategyhybridLEX_AI_RAG__SYNTHESIS_STRATEGYdirect, extractive, abstractive, hybrid
MethodDescription
RAGModule.configure(config)Production pipeline
RAGModule.stub()In-memory / no-op pipeline for tests
  • Multi-stage pipeline: Ingestion, query processing, retrieval, context optimization, synthesis, quality assurance, post-processing
  • Chunking strategies: recursive, semantic, token, fixed_size, sliding_window
  • Retrieval: Vector search, BM25 keyword search, knowledge graph traversal
  • Reranking: Cross-encoder and LLM-based rerankers
  • Synthesis: Direct, extractive, abstractive, and hybrid synthesizers
  • HyDE support: Hypothetical Document Embeddings for query expansion
  • Citations: Inline, footnote, or numbered citation styles
  • Quality assurance: Faithfulness check and hallucination detection
async with Application.boot(modules=[RAGModule.stub()]) as app:
# your test code
...
FileWhat it contains
src/lexigram/ai/rag/module.pyRAGModule.configure() and RAGModule.stub()
src/lexigram/ai/rag/config.pyRAGConfig, PipelineConfig, all stage configs
src/lexigram/ai/rag/di/provider.pyRAGProvider — registers pipeline and supporting services
src/lexigram/ai/rag/pipeline/Stage executor and pipeline runner
src/lexigram/ai/rag/exceptions.pyFull exception hierarchy
src/lexigram/ai/rag/protocols.pyPackage-local protocol extensions
src/lexigram/ai/rag/types.pyRAG-specific domain types