What is an embedding?
An embedding is a dense vector—a list of numbers—that captures the meaning of text. Similar texts map to nearby vectors. "M-Pesa refund" and "M-Pesa dispute" are semantically close, so their embeddings will be close in vector space. You can measure closeness with cosine similarity and build search, clustering, and retrieval systems on top.
Before embeddings, your only option was keyword matching: does the query contain the exact words? That breaks for synonyms. "Payment reversal" is similar to "refund," but keyword matching misses it. Embeddings capture semantic meaning, so synonyms land near each other in the vector space.
SouthBank India's support team fields hundreds of questions daily: "Why was my card declined?", "How do I dispute a charge?", "My transaction failed, what now?" Most are subtle variations on the same FAQ. The bank wants a system that takes a customer's question and returns the relevant FAQ in 100ms, locally, without sending data to OpenAI.
Keyword search fails: "transaction declined" doesn't match "payment failed." But if they embed all 50 FAQs and compute similarity against each incoming query, they can rank the top 3 matches instantly. That's embeddings in production.
Pull nomic-embed-text via Ollama
nomic-embed-text is a free, 768-dimensional embedding model from Nomic AI. It runs locally. No API keys. No data leaving your machine. Pull it with one command:
That's it. The model lands in ~/.ollama/models. Now you can embed text in Python:
Each embedding is a list of 768 floats. The values are small numbers between -1 and 1 (roughly). To compare two texts, you compute cosine similarity—how aligned are the two vectors?
Cosine similarity: measuring meaning overlap
Cosine similarity measures the angle between two vectors. If they point in the same direction, similarity = 1 (identical meaning). Opposite direction? Similarity = -1. Perpendicular? Similarity = 0 (no overlap).
The formula: similarity(A, B) = (A · B) / (||A|| × ||B||), where A · B is the dot product, and ||A|| is the magnitude. For text embeddings, scipy makes it one line:
Worked example: rank FAQs by query similarity
You have 5 SouthBank India FAQ snippets. A customer asks: "My card was declined yesterday." Embed all 6 texts (5 FAQs + 1 query) and rank by similarity to the query.
The top match will likely be FAQ #0 (card declined)—similarity ~0.82. FAQ #4 (international payments) ranks lowest (~0.35). Embeddings capture semantic meaning, not keyword matches.
Chunking strategy: split docs into searchable pieces
Real documents are long: a 20-page policy, an entire FAQ section, a customer contract. Embedding a 10,000-word document as one vector loses granularity. Instead, chunk it into bite-sized pieces (300–500 tokens each) with overlap so context carries across chunks.
Why overlap?
- A sentence split across chunks loses context. 50-token overlap ensures continuity.
- Keyword near a chunk boundary might only match with the overlap included.
- Overlap raises recall at the cost of slightly more embeddings to compute.
Example: a 1000-token HDFCBANK policy might become 3 chunks (tokens ~330 each, with 50-token overlap).
Worked example: embed an HDFCBANK policy corpus
You have 5 paragraphs from HDFCBANK's refund policy. Embed each, then find which 2 are most similar to a query: "How long until money comes back?"
Chunks 0 and 3 rank highest (~0.79, ~0.75) because they directly answer "how long." Chunk 4 ranks lowest (~0.51) because it's about refund eligibility, not timing.
Why nomic-embed-text vs OpenAI ada-002 vs BGE?
Privacy: Nomic runs locally. No API calls. Your data never leaves your machine. OpenAI ada-002 requires sending text to their servers.
Cost: Nomic is free. OpenAI ada-002 costs $0.10 per 1M tokens. For a 10,000-document corpus, you'd spend $1–2.
Latency: Nomic embeddings compute in ~50ms on a modern laptop. OpenAI API adds network round-trip time (~200–500ms).
Quality trade-off: Nomic is strong for English, semantics, and ranking. BGE (from Alibaba) is excellent for multilingual and retrieval-augmented generation (RAG). OpenAI ada-002 is slightly better at nuance but requires internet and payment. For a SouthBank India FAQ system, nomic is plenty—similarity scores differ by <0.02 from ada-002 on most queries.
Choose nomic for: local-first systems, frequent queries, privacy-sensitive data (financial, healthcare), cost-conscious projects. Choose OpenAI ada-002 if: you're already in their ecosystem, you need the absolute best quality, or latency doesn't matter.
Normalization and why cosine wins for text
Cosine similarity assumes normalized vectors (magnitude 1). Fortunately, nomic-embed-text outputs already-normalized embeddings, so you can use cosine directly.
Why is cosine better than Euclidean for text? Euclidean distance cares about absolute position in space. A long document might have large-magnitude embeddings, a short snippet might have small magnitudes—same meaning, but different distance. Cosine ignores magnitude and compares direction, so "the same concept in two different documents" scores high similarity regardless of document length.
embedding / np.linalg.norm(embedding). Then cosine similarity = dot product.
Next: storing and searching with chromadb
You now know how to embed and rank. In Lesson 13, you'll learn to store thousands of embeddings in a vector database (chromadb) and search them in milliseconds. You'll build a retrieval-augmented generation (RAG) system that answers M-Pesa policy questions by finding the most relevant chunks and feeding them to an LLM. Semantic search + LLM = powerful retrieval pipelines.
How AI agents use embeddings
Every RAG system (retrieval-augmented generation) starts here. An agent queries a vector database, finds relevant documents by embedding similarity, and feeds them to an LLM for synthesis. By Lesson 13, you'll build a multi-agent system that retrieves M-Pesa policies, SouthBank FAQs, and Jumia logistics rules—all via embedding search. This is the backbone of modern semantic search and knowledge-grounded agents.
Task: You have 3 Jumia Kenya customer reviews. A user query is: "Is the product durable?" Embed all 4 texts (query + 3 reviews), compute pairwise similarities, and rank the reviews by relevance to the query.
Hint 1: Use ollama.embeddings(...) for each text.
Hint 2: Convert embeddings to numpy arrays for easy computation.
Hint 3: Loop through reviews, compute 1 - cosine(query_emb, review_emb), and sort descending.
Test your knowledge
You've mastered semantic meaning.
Now build retrieval systems that think.
Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Agentic Engineer →