Skip to main content

Mathematical and adversarial verification library for calculating drift/hallucination scores between outputs

Project description

CMVK - Cross-Model Verification Kernel

PyPI version Python 3.11+ License: MIT

Layer 1: The Primitive — A mathematical and adversarial verification library for calculating drift/hallucination scores between outputs.

Installation

pip install cmvk

For enhanced statistical functions (recommended):

pip install cmvk[scipy]

Quick Start

from cmvk import verify

# Compare two outputs
score = verify(
    output_a="def add(a, b): return a + b",
    output_b="def add(x, y): return x + y"
)

print(f"Drift Score: {score.drift_score:.3f}")  # 0.0 = identical, 1.0 = completely different
print(f"Confidence: {score.confidence:.3f}")
print(f"Drift Type: {score.drift_type.value}")

Core Philosophy

CMVK is a pure verification tool. It calculates the mathematical drift between two outputs without any side effects:

  • verify(output_a, output_b) -> VerificationScore
  • ✅ Pure functions with no side effects
  • ✅ Minimal dependencies (numpy only, scipy optional)
  • ❌ No self-correction loops
  • ❌ No agent control plane logic
  • ❌ No LLM API calls

CMVK is the tool used to verify; it is not the loop that triggers the correction.

API Reference

verify(output_a: str, output_b: str) -> VerificationScore

Calculate drift/hallucination score between two text outputs.

from cmvk import verify

score = verify(
    "The capital of France is Paris.",
    "Paris is the capital city of France."
)

# Returns VerificationScore with:
# - drift_score: float (0.0 to 1.0)
# - confidence: float (0.0 to 1.0)
# - drift_type: DriftType (SEMANTIC, STRUCTURAL, NUMERICAL, LEXICAL)
# - details: dict with component scores

verify_embeddings(embedding_a, embedding_b) -> VerificationScore

Compare pre-computed embedding vectors using cosine distance and euclidean metrics.

from cmvk import verify_embeddings
import numpy as np

emb_a = np.array([0.1, 0.2, 0.3, 0.4])
emb_b = np.array([0.15, 0.25, 0.28, 0.42])

score = verify_embeddings(emb_a, emb_b)
print(f"Semantic drift: {score.drift_score:.3f}")

verify_distributions(dist_a, dist_b) -> VerificationScore

Compare probability distributions using KL divergence and Jensen-Shannon divergence.

from cmvk import verify_distributions
import numpy as np

dist_a = np.array([0.2, 0.3, 0.5])
dist_b = np.array([0.25, 0.25, 0.5])

score = verify_distributions(dist_a, dist_b)
print(f"Distribution drift: {score.drift_score:.3f}")
print(f"KL divergence: {score.details['kl_divergence']:.4f}")

verify_sequences(seq_a, seq_b) -> VerificationScore

Compare sequences using edit distance and longest common subsequence.

from cmvk import verify_sequences

tokens_a = ["def", "add", "(", "a", ",", "b", ")"]
tokens_b = ["def", "add", "(", "x", ",", "y", ")"]

score = verify_sequences(tokens_a, tokens_b)
print(f"Sequence drift: {score.drift_score:.3f}")
print(f"Edit distance: {score.details['edit_distance']}")

Batch Operations

from cmvk import verify_batch, aggregate_scores

outputs_a = ["output 1", "output 2", "output 3"]
outputs_b = ["output 1 modified", "output 2 changed", "output 3 different"]

scores = verify_batch(outputs_a, outputs_b)
summary = aggregate_scores(scores)

print(f"Mean drift: {summary['mean_drift']:.3f}")
print(f"Std drift: {summary['std_drift']:.3f}")
print(f"Drift distribution: {summary['drift_type_distribution']}")

Drift Types

Type Description
SEMANTIC Meaning/embedding-based differences
STRUCTURAL Code structure, indentation, line count
NUMERICAL Differences in extracted numbers
LEXICAL Word and character-level differences

VerificationScore

The VerificationScore is an immutable dataclass:

@dataclass(frozen=True)
class VerificationScore:
    drift_score: float    # 0.0 (identical) to 1.0 (completely different)
    confidence: float     # 0.0 to 1.0
    drift_type: DriftType # Primary type of drift detected
    details: dict         # Component scores and metadata

Dependencies

  • Required: numpy>=1.24.0
  • Optional: scipy>=1.11.0 (enhanced statistical functions)

Use Cases

  1. LLM Output Verification: Compare outputs from different models to detect hallucinations
  2. Hallucination Detection: Measure drift from ground truth or reference outputs
  3. Regression Testing: Track output changes across model versions
  4. Adversarial Evaluation: Quantify semantic preservation after perturbations
  5. Model Comparison: Systematically compare outputs from different LLM providers

Example: Detecting Hallucinations

from cmvk import verify, DriftType

ground_truth = "The speed of light is approximately 299,792,458 meters per second."
model_output = "The speed of light is approximately 300 million meters per second."

score = verify(ground_truth, model_output)

if score.drift_score > 0.3:
    print(f"⚠️ Potential hallucination detected!")
    print(f"Drift score: {score.drift_score:.3f}")
    print(f"Primary drift type: {score.drift_type.value}")
else:
    print(f"✅ Output appears consistent (drift: {score.drift_score:.3f})")

License

MIT License - see LICENSE for details.

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

cmvk-0.1.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

cmvk-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file cmvk-0.1.0.tar.gz.

File metadata

  • Download URL: cmvk-0.1.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for cmvk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1879e374eadf8df5bfea29e99c9dfb3b0099ba97b553b7506926a7a98d0c4721
MD5 e864bd269fbe633e9990c48ef1f504bd
BLAKE2b-256 fed0bffac6deeca2033bf35f80d3249f26f9635b42a9ae24e9a1ad73e5a5e076

See more details on using hashes here.

File details

Details for the file cmvk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cmvk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for cmvk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f0e2db1f6c81930ef84ad2e7e3298c085ae4f5da48d41fc637627c2352399fa
MD5 4711a7c73673782b06c5463f6e936698
BLAKE2b-256 ef82fea72254fa7b7941cf835266f372e534b2efa90961e2e4d35d5f416226ef

See more details on using hashes here.

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