LlamaIndex中的简单融合检索器:多查询与多索引的结合
在本示例中,我们将介绍如何结合多个查询和多个索引的检索结果。检索到的节点将作为所有查询和索引中的top-k返回,并处理任何节点的去重。
设置
对于本笔记本,我们将使用两个非常相似的文档页面,每个页面存储在一个单独的索引中。
from llama_index.core import SimpleDirectoryReader
documents_1 = SimpleDirectoryReader(
input_files=["../../community/integrations/vector_stores.md"]
).load_data()
documents_2 = SimpleDirectoryReader(
input_files=["../../module_guides/storing/vector_stores.md"]
).load_data()
创建向量存储索引:
from llama_index.core import VectorStoreIndex
index_1 = VectorStoreIndex.from_documents(documents_1)
index_2 = VectorStoreIndex.from_documents(documents_2)
融合索引!
在这一步中,我们将索引融合成一个单一的检索器。这个检索器还将通过生成与原始问题相关的额外查询来增强我们的查询,并聚合结果。
这个设置将查询4次,一次使用你的原始查询,并生成3个更多查询。
默认情况下,它使用以下提示生成额外查询:
QUERY_GEN_PROMPT = (
"You are a helpful assistant that generates multiple search queries based on a "
"single input query. Generate {num_queries} search queries, one on each line, "
"related to the following input query:\n"
"Query: {query}\n"
"Queries:\n"
)
创建融合检索器:
from llama_index.core.retrievers import QueryFusionRetriever
retriever = QueryFusionRetriever(
[index_1.as_retriever(), index_2.as_retriever()],
similarity_top_k=2,
num_queries=4, # 设置为1以禁用查询生成
use_async=True,
verbose=True,
# query_gen_prompt="...", # 我们可以在这里覆盖查询生成提示
)
# 应用嵌套异步以在笔记本中运行
import nest_asyncio
nest_asyncio.apply()
检索节点:
nodes_with_scores = retriever.retrieve("How do I setup a chroma vector store?")
生成的查询:
- 设置chroma向量存储的步骤是什么?
- 配置chroma向量存储的最佳实践
- 设置chroma向量存储时常见问题的故障排除
打印节点及其分数:
for node in nodes_with_scores:
print(f"Score: {node.score:.2f} - {node.text[:100]}...")
输出:
Score: 0.78 - # Vector Stores
Vector stores contain embedding vectors of ingested document chunks
(and sometimes ...
Score: 0.78 - # Using Vector Stores
LlamaIndex offers multiple integration points with vector stores / vector dat...
在查询引擎中使用!
现在,我们可以将我们的检索器插入查询引擎以合成自然语言响应。
创建查询引擎:
from llama_index.core.query_engine import RetrieverQueryEngine
query_engine = RetrieverQueryEngine.from_args(retriever)
查询并生成响应:
response = query_engine.query(
"How do I setup a chroma vector store? Can you give an example?"
)
生成的查询:
- 如何设置chroma向量存储?
- 创建chroma向量存储的分步指南。
- chroma向量存储设置和配置的示例。
显示响应:
from llama_index.core.response.notebook_utils import display_response
display_response(response)
最终响应:要设置Chroma向量存储,您需要按照以下步骤操作:
导入必要的库:
import chromadb
from llama_index.vector_stores.chroma import ChromaVectorStore
创建Chroma客户端:
chroma_client = chromadb.EphemeralClient()
chroma_collection = chroma_client.create_collection("quickstart")
构造向量存储:
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
以下是如何使用上述步骤设置Chroma向量存储的示例:
import chromadb
from llama_index.vector_stores.chroma import ChromaVectorStore
# 创建Chroma客户端
# EphemeralClient在内存中操作,PersistentClient也会保存到磁盘
chroma_client = chromadb.EphemeralClient()
chroma_collection = chroma_client.create_collection("quickstart")
# 构造向量存储
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
这个示例演示了如何创建Chroma客户端,创建名为“quickstart”的集合,然后使用该集合构造Chroma向量存储。
通过这些步骤,你可以在LlamaIndex中结合多个查询和多个索引的检索结果,从而提升检索的效率和准确性。融合检索器不仅能够处理多个查询,还能生成相关查询并聚合结果,使得检索过程更加智能和高效。