检索器路由查询引擎
概述
在本教程中,我们定义了一个基于检索器(retriever)的路由查询引擎。检索器将选择一组节点,然后我们选择正确的查询引擎(QueryEngine)。
我们使用新的 ToolRetrieverRouterQueryEngine
类来实现这一点!
安装依赖
首先,我们需要安装 LlamaIndex:
!pip install llama-index
在 Jupyter Notebook 中,我们需要应用 nest_asyncio
以允许嵌套的异步查询:
import nest_asyncio
nest_asyncio.apply()
设置日志记录:
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
下载数据
下载 Paul Graham 的散文:
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
加载数据
将文档转换为节点,并插入到文档存储中:
from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
)
from llama_index.core import SummaryIndex
# 加载文档
documents = SimpleDirectoryReader("./data/paul_graham").load_data()
from llama_index.core import Settings
# 初始化设置(设置分块大小)
Settings.chunk_size = 1024
nodes = Settings.node_parser.get_nodes_from_documents(documents)
# 初始化存储上下文(默认是内存中的)
storage_context = StorageContext.from_defaults()
storage_context.docstore.add_documents(nodes)
定义摘要索引和向量索引
在相同的文档存储上构建摘要索引和向量索引:
summary_index = SummaryIndex(nodes, storage_context=storage_context)
vector_index = VectorStoreIndex(nodes, storage_context=storage_context)
定义查询引擎和工具
我们为每个索引定义一个查询引擎。然后使用 QueryEngineTool
包装这些查询引擎。
from llama_index.core.tools import QueryEngineTool
list_query_engine = summary_index.as_query_engine(
response_mode="tree_summarize", use_async=True
)
vector_query_engine = vector_index.as_query_engine(
response_mode="tree_summarize", use_async=True
)
list_tool = QueryEngineTool.from_defaults(
query_engine=list_query_engine,
description="Useful for questions asking for a biography of the author.",
)
vector_tool = QueryEngineTool.from_defaults(
query_engine=vector_query_engine,
description=(
"Useful for retrieving specific snippets from the author's life, like"
" his time in college, his time in YC, or more."
),
)
定义检索增强的路由查询引擎
我们定义一个路由查询引擎,该引擎通过检索机制增强,以帮助处理选项集过大的情况。
首先,我们在查询引擎工具集上定义一个 ObjectIndex
。然后使用 ToolRetrieverRouterQueryEngine
类,并传入一个 ObjectRetriever
对象。这个检索器可以在查询时动态检索相关的查询引擎。
from llama_index.core import VectorStoreIndex
from llama_index.core.objects import ObjectIndex
obj_index = ObjectIndex.from_objects(
[list_tool, vector_tool],
index_cls=VectorStoreIndex,
)
from llama_index.core.query_engine import ToolRetrieverRouterQueryEngine
query_engine = ToolRetrieverRouterQueryEngine(obj_index.as_retriever())
运行查询
运行查询以获取作者的传记:
response = query_engine.query("What is a biography of the author's life?")
print(str(response))
运行查询以获取作者在大学期间的活动:
response = query_engine.query("What did Paul Graham do during his time in college?")
print(str(response))
拓展内容
路由查询引擎的优势:
- 灵活性:可以根据查询内容动态选择合适的查询引擎,提高查询效率和准确性。
- 扩展性:可以轻松添加新的查询引擎和工具,而无需修改现有代码。
- 性能优化:通过选择最合适的查询引擎,可以减少不必要的计算和资源消耗。
实际应用场景:
- 问答系统:在问答系统中,可以根据问题的类型选择不同的查询引擎,提高回答的准确性。
- 信息检索:在信息检索系统中,可以根据用户的查询内容选择合适的检索策略,提高检索结果的质量。
- 知识图谱:在知识图谱中,可以根据查询的复杂度选择不同的查询引擎,提高查询效率。
通过这些详细的讲解和示例,学生们可以更好地理解和掌握检索器路由查询引擎的定义与实现方法,从而在实际项目中高效地应用。