Skip to content

路由器#

概念#

路由器是接收用户查询和一组"选项"(由元数据定义)并返回一个或多个选定选项的模块。

它们可以单独使用(作为"选择器模块"),也可以作为查询引擎或检索器使用(例如在其他查询引擎/检索器之上)。

这些简单但功能强大的模块利用LLM进行决策,适用于以下用例及其他场景:

  • 在多样化数据源中选择正确的数据源
  • 决定是进行摘要(使用摘要索引查询引擎)还是语义搜索(使用向量索引查询引擎)
  • 决定是否"尝试"多个选项并组合结果(使用多路由功能)

核心路由器模块存在以下形式:

  • LLM选择器将选项作为文本转储到提示中,并使用LLM文本补全端点进行决策
  • Pydantic选择器将选项作为Pydantic模式传递到函数调用端点,并返回Pydantic对象

使用模式#

以下是将路由器模块作为查询引擎组成部分的简单示例:

from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core.selectors import PydanticSingleSelector
from 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, LLMMultiSelector
from llama_index.core.selectors import (
    PydanticMultiSelector,
    PydanticSingleSelector,
)

# pydantic选择器将pydantic对象输入函数调用API
# 单选选择器(pydantic)
selector = PydanticSingleSelector.from_defaults()
# 多选选择器(pydantic)
selector = PydanticMultiSelector.from_defaults()

# LLM选择器使用文本补全端点
# 单选选择器(LLM)
selector = LLMSingleSelector.from_defaults()
# 多选选择器(LLM)
selector = LLMMultiSelector.from_defaults()

作为查询引擎使用#

RouterQueryEngine由作为工具的其他查询引擎组成。

from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core.selectors import PydanticSingleSelector
from llama_index.core.selectors.pydantic_selectors import Pydantic
from llama_index.core.tools import QueryEngineTool
from llama_index.core import VectorStoreIndex, SummaryIndex

# 定义查询引擎
...

# 初始化工具
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",
)

# 初始化路由器查询引擎(单选,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 RouterRetriever
from llama_index.core.selectors import PydanticSingleSelector
from llama_index.core.tools import RetrieverTool

# 定义索引
...

# 定义检索器
vector_retriever = vector_index.as_retriever()
keyword_retriever = keyword_index.as_retriever()

# 初始化工具
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)",
)

# 定义检索器
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 = [
    ToolMetadata(description="description for choice 1", name="choice_1"),
    ToolMetadata(description="description for choice 2", name="choice_2"),
]

# 选项作为字符串列表
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)

更多示例: