llm
retrieval
ai_generated
true
RAG retrieval misses relevant docs — cosine similarity threshold too high
ID: llm/embedding-cosine-similarity-threshold-arbitrary
82%Fix Rate
85%Confidence
4Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| any | active | — | — | — |
Root Cause
Hardcoded cosine similarity threshold misses semantically relevant documents.
genericWorkarounds
-
88% success Calibrate threshold per embedding model using a validation set
Find the threshold that maximizes F1 score on known-relevant docs
-
90% success Use top-K retrieval instead of threshold-based filtering
retriever = vectorstore.as_retriever(search_kwargs={'k': 5})Sources: https://python.langchain.com/docs/how_to/vectorstore_retriever/
Dead Ends
Common approaches that don't work:
-
Set a universal threshold like 0.8 for all embedding models
85% fail
Different models have different similarity distributions
-
Use Euclidean distance instead of cosine for normalized embeddings
70% fail
For normalized vectors, cosine and Euclidean are monotonically related