From 3136b2e12064d9733e8be2f2df97f0f4bc9ccfe4 Mon Sep 17 00:00:00 2001 From: dhug <38571110+danielpatrickhug@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:53:53 -0500 Subject: [PATCH] added similarity functions and ran precommit (#633) --- .../oasst_backend/utils/similarity_functions.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 backend/oasst_backend/utils/similarity_functions.py diff --git a/backend/oasst_backend/utils/similarity_functions.py b/backend/oasst_backend/utils/similarity_functions.py new file mode 100644 index 00000000..c166661f --- /dev/null +++ b/backend/oasst_backend/utils/similarity_functions.py @@ -0,0 +1,17 @@ +from typing import List + +import numpy as np + + +def cosine_similarity(a: List[float], b: List[float]): + """Compute cosine similarity (dot product of two vectors divided by the product of their norms.)""" + norm_a = np.linalg.norm(a) + norm_b = np.linalg.norm(b) + if norm_a == 0 or norm_b == 0: + raise ZeroDivisionError("One of the vectors has a norm of zero.") + return np.dot(a, b) / (norm_a * norm_b) + + +def euclidean_distance(a: List[float], b: List[float]): + """Compute euclidean distance (norm of the difference of two vectors.)""" + return np.linalg.norm(np.subtract(a, b))