-
Is it possible to add a subquestion query engine to a query pipeline? In my example, I'm using a pandas query pipeline. I know how to add SubQuestionQueryEngine to a regular query by adding it to the query engine tool. However, I'm unsure how to do this for the pandas query pipeline I posted below. Please help. My pandas query pipeline sample:
This is the way I used to add my subquestionqueryengine to regular query engines:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Yes, it is possible to add a
Here's an example of how you can do this: from llama_index.core.query_pipeline import QueryPipeline as QP, InputComponent, Link
from llama_index.query_engine.sub_question_query_engine import SubQuestionQueryEngine
from llama_index.query_engine.query_engine_tool import QueryEngineTool
from llama_index.query_engine.base_question_generator import BaseQuestionGenerator
from llama_index.synthesizer.base_synthesizer import BaseSynthesizer
from llama_index.service_context import ServiceContext
# Setup base query engine as tool
query_engine_tools = [
QueryEngineTool(
query_engine=vector_query_engine,
metadata=ToolMetadata(
name="pg_essay",
description="Paul Graham essay on What I Worked On",
),
),
]
# Instantiate the SubQuestionQueryEngine
sub_question_query_engine = SubQuestionQueryEngine.from_defaults(
query_engine_tools=query_engine_tools,
question_gen=BaseQuestionGenerator(),
response_synthesizer=BaseSynthesizer(),
service_context=ServiceContext(),
use_async=True,
)
# Define your Pandas query pipeline
qp = QP(
modules={
"input": InputComponent(),
"pandas_prompt": pandas_prompt,
"llm1": llm,
"pandas_output_parser": pandas_output_parser,
"response_synthesis_prompt": response_synthesis_prompt,
"llm2": llm,
"sub_question_query_engine": sub_question_query_engine, # Add the SubQuestionQueryEngine
},
verbose=True,
)
# Add chains and links
qp.add_chain(["input", "pandas_prompt", "llm1", "pandas_output_parser"])
qp.add_links(
[
Link("input", "response_synthesis_prompt", dest_key="query_str"),
Link("llm1", "response_synthesis_prompt", dest_key="pandas_instructions"),
Link("pandas_output_parser", "response_synthesis_prompt", dest_key="pandas_output"),
]
)
# Add link from response synthesis prompt to llm2
qp.add_link("response_synthesis_prompt", "llm2")
# Add link to integrate SubQuestionQueryEngine
qp.add_link("response_synthesis_prompt", "sub_question_query_engine") In this example, the This approach allows you to leverage the capabilities of the |
Beta Was this translation helpful? Give feedback.
-
@dosu in your example, where you got 'query_engine=vector_query_engine'? I don't have that. All I have here is query pipeline and I like to add sub_question_query_engine to pandas query pipe line. Please help. |
Beta Was this translation helpful? Give feedback.
To get the
vector_query_engine
in the example, you can create it from aVectorStoreIndex
using the following code snippet:This
vector_query_engine
can then be used to addsub_question_query_engine
to a Pandas query pipeline as shown in the example: