RAGMechanic
A modular Python framework for building, evaluating, and diagnosing Retrieval-Augmented Generation (RAG) systems.
RAGMechanic provides reusable components for document ingestion, text splitting, embeddings, vector storage, retrieval, contextual compression, LLM integration, evaluation, persistent result storage, and HTML reporting.
It is designed to help developers experiment with RAG configurations, inspect retrieved context, track pipeline latency, and review evaluation results without implementing every component from scratch.
Table of Contents
- Overview
- Features
- Architecture
- Project Structure
- Installation
- Quick Start
- Working with Query Results
- Evaluation and Reporting
- Database and Persistence
- Output Files
- Core Components
- Vector Stores
- LLM Providers
Overview
A RAG application involves more than connecting a language model to a vector database. Document parsing, chunking, embedding quality, retrieval relevance, context selection, and answer generation all affect the final result.
RAGMechanic organizes these stages into modular components so developers can build and experiment with different RAG configurations.
It may be useful for: - Developers building document question-answering applications. - AI engineers experimenting with retrieval strategies and vector stores. - Researchers comparing RAG configurations and evaluation results. - Students learning how RAG systems work internally. - Contributors building reusable components for retrieval-augmented applications.
Features
Document ingestion and text splitting
Load supported documents and split their content into chunks for embedding and retrieval. Supported formats and loader requirements depend on the installed dependencies and configured implementation; common formats in the project include PDF, DOCX, PPTX, and TXT.
Chunk size and overlap can be configured when supported by the selected pipeline configuration.
Embeddings and vector storage
Generate vector representations for document chunks and queries, then use a configured vector-store backend for similarity search. The repository includes modules for Chroma, FAISS, Pinecone, and Qdrant. Each backend may require its own package, credentials, and configuration.
Retrieval and contextual compression
Retrieve candidate chunks relevant to a question and, when enabled, refine the retrieved context before sending it to the language model.
LLM integrations
RAGMechanic provides an LLM management module. The repository's dependency configuration includes integrations for Google Gemini, xAI, Mistral, and Anthropic. Actual availability depends on the installed integration and the implementation/configuration used.
Evaluation
The evaluation workflow supports the metrics represented in the current result/database model: - Faithfulness - Answer relevancy - Hallucination
Metric availability and prerequisites depend on the evaluator configuration and installed evaluation dependencies.
Persistent storage and HTML reports
RAGMechanic stores query information, retrieved and compressed chunks, latency/count metadata, and evaluation results in a SQLite database. An HTML report can be generated to inspect available evaluation results. The exact report contents depend on the report implementation and available records.
Architecture
A typical workflow follows these stages:
- Ingestion: Load source documents.
- Splitting: Divide document content into chunks.
- Embedding: Convert chunks into vector representations.
- Indexing: Store vectors and associated document information.
- Retrieval: Find candidate chunks for a query.
- Compression: Refine retrieved context when enabled.
- Generation: Generate an answer using the query and selected context.
- Persistence: Store the query, answer, retrieved/compressed chunks, and pipeline metadata.
- Evaluation: Evaluate queries that do not yet have stored evaluation results.
- Reporting: Generate an HTML report from available stored results.
The exact execution flow depends on the selected components and pipeline configuration.
Project Structure
The repository is organized around the following modules:
ragmechanic/
├── src/
│ └── ragmechanic/
│ ├── compression/
│ ├── embeddings/
│ ├── evaluation/
│ ├── ingestion/
│ ├── llm/
│ ├── report/
│ ├── retriever/
│ ├── storage/
│ ├── vector_store/
│ └── pipeline.py
├── docs/
├── examples.ipynb
├── pyproject.toml
├── README.md
└── .gitignore
Your local working directory may also contain documents/ and output/
folders. These are runtime/development folders, not necessarily part of
the installed package.
Installation
Option 1: Install from PyPI
Install the latest published release:
python -m pip install ragmechanic
Option 2: Install from GitHub for development
Clone the repository:
git clone https://github.com/Divyanshu-Mathur/RAGMechanic.git
cd RAGMechanic
Create and activate a virtual environment.
Windows PowerShell:
python -m venv .venv
.\.venv\Scripts\Activate.ps1
Linux or macOS:
python -m venv .venv
source .venv/bin/activate
Quick Start
Get started with RAGMechanic in just a few steps.
1. Install RAGMechanic
pip install ragmechanic
2. Configure environment variables
Create a .env file in your project root directory and add the API key for your chosen LLM provider.
# Google Gemini
GOOGLE_API_KEY=your_google_api_key
# OpenAI
OPENAI_API_KEY=your_openai_api_key
# xAI Grok
XAI_API_KEY=your_xai_api_key
# Anthropic Claude
ANTHROPIC_API_KEY=your_anthropic_api_key
# Mistral AI
MISTRAL_API_KEY=your_mistral_api_key
# HuggingFace Embedding Model
HF_TOKEN=your_hf_api_key
You only need to configure the API key for the provider you intend to use.
3. Initialize the RAG pipeline
Create a Python file (for example, main.py) and initialize your pipeline:
import os
from dotenv import load_dotenv
from ragmechanic import RAGPipeline
# Load environment variables from .env
load_dotenv()
rag = RAGPipeline(
docs="path/to/your/documents",
# Embedding model (Hugging Face model ID)
embedding_model="sentence-transformers/all-MiniLM-L6-v2",
# LLM configuration
llm_provider="gemini",
llm_model="gemini-2.5-flash",
# Vector database
vector_store="chroma",
# Chunking configuration
chunk_size=800,
chunk_overlap=150,
# SQLite database for query/evaluation records
db_path="ragmechanic.db"
)
4. Ask questions
Once the pipeline is initialized, you can ask questions about your documents.
# Ask a question
result = rag.ask("What is machine learning?")
# Print the generated answer
print(result.answer)
# Inspect retrieved source documents
for document in result.retrieved_documents:
print(document.page_content)
5. Evaluate your RAG pipeline
Evaluate the queries stored in the database:
rag.evaluate()
6. Generate an HTML report
Generate an HTML report to inspect your RAG pipeline's evaluation results:
rag.save_html()
Configuration Guide
LLM Providers
RAGMechanic supports the following LLM providers:
| Provider | Accepted llm_provider values |
|---|---|
| Google Gemini | "gemini", "google", "google_genai" |
| OpenAI | "openai", "gpt" |
| xAI Grok | "grok", "xai" |
| Anthropic Claude | "anthropic", "claude" |
| Mistral AI | "mistral", "mistralai" |
Provider aliases are automatically normalized by RAGMechanic. For example, "google" and "google_genai" are treated as "gemini".
Choosing an LLM Model
The llm_model argument specifies the actual model you want to use from your selected provider.
Use the model identifier supported by the corresponding provider integration.
Examples:
| Provider | Example llm_model |
|---|---|
| Gemini | "gemini-2.5-flash" |
| OpenAI | "gpt-4o-mini" |
| Grok | "grok-3-mini" |
| Anthropic | "claude-3-5-haiku-latest" |
| Mistral | "mistral-small-latest" |
These are example model identifiers. Check your provider's documentation for currently available models and ensure your account has access to the selected model.
Choosing a Vector Store
RAGMechanic supports four vector store backends:
| Vector Store | Configuration value | Description |
|---|---|---|
| ChromaDB | "chroma" |
Local persistent vector database |
| FAISS | "faiss" |
Local similarity search library |
| Qdrant | "qdrant" |
Vector database supporting local and server-based deployments |
| Pinecone | "pinecone" |
Managed cloud vector database |
Set the vector_store argument to the backend you want to use.
For example:
vector_store="chroma"
Or:
vector_store="faiss"
Understanding db_path
The db_path argument specifies the location of the SQLite database used by RAGMechanic to store query and evaluation records.
For example:
db_path="ragmechanic.db"
You can also specify a custom location:
db_path="database/my_rag_project.db"
- If you use a relative path, it is resolved relative to your current working directory.
- If you use an absolute path, the database is stored at that location.
- Reuse the same path when you want to retain access to previously stored records across runs.
- This database is separate from the vector store and does not specify where document embeddings are stored.
Customizing Chunking
You can also customize how documents are split into chunks:
chunk_size=800,
chunk_overlap=150
chunk_size: Maximum size of each document chunk.chunk_overlap: Number of overlapping characters between consecutive chunks.
Adjust these values depending on your document types and retrieval requirements.
Working with Query Results
Each call to rag.ask() returns a RAGResult object. The result
includes the question, generated answer, retrieved documents, compressed
documents, available latency information, and evaluation data when
present.
result = rag.ask(
"What is the syllabus of Module 1 of Computer Networks?"
)
print("Query ID:", result.id)
print("Question:", result.question)
print("Answer:", result.answer)
print("Retrieved document count:", len(result.retrieved_documents))
print("Compressed document count:", len(result.compressed_documents))
print("Retrieval latency:", result.retrieval_latency)
print("Compression latency:", result.compression_latency)
print("Generation latency:", result.generation_latency)
for document in result.retrieved_documents:
print("Source:", document.metadata.get("source"))
print("Page:", document.metadata.get("page"))
print("Content:", document.page_content)
The id may be populated when the result is read back from persistent
storage; whether it is available immediately from ask() depends on the
pipeline implementation. Latency values are measured by the pipeline and
may be zero or unavailable for fields that were not recorded.
Evaluation and Reporting
RAGMechanic separates query execution, evaluation, persistence, and report generation.
Single-query workflow
result = rag.ask("What is machine learning?")
# Check the database and evaluate queries with no stored evaluation
rag.evaluate()
# Create/update the HTML report from available stored results
rag.save_html()
Batch workflow
You can run several questions before triggering evaluation:
questions = [
"What is machine learning?",
"What is deep learning?",
"What is retrieval-augmented generation?",
]
for question in questions:
rag.ask(question)
# Evaluate all stored queries that are missing evaluation records
rag.evaluate()
# Generate the report
rag.save_html()
Incremental evaluation
The database tracks evaluations separately from query records. When
evaluate() uses the database's pending-query workflow, it can identify
query IDs that have no corresponding evaluation record. This supports
incremental processing: previously stored unevaluated queries can be
evaluated along with newly added queries.
Completed evaluations are stored by query ID. Calling evaluation again should not require reevaluating records that already have an evaluation row, unless the implementation or configuration explicitly updates/re-runs them.
Evaluation metrics
The current database result model has fields for:
Metric Description
Faithfulness Assesses whether the answer is supported by the supplied context.
Answer relevancy Assesses how relevant the answer is to the question.
Hallucination Represents the evaluator's hallucination metric/result.
Interpretation, score ranges, and metric prerequisites depend on the configured evaluator.
What the HTML report is for
The report is intended to make stored evaluation results easier to inspect. Use it after evaluation:
rag.evaluate()
rag.save_html()
Database and Persistence
RAGMechanic uses SQLite for persistent query and evaluation records. SQLite stores data in a local database file and does not require a separate database server.
The database layer defines four tables.
1. queries
Stores one record per saved query, including: - id: unique query
identifier. - question and answer. - Retrieval, compression,
generation, and total latency fields. - Retrieved and compressed chunk
counts. - Compression ratio. - Creation timestamp.
2. retrieved_chunks
Stores retrieved document chunks associated with a query: - query_id:
links the chunk to its query. - Content and source. - Page and chunk
ID. - Rank and similarity score.
A query may have multiple retrieved chunks.
3. compressed_chunks
Stores compressed/refined chunks associated with a query. It includes the query relationship, content, source, page, chunk ID, rank, and similarity score where available.
4. evaluations
Stores evaluation results linked to a query: - query_id: unique
relationship to the query record. - Faithfulness. - Answer relevancy. -
Hallucination. - Evaluation timestamp.
The database schema allows at most one evaluation row per query ID; saving an evaluation for the same query ID updates that row.
How the tables relate
queries
├── retrieved_chunks (one query can have many chunks)
├── compressed_chunks (one query can have many chunks)
└── evaluations (zero or one evaluation record per query)
Example lifecycle
rag.ask("What is machine learning?")runs the query. The pipeline's storage integration saves the query and associated chunk information when persistence is enabled by the implementation.- The query receives a database ID.
rag.evaluate()checks for query IDs that do not yet have evaluation records and evaluates pending queries.- Evaluation scores and the evaluation timestamp are saved.
rag.save_html()generates the report using the reporting implementation and available stored data.
For example, if the database contains query IDs 1, 2, and 3, and
only query 1 has an evaluation row, the pending-query lookup
identifies 2 and 3 for evaluation. A subsequent query can be added
and evaluated on a later call.
Database location
The database class defaults to a file named ragmechanic.db relative to
the process's current working directory when no other path is supplied.
For predictable output locations, run your application from the intended working directory or configure an explicit database/output path if the installed pipeline version exposes that option. Avoid storing generated data inside a virtual environment, because virtual environments are disposable and may be recreated.
Output Files
A typical output layout may look like this:
output/
├── database/
│ └── ragmechanic.db
└── report/
└── rag_report.html
This is an example layout, not a guarantee of the default paths. The actual database and report paths depend on the configuration used by the installed package and the current working directory.
- Database: persists query, chunk, and evaluation records.
- HTML report: presents available report data for review.
- Source documents: remain at the location you provide unless your own application moves or copies them.
Back up the SQLite database if you need to preserve evaluation history. Do not commit databases containing private documents or sensitive query content to a public repository.
Core Components
Module Responsibility
ragmechanic.ingestion Loading documents and preparing
content for processing.
ragmechanic.embeddings Creating vector representations.
ragmechanic.vector_store Vector-store implementations and
related logic.
ragmechanic.retriever Retrieving candidate chunks for a
query.
ragmechanic.compression Refining retrieved context when
enabled.
ragmechanic.llm LLM configuration and integration
logic.
ragmechanic.evaluation Evaluating supported RAG outputs.
ragmechanic.storage Persisting query, chunk, and
evaluation data.
ragmechanic.report Result models and HTML report
generation.
ragmechanic.pipeline Orchestrating the RAG workflow.
Vector Stores
The repository includes modules for these vector-store backends:
Backend Integration package
Chroma chromadb, langchain-chroma
FAISS faiss-cpu
Pinecone langchain-pinecone
Qdrant langchain-qdrant
Install and configure the integration required for your selected backend. Credentials, persistence behavior, and supported configuration values differ by provider.
LLM Providers
The project's dependency configuration includes these LangChain integrations:
Provider Integration package
Google Gemini langchain-google-genai
xAI langchain-xai
Mistral langchain-mistralai
Anthropic langchain-anthropic
Provider availability depends on the implementation and installed integration. Configure API keys securely using the method supported by the selected provider and your application.
Metadata
Release files for ragmechanic 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ragmechanic-0.1.2.tar.gz | 32.2 kB | Without provenance | Details for ragmechanic-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ragmechanic-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for ragmechanic-0.1.2-py3-none-any.whl |
Total release size: 62.6 kB
Release files / ragmechanic-0.1.2.tar.gz
| Download URL | ragmechanic-0.1.2.tar.gz |
|---|---|
| Size | 32.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
80c934c29e6b91e9e95194858056e1224585e759ed4a62163691138b5db67c29
|
|
BLAKE2b-256 checksum How to use checksums |
40f7449cc3994e1b87cedf3d8b9ec2986c93351ca5c34a88b99c309f004d57be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.9
|
Release files / ragmechanic-0.1.2-py3-none-any.whl
| Download URL | ragmechanic-0.1.2-py3-none-any.whl |
|---|---|
| Size | 30.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d70dfd47c79a0deac99ef1adfa19605771cc2b26f97e478259ea98463236311a
|
|
BLAKE2b-256 checksum How to use checksums |
45ae4f2f286b5df2812f9337638ddf79a96aedb9467d41f0701d6cb8bdbd1114
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.9
|
Security for ragmechanic 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.