Spaces:
Sleeping
Sleeping
Nagendravarma commited on
Commit ·
bb9d034
1
Parent(s): 02057f3
Cap cosine similarity values to max 1.0 to prevent floating point inaccuracies showing >100% similarity score
Browse files
orchestration/semantic_cache.py
CHANGED
|
@@ -139,10 +139,12 @@ class SemanticCache:
|
|
| 139 |
Calculate cosine similarity between two vectors.
|
| 140 |
OpenAI text-embedding-3-small vectors are already normalized (L2 norm = 1.0).
|
| 141 |
Thus, cosine similarity is exactly the dot product.
|
|
|
|
| 142 |
"""
|
| 143 |
if len(vec_a) != len(vec_b):
|
| 144 |
return 0.0
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
def normalize_query(self, query: str) -> str:
|
| 148 |
"""
|
|
@@ -282,7 +284,7 @@ class SemanticCache:
|
|
| 282 |
if results:
|
| 283 |
doc, distance = results[0]
|
| 284 |
# Convert distance to similarity score
|
| 285 |
-
similarity = 1.0 - (distance / 2.0)
|
| 286 |
|
| 287 |
if similarity >= SEMANTIC_CACHE_THRESHOLD:
|
| 288 |
# Entity safeguard check
|
|
|
|
| 139 |
Calculate cosine similarity between two vectors.
|
| 140 |
OpenAI text-embedding-3-small vectors are already normalized (L2 norm = 1.0).
|
| 141 |
Thus, cosine similarity is exactly the dot product.
|
| 142 |
+
Capped between -1.0 and 1.0 to prevent float precision overflows.
|
| 143 |
"""
|
| 144 |
if len(vec_a) != len(vec_b):
|
| 145 |
return 0.0
|
| 146 |
+
val = sum(a * b for a, b in zip(vec_a, vec_b))
|
| 147 |
+
return min(1.0, max(-1.0, val))
|
| 148 |
|
| 149 |
def normalize_query(self, query: str) -> str:
|
| 150 |
"""
|
|
|
|
| 284 |
if results:
|
| 285 |
doc, distance = results[0]
|
| 286 |
# Convert distance to similarity score
|
| 287 |
+
similarity = min(1.0, max(-1.0, 1.0 - (distance / 2.0)))
|
| 288 |
|
| 289 |
if similarity >= SEMANTIC_CACHE_THRESHOLD:
|
| 290 |
# Entity safeguard check
|