← Lesson
Why Agents Need Math
Joeven
Run
Reset
Python loads on first run
query = [0.2, 0.8, 0.1, 0.0] docs = { "refund policy": [0.1, 0.9, 0.0, 0.1], "shipping times": [0.8, 0.1, 0.7, 0.0], "password reset": [0.0, 0.2, 0.1, 0.9], } def mag(v): return sum(x * x for x in v) ** 0.5 def cosine(a, b): return sum(x * y for x, y in zip(a, b)) / (mag(a) * mag(b)) for name, vec in docs.items(): score = cosine(query, vec) print(name, round(score, 3))
Run to execute this in your browser. Nothing is sent to a server.