LSH Overview
Locality-Sensitive Hashing (LSH) is a technique for efficiently finding similar items in high-dimensional spaces.
The Problem
Traditional similarity search requires comparing every pair of items:
This is computationally prohibitive.
The Solution: LSH
LSH maps similar items to the same "buckets" with high probability:
- Hash items into short signatures
- Index by hash buckets
- Query only items in matching buckets
Result: instead of
SimHash Algorithm
odin-prompt-toolkit uses SimHash via random hyperplane LSH (Charikar 2002):
Embedding Vector → Normalize → Generate Hyperplanes →
Compute Dot Products → Sign Bits → Pack to Hex → 256-bit Signature
Step 1: Normalize
Convert embedding to unit length:
Step 2: Random Hyperplanes
Generate 256 deterministic random hyperplanes using SplitMix64 PRNG with seed:
Step 3: Project & Quantize
For each hyperplane, compute dot product and extract sign bit:
Step 4: Pack to Hex
Pack 256 bits into 64 hex characters.
Why It Works
Key insight: If two vectors are similar (high cosine similarity), they're likely to have the same sign when projected onto a random hyperplane.
Probability: where is the angle between vectors.
For cosine similarity 0.9 ():
- Expected Hamming distance:
Banding for Efficiency
Splitting signatures into bands enables efficient candidate generation:
Signature: 8d000000ac854dae91814006c580080a...
Bands: [8d00] [0000] [00ac] [854d] ...
band0 band1 band2 band3
Algorithm:
- Index documents by band values
- Query: Find all documents sharing any band
- Verify candidates with full Hamming distance
Trade-off:
- More bands → Higher recall (catch more candidates)
- Fewer bands → Higher precision (fewer false positives)
Next Steps
- Signature Versions — V0 vs V1
- Duplicate Detection Guide — Build a detector
- Algorithm Specification — Formal specification