跳到内容

路由器#

概念#

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

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

它们是使用大型语言模型(LLM)进行决策的简单而强大的模块。它们可用于以下及更多用例:

  • 在各种数据源中选择正确的数据源
  • 决定是执行摘要(例如使用摘要索引查询引擎)还是语义搜索(例如使用向量索引查询引擎)
  • 决定是否同时“尝试”一组选项并合并结果(使用多路路由功能)。

核心路由器模块有以下几种形式:

  • 大型语言模型(LLM)选择器将选项作为文本内容放入提示中,并使用大型语言模型文本补全端点进行决策
  • Pydantic 选择器将选项作为 Pydantic schema 传递给函数调用端点,并返回 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` - 这适用于选项集本身可能非常大且可能需要建立索引的情况。注意:这是一个 Beta 功能。

我们还重点介绍了如何将我们的路由器用作独立模块。

定义选择器#

下面给出了一些基于大型语言模型(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()

用作查询引擎#

一个 `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

# define query engines
...

# 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>")

用作检索器#

类似地,一个 `RouterRetriever` 是构建在其他检索器之上作为工具使用的。下面给出了一个示例。

from llama_index.core.retrievers import RouterRetriever
from llama_index.core.selectors import PydanticSingleSelector
from llama_index.core.tools import RetrieverTool

# define indices
...

# 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)

更多示例