路由器
路由器是接收用户查询和一组“选项”(由元数据定义)并返回一个或多个选定选项的模块。
它们可以单独使用(作为“选择器模块”),或用作查询引擎或检索器(例如,在其他查询引擎/检索器之上)。
它们是简单但功能强大的模块,利用大型语言模型实现决策能力。它们可用于以下用例及更多场景:
- 在多样化的数据源中选择合适的数据源
- 决定是进行摘要(例如使用摘要索引查询引擎)还是语义搜索(例如使用向量索引查询引擎)
- 决定是否一次性“尝试”多个选项并合并结果(使用多路由功能)。
核心路由器模块以以下形式存在:
- LLM选择器将选项作为文本转储放入提示中,并使用LLM文本补全端点来做出决策
- Pydantic 选择器将选项作为 Pydantic 模式传递给函数调用端点,并返回 Pydantic 对象
以下给出了一个使用我们路由模块作为查询引擎一部分的简单示例。
from llama_index.core.query_engine import RouterQueryEnginefrom llama_index.core.selectors import PydanticSingleSelectorfrom llama_index.core.tools import QueryEngineTool
list_tool = QueryEngineTool.from_defaults( query_engine=list_query_engine, description="Useful for summarization questions related to the data source",)vector_tool = QueryEngineTool.from_defaults( query_engine=vector_query_engine, description="Useful for retrieving specific context related to the data source",)
query_engine = RouterQueryEngine( selector=PydanticSingleSelector.from_defaults(), query_engine_tools=[ list_tool, vector_tool, ],)query_engine.query("<query>")定义“选择器”是定义路由器的核心。
您可以轻松地将我们的路由器用作查询引擎或检索器。在这些情况下,路由器将负责“选择”查询引擎或检索器,以将用户查询路由到相应的目标。
我们还重点介绍了用于检索增强路由的ToolRetrieverRouterQueryEngine - 这种情况适用于选项集合本身可能非常庞大且需要建立索引的场景。注意:这是一个测试版功能。
我们还重点展示了将我们的路由器作为独立模块使用。
以下给出了一些基于LLM和Pydantic的单选/多选器示例:
from llama_index.core.selectors import LLMSingleSelector, LLMMultiSelectorfrom llama_index.core.selectors import ( PydanticMultiSelector, PydanticSingleSelector,)
# pydantic selectors feed in pydantic objects to a function calling API# single selector (pydantic)selector = PydanticSingleSelector.from_defaults()# multi selector (pydantic)selector = PydanticMultiSelector.from_defaults()
# LLM selectors use text completion endpoints# single selector (LLM)selector = LLMSingleSelector.from_defaults()# multi selector (LLM)selector = LLMMultiSelector.from_defaults()一个 RouterQueryEngine 构建在其他查询引擎之上作为工具。
from llama_index.core.query_engine import RouterQueryEnginefrom llama_index.core.selectors import PydanticSingleSelectorfrom llama_index.core.selectors.pydantic_selectors import Pydanticfrom llama_index.core.tools import QueryEngineToolfrom llama_index.core import VectorStoreIndex, SummaryIndex
# define query engines...
# initialize toolslist_tool = QueryEngineTool.from_defaults( query_engine=list_query_engine, description="Useful for summarization questions related to the data source",)vector_tool = QueryEngineTool.from_defaults( query_engine=vector_query_engine, description="Useful for retrieving specific context related to the data source",)
# initialize router query engine (single selection, pydantic)query_engine = RouterQueryEngine( selector=PydanticSingleSelector.from_defaults(), query_engine_tools=[ list_tool, vector_tool, ],)query_engine.query("<query>")类似地,一个 RouterRetriever 由其他检索器作为工具组合而成。以下给出一个示例:
from llama_index.core.retrievers import RouterRetrieverfrom llama_index.core.selectors import PydanticSingleSelectorfrom llama_index.core.tools import RetrieverTool
# define indices...
# define retrieversvector_retriever = vector_index.as_retriever()keyword_retriever = keyword_index.as_retriever()
# initialize toolsvector_tool = RetrieverTool.from_defaults( retriever=vector_retriever, description="Useful for retrieving specific context from Paul Graham essay on What I Worked On.",)keyword_tool = RetrieverTool.from_defaults( retriever=keyword_retriever, description="Useful for retrieving specific context from Paul Graham essay on What I Worked On (using entities mentioned in query)",)
# define retrieverretriever = RouterRetriever( selector=PydanticSingleSelector.from_defaults(llm=llm), retriever_tools=[ list_tool, vector_tool, ],)您可以将选择器作为独立模块使用。将选项定义为 ToolMetadata 列表或字符串列表。
from llama_index.core.tools import ToolMetadatafrom llama_index.core.selectors import LLMSingleSelector
# choices as a list of tool metadatachoices = [ ToolMetadata(description="description for choice 1", name="choice_1"), ToolMetadata(description="description for choice 2", name="choice_2"),]
# choices as a list of stringschoices = [ "choice 1 - description for choice 1", "choice 2: description for choice 2",]
selector = LLMSingleSelector.from_defaults()selector_result = selector.select( choices, query="What's revenue growth for IBM in 2007?")print(selector_result.selections)更多示例: