MCPcopy Create free account
hub / github.com/dataease/SQLBot / cosine_similarity

Function cosine_similarity

backend/apps/datasource/embedding/utils.py:6–18  ·  view source on GitHub ↗
(vec_a, vec_b)

Source from the content-addressed store, hash-verified

4
5
6def cosine_similarity(vec_a, vec_b):
7 if len(vec_a) != len(vec_b):
8 raise ValueError("The vector dimension must be the same")
9
10 dot_product = sum(a * b for a, b in zip(vec_a, vec_b))
11
12 norm_a = math.sqrt(sum(a * a for a in vec_a))
13 norm_b = math.sqrt(sum(b * b for b in vec_b))
14
15 if norm_a == 0 or norm_b == 0:
16 return 0.0
17
18 return dot_product / (norm_a * norm_b)

Callers 3

get_ds_embeddingFunction · 0.90
get_table_embeddingFunction · 0.90
calc_table_embeddingFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected