Router Query Engine
In this tutorial, we define a custom router query engine that selects one out of several candidate query engines to execute a query.
Setup
First, we need to install import the necessary modules from llamaindex
:
Loading Data
Next, we need to load some data. We will use the SimpleDirectoryReader
to load documents from a directory:
Service Context
Next, we need to define some basic rules and parse the documents into nodes. We will use the SentenceSplitter
to parse the documents into nodes and Settings
to define the rules (eg. LLM API key, chunk size, etc.):
Creating Indices
Next, we need to create some indices. We will create a VectorStoreIndex
and a SummaryIndex
:
Creating Query Engines
Next, we need to create some query engines. We will create a VectorStoreQueryEngine
and a SummaryQueryEngine
:
Creating a Router Query Engine
Next, we need to create a router query engine. We will use the RouterQueryEngine
to create a router query engine:
We're defining two query engines, one for summarization and one for retrieving specific context. The router query engine will select the most appropriate query engine based on the query.
Querying the Router Query Engine
Finally, we can query the router query engine: