AI Hints
Home›LangChain Tutorial›Chunks and Embeddings
LangChain · Lesson 63 of 120

Embeddings

Meaning as numbers.

⏱ 2 min read▶ Video at 1:11:01🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

To search by meaning, not just by exact words, we need to turn text into numbers. An embedding is a long list of numbers, that captures what a piece of text means.

Embeddings
From the video: Embeddings
  • Search by meaning
  • Text to numbers
  • An embedding

Making an embedding

We create an embeddings model, Gemini embedding zero zero one. Embed query turns one piece of text into a vector. Run it. The vector has over three thousand numbers. On their own, they mean nothing to us. What matters is how they compare with the vectors of other texts.

embed.pypython
from langchain_google_genai import GoogleGenerativeAIEmbeddings

embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")
vector = embeddings.embed_query("How long does delivery take?")

print("length:", len(vector))
print("first five:", [round(x, 3) for x in vector[:5]])
Output
length: 3072
first five: [0.025, 0.006, -0.024, -0.03, -0.001]

How embeddings work

An embedding model is trained so that texts with similar meanings get similar vectors. When does my parcel arrive, and how long is delivery, share almost no words, but their vectors are close. That's what lets RAG find the right chunk, even when the customer uses different words from the document.

keyvalue
Similar meaningsimilar vectors
Different wordscan still match
Embed documentsonce, when indexing
Embed questionsat query time

Summary

An embedding turns text into a vector that captures its meaning. Similar meanings give similar vectors. Next, measuring how similar two vectors are.

Key takeaways
  • Text → vector
  • Thousands of numbers
  • Similar meaning, close vectors
Next up: Similarity — close in meaning