ragmint 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ragmint might be problematic. Click here for more details.

Files changed (46) hide show
  1. ragmint/__init__.py +0 -0
  2. ragmint/__main__.py +28 -0
  3. ragmint/autotuner.py +138 -0
  4. ragmint/core/__init__.py +0 -0
  5. ragmint/core/chunking.py +86 -0
  6. ragmint/core/embeddings.py +55 -0
  7. ragmint/core/evaluation.py +38 -0
  8. ragmint/core/pipeline.py +62 -0
  9. ragmint/core/reranker.py +62 -0
  10. ragmint/core/retriever.py +165 -0
  11. ragmint/experiments/__init__.py +0 -0
  12. ragmint/experiments/validation_qa.json +14 -0
  13. ragmint/explainer.py +63 -0
  14. ragmint/integrations/__init__.py +0 -0
  15. ragmint/integrations/config_adapter.py +96 -0
  16. ragmint/integrations/langchain_prebuilder.py +99 -0
  17. ragmint/leaderboard.py +45 -0
  18. ragmint/optimization/__init__.py +0 -0
  19. ragmint/optimization/search.py +48 -0
  20. ragmint/tests/__init__.py +0 -0
  21. ragmint/tests/conftest.py +16 -0
  22. ragmint/tests/test_autotuner.py +51 -0
  23. ragmint/tests/test_config_adapter.py +39 -0
  24. ragmint/tests/test_embeddings.py +46 -0
  25. ragmint/tests/test_explainer.py +20 -0
  26. ragmint/tests/test_explainer_integration.py +18 -0
  27. ragmint/tests/test_integration_autotuner_ragmint.py +47 -0
  28. ragmint/tests/test_langchain_prebuilder.py +82 -0
  29. ragmint/tests/test_leaderboard.py +39 -0
  30. ragmint/tests/test_pipeline.py +20 -0
  31. ragmint/tests/test_retriever.py +15 -0
  32. ragmint/tests/test_search.py +17 -0
  33. ragmint/tests/test_tuner.py +71 -0
  34. ragmint/tuner.py +189 -0
  35. ragmint/utils/__init__.py +0 -0
  36. ragmint/utils/caching.py +37 -0
  37. ragmint/utils/data_loader.py +65 -0
  38. ragmint/utils/logger.py +36 -0
  39. ragmint/utils/metrics.py +27 -0
  40. ragmint-0.3.1.data/data/LICENSE +19 -0
  41. ragmint-0.3.1.data/data/README.md +397 -0
  42. ragmint-0.3.1.dist-info/METADATA +441 -0
  43. ragmint-0.3.1.dist-info/RECORD +46 -0
  44. ragmint-0.3.1.dist-info/WHEEL +5 -0
  45. ragmint-0.3.1.dist-info/licenses/LICENSE +19 -0
  46. ragmint-0.3.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,397 @@
