Skip to content
GitHub

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.

Full documentation: docs.lexigram.dev

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
async with Application.boot(modules=[AppModule]) as app:
# use app.container to resolve services
...

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
tenancy.enabledFalseEnable per-tenant pipeline isolation
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 with top_k / similarity_threshold controls
  • Reranking: FlashRank cross-encoder reranker
  • 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, RAGTenancyConfig, 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/tenancy/TenantScopedRAGPipeline factory + resolver
src/lexigram/ai/rag/exceptions.pyFull exception hierarchy
src/lexigram/ai/rag/types.pyRAG-specific domain types

lexigram-ai-rag supports per-tenant pipeline isolation. When tenancy is enabled, the provider registers a TenantScopedRAGPipeline — a caching wrapper that builds a dedicated RAGPipelineProtocol per tenant, with a tenant-resolved collection_name.

Note: Enabling tenancy requires the app-wide Context binding, which ships with the core bootstrap module (CoreModule / StandardModule from lexigram.app). A bare Application.boot(modules=[RAGModule...]) without it fails with UnresolvableDependencyError: Context.

from lexigram.ai.rag import RAGModule
from lexigram.ai.rag.config import RAGConfig, RAGTenancyConfig
config = RAGConfig(
tenancy=RAGTenancyConfig(enabled=True),
collection_name="my_docs",
)
RAGModule.configure(config)

Use RAGConfig.with_collection() to create configs scoped to different collection names — handy for dynamic per-tenant pipeline construction:

tenant_config = RAGConfig().with_collection("tenant_a_collection")
ComponentRole
RAGTenancyConfigDataclass with enabled flag
TenantScopedRAGPipelineCaches per-tenant pipelines (LRU eviction)
TemplatedTenantCollectionResolverResolves logical → physical collection name per tenant