Working through this module?
Module 15 · Page 16 of 20 · 11 sections
Embeddings and Semantic Search
What embeddings are, popular embedding models, the workflow, how semantic search and similarity metrics work, advanced techniques and use cases.
What are Embeddings?
- Embeddings are numerical vector representations of text, capturing semantic meaning.
- They convert text (documents, queries) into dense vectors (e.g., 1536 dimensions) using embedding models.
- Similar content will have similar vectors, enabling semantic search, RAG and AI applications.
Popular Embedding Models
- OpenAI Embeddings - text-embedding-3-small
- Cohere Embeddings - multilingual models
- Hugging Face - sentence-transformers
- Local models - BGE, all-MiniLM, Instructor models
- Multimodal models - for text, images, code, etc.
Embedding Workflow in LangChain
Raw DocumentsPDF, web, text
Text SplitterSplit into chunks
Embedding ModelCreate embeddings
Vector DatabaseStore vectors
Semantic SearchRetrieve relevant chunks
How Semantic Search Works
- 1The user query is converted into an embedding vector.
- 2The vector is compared with stored document vectors (using similarity search).
- 3The top-k most relevant chunks are retrieved and used by the LLM to generate accurate answers.
Similarity Metrics
| Metric | Description | Use Case |
|---|---|---|
| Cosine Similarity | Measures angle between vectors | Most common |
| Euclidean Distance | Measures actual distance | When magnitude matters |
| Dot Product | Simple and efficient | Suitable for large-scale search |
Implementing Embeddings in LangChain
python
from langchain.embeddings import OpenAIEmbeddings from langchain.vectorstores import FAISS # Create embeddings embeddings = OpenAIEmbeddings() vectorstore = FAISS.from_documents(docs, embeddings)
Semantic Search with LangChain
python
from langchain.vectorstores import FAISS from langchain.chains import RetrievalQA retriever = vectorstore.as_retriever(search_kwargs={ "k": 3 }) qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever) result = qa_chain.run("What is LangChain?") print(result)
Advanced Techniques
- Hybrid Search - combine keyword + vector search.
- Metadata Filtering - filter results by source, date, tags.
- Re-ranking - improve results using cross-encoders.
- Multi-vector Retrieval - use multiple embeddings (e.g., title + content).
- Semantic Chunking - split content based on meaning, not just size.
Use Cases of Embeddings
- RAG applications - retrieve relevant context for the LLM.
- Semantic search - intelligent search in docs and products.
- Recommendation systems - find similar items.
- Chatbots and assistants - answer using your data.
- Multimodal search - search across text, images and code.
Example: RAG with Embeddings
PDFCompany docs
Split & Embed
Vector DBFAISS / Qdrant
Retrieve Relevant Docs
LLMGive accurate answer
Key Takeaways
- Embeddings bridge the gap between text and machine understanding.
- Semantic search helps find the most relevant information efficiently.
- LangChain makes it easy to generate embeddings, store them in vector databases and use them for powerful RAG applications.
- Choose the right model, metric and database based on your use case and scale.