1
+ # Ragmint
2
+
3
+ ![Python](https://img.shields.io/badge/python-3.9%2B-blue)
4
+ ![License](https://img.shields.io/badge/license-Apache%202.0-green)
5
+ ![Tests](https://github.com/andyolivers/ragmint/actions/workflows/tests.yml/badge.svg)
6
+ ![Optuna](https://img.shields.io/badge/Optuna-Integrated-orange)
7
+ ![Status](https://img.shields.io/badge/Status-Active-success)
8
+
9
+ ![](/assets/images/ragmint-banner.png)
10
+
11
+ **Ragmint** (Retrieval-Augmented Generation Model Inspection & Tuning) is a modular, developer-friendly Python library for **evaluating, optimizing, and tuning RAG (Retrieval-Augmented Generation) pipelines**.
12
+
13
+ It provides a complete toolkit for **retriever selection**, **embedding model tuning**, **automated RAG evaluation**, and **config-driven prebuilding** of pipelines with support for **Optuna-based Bayesian optimization**, **Auto-RAG tuning**, **chunking**, and **explainability** through Gemini or Claude.
14
+
15
+ ---
16
+
17
+ ## ✨ Features
18
+
19
+ - βœ… **Automated hyperparameter optimization** (Grid, Random, Bayesian via Optuna)
20
+ - πŸ€– **Auto-RAG Tuner** β€” dynamically recommends retriever–embedding pairs based on corpus size
21
+ - 🧠 **Explainability Layer** β€” interprets RAG performance via Gemini or Claude APIs
22
+ - πŸ† **Leaderboard Tracking** β€” stores and ranks experiment runs via JSON or external DB
23
+ - πŸ” **Built-in RAG evaluation metrics** β€” faithfulness, recall, BLEU, ROUGE, latency
24
+ - βš™οΈ **Retrievers** β€” FAISS, Chroma, scikit-learn
25
+ - 🧩 **Embeddings** β€” Hugging Face
26
+ - πŸ’Ύ **Caching, experiment tracking, and reproducibility** out of the box
27
+ - 🧰 **Clean modular structure** for easy integration in research and production setups
28
+ - πŸ“¦ **Chunking system** β€” automatic or configurable chunk_size and overlap for documents
29
+ - πŸ—οΈ **Langchain Prebuilder** β€” prepares pipelines, applies chunking, embeddings, and vector store creation automatically
30
+ - βš™οΈ **Config Adapter (LangchainConfigAdapter)** β€” normalizes configuration, fills defaults, validates retrievers
31
+
32
+ ---
33
+
34
+ ## πŸš€ Quick Start
35
+
36
+ ### 1️⃣ Installation
37
+
38
+ ```bash
39
+ git clone https://github.com/andyolivers/ragmint.git
40
+ cd ragmint
41
+ pip install -e .
42
+ ```
43
+
44
+ > The `-e` flag installs Ragmint in editable (development) mode.
45
+ > Requires **Python β‰₯ 3.9**.
46
+
47
+ ---
48
+
49
+ ### 2️⃣ Run a RAG Optimization Experiment
50
+
51
+ ```bash
52
+ python ragmint/main.py --config configs/default.yaml --search bayesian
53
+ ```
54
+
55
+ Example `configs/default.yaml`:
56
+ ```yaml
57
+ retriever: faiss
58
+ embedding_model: text-embedding-3-small
59
+ chunk_size: 500
60
+ overlap: 100
61
+ reranker:
62
+ mode: mmr
63
+ lambda_param: 0.5
64
+ optimization:
65
+ search_method: bayesian
66
+ n_trials: 20
67
+ ```
68
+
69
+ ---
70
+
71
+ ### 3️⃣ Manual Pipeline Usage
72
+
73
+ ```python
74
+ from ragmint.prebuilder import PreBuilder
75
+ from ragmint.tuner import RAGMint
76
+
77
+ # Prebuild pipeline (chunking, embeddings, vector store)
78
+ prebuilder = PreBuilder(
79
+ docs_path="data/docs/",
80
+ config_path="configs/default.yaml"
81
+ )
82
+ pipeline = prebuilder.build_pipeline()
83
+
84
+ # Initialize RAGMint with prebuilt components
85
+ rag = RAGMint(pipeline=pipeline)
86
+
87
+ # Run optimization
88
+ best, results = rag.optimize(validation_set=None, metric="faithfulness", trials=3)
89
+ print("Best configuration:", best)
90
+
91
+ ```
92
+ ---
93
+ # 🧩 Embeddings and Retrievers
94
+
95
+ **Ragmint** supports a flexible set of embeddings and retrievers, allowing you to adapt easily to various **RAG architectures**.
96
+
97
+ ---
98
+ ## 🧩 Chunking System
99
+
100
+ * **Automatically splits documents** into chunks with `chunk_size` and `overlap` parameters.
101
+ * **Supports default values** if not provided in configuration.
102
+ * **Optimized** for downstream **retrieval and embeddings**.
103
+ * **Enables adaptive chunking strategies** in future releases.
104
+
105
+ ---
106
+ ## 🧩 Langchain Config Adapter
107
+
108
+ * **Ensures consistent configuration** across pipeline components.
109
+ * **Normalizes retriever and embedding names** (e.g., `faiss`, `sentence-transformers/...`).
110
+ * **Adds default chunk parameters** when missing.
111
+ * **Validates retriever backends** and **raises clear errors** for unsupported options.
112
+
113
+ ---
114
+ ## 🧩 Langchain Prebuilder
115
+
116
+ **Automates pipeline preparation:**
117
+ 1. Reads documents
118
+ 2. Applies chunking
119
+ 3. Creates embeddings
120
+ 4. Initializes retriever / vector store
121
+ 5. Returns ready-to-use pipeline** for RAGMint or custom usage.
122
+
123
+ ---
124
+
125
+ ## πŸ”€ Available Embeddings (Hugging Face)
126
+
127
+ You can select from the following models:
128
+
129
+ * `sentence-transformers/all-MiniLM-L6-v2` β€” **lightweight**, general-purpose
130
+ * `sentence-transformers/all-mpnet-base-v2` β€” **higher accuracy**, slower
131
+ * `BAAI/bge-base-en-v1.5` β€” **multilingual**, dense embeddings
132
+ * `intfloat/multilingual-e5-base` β€” ideal for **multilingual corpora**
133
+
134
+
135
+
136
+ ### Configuration Example
137
+
138
+ Use the following format in your config file to specify the embedding model:
139
+
140
+ ```yaml
141
+ embedding_model: sentence-transformers/all-MiniLM-L6-v2
142
+ ```
143
+ ---
144
+
145
+ ## πŸ” Available Retrievers
146
+
147
+ **Ragmint** integrates multiple **retrieval backends** to suit different needs:
148
+
149
+ | Retriever | Description |
150
+ | :--- | :--- |
151
+ | **FAISS** | Fast vector similarity search; efficient for dense embeddings |
152
+ | **Chroma** | Persistent vector DB; works well for incremental updates |
153
+ | **scikit-learn (NearestNeighbors)** | Lightweight, zero-dependency local retriever |
154
+
155
+
156
+ ### Configuration Example
157
+
158
+ To specify the retriever in your configuration file, use the following format:
159
+
160
+ ```yaml
161
+ retriever: faiss
162
+ ```
163
+
164
+ ---
165
+
166
+ ## πŸ§ͺ Dataset Options
167
+
168
+ Ragmint can automatically load evaluation datasets for your RAG pipeline:
169
+
170
+ | Mode | Example | Description |
171
+ |------|----------|-------------|
172
+ | 🧱 **Default** | `validation_set=None` | Uses built-in `experiments/validation_qa.json` |
173
+ | πŸ“ **Custom File** | `validation_set="data/my_eval.json"` | Load your own QA dataset (JSON or CSV) |
174
+ | 🌐 **Hugging Face Dataset** | `validation_set="squad"` | Automatically downloads benchmark datasets (requires `pip install datasets`) |
175
+
176
+ ### Example
177
+
178
+ ```python
179
+ from ragmint.tuner import RAGMint
180
+
181
+ ragmint = RAGMint(
182
+ docs_path="data/docs/",
183
+ retrievers=["faiss", "chroma"],
184
+ embeddings=["text-embedding-3-small"],
185
+ rerankers=["mmr"],
186
+ )
187
+
188
+ # Use built-in default
189
+ ragmint.optimize(validation_set=None)
190
+
191
+ # Use Hugging Face benchmark
192
+ ragmint.optimize(validation_set="squad")
193
+
194
+ # Use your own dataset
195
+ ragmint.optimize(validation_set="data/custom_qa.json")
196
+ ```
197
+
198
+ ---
199
+
200
+ ## 🧠 Auto-RAG Tuner
201
+
202
+ The **AutoRAGTuner** automatically recommends retriever–embedding combinations
203
+ based on corpus size and average document length.
204
+
205
+ ```python
206
+ from ragmint.autotuner import AutoRAGTuner
207
+
208
+ corpus_stats = {"size": 5000, "avg_len": 250}
209
+ tuner = AutoRAGTuner(corpus_stats)
210
+ recommendation = tuner.recommend()
211
+ print(recommendation)
212
+ # Example output: {"retriever": "Chroma", "embedding_model": "SentenceTransformers"}
213
+ ```
214
+
215
+ ---
216
+
217
+ ## πŸ† Leaderboard Tracking
218
+
219
+ Track and visualize your best experiments across runs.
220
+
221
+ ```python
222
+ from ragmint.leaderboard import Leaderboard
223
+
224
+ lb = Leaderboard("experiments/leaderboard.json")
225
+ lb.add_entry({"trial": 1, "faithfulness": 0.87, "latency": 0.12})
226
+ lb.show_top(3)
227
+ ```
228
+
229
+ ---
230
+
231
+ ## 🧠 Explainability with Gemini / Claude
232
+
233
+ Compare two RAG configurations and receive **natural language insights** on why one performs better.
234
+
235
+ ```python
236
+ from ragmint.explainer import explain_results
237
+
238
+ config_a = {"retriever": "FAISS", "embedding_model": "OpenAI"}
239
+ config_b = {"retriever": "Chroma", "embedding_model": "SentenceTransformers"}
240
+
241
+ explanation = explain_results(config_a, config_b, model="gemini")
242
+ print(explanation)
243
+ ```
244
+
245
+ > Set your API keys in a `.env` file or via environment variables:
246
+ > ```
247
+ > export GEMINI_API_KEY="your_gemini_key"
248
+ > export ANTHROPIC_API_KEY="your_claude_key"
249
+ > ```
250
+
251
+ ---
252
+
253
+ ## 🧩 Folder Structure
254
+
255
+ ```
256
+ ragmint/
257
+ β”œβ”€β”€ core/
258
+ β”‚ β”œβ”€β”€ pipeline.py
259
+ β”‚ β”œβ”€β”€ retriever.py
260
+ β”‚ β”œβ”€β”€ reranker.py
261
+ β”‚ β”œβ”€β”€ embeddings.py
262
+ β”‚ β”œβ”€β”€ chunking.py
263
+ β”‚ └── evaluation.py
264
+ β”œβ”€β”€ integration/
265
+ β”‚ β”œβ”€β”€ config_adapter.py
266
+ β”‚ └── langchain_prebuilder.py
267
+ β”œβ”€β”€ autotuner.py
268
+ β”œβ”€β”€ explainer.py
269
+ β”œβ”€β”€ leaderboard.py
270
+ β”œβ”€β”€ tuner.py
271
+ β”œβ”€β”€ utils/
272
+ β”œβ”€β”€ configs/
273
+ β”œβ”€β”€ experiments/
274
+ β”œβ”€β”€ tests/
275
+ └── main.py
276
+ ```
277
+
278
+ ---
279
+
280
+ ## πŸ§ͺ Running Tests
281
+
282
+ ```bash
283
+ pytest -v
284
+ ```
285
+
286
+ To include integration tests with Gemini or Claude APIs:
287
+ ```bash
288
+ pytest -m integration
289
+ ```
290
+
291
+ ---
292
+
293
+ ## βš™οΈ Configuration via `pyproject.toml`
294
+
295
+ Your `pyproject.toml` includes all required dependencies:
296
+
297
+ ```toml
298
+ [project]
299
+ name = "ragmint"
300
+ version = "0.1.0"
301
+ dependencies = [
302
+ # Core ML + Embeddings
303
+ "numpy<2.0.0",
304
+ "pandas>=2.0",
305
+ "scikit-learn>=1.3",
306
+ "sentence-transformers>=2.2.2",
307
+
308
+ # Retrieval backends
309
+ "chromadb>=0.4",
310
+ "faiss-cpu; sys_platform != 'darwin'", # For Linux/Windows
311
+ "faiss-cpu==1.7.4; sys_platform == 'darwin'", # Optional fix for macOS MPS
312
+ "rank-bm25>=0.2.2", # For BM25 retriever
313
+
314
+ # Optimization & evaluation
315
+ "optuna>=3.0",
316
+ "tqdm",
317
+ "colorama",
318
+
319
+ # RAG evaluation and data utils
320
+ "pyyaml",
321
+ "python-dotenv",
322
+
323
+ # Explainability and LLM APIs
324
+ "openai>=1.0.0",
325
+ "google-generativeai>=0.8.0",
326
+ "anthropic>=0.25.0",
327
+
328
+ # Integration / storage
329
+ "supabase>=2.4.0",
330
+
331
+ # Testing
332
+ "pytest",
333
+
334
+ # LangChain integration layer
335
+ "langchain>=0.2.5",
336
+ "langchain-community>=0.2.5",
337
+ "langchain-text-splitters>=0.2.1"
338
+ ]
339
+ ```
340
+
341
+ ---
342
+
343
+ ## πŸ“Š Example Experiment Workflow
344
+
345
+ 1. Define your retriever, embedding, and reranker setup
346
+ 2. Launch optimization (Grid, Random, Bayesian) or AutoTune
347
+ 3. Compare performance with explainability
348
+ 4. Persist results to leaderboard for later inspection
349
+
350
+ ---
351
+
352
+ ## 🧬 Architecture Overview
353
+
354
+ ```mermaid
355
+ flowchart TD
356
+ A[Query] --> B[Embedder]
357
+ B --> C[Retriever]
358
+ C --> D[Reranker]
359
+ D --> E[Generator]
360
+ E --> F[Evaluation]
361
+ F --> G[Optuna / AutoRAGTuner]
362
+ G -->|Best Params| B
363
+ ```
364
+
365
+ ---
366
+
367
+ ## πŸ“˜ Example Output
368
+
369
+ ```
370
+ [INFO] Starting Bayesian optimization with Optuna
371
+ [INFO] Trial 7 finished: faithfulness=0.83, latency=0.42s
372
+ [INFO] Best parameters: {'lambda_param': 0.6, 'retriever': 'faiss'}
373
+ [INFO] AutoRAGTuner: Suggested retriever=Chroma for medium corpus
374
+ ```
375
+
376
+ ---
377
+
378
+ ## 🧠 Why Ragmint?
379
+
380
+ - Built for **RAG researchers**, **AI engineers**, and **LLM ops**
381
+ - Works with **LangChain**, **LlamaIndex**, or standalone setups
382
+ - Designed for **extensibility** β€” plug in your own retrievers, models, or metrics
383
+ - Integrated **explainability and leaderboard** modules for research and production
384
+
385
+ ---
386
+
387
+ ## βš–οΈ License
388
+
389
+ Licensed under the **Apache License 2.0** β€” free for personal, research, and commercial use.
390
+
391
+ ---
392
+
393
+ ## πŸ‘€ Author
394
+
395
+ **AndrΓ© Oliveira**
396
+ [andyolivers.com](https://andyolivers.com)
397
+ Data Scientist | AI Engineer