fix(vector-store): add epsilon tolerance for norm checks

Replace exact zero comparison with epsilon-based check (< 1e-10)
in _cosine_similarity to handle floating point precision issues.
Also check for product underflow to prevent inf/nan from division
by very small numbers.

Solution-ID: SOL-20251228113619
Issue-ID: ISS-1766921318981-11
Task-ID: T1
This commit is contained in:
catlog22
2025-12-28 21:11:30 +08:00
parent 93dcdd2293
commit 6486c56850

View File

@@ -46,10 +46,16 @@ def _cosine_similarity(a: List[float], b: List[float]) -> float:
norm_a = np.linalg.norm(a_arr)
norm_b = np.linalg.norm(b_arr)
if norm_a == 0 or norm_b == 0:
EPSILON = 1e-10
# Use epsilon tolerance to avoid division by (near-)zero due to floating point precision.
if norm_a < EPSILON or norm_b < EPSILON:
return 0.0
return float(np.dot(a_arr, b_arr) / (norm_a * norm_b))
denom = norm_a * norm_b
if denom < EPSILON:
return 0.0
return float(np.dot(a_arr, b_arr) / denom)
class VectorStore: