LEANN provides automatic detection for normalized embedding models (vectors with L2 norm = 1). When a normalized model is detected, LEANN automatically sets distance_metric="cosine" to ensure optimal search performance and ranking quality.
Using the wrong metric (like MIPS) with normalized embeddings can lead to poor search quality because HNSW might terminate early due to narrow score ranges.
Automatic Detection Behavior:
- If
distance_metric is not specified, LEANN sets it to "cosine" for supported models. - If you manually specify a different metric (e.g.,
"mips"), LEANN will issue a warning.
Non-Normalized Models:
Models that are not normalized (such as facebook/contriever or other sentence-transformers) will continue to use "mips" by default.
from leann.api import LeannBuilder
# Automatic detection - will use cosine distance
builder = LeannBuilder(
backend_name="hnsw",
embedding_model="text-embedding-3-small",
embedding_mode="openai"
)