run-llama / llama_index

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

[Question]: How can I use the SubQuestionQueryEngine to always decompose a user query to a ReActAgent? #13263

Closed brycecf closed 1 week ago

brycecf commented 4 months ago

Question Validation

Question

How can I use the SubQuestionQueryEngine to always decompose a user query to a ReActAgent?

dosubot[bot] commented 4 months ago

To ensure the SubQuestionQueryEngine always decomposes a user query into a ReActAgent, you'll need to use a question generator designed for ReActAgent. This involves configuring the SubQuestionQueryEngine with a question_gen that generates sub-questions tailored for a ReActAgent. Here's how you can do it:

from my_custom_generators import ReActAgentQuestionGenerator

react_agent_question_gen = ReActAgentQuestionGenerator(...)

sub_question_query_engine = SubQuestionQueryEngine(
    question_gen=react_agent_question_gen,
    response_synthesizer=my_response_synthesizer,
    query_engine_tools=my_query_engine_tools,
    verbose=True,
    use_async=True
)

This setup requires a ReActAgentQuestionGenerator, which you need to implement based on the specific needs for decomposing queries for a ReActAgent. The flexibility of the SubQuestionQueryEngine allows it to accept any subclass of BaseQuestionGenerator, enabling customization in query decomposition.

For implementation details, refer to the SubQuestionQueryEngine source code in the LlamaIndex repository.

#### Details **Sources** - [[Question]: Is it possible to use LlamaIndex ReAct Agent with Citation Query Engine](https://github.com/run-llama/llama_index/issues/11525) - [llama-index-core/llama_index/core/query_engine/sub_question_query_engine.py](https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/query_engine/sub_question_query_engine.py) - [llama-index-legacy/llama_index/legacy/query_engine/sub_question_query_engine.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/query_engine/sub_question_query_engine.py) - [llama-index-legacy/llama_index/legacy/question_gen/openai_generator.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/question_gen/openai_generator.py) - [llama-index-legacy/llama_index/legacy/agent/react/prompts.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/agent/react/prompts.py) - [llama-index-core/llama_index/core/question_gen/llm_generators.py](https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/question_gen/llm_generators.py) - [llama-index-legacy/llama_index/legacy/question_gen/llm_generators.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/question_gen/llm_generators.py) - [llama-index-integrations/question_gen/llama-index-question-gen-guidance/tests/test_question_gen_guidance_generator.py](https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/question_gen/llama-index-question-gen-guidance/tests/test_question_gen_guidance_generator.py) - [llama-index-legacy/llama_index/legacy/agent/react/base.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/agent/react/base.py) - [llama-index-legacy/llama_index/legacy/question_gen/guidance_generator.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/question_gen/guidance_generator.py) - [llama-index-integrations/question_gen/llama-index-question-gen-guidance/llama_index/question_gen/guidance/base.py](https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/question_gen/llama-index-question-gen-guidance/llama_index/question_gen/guidance/base.py) - [llama-index-integrations/program/llama-index-program-guidance/llama_index/program/guidance/base.py](https://github.com/run-llama/llama_index/blob/main/llama-index-integrations/program/llama-index-program-guidance/llama_index/program/guidance/base.py) - [llama-index-legacy/llama_index/legacy/program/guidance_program.py](https://github.com/run-llama/llama_index/blob/main/llama-index-legacy/llama_index/legacy/program/guidance_program.py) - [docs/docs/examples/output_parsing/guidance_sub_question.ipynb](https://github.com/run-llama/llama_index/blob/main/docs/docs/examples/output_parsing/guidance_sub_question.ipynb) - [llama-index-core/llama_index/core/agent/legacy/react/base.py](https://github.com/run-llama/llama_index/blob/main/llama-index-core/llama_index/core/agent/legacy/react/base.py)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

zapatacc commented 3 months ago

I faced the same question. I founded a medium post with an interesting approch, levering recursive retrieval to the tools (react agents) and a subqueryengine in the top level:

https://medium.com/@sauravjoshi23/complex-query-resolution-through-llamaindex-utilizing-recursive-retrieval-document-agents-and-sub-d4861ecd54e6

Relevant code to this question:

# # define recursive retriever
recursive_retriever = RecursiveRetriever(
    "vector",
    retriever_dict={"vector": vector_retriever},
    query_engine_dict=agents,
    verbose=True,
)
response_synthesizer = get_response_synthesizer(
    response_mode="compact",
)
retriever_query_engine = RetrieverQueryEngine.from_args(
    recursive_retriever,
    response_synthesizer=response_synthesizer,
)

# convert the recursive retriever into a tool
query_engine_tools = [
    QueryEngineTool(
        query_engine=retriever_query_engine,
        metadata=ToolMetadata(
            name="index_node_agents",
            description="Tool for access the agent for each city"
        ),
    ),
]

# setup sub question query engine
top_query_engine = SubQuestionQueryEngine.from_defaults(
    query_engine_tools=query_engine_tools,
    use_async=True,
)

response = top_query_engine.query(
    "Tell me about the sports teams in Boston and the positive aspects of Seattle"
)