← Lesson
Embeddings and Retrieval
Joeven
Run
Reset
Python loads on first run
import math def dot(a, b): return sum(x * y for x, y in zip(a, b)) def norm(a): return math.sqrt(sum(x * x for x in a)) def cosine(a, b): na, nb = norm(a), norm(b) if na == 0 or nb == 0: return 0.0 return dot(a, b) / (na * nb) # Toy dims: [infra, billing, security, oom] CHUNKS = { "runner-oom": [0.9, 0.0, 0.1, 1.0], "refunds": [0.0, 1.0, 0.2, 0.0], "api-keys": [0.1, 0.0, 1.0, 0.0], } QUERIES = { "worker ran out of memory": [0.8, 0.0, 0.0, 0.9], "how long do refunds take": [0.0, 0.9, 0.0, 0.0], "where do I put secrets": [0.0, 0.0, 0.9, 0.0], } def search(query_vec, k=2): ranked = [(name, cosine(query_vec, vec)) for name, vec in CHUNKS.items()] ranked.sort(key=lambda x: x[1], reverse=True) return ranked[:k] for q, vec in QUERIES.items(): print(q) for name, score in search(vec): print(" ", name, "cos=" + str(round(score, 3)))
Run to execute this in your browser. Nothing is sent to a server.