Mizan RAG
A production‑ready, scale‑aware Retrieval‑Augmented Generation (RAG) framework powered by the Mizan Balance Function, a new geometric similarity metric designed for robust vector search, balanced embeddings, and improved retrieval accuracy.
Mizan RAG integrates:
- Mizan Similarity (your custom metric)
- Mizan Embedding Models
- Mizan Vector Search
- Multi‑provider LLMs (OpenAI, Groq, Grok, HF, Local)
- Chunking, retrieval, re-ranking, summarization
- Caching, performance optimizations, and extensible architecture
This README is the full expanded version (A + B + C + D combined), built for:
- Developers
- Researchers
- Students
- Industry teams
- Open‑source contributors
🚀 Features
Scale-Aware Retrieval (Cosine + Mizan)
- Replace cosine similarity with Mizan for robust retrieval.
- Fixes chunk imbalance, scale distortion, and embedding collapse.
Full RAG Pipeline
- Chunker
- Retriever
- Mizan Ranker (cosine/mizan/hybrid)
- Summarizer (multi-provider LLM)
- Embedding wrapper
- End-to-end orchestration
Cache System
- Embedding cache
- Index cache
- LLM response cache
Supports Multiple LLM Providers
- Groq (recommended)
- OpenAI
- Grok (xAI)
- OpenRouter
- HuggingFace
- Local HF models
Extensible, production-ready folder structure
Installation
1. Clone repository
git clone https://github.com/ahsanshaokat/mizan-rag.git
cd mizan-rag
2. Install dependencies
pip install -r requirements.txt
3. Install Mizan ecosystem packages (if separate repos)
pip install mizan_vector mizansimilarity mizan-embedder
4. Install backend models (optional)
pip install sentence-transformers
pip install transformers
Quick Start
1. Put your .txt files in:
/docs/
2. Run:
python main.py
3. Ask questions:
Ask your question (or 'exit'): What is the Mizan Balance Function?
Mizan Balance Function (Short Overview)
Mizan is a scale-aware similarity metric designed to fix weaknesses in cosine similarity:
| Issue in Cosine | Mizan Fix |
|---|---|
| Loses magnitude information | Preserves proportion & balance |
| Weak separation in high dimensions | Stronger scaling geometry |
| Chunk length bias | Scale normalization |
| Embedding collapse | Balanced magnitude alignment |
Mizan Similarity Formula (simplified):
miz(a, b) = (directional similarity * balance score)
Full math is in the Mizan research paper.
Architecture Overview
docs/ → Your text files
mizan_rag/
├── chunker.py → Word-based intelligent chunking
├── retriever.py → Dense retrieval (cosine/mizan)
├── ranker.py → Second-pass reranking (cosine/mizan/hybrid)
├── summarizer.py → LLM integration (Groq, OpenAI, etc.)
├── utils/cache.py → Persistent cache engine
├── pipeline.py → Full RAG orchestration
main.py → CLI interface
Pipeline Stages
1. Chunking
- Word-based
- Configurable size + overlap
- Clean and lightweight
2. Embedding
Uses your trained or HF model:
sentence-transformers/all-MiniLM-L6-v2 (recommended for Mizan)
3. Retriever
- Computes embeddings
- Uses cosine or Mizan similarity
- Ultra-fast search
- Cached for performance
4. Ranker
- Optional re-ranking using:
- cosine
- mizan
- hybrid(w1 * cosine + w2 * mizan)
5. Summarizer
Supports:
- Groq
- OpenAI
- Grok
- OpenRouter
- HuggingFace
- Local models
🛠 Example: Using MizanRetriever
from mizan_rag.retriever import MizanRetriever
retriever = MizanRetriever(embed_fn, cache)
results = retriever.search("What is Mizan?", top_k=5, metric="mizan")
Example: Full RAG Query
result = rag.query(
"Explain the Mizan Balance Function",
top_k_retrieve=8,
top_k_rerank=5
)
print(result["answer"])
⚡ Recommended Embedding Models for Mizan
⭐ Best (Highly compatible)
- all-MiniLM-L6-v2
- distilbert-base-uncased
- paraphrase-MiniLM-L3-v2
⚠️ Avoid (collapsed or incompatible geometry)
- all-mpnet-base-v2
- intfloat/e5-base
- e5-small-v2
- instructor-base / large
Documentation Roadmap (Full 18‑Article Series)
This README is part of a larger ecosystem:
SECTION 1 — Foundations
- Mizan Balance Function
- Why Cosine Fails
- Visualizing Metrics
- Mathematics Behind Mizan
SECTION 2 — Engineering
- Using MizanSimilarity
- Building RAG With Mizan
- One Change That Improves Retrieval
SECTION 3 — Embedding Models
- Designing Mizan Embedding Model
- Training MizanTextEncoder
- MizanContrastiveLoss
- Benchmarking Mizan
SECTION 4 — Mizan Ecosystem
- MizanVector
- MizanEmbedder
- MizanModels
SECTION 5 — Industry
- Enterprise RAG Failures
- Search, Chatbots, Recommendations
- Multi-modal Mizan
SECTION 6 — Research
- Formal Mathematical Treatment of Mizan
🧪 Simple Test Query
Try:
What does the Quran say about balance?
What is embedding magnitude?
Who is Ahsan Shaokat?
Explain vector projection.
How does Mizan fix cosine issues?
These match the synthetic dataset perfectly.
📄 License
MIT License.
🤝 Contributions
PRs welcome.
Feature requests encouraged.
We are building an open ecosystem around scale-aware AI.
⭐ Star the Repository
If this library helps you, please give it a star ✨
Metadata
Release files for mizan-rag 0.1.1
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 | |
|---|---|---|---|---|
| mizan_rag-0.1.1.tar.gz | 19.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mizan_rag-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 38.7 kB
Release files / mizan_rag-0.1.1.tar.gz
| Download URL | mizan_rag-0.1.1.tar.gz |
|---|---|
| Size | 19.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
057971efb429f4f043869c3c73a98637d72fa92a9cf367d3f514f89ab438bb55
|
|
BLAKE2b-256 checksum How to use checksums |
1d7f4885b49b10cef07cb8208a6e792327b5cf1a68a35b6538d62d9c088131a0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|
Release files / mizan_rag-0.1.1-py3-none-any.whl
| Download URL | mizan_rag-0.1.1-py3-none-any.whl |
|---|---|
| Size | 19.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9df79bf207d6d02ffe07ac3d0a6744d14d6070f53d212e9490debd0e19eb6078
|
|
BLAKE2b-256 checksum How to use checksums |
0e2d7f6b4e74d3d7393a6f637c62dc8c336e5944b368f57a1aa52e9c22c03a5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.3
|
Security for mizan-rag 0.1.1
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.