Working with Embeddings, Similarity, and Retrieval for Modern AI Applications¶
Notebook Guide¶
This notebook preserves the original content and frames the core ideas for study.
Learning objectives¶
understand embeddings as numerical representations
connect similarity search to semantic retrieval
recognize how vector databases differ from relational stores
relate retrieval systems to business and AI use cases
(a.k.a. “How Large Language Models Remember Without Actually Remembering”)
You’ve heard of SQL and NoSQL. Now welcome to the third sibling of the database family — the one who listens to lo-fi music, reads embeddings for fun, and speaks in cosine similarity. 🎧🧠
Say hello to the Vector Database.
🤔 What’s a Vector Database?¶
Traditional databases store structured data — rows, columns, keys. Vector databases store meaning — numerical embeddings that represent concepts.
For example, the words:
"dog", "puppy", "canine"all become vectors close together in a multi-dimensional space.
But "car" or "finance report" are way off in another galaxy. 🚀
This lets AI systems find similarity by meaning, not just by keyword.
🧮 How It Works¶
It all starts with embeddings — numerical representations of data (text, image, audio, etc.).
For example, with OpenAI’s embeddings API:
from openai import OpenAI
client = OpenAI()
embedding = client.embeddings.create(
input="machine learning for business",
model="text-embedding-3-small"
)
print(embedding.data[0].embedding[:5]) # [0.0112, -0.0451, ...]You get a vector — a list of floating-point numbers that captures the meaning of your text.
Then you store these vectors in a vector database like:
Pinecone 🪵
Weaviate 🧩
FAISS (Facebook AI Similarity Search) 💻
Milvus 🧠
Chroma 🍫
🧭 The “Vector Search” Idea¶
Traditional SQL:
SELECT * FROM customers WHERE name = 'Alice';Vector Search:
“Show me all customers who talk like Alice.”
This is done using similarity search — typically cosine similarity or Euclidean distance between embeddings.
from sklearn.metrics.pairwise import cosine_similarity
similarity = cosine_similarity([vector1], [vector2])High similarity = close meaning.
⚙️ Example: Using Chroma or FAISS¶
Let’s say we want to build a semantic search system for business documents.
from chromadb import Client
client = Client()
collection = client.create_collection("business_docs")
collection.add(
documents=[
"Quarterly revenue grew by 15%",
"The CEO announced new pricing models"
],
ids=["doc1", "doc2"]
)
results = collection.query(query_texts=["How did the company perform?"], n_results=1)
print(results)Boom — it finds documents related in meaning, even without the same words. 💡
Vector DBs are like librarians who understand concepts, not just titles.
🧠 Business Use Cases¶
| Use Case | Example | Why Vector DB Helps |
|---|---|---|
| Semantic Search | “Find all policies about sustainability” | No need for keyword matches |
| Recommendation Systems | “Customers who liked this also liked…” | Finds conceptual similarity |
| Chatbots / LLMs | Retrieve context chunks for long conversations | Enables long-term “memory” |
| Customer Support | Find similar past issues | Context-aware retrieval |
| Fraud Detection | Match behavior patterns | Compare feature embeddings |
In short: Vector DBs don’t store rows — they store relationships in meaning.
🔍 How LLMs Use Vector Databases¶
When you ask a chatbot a question, it doesn’t “remember” your company docs directly. It:
Converts your query → embedding
Searches a vector database for the most similar document embeddings
Pulls them back and sends them to the LLM as context
This technique is called RAG (Retrieval-Augmented Generation) — a fancy way of saying “fetch the right info before answering.”
Example Flow:
[User Question] → [Embedding] → [Vector Search] → [Relevant Docs] → [LLM Answer]LLMs without a vector database are like geniuses with short-term memory loss. 🧠💭
⚡ SQL vs NoSQL vs Vector — The Final Showdown¶
| Feature | SQL | NoSQL | Vector |
|---|---|---|---|
| Structure | Tables | Documents | Multi-dimensional vectors |
| Query Type | Exact match | Flexible, hierarchical | Semantic similarity |
| Best For | Transactions, reports | Dynamic data | AI + NLP + Recommendations |
| Example | SELECT * FROM users | {name: "Alice"} | “Find items like this idea” |
| Tech Examples | PostgreSQL | MongoDB, Firebase | Pinecone, FAISS, Chroma |
SQL stores facts 🧾 NoSQL stores stories 📚 Vector DBs store understanding. 🤯
🧩 Bonus: Hybrid Databases¶
The newest trend? Databases that do all three:
Postgres + pgvector extension 🧠
Weaviate’s hybrid search (keyword + semantic)
ElasticSearch + vectors
This means you can write queries like:
“Find all invoices mentioning ‘shipping delays’ that are semantically similar to recent customer complaints.”
Welcome to the future of enterprise search — where business meets meaning.
💬 Final Thoughts¶
Vector databases are the neural memory systems of modern AI. They don’t just store — they understand.
So next time someone says,
“Why not just use SQL?” you can smile and reply: “Because my data has feelings now.” 🤖❤️
Wrap-Up¶
As you work through the original notebook material, keep one practical question in mind: what does the embedding represent, and what does a nearest-neighbor result actually mean for a business user?
8. Interactive Code¶
Expected output
[0.1, 0.2, 0.9]Expected output
0.825