QueryEngineTool
from LlamaIndex. This tool allows you to define query engines for both your summary and vector indexes and then use them to perform searches.from llama_index.llms.openai import OpenAI from llama_index.core import Settings from llama_index.core import StorageContext, VectorStoreIndex from llama_index.core import SummaryIndex from llama_index.core.tools import QueryEngineTool # initialize settings (set chunk size) Settings.llm = OpenAI() Settings.chunk_size = 1024 nodes = Settings.node_parser.get_nodes_from_documents(documents) # initialize storage context (by default it's in-memory) storage_context = StorageContext.from_defaults() storage_context.docstore.add_documents(nodes) # Define Summary Index and Vector Index over Same Data summary_index = SummaryIndex(nodes, storage_context=storage_context) vector_index = VectorStoreIndex(nodes, storage_context=storage_context) # define query engines summary_query_engine = summary_index.as_query_engine( response_mode="tree_summarize", use_async=True, ) vector_query_engine = vector_index.as_query_engine() summary_tool = QueryEngineTool.from_defaults( query_engine=summary_query_engine, name="summary_tool", description=( "Useful for summarization questions related to the author's life" ), ) vector_tool = QueryEngineTool.from_defaults( query_engine=vector_query_engine, name="vector_tool", description=( "Useful for retrieving specific context to answer specific questions about the author's life" ), )
summary_tool
and vector_tool
are the tools that you can use to query your summary and vector indexes respectively. You can then use these tools to perform your search.def query_both_indexes(query): summary_response = summary_query_engine.query(query) vector_response = vector_query_engine.query(query) return summary_response, vector_response
query
is the question or search term you want to look up in the indexes. The function queries both the summary and vector indexes and returns the responses from both.summary_query_engine
and vector_query_engine
with your actual query engines.RouterQueryEngine
in LlamaIndex. This allows you to route a query to different underlying indices or sub-indices based on the query context. from llama_index.core import VectorStoreIndex from llama_index.core.tools import QueryEngineTool from llama_index.core.query_engine import RouterQueryEngine # define sub-indices index1 = VectorStoreIndex.from_documents(notion_docs) # replace with your first index index2 = VectorStoreIndex.from_documents(slack_docs) # replace with your second index # define query engines and tools tool1 = QueryEngineTool.from_defaults( query_engine=index1.as_query_engine(), description="Use this query engine to do...", ) tool2 = QueryEngineTool.from_defaults( query_engine=index2.as_query_engine(), description="Use this query engine for something else...", ) # define a RouterQueryEngine over them query_engine = RouterQueryEngine.from_defaults( query_engine_tools=[tool1, tool2] ) response = query_engine.query( "In Notion, give me a summary of the product roadmap." )
index1
and index2
are your two indices. The RouterQueryEngine
uses an LLM to choose the best sub-index to route the query to, given the descriptions. You can replace "In Notion, give me a summary of the product roadmap."
with your actual query.