Embeddings and Semantic Search
An embedding maps an input to a vector whose geometry is useful for a trained objective. Similarity is model-, version-, language-, and domain-dependent: nearby vectors are not proof that two statements are equivalent or true.
Prerequisites
Review vectors, dot products, normalization, and tokenization.
Learning goals
- compute cosine similarity between normalized vectors.
- distinguish an encoder from a similarity rule and a retrieval policy.
- validate semantic search with labeled queries from the intended domain.
Worked example
The local encoder below maps a few aliases to three declared concepts. It demonstrates the interface reproducibly and is not a learned embedding or evidence about a real model.
import math
concepts = ["retention", "travel", "security"]
aliases = {
"receipt": "retention", "receipts": "retention", "retained": "retention",
"trip": "travel", "travel": "travel", "incident": "security", "security": "security",
}
def encode(text):
counts = [0.0] * len(concepts)
for token in text.lower().replace("?", "").split():
concept = aliases.get(token)
if concept:
counts[concepts.index(concept)] += 1.0
norm = math.sqrt(sum(value * value for value in counts)) or 1.0
return [value / norm for value in counts]
documents = {
"kb-retention": "expense receipts retained",
"kb-travel": "international travel",
"kb-security": "security incident",
}
query = encode("How long are receipts retained?")
ranking = sorted(
((doc_id, round(sum(a * b for a, b in zip(query, encode(text))), 3)) for doc_id, text in documents.items()),
key=lambda item: (-item[1], item[0]),
)
print(f"query_vector={query}")
print(f"ranking={ranking}")
Output:
query_vector=[1.0, 0.0, 0.0]
ranking=[('kb-retention', 1.0), ('kb-security', 0.0), ('kb-travel', 0.0)]
The verifier expects deterministic ordering for tied scores. A production encoder needs a pinned artifact and preprocessing contract, plus tests for paraphrases, negation, dates, identifiers, languages, and access-controlled slices.
Common mistakes
- Calling any vector comparison “semantic” without relevance evaluation.
- Mixing embeddings from different model revisions or normalization policies in one index.
- Assuming cosine similarity is calibrated probability.
- Embedding secrets or PII without a retention, deletion, access, and incident-response plan.
Practice
Add “How many years must I keep a reimbursement record?” to a labeled query set. Explain why this toy encoder fails and what evidence a replacement encoder must produce.
Next steps
Vector Indexes and Retrieval turns similarity scores into an operational retrieval system.
Primary sources
Checked 2026-10-16: Sentence-BERT, scikit-learn cosine similarity, and NIST AI RMF Generative AI Profile.