Bases: BaseQueryEngine
Tool Retriever router query engine.
Selects a set of candidate query engines to execute a query.
Parameters:
Name |
Type |
Description |
Default |
retriever |
ObjectRetriever
|
A retriever that retrieves a set of
query engine tools.
|
required
|
summarizer |
Optional[TreeSummarize]
|
Tree summarizer to summarize sub-results.
|
None
|
Source code in llama-index-core/llama_index/core/query_engine/router_query_engine.py
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393 | class ToolRetrieverRouterQueryEngine(BaseQueryEngine):
"""Tool Retriever router query engine.
Selects a set of candidate query engines to execute a query.
Args:
retriever (ObjectRetriever): A retriever that retrieves a set of
query engine tools.
summarizer (Optional[TreeSummarize]): Tree summarizer to summarize sub-results.
"""
def __init__(
self,
retriever: ObjectRetriever[QueryEngineTool],
llm: Optional[LLM] = None,
summarizer: Optional[TreeSummarize] = None,
) -> None:
llm = llm or Settings.llm
self._summarizer = summarizer or TreeSummarize(
llm=llm,
summary_template=DEFAULT_TREE_SUMMARIZE_PROMPT_SEL,
)
self._retriever = retriever
super().__init__(Settings.callback_manager)
def _get_prompt_modules(self) -> PromptMixinType:
"""Get prompt sub-modules."""
# NOTE: don't include tools for now
return {"summarizer": self._summarizer}
def _query(self, query_bundle: QueryBundle) -> RESPONSE_TYPE:
with self.callback_manager.event(
CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str}
) as query_event:
query_engine_tools = self._retriever.retrieve(query_bundle)
responses = []
for query_engine_tool in query_engine_tools:
query_engine = query_engine_tool.query_engine
responses.append(query_engine.query(query_bundle))
if len(responses) > 1:
final_response = combine_responses(
self._summarizer, responses, query_bundle
)
else:
final_response = responses[0]
# add selected result
final_response.metadata = final_response.metadata or {}
final_response.metadata["retrieved_tools"] = query_engine_tools
query_event.on_end(payload={EventPayload.RESPONSE: final_response})
return final_response
async def _aquery(self, query_bundle: QueryBundle) -> RESPONSE_TYPE:
with self.callback_manager.event(
CBEventType.QUERY, payload={EventPayload.QUERY_STR: query_bundle.query_str}
) as query_event:
query_engine_tools = self._retriever.retrieve(query_bundle)
tasks = []
for query_engine_tool in query_engine_tools:
query_engine = query_engine_tool.query_engine
tasks.append(query_engine.aquery(query_bundle))
responses = run_async_tasks(tasks)
if len(responses) > 1:
final_response = await acombine_responses(
self._summarizer, responses, query_bundle
)
else:
final_response = responses[0]
# add selected result
final_response.metadata = final_response.metadata or {}
final_response.metadata["retrieved_tools"] = query_engine_tools
query_event.on_end(payload={EventPayload.RESPONSE: final_response})
return final_response
|