集成查询引擎指南
概述
在构建检索增强生成(RAG)应用程序时,通常需要尝试不同的查询管道(例如,top-k 检索、关键词搜索、知识图谱)。如果我们能够同时尝试多种策略,并让语言模型(LLM)评估每个查询的相关性并综合结果,那将会非常有用。
本指南展示了如何在《了不起的盖茨比》上进行集成检索。我们通过不同的分块大小和不同的索引进行集成检索。
安装依赖
首先,我们需要安装必要的库:
%pip install llama-index-llms-openai
!pip install llama-index
设置
在 Jupyter Notebook 中,我们需要应用 nest_asyncio
以允许嵌套的异步查询:
import nest_asyncio
nest_asyncio.apply()
下载数据
下载《了不起的盖茨比》的全文:
!wget 'https://raw.githubusercontent.com/jerryjliu/llama_index/main/examples/gatsby/gatsby_full.txt' -O 'gatsby_full.txt'
加载数据
我们将文档转换为节点,并插入到文档存储中:
from llama_index.core import SimpleDirectoryReader
documents = SimpleDirectoryReader(
input_files=["./gatsby_full.txt"]
).load_data()
定义查询引擎
初始化设置(设置分块大小):
from llama_index.llms.openai import OpenAI
from llama_index.core import Settings
Settings.llm = OpenAI(model="gpt-3.5-turbo")
Settings.chunk_size = 1024
nodes = Settings.node_parser.get_nodes_from_documents(documents)
初始化存储上下文(默认是内存中的):
from llama_index.core import StorageContext
storage_context = StorageContext.from_defaults()
storage_context.docstore.add_documents(nodes)
初始化关键词表索引和向量存储索引:
from llama_index.core import SimpleKeywordTableIndex, VectorStoreIndex
keyword_index = SimpleKeywordTableIndex(
nodes,
storage_context=storage_context,
show_progress=True,
)
vector_index = VectorStoreIndex(
nodes,
storage_context=storage_context,
show_progress=True,
)
定义查询模板:
from llama_index.core import PromptTemplate
QA_PROMPT_TMPL = (
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the context information and not prior knowledge, "
"answer the question. If the answer is not in the context, inform "
"the user that you can't answer the question - DO NOT MAKE UP AN ANSWER.\n"
"In addition to returning the answer, also return a relevance score as to "
"how relevant the answer is to the question. "
"Question: {query_str}\n"
"Answer (including relevance score): "
)
QA_PROMPT = PromptTemplate(QA_PROMPT_TMPL)
初始化关键词查询引擎和向量查询引擎:
keyword_query_engine = keyword_index.as_query_engine(
text_qa_template=QA_PROMPT
)
vector_query_engine = vector_index.as_query_engine(text_qa_template=QA_PROMPT)
测试查询引擎:
response = vector_query_engine.query(
"Describe and summarize the interactions between Gatsby and Daisy"
)
print(response)
response = keyword_query_engine.query(
"Describe and summarize the interactions between Gatsby and Daisy"
)
print(response)
定义路由器查询引擎
初始化查询引擎工具:
from llama_index.core.tools import QueryEngineTool
keyword_tool = QueryEngineTool.from_defaults(
query_engine=keyword_query_engine,
description="Useful for answering questions about this essay",
)
vector_tool = QueryEngineTool.from_defaults(
query_engine=vector_query_engine,
description="Useful for answering questions about this essay",
)
初始化路由器查询引擎:
from llama_index.core.query_engine import RouterQueryEngine
from llama_index.core.selectors import LLMSingleSelector, LLMMultiSelector
from llama_index.core.selectors import (
PydanticMultiSelector,
PydanticSingleSelector,
)
from llama_index.core.response_synthesizers import TreeSummarize
TREE_SUMMARIZE_PROMPT_TMPL = (
"Context information from multiple sources is below. Each source may or"
" may not have \na relevance score attached to"
" it.\n---------------------\n{context_str}\n---------------------\nGiven"
" the information from multiple sources and their associated relevance"
" scores (if provided) and not prior knowledge, answer the question. If"
" the answer is not in the context, inform the user that you can't answer"
" the question.\nQuestion: {query_str}\nAnswer: "
)
tree_summarize = TreeSummarize(
summary_template=PromptTemplate(TREE_SUMMARIZE_PROMPT_TMPL)
)
query_engine = RouterQueryEngine(
selector=LLMMultiSelector.from_defaults(),
query_engine_tools=[
keyword_tool,
vector_tool,
],
summarizer=tree_summarize,
)
实验查询
使用异步查询进行实验:
response = await query_engine.aquery(
"Describe and summarize the interactions between Gatsby and Daisy"
)
print(response)
response = await query_engine.aquery(
"What part of his past is Gatsby trying to recapture?"
)
print(response)
拓展内容
集成查询引擎的优势:
- 多样性:通过集成多种查询策略,可以提高检索结果的多样性和全面性。
- 鲁棒性:不同的查询策略可能在不同的场景下表现更好,集成查询引擎可以提高系统的鲁棒性。
- 自动化评估:通过 LLM 自动评估查询结果的相关性,可以减少人工干预,提高效率。
实际应用场景:
- 问答系统:在问答系统中,集成查询引擎可以提供更准确和全面的答案。
- 信息检索:在信息检索系统中,集成查询引擎可以提高检索结果的质量。
- 知识图谱:在知识图谱中,集成查询引擎可以提供更丰富的知识关联和推理。
通过这些详细的讲解和示例,学生们可以更好地理解和掌握集成查询引擎的定义与实现方法,从而在实际项目中高效地应用。