路由器(Routers)概念讲解
概念解释
路由器(Routers)是模块化的组件,它们接收用户查询和一组由元数据定义的“选择”,并返回一个或多个选定的选择。路由器可以单独使用(作为“选择器模块”),也可以作为查询引擎或检索器的一部分(例如,在其他查询引擎/检索器之上)。
路由器简单但功能强大,使用大型语言模型(LLMs)进行决策。它们可以用于以下用例以及更多:
- 从多种数据源中选择正确的数据源。
- 决定是进行总结(例如使用总结索引查询引擎)还是语义搜索(例如使用向量索引查询引擎)。
- 决定是否“尝试”一组选择并组合结果(使用多路由功能)。
核心路由器模块有以下形式:
- LLM选择器:将选择作为文本转储放入提示中,并使用LLM文本完成端点进行决策。
- Pydantic选择器:将选择作为Pydantic模式传递给函数调用端点,并返回Pydantic对象。
使用模式
定义选择器
定义路由器的核心是定义一个选择器。以下是一些使用LLM和Pydantic选择器的示例:
from llama_index.core.selectors import LLMSingleSelector, LLMMultiSelector
from 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()
作为查询引擎使用
路由器查询引擎由其他查询引擎作为工具组成。以下是一个示例:
from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core.selectors import PydanticSingleSelector
from llama_index.core.tools import QueryEngineTool
# define query engines
list_query_engine = ...
vector_query_engine = ...
# initialize tools
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",
)
# 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>")
作为检索器使用
类似地,路由器检索器由其他检索器作为工具组成。以下是一个示例:
from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core.selectors import PydanticSingleSelector
from llama_index.core.tools import RetrieverTool
# define indices
vector_index = ...
keyword_index = ...
# define retrievers
vector_retriever = vector_index.as_retriever()
keyword_retriever = keyword_index.as_retriever()
# initialize tools
vector_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 retriever
retriever = RouterRetriever(
selector=PydanticSingleSelector.from_defaults(llm=llm),
retriever_tools=[
list_tool,
vector_tool,
],
)
作为独立模块使用
你可以将选择器作为独立模块使用。定义选择为ToolMetadata列表或字符串列表。
from llama_index.core.tools import ToolMetadata
from llama_index.core.selectors import LLMSingleSelector
# choices as a list of tool metadata
choices = [
ToolMetadata(description="description for choice 1", name="choice_1"),
ToolMetadata(description="description for choice 2", name="choice_2"),
]
# choices as a list of strings
choices = [
"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)
拓展
路由器模块的设计理念是模块化和灵活性。通过使用不同的选择器(LLM或Pydantic),你可以根据具体需求选择最合适的决策方式。此外,路由器不仅可以作为查询引擎和检索器的一部分,还可以独立使用,为复杂的决策过程提供支持。
在实际应用中,路由器可以用于处理复杂的查询场景,例如在多个数据源中选择最合适的数据源,或者在不同的查询策略(如总结和语义搜索)之间进行选择。通过合理配置选择器和工具,可以大大提高查询的准确性和效率。
希望这些解释和示例能帮助你更好地理解和使用路由器模块。如果有任何问题或需要进一步的解释,请随时提问。