45 LlamaIndex中的简单融合检索器:多查询与多索引的结合

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

生成的查询:

  1. 设置chroma向量存储的步骤是什么?
  2. 配置chroma向量存储的最佳实践
  3. 设置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?"
)

生成的查询:

  1. 如何设置chroma向量存储?
  2. 创建chroma向量存储的分步指南。
  3. 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中结合多个查询和多个索引的检索结果,从而提升检索的效率和准确性。融合检索器不仅能够处理多个查询,还能生成相关查询并聚合结果,使得检索过程更加智能和高效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要重新演唱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值