Reference/Math
Vectors, dot product, cosine
The geometry behind RAG: lists of floats, dot product, L2 norm, cosine in [0, 1] for TF vectors.
A vector here is a Python list of floats, length = vocab size.
Formulas
| Name | Formula | Code |
|---|---|---|
| Dot | Σ a_i b_i | sum(x*y for x,y in zip(a,b)) |
| L2 norm | sqrt(Σ a_i²) | math.sqrt(sum(x*x for x in a)) |
| Cosine | dot / (‖a‖‖b‖) | 0 if either norm is 0 |
Cosine of bag-of-counts vectors is in [0, 1] (non-negative counts). Centered embeddings can be negative; still clamp or just rank.
Why cosine not Euclidean
Long chunks have large L2. Cosine ignores length. Two copies of the same sentence have cosine 1.
Retrieval
- Vectorize query with the same vocab as chunks
- Score all N chunks (N tiny in class; ANN later)
- Sort by score desc, tie-break on id
- Return top-k and the scores
Zero query
All tokens OOV → zero vector → cosine 0 → refuse. Do not special-case "return chunk 0".
python
def cosine(a, b):
na = math.sqrt(sum(x*x for x in a))
nb = math.sqrt(sum(x*x for x in b))
if na == 0 or nb == 0:
return 0.0
return sum(x*y for x,y in zip(a,b)) / (na * nb)| Knob | Failure |
|---|---|
| tau too low | answers equine insurance |
| tau too high | refuses real refunds |
| fix | plot in-scope vs out-of-scope best scores; put tau in the gap |