Skip to main content

Detect when your RAG vector index silently drifts from reality — pure Python, no external services.

Project description

embedding-drift-lib

Detect when your RAG vector index silently drifts from reality.

Pure Python library — no external services, no API keys, no database required. 3 dependencies: numpy, scikit-learn, scipy.

Install

pip install embedding-drift-lib

Quick Start

from embedding_drift_lib import DriftAnalyser, DriftConfig
import numpy as np

config   = DriftConfig(index_name="my-rag")
analyser = DriftAnalyser(config)

# Pass your index vectors and recent query vectors
report = analyser.analyse(reference_vectors, current_vectors)

print(report.drift_type)       # "none" | "mild" | "significant" | "severe"
print(report.mmd_score)        # 0.0 = identical, > 0.20 = severe drift
print(report.primary_cause)    # "model_drift" | "query_shift" | "data_staleness"
print(report.recommendation)   # human-readable action to take

Wrap Your Embedding Function (Zero-Touch Monitoring)

from embedding_drift_lib import DriftMonitor, DriftConfig
from openai import AsyncOpenAI

client  = AsyncOpenAI()
monitor = DriftMonitor(DriftConfig(index_name="my-rag"))

# Create baseline from your existing index (run once)
await monitor.create_snapshot(index_vectors, label="v1-baseline")

# Wrap your embed call — your RAG code does not change
embed = monitor.wrap(client.embeddings.create)

# Use normally — drift analysis runs in the background automatically
response = await embed(model="text-embedding-3-small", input=["user query"])

# Check anytime
report = monitor.last_report

What It Detects

Drift Type Signal Example
Model drift High MMD + concentrated Wasserstein OpenAI silently updates model weights
Query shift High coverage gap + low MMD Users ask about a new topic your corpus never covered
Data staleness High MMD + diffuse Wasserstein Corpus ages relative to current user queries

Configuration

config = DriftConfig(
    index_name                   = "my-rag",
    pca_components               = 50,      # reduce embedding dims before MMD
    n_permutations               = 200,     # permutation test iterations
    sample_rate                  = 0.05,    # sample 5% of queries (default)
    elevated_sample_rate         = 0.30,    # 30% when drift suspected
    analysis_batch_size          = 200,     # analyse every N queries
    mmd_mild_threshold           = 0.02,
    mmd_significant_threshold    = 0.08,
    mmd_severe_threshold         = 0.20,
    coverage_gap_alert_threshold = 0.15,    # alert when 15%+ queries uncovered
    snapshot_dir                 = "./snapshots",
)

How It Works

Your RAG App
    |
    | query -> embed() -> vector search -> LLM -> answer
    v
DriftMonitor wrapper (transparent)
    |
    +-- Samples 5% of embedding calls (adaptive: 30% when drift suspected)
    |
    +-- Every 200 samples, runs DriftAnalyser:
    |       1. PCA projection      1536-dim -> 50-dim
    |       2. MMD^2               primary drift metric
    |       3. Wasserstein         which semantic directions shifted
    |       4. Coverage gap        query shift detection
    |       5. Permutation test    200 shuffles, p < 0.05
    |
    +-- Classifies: none / mild / significant / severe
    +-- Diagnoses:  model_drift / query_shift / data_staleness
    +-- Returns DriftReport

The Math

MMD (Maximum Mean Discrepancy) — non-parametric test measuring distance between two distributions without assuming any shape. Uses RBF kernel with median heuristic bandwidth.

Wasserstein distance — computed per PCA dimension. Concentration ratio (max/mean) distinguishes sudden model swap (high concentration) from gradual data staleness (diffuse).

Coverage gap — fraction of queries whose best cosine similarity to any index document falls below threshold. Catches query shift that MMD misses.

Permutation test — 200 label shuffles build a null distribution. Observed MMD must exceed the 95th percentile of the null to be reported as statistically significant.

Author

Sarthak PandeGitHub

License

MIT #\x00 \x00e\x00m\x00b\x00e\x00d\x00d\x00i\x00n\x00g\x00-\x00d\x00r\x00i\x00f\x00t\x00-\x00l\x00i\x00b\x00 \x00 \x00

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

embedding_drift_lib-0.1.1.tar.gz (16.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

embedding_drift_lib-0.1.1-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file embedding_drift_lib-0.1.1.tar.gz.

File metadata

  • Download URL: embedding_drift_lib-0.1.1.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for embedding_drift_lib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a7f99b383f37be39dd00dfbeb123dfdb3a3b9af1fc6448932323dc01b91f9b14
MD5 41347abcab2d87cdaf585f619213ffc5
BLAKE2b-256 ffc0346d10732d7124476fbd7b93b11459f18fc56b9f2f4140e1fda3f79fa09a

See more details on using hashes here.

Provenance

The following attestation bundles were made for embedding_drift_lib-0.1.1.tar.gz:

Publisher: publish.yml on sarthakpande108/embedding-drift-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file embedding_drift_lib-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for embedding_drift_lib-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8efd6b13d1f381d14ed8ee7ee834bd8fade5df4ed1092068cd9ba8af7ed00780
MD5 83550ad0a219650d9058589953c20394
BLAKE2b-256 c0177d693e451afbaeb961434f96b77661738d8f4609dcbf7219eab3b90740c1

See more details on using hashes here.

Provenance

The following attestation bundles were made for embedding_drift_lib-0.1.1-py3-none-any.whl:

Publisher: publish.yml on sarthakpande108/embedding-drift-lib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page