1. Overview
The retriever is a part of the query engine, responsible for obtaining the most relevant context based on user queries (or chat messages).
It can be built on existing indexes but can also be defined independently. It serves as a key building block for query engines (and chat engines) to retrieve relevant context.
2. Types of Retrievers
Retrievers include various types and can form comprehensive retrievers through combinations of multiple components, such as:
-
Automated Retrieval: These retrieval techniques perform semi-structured queries, combining semantic search with structured filtering.
-
Advanced Retrievers: These guidelines include advanced retrieval techniques. Some are common, such as keyword/mixed search, re-ranking, etc. Some are specific to LLM + RAG workflows, such as small-to-large and automatic merge retrieval.
-
Knowledge Graph-Based Retrievers: Retrievers based on graph databases.
-
Composite Retrievers: These retrieval techniques consist of other retrieval techniques, providing higher-level functionalities such as hierarchical retrieval and query decomposition.
3. Usage Paradigms
Creating and using index-based retrievers is very simple, as follows:
retriever=index.as_retriever()
nodes=retriever.retrieve("Who is Paul Graham?")
4. Advanced API
4.1. Selecting Retrievers
You can choose a specific index-based retriever class through retrieve_mode. For example, using SummaryIndex:
retriever = summary_index.as_retriever(
retriever_mode="llm",
)
This will create a SummaryIndexLLMRetriever on top of the summary index. Refer to the subsequent section on “Retriever Modes” for details.
4.2. Configuring Retrievers
Similarly, you can configure the selected retriever through kwargs. Note: Please refer to the API reference for the constructor parameters of the selected retriever class to get a list of valid kwargs.For example, if we choose the “llm” retriever mode, we might do the following:
retriever = summary_index.as_retriever(
retriever_mode="llm",
choice_batch_size=5,
)
4.3. Low-Level Composite API
If you need finer control, you can use the low-level composite API. To achieve the same results as above, you can directly import and construct the required retriever class:
from llama_index.core.retrievers import SummaryIndexLLMRetriever
retriever = SummaryIndexLLMRetriever(
index=summary_index,
choice_batch_size=5,
)
5. Retriever Modes
The following shows the mapping from retrieve_mode to the selected retriever class.
Vector Index
Specifying retrieve_mode has no effect (silently ignored). vector_index.as_retriever(…) always returns VectorIndexRetriever.
Summary Index
-
default: SummaryIndexRetriever
-
embedding: SummaryIndexEmbeddingRetriever
-
llm: SummaryIndexLLMRetriever
Tree Index
-
select_leaf: TreeSelectLeafRetriever
-
select_leaf_embedding: TreeSelectLeafEmbeddingRetriever
-
all_leaf: TreeAllLeafRetriever
-
root: TreeRootRetriever
Keyword Table Index
-
default: KeywordTableGPTRetriever
-
simple: KeywordTableSimpleRetriever
-
rake: KeywordTableRAKERetriever
Knowledge Graph Index
-
keyword: KGTableRetriever
-
embedding: KGTableRetriever
-
hybrid: KGTableRetriever
Document Summary Index
-
llm: DocumentSummaryIndexLLMRetriever
-
embedding: DocumentSummaryIndexEmbeddingRetrievers
Conclusion
Retrievers are the core components of content querying. In completing the RAG mode, various retrievers are needed to obtain better query data, allowing large models to return better answers.