Querying for Relevant Context with Embeddings
Introduction: Why Retrieve Relevant Context?
Welcome back! In the last lesson, you learned how to embed document chunks and store them in a vector database. Now, you are ready to make your smart email assistant even more useful.
Imagine you receive a new email and want your assistant to reply with helpful information. To do this, the assistant needs to find the most relevant pieces of information from all the documents you have processed so far. This is where retrieving relevant context comes in.
In this lesson, you will learn how to use semantic search to find the most useful document chunks for a given email. This is a key step in making your assistant truly smart and responsive.
Quick Recall: Embeddings and Vector Search
Let's quickly remind ourselves of two important ideas:
- Embeddings: These are special lists of numbers (vectors) that represent the meaning of text. You learned how to create them from document chunks in the last lesson.
- Vector Databases: These are databases designed to store and search embeddings efficiently. You used LibSQLVector to store your chunk embeddings.
Why do we use them? Because searching by embeddings lets us find text that is similar in meaning, not just in exact words. This is called semantic search.
Embedding the Query (Email Body)
When a new email arrives, the first step is to turn its content into an embedding. This embedding will help us search for similar content in our database.
Let's see how to do this step by step.
First, suppose you have an email object with a body property:
To create an embedding for this email, you use the embed method from your AI library:
What does this do?
- The
embedmethod takes a text string and converts it into a numerical vector that represents its meaning. - The
valueparameter is the text you want to convert to an embedding. - The
modelparameter specifies which embedding model to use. - The result,
queryEmbedding, is the embedding vector for your email.
At this point, you have a vector that represents the meaning of the email. This is what you will use to search for similar document chunks.
