----------------------
To handle complex queries, you can use the
SubQuestionQueryEngine
from LlamaIndex. This engine breaks down a complex query into sub-questions for each relevant data source, gathers all the intermediate responses, and synthesizes a final response.
First, you need to import the necessary modules and set up your environment:
import os
import nest_asyncio
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.callbacks import CallbackManager, LlamaDebugHandler
from llama_index.core import Settings
os.environ["OPENAI_API_KEY"] = "sk-..."
nest_asyncio.apply()
Next, you can use the
LlamaDebugHandler
to print the trace of the sub-questions captured by the
SUB_QUESTION
callback event type:
llama_debug = LlamaDebugHandler(print_trace_on_end=True)
callback_manager = CallbackManager([llama_debug])
Settings.callback_manager = callback_manager
Then, you can create a
SubQuestionQueryEngine
using your tools:
query_engine = SubQuestionQueryEngine.from_defaults(
[get_trim_list, specific_trim_specs_tool],
llm=open_AI_agent, # OpenAIAgent
verbose=True,
)
Finally, you can use the
query_engine
to handle complex queries:
response = query_engine.query("Your complex question here")
print(response)
This will break down the complex question into sub-questions, use the specified tools to answer each sub-question, and then synthesize the responses into a final answer.
Please note that you need to replace
"Your complex question here"
with the actual complex question you want to answer.