run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
36.08k stars 5.14k forks source link

[Question]: KeyError Associated With Sub Question Query Engine #15106

Open HariNuve opened 2 months ago

HariNuve commented 2 months ago

Question Validation

Question

I have tried Sub QUestion Query Engine with different complex questions associated with pdfs,And for most of the trials it properly worked and returned the answer appropriately,But for some trials the issue is as follows

Traceback (most recent call last): File "/home/paperspace/clinsight/backend/dataingestion/test/h_dev/demo3_working.py", line 254, in result = qa_test(input_dict) File "/home/paperspace/clinsight/backend/dataingestion/test/h_dev/demo3_working.py", line 239, in qa_test qa_response = sub_query_engine.query(f"{query}") File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 235, in wrapper result = func(*args, *kwargs) File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/site-packages/llama_index/core/base/base_query_engine.py", line 52, in query query_result = self._query(str_or_query_bundle) File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 235, in wrapper result = func(args, *kwargs) File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/site-packages/llama_index/core/query_engine/sub_question_query_engine.py", line 158, in _query qa_pairs_all = run_async_tasks(tasks) File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/site-packages/llama_index/core/async_utils.py", line 77, in run_async_tasks outputs: List[Any] = asyncio_run(_gather()) File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/site-packages/llama_index/core/async_utils.py", line 33, in asyncio_run return loop.run_until_complete(coro) File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete return future.result() File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/site-packages/llama_index/core/async_utils.py", line 75, in _gather return await asyncio.gather(tasks_to_execute) File "/home/paperspace/anaconda3/envs/prod_env/lib/python3.10/site-packages/llama_index/core/query_engine/sub_question_query_engine.py", line 234, in _aquery_subq query_engine = self._query_engines[sub_q.tool_name] KeyError: 'LMAR'

have thorughly cross checked the code with your documentations and github page.But unable to resolve this issue.Expecting an early responnse to resolve this issue.

dosubot[bot] commented 2 months ago

The KeyError: 'LMAR' indicates that the key 'LMAR' is not found in the _query_engines dictionary. This dictionary is populated with the metadata.name of each QueryEngineTool provided to the SubQuestionQueryEngine.

To fix this issue, ensure that the QueryEngineTool with the metadata.name 'LMAR' is correctly added to the query_engine_tools list when initializing the SubQuestionQueryEngine.

Here is an example of how you can initialize the SubQuestionQueryEngine with the required QueryEngineTool:

from llama_index.core.tools.query_engine import QueryEngineTool
from llama_index.core.query_engine.sub_question_query_engine import SubQuestionQueryEngine
from llama_index.core.schema import ToolMetadata

# Assuming you have a query engine for 'LMAR'
lmar_query_engine = YourLMARQueryEngine()  # Replace with your actual query engine

# Create a QueryEngineTool for 'LMAR'
lmar_tool = QueryEngineTool(
    metadata=ToolMetadata(name='LMAR', description='Description of LMAR tool'),
    query_engine=lmar_query_engine
)

# Initialize the SubQuestionQueryEngine with the required tools
sub_query_engine = SubQuestionQueryEngine.from_defaults(
    query_engine_tools=[lmar_tool, ...]  # Add other tools as needed
)

# Now you can use sub_query_engine to query

Ensure that the metadata.name for each QueryEngineTool matches the tool_name used in your sub-questions. This should resolve the KeyError. Additionally, make sure that the SubQuestionQueryEngine is correctly handling the asynchronous tasks if you are using the async mode, as shown in the provided code snippets [1][2].

To continue talking to Dosu, mention @dosu.

logan-markewich commented 2 months ago

The LLM hallucinated the name of a query engine tool

I suggest giving your query engine tools distinct names/descriptions

HariNuve commented 2 months ago

@logan-markewich Thank you for your response, I have already tried with distinct names.Yet the issue persists.