Embedding diagnostics toolkit for RAG systems
Project description
Raglens: Visual Diagnostics for Embedding Models in RAG Pipelines
Raglens is a visual, CLI, and UI-based diagnostic toolkit designed to inspect and compare embedding models in Retrieval-Augmented Generation (RAG) systems. It allows you to probe token-level and chunk-level representations, assess pooling strategies, analyze similarity matrices, and understand how embeddings change across model layers. Optionally, you can generate AI-powered explanations for each plot in your preferred language using LLMs.
Installation
You can install Raglens from PyPI or from source, using either pip or the uv package manager:
From PyPI (recommended for most users)
Using pip
pip install raglens
Using uv
uv pip install raglens
or
uv add raglens
From source (for development)
First, clone the repository:
git clone https://github.com/gegedenice/raglens.git
cd raglens
Using pip
pip install -e .
Using uv
uv pip install -e .
If you need to install dependencies from requirements.txt:
uv pip install -r requirements.txt
You can use uv as a drop-in replacement for pip in all commands throughout this README.
Features Overview
Token-Level Diagnostics
plot_token_geometry: Projects token embeddings into 2D using PCA or UMAP. Reveals how tokens are distributed in embedding space, helping you understand semantic clustering and outliers.compare_pooling_methods: Visualizes and compares CLS, mean, and max pooling strategies for sentence embeddings. Useful for selecting the most representative pooling method for downstream tasks.layerwise_token_drift: Shows how selected token representations evolve across transformer layers. Helps diagnose how information propagates and transforms through the model.embedding_distribution_stats: Plots distributional statistics (e.g., L2 norms, explained variance) for token embeddings. Useful for detecting anomalies or understanding embedding magnitude and variance.
Chunk-Level Diagnostics
chunking_sanity: Inspects how tokenization splits long texts into chunks. Highlights chunk boundaries and overlap, ensuring proper chunking for retrieval.plot_chunk_geometry: Scatter plots chunk embeddings (optionally with a query) in 2D space, colored by pooling strategy. Reveals chunk clustering and query proximity.semantic_similarity_matrix: Displays a heatmap of inter-sentence or inter-chunk similarities. Useful for visualizing semantic relationships and redundancy.
Retrieval Diagnostics
compare_retrieval_pooling: Visualizes the impact of different pooling strategies on top-k chunk retrieval. Helps you select the best pooling method for retrieval accuracy.
LLM-based Interpretation (Optional)
--generate-explanation: Adds AI-generated interpretation of the plots using OpenAI or Hugging Face APIs.--language: Selects the language for LLM explanations (e.g., English, French, Spanish).- Requires setting an environment variable or
--api-keyflag with your access key.
EmbeddingModel Class
Raglens uses a flexible embedding interface:
from raglens.embeddings import EmbeddingModel
model = EmbeddingModel(model_name="sentence-transformers/all-MiniLM-L6-v2")
Optional: Custom Model Storage
model = EmbeddingModel(model_name="...", model_dir="./my_models")
Automatic Strategy Support Detection
print("Supported pooling strategies:", model.get_supported_strategies())
Model Compatibility
Supports any HuggingFace-compatible encoder-based transformer model. Pooling strategies adapt based on architecture (e.g., CLS only enabled if present).
Function Reference
Token-Level
- plot_token_geometry: Visualizes token embeddings in 2D (PCA/UMAP). Shows semantic clusters and outliers.
- compare_pooling_methods: Compares sentence-level embeddings from different pooling strategies.
- layerwise_token_drift: Tracks how specific tokens change across model layers.
- embedding_distribution_stats: Plots L2 norms and explained variance for embeddings.
Chunk-Level
- chunking_sanity: Shows how text is split into chunks, highlighting overlaps and boundaries.
- plot_chunk_geometry: Plots chunk embeddings and optionally a query, showing their spatial relationships.
- semantic_similarity_matrix: Heatmap of similarities between chunks.
Retrieval
- compare_retrieval_pooling: Compares retrieval results for different pooling strategies, visualizing top-k matches.
LLM Explanation
All plotting functions support generate_explanation=True and language="..." for AI-powered, language-specific plot interpretation.
How to Use LLM Explanations
- Set
--generate-explanationin CLI orgenerate_explanation=Truein Python. - Set
--languagein CLI orlanguage="..."in Python. - Provide your API key via
--api-keyor environment variable. - The explanation will be generated in your chosen language and printed after each plot.
CLI Usage
python cli/main.py --mode <mode> [options]
Modes and Arguments
| Mode | Required Input | Description |
|---|---|---|
token-geometry |
--text |
Token PCA/UMAP visualization |
pooling-compare |
--text |
Compare pooling embeddings |
embedding-stats |
--text |
Show distribution stats |
token-drift |
--text + prompt |
Token evolution through layers |
semantic-similarity |
--chunks (list) |
Similarity matrix |
chunk-geometry |
--chunks (list) |
Chunk scatter plot w/ query |
chunk-sanity |
--chunks (list) |
Inspect token-based chunking |
retrieval-compare |
--chunks + --query |
Retrieval across pooling strategies |
Common Options
--generate-explanation: If set, returns an LLM-generated explanation for each plot (default LLM is OpenAI o4-mini model).--language: Specify the language for explanations (default: English).--api-key: Your OpenAI API key (can also be set viaOPENAI_API_KEYenvironment variable).
Example:
python cli/main.py --mode token-geometry --text "Learning is a continuous journey." --generate-explanation --language French --api-key sk-xxx
Notebook & Script Examples
Demo Notebook
Explore all features interactively in notebooks/01_demo.ipynb. The notebook guides you through:
- Visualizing token embeddings (PCA/UMAP)
- Comparing pooling strategies
- Exploring layerwise drift
- Plotting chunk-level geometry
- Inspecting retrieval results
- Generating LLM explanations in your chosen language
Example Script
See scripts/plot_example.py for a step-by-step CLI demo. The script:
- Prompts for your preferred explanation language and API key
- Sequentially runs each diagnostic, asking for confirmation before each step
- Displays each plot and its LLM-generated explanation (if enabled)
- Keeps user control and clarity throughout the process
Streamlit UI
A user-friendly interactive app is available in ui/streamlit_app.py:
- All features accessible via tabs: Token geometry, pooling comparison, layerwise drift, embedding stats, chunking sanity, chunk geometry, semantic similarity, and retrieval diagnostics.
- Model selection and API key input: Choose your embedding model, storage directory, and provide your OpenAI API key.
- Language selection for explanations: Choose the language for LLM-generated plot explanations.
- Clear descriptions: Each tab includes a short paragraph (in English and French) explaining the purpose of the visualization.
- No code required: Run with
streamlit run ui/streamlit_app.pyand explore all diagnostics visually. - Smart output capture: Functions that print to stdout (like
chunking_sanityandcompare_retrieval_pooling) are captured and displayed in the UI.
Example Usage
streamlit run ui/streamlit_app.py
Details
- Tabs for each diagnostic: Each tab runs a specific visualization or analysis, with a clear description and input fields.
- Model and API settings: Select model, directory, API key, and explanation language in the sidebar.
- Smart output capture: Functions that print to stdout are captured and shown in the UI.
- Internationalization-ready: Tab descriptions support both English and French (via the
helpargument). - Easy to extend: Add new diagnostics or explanations by adding new tabs.
Screenshots
License
MIT License
Further Reading
- See
notebooks/01_demo.ipynbfor a full workflow. - Try
scripts/plot_example.pyfor a guided CLI demo. - Explore the codebase for extensibility and advanced diagnostics.
- Use
ui/streamlit_app.pyfor a full-featured, interactive visual experience.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file raglens-0.1.4.tar.gz.
File metadata
- Download URL: raglens-0.1.4.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98c443621104187a329d51f2d7bf883e8755f94ff66e92686bd5b4962355fc13
|
|
| MD5 |
5f3f50735743ce5b5a8cd06a5d035ae6
|
|
| BLAKE2b-256 |
a23187021700a73bae3ac90d247b66e1720e48c870d2f7872a3e830b7832e339
|
File details
Details for the file raglens-0.1.4-py3-none-any.whl.
File metadata
- Download URL: raglens-0.1.4-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
424db4d20b457e9f22b99ce141b038cbccb24fab55b8e5166d08c14c04badf8d
|
|
| MD5 |
61e703595c8cfc24f4c1f1e5257e5384
|
|
| BLAKE2b-256 |
2a541ef698a7b81ba7eb608f0559f2c323ff08eb133e18cc42dd8900d6b7efc7
|