odin-prompt-toolkit (Python)
Multi-language SDK for LSH (Locality-Sensitive Hashing) signature generation for AI prompt similarity detection.
This is the Python implementation of the odin-prompt-toolkit algorithm, also available in Rust and TypeScript.
Installation
0din-prompt-toolkit ships as two packages so you only ship compiled code when you want it:
| Package | What it is | When you get it |
|---|---|---|
0din-prompt-toolkit |
Pure-Python core. One universal (py3-none-any) wheel — installs on any OS/arch/Python, no compiler. |
Always (base install) |
0din-prompt-toolkit-native |
Optional Rust accelerator (PyO3). Prebuilt wheels for Linux/macOS/Windows × CPython 3.10–3.13. | Only with the [native] extra |
# Pure Python — works everywhere, slower signature generation
pip install 0din-prompt-toolkit
# With native Rust acceleration — recommended for production
pip install "0din-prompt-toolkit[native]"
# Optional features
pip install "0din-prompt-toolkit[onnx]" # local ONNX embeddings + SusFactor
pip install "0din-prompt-toolkit[openai]" # OpenAI embeddings
pip install "0din-prompt-toolkit[cm-lsh]" # Confidence Matrix LSH
pip install "0din-prompt-toolkit[threatfeed]" # 0DIN threat feed
pip install "0din-prompt-toolkit[all]" # everything, including native
From git (development):
pip install "0din-prompt-toolkit[native] @ git+https://github.com/0din-ai/prompt-toolkit#subdirectory=packages/python"
Native vs pure Python
Both paths produce bit-identical signatures (verified across all implementations via canonical test vectors). They differ only in how they install and how fast signature generation runs.
- Pure Python (
0din-prompt-toolkit) — a single universal wheel with no compiled code. Installs anywhere with zero build tools. Signature generation runs in a Python loop. - Native (
[native]→0din-prompt-toolkit-native) — a prebuilt compiled Rust extension.pipdownloads a wheel matching your platform, so no Rust toolchain or compiler is required on Linux/macOS/Windows with CPython 3.10–3.13. On any platform without a matching wheel, the base package still installs and automatically falls back to pure Python — nothing breaks, it's just slower.
The accelerator is transparent — the same API uses native automatically when it's present:
from odin_prompt_toolkit import NATIVE_AVAILABLE
print("native" if NATIVE_AVAILABLE else "pure Python")
Force pure Python even when native is installed: export ODIN_PROMPT_TOOLKIT_NO_NATIVE=1.
Speed tradeoff
Native replaces the hot signature-generation loop with compiled, SIMD-optimized Rust:
| Native (Rust) | Pure Python | |
|---|---|---|
| Throughput | ~5,300 sigs/sec | ~85 sigs/sec (384-dim) … ~9 sigs/sec (1024-dim) |
| Per signature | ~0.2 ms | ~12 ms … ~115 ms |
| vs native | — | ~60×–600× slower |
The multiplier depends on embedding dimension: pure Python loops over every dimension, so the gap widens with larger vectors (~63× at 384-dim, ~590× at 1024-dim). Native throughput is roughly constant.
What it means end-to-end: in a real pipeline, embedding generation usually dominates. On the 3,714-prompt benchmark (local ONNX, CPU) embedding took 112.6 s; adding signature generation cost 0.7 s with native (+0.6%) vs 43.8 s pure Python (+38%). When embeddings are pre-computed or cached (e.g. real-time dedup), signature generation is the cost — and the full speedup applies.
Rule of thumb: use [native] in production; pure Python is a correct, always-available fallback for prototyping or unusual platforms.
Quick Start
Basic LSH Signatures
from odin_prompt_toolkit import simhash_lsh_multi, normalize_vector
# Your embedding vector (must be L2-normalized)
vector = [0.5, 0.5, 0.5, 0.5]
normalized = normalize_vector(vector)
# Generate LSH signatures (3 families, 256 bits, 16 bands)
families = simhash_lsh_multi(normalized)
print(f"Signature: {families[0].signature}")
print(f"Bands: {families[0].bands}")
Similarity Comparison
from odin_prompt_toolkit import simhash_lsh_multi, hamming_distance_hex, cosine_from_hamming
# Generate signatures for two vectors
families1 = simhash_lsh_multi(vector1)
families2 = simhash_lsh_multi(vector2)
# Compute Hamming distance
distance = hamming_distance_hex(families1[0].signature, families2[0].signature)
# Estimate cosine similarity
similarity = cosine_from_hamming(distance, 256)
print(f"Estimated cosine similarity: {similarity:.3f}")
Confidence Matrix LSH (CM-LSH)
from odin_prompt_toolkit.cm_lsh import create_default_cm_lsh
# Create CM-LSH hasher (1024 dimensions)
cm_lsh = create_default_cm_lsh(1024, family=0)
# Generate 512-bit signature with confidence matrix
hash1 = cm_lsh.hash(embedding1)
hash2 = cm_lsh.hash(embedding2)
# Compute calibrated similarity
similarity = cm_lsh.sim(hash1, hash2)
print(f"CM-LSH similarity: {similarity:.3f}")
# Check for duplicates
is_duplicate = cm_lsh.is_dup(hash1, hash2, threshold=0.85)
Signature Versions
- V0: OpenAI text-embedding-3-large (1536 dimensions, API-based)
- V1: 0din-jailbreak-embeddings-small ONNX (1024 dimensions, local)
- Latest: Resolves to V1
Important: V0 and V1 signatures are not comparable due to different embedding spaces.
Algorithm
SimHash via Random Hyperplane LSH (Charikar 2002):
- Deterministic hyperplanes via SplitMix64 PRNG
- Default: 3 families × 256 bits × 16 bands
- Hex-encoded signatures (64 hex chars = 256 bits)
- Hamming distance → cosine similarity via
cos(π × d/n)
See the specification for complete algorithm details.
Development
Setup
cd python
pip install -e ".[dev]"
Run Tests
pytest tests/
Type Checking
mypy odin_prompt_toolkit/
Formatting
black odin_prompt_toolkit/ tests/
ruff check odin_prompt_toolkit/ tests/
License
Apache License 2.0
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 0din_prompt_toolkit-0.9.4.tar.gz.
File metadata
- Download URL: 0din_prompt_toolkit-0.9.4.tar.gz
- Upload date:
- Size: 65.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bd1525a8774a99748c2be7264d83c5ab6501272fda126a92f12fb9c897f62126
|
|
| MD5 |
63367fa4f4ae9225955485355fe81b9e
|
|
| BLAKE2b-256 |
040930c6eecf8b8d93cf7857270a0dfacbaa22c29e8e4bf9a327703953dd4575
|
Provenance
The following attestation bundles were made for 0din_prompt_toolkit-0.9.4.tar.gz:
Publisher:
release.yml on 0din-ai/prompt-toolkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
0din_prompt_toolkit-0.9.4.tar.gz -
Subject digest:
bd1525a8774a99748c2be7264d83c5ab6501272fda126a92f12fb9c897f62126 - Sigstore transparency entry: 2638801966
- Sigstore integration time:
-
Permalink:
0din-ai/prompt-toolkit@b6f9082b0d79cc81839efee73712ca2d5eb50f13 -
Branch / Tag:
refs/tags/v0.9.4 - Owner: https://github.com/0din-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b6f9082b0d79cc81839efee73712ca2d5eb50f13 -
Trigger Event:
push
-
Statement type:
File details
Details for the file 0din_prompt_toolkit-0.9.4-py3-none-any.whl.
File metadata
- Download URL: 0din_prompt_toolkit-0.9.4-py3-none-any.whl
- Upload date:
- Size: 52.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ceb784a23b2f65240dc4c3168657dc7deb5aef2e6267f48e3a41b23f7bbb8ce
|
|
| MD5 |
c2d827294a0d3f14c955d4181ffcbe85
|
|
| BLAKE2b-256 |
9c45967e3c733042dfca7055ca27dc1e5f9433b16e4d3aa775e0c59f8371ebf5
|
Provenance
The following attestation bundles were made for 0din_prompt_toolkit-0.9.4-py3-none-any.whl:
Publisher:
release.yml on 0din-ai/prompt-toolkit
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
0din_prompt_toolkit-0.9.4-py3-none-any.whl -
Subject digest:
3ceb784a23b2f65240dc4c3168657dc7deb5aef2e6267f48e3a41b23f7bbb8ce - Sigstore transparency entry: 2638801977
- Sigstore integration time:
-
Permalink:
0din-ai/prompt-toolkit@b6f9082b0d79cc81839efee73712ca2d5eb50f13 -
Branch / Tag:
refs/tags/v0.9.4 - Owner: https://github.com/0din-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@b6f9082b0d79cc81839efee73712ca2d5eb50f13 -
Trigger Event:
push
-
Statement type: