Retrievers
A retriever finds the right pieces of text for a question.
Think of a librarian. You ask a question, and the librarian walks off and brings back the three most useful pages.
You need one because a model cannot read your whole library. It can only read a few pages at a time.
From vector store to retriever
In the last lesson you built a vector store. A retriever is a thin wrapper around it.
as_retriever turns the store into a retriever. k is how many pages the librarian brings back.
Ask the retriever a question
invoke takes a question and returns a list of Documents. The best match comes first.
Notice there is no word loop in the question. The retriever still finds the loop page because the meaning is close.
Fewer or more pages
A small k gives focused answers. A big k gives more context but costs more tokens.
Start with k equal to 3 or 4. Change it later if the answers feel thin or bloated.
MMR: ask for variety
Plain search can return three pages that all say the same thing.
search_type='mmr' picks pages that are relevant and also different from each other.
fetch_k is how many pages to look at first. From those, MMR keeps the 3 most varied.
A retriever is a Runnable
This is the important part. A retriever understands invoke, batch and the pipe, just like a model.
So it can sit inside a chain.
The question goes into the retriever. The pages flow into join_pages. Out comes one block of text.
In the next lesson, that block of text becomes the context for a model.
MultiQueryRetriever: ask in many ways
Sometimes one question is worded badly and misses the right page.
MultiQueryRetriever asks a model to rewrite your question a few ways, searches with all of them, and merges the results.
It costs one extra model call. Use it when plain search keeps missing.
What you learned
as_retriever wraps a vector store
invoke(question) returns a list of Documents
k controls how many pages come back
search_type="mmr" gives variety
a retriever is a Runnable, so it fits in a chain
Remember: the retriever only fetches pages. It does not answer. The model answers, using the pages. That is RAG, and it is next.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.