-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembeddings.py
More file actions
28 lines (19 loc) · 773 Bytes
/
Copy pathembeddings.py
File metadata and controls
28 lines (19 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""Explicit 2D vector embeddings and cosine similarity calculations."""
import math
VECTORS = {
"query": [0.10, 0.90],
"calculator": [0.95, 0.05],
"graph_lookup": [0.15, 0.85]
}
def dot_product(vector_a: list[float], vector_b: list[float]) -> float:
return vector_a[0] * vector_b[0] + vector_a[1] * vector_b[1]
def magnitude(vector: list[float]) -> float:
return math.sqrt(vector[0] ** 2 + vector[1] ** 2)
def calculate_cosine_similarity(vector_a: list[float], vector_b: list[float]) -> float:
"""Compute cosine similarity from first principles.
Similarity = (A ⋅ B) / (||A|| ||B||)
"""
denom = magnitude(vector_a) * magnitude(vector_b)
if denom == 0:
return 0.0
return dot_product(vector_a, vector_b) / denom