文档API 参考📓 教程🧑‍🍳 食谱🤝 集成💜 Discord🎨 Studio
文档

PineconeEmbeddingRetriever

一个与 Pinecone Document Store 兼容的基于嵌入的 Retriever。

pipeline 中的最常见位置1. 在 Text Embedder 之后,在 RAG pipeline 的 PromptBuilder 之前 2. 语义搜索 pipeline 中的最后一个组件 3. 在 Text Embedder 之后,在 extractive QA pipeline 的 ExtractiveReader 之前
必需的初始化变量"document_store": PineconeDocumentStore 的实例
强制运行变量“query_embedding”: 代表查询的向量(一个浮点数列表)
输出变量“documents”:文档列表
API 参考Pinecone
GitHub 链接https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/pinecone

概述

PineconeEmbeddingRetriever 是一个与PineconeDocumentStore 兼容的基于嵌入的 Retriever。它会比较查询和 Document 的嵌入,并根据结果从PineconeDocumentStore 中获取与查询最相关的 Document。

使用要在您的 NLP 系统中使用 PineconeEmbeddingRetriever,请确保您拥有查询和 Document 的嵌入。您可以通过在索引 Pipeline 中添加 Document Embedder,并在查询 Pipeline 中添加 Text Embedder 来实现此目的。

除了query_embedding 之外,PineconeEmbeddingRetriever 还接受其他可选参数,包括top_k(要检索的文档的最大数量)和filters(用于缩小搜索范围)。

在初始化相应的PineconeDocumentStore 时,必须定义影响嵌入检索的一些相关参数:这些参数包括嵌入的dimension 和要使用的距离metric

用法

单独使用

此检索器需要运行 PineconeDocumentStore 和索引的 Documents。

from haystack_integrations.components.retrievers.pinecone import PineconeEmbeddingRetriever
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore


# Make sure you have the PINECONE_API_KEY environment variable set
document_store = PineconeDocumentStore(index="my_index_with_documents",
																			 namespace="my_namespace",
                                       dimension=768)

retriever = PineconeEmbeddingRetriever(document_store=document_store)

# using an imaginary vector to keep the example simple, example run query:
retriever.run(query_embedding=[0.1]*768)

在 pipeline 中

安装您需要的依赖项

pip install pinecone-haystack
pip install sentence-transformers

像这样在查询管道中使用此 Retriever

from haystack.document_stores.types import DuplicatePolicy
from haystack import Document
from haystack import Pipeline
from haystack.components.embedders import SentenceTransformersTextEmbedder, SentenceTransformersDocumentEmbedder
from haystack_integrations.components.retrievers.pinecone import PineconeEmbeddingRetriever
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore


# Make sure you have the PINECONE_API_KEY environment variable set
document_store = PineconeDocumentStore(index="my_index",
																			 namespace="my_namespace",
                                       dimension=768)

documents = [Document(content="There are over 7,000 languages spoken around the world today."),
						Document(content="Elephants have been observed to behave in a way that indicates a high level of self-awareness, such as recognizing themselves in mirrors."),
						Document(content="In certain parts of the world, like the Maldives, Puerto Rico, and San Diego, you can witness the phenomenon of bioluminescent waves.")]

document_embedder = SentenceTransformersDocumentEmbedder()  
document_embedder.warm_up()
documents_with_embeddings = document_embedder.run(documents)

document_store.write_documents(documents_with_embeddings.get("documents"), policy=DuplicatePolicy.OVERWRITE)

query_pipeline = Pipeline()
query_pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder())
query_pipeline.add_component("retriever", PineconeEmbeddingRetriever(document_store=document_store))
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")

query = "How many languages are there?"

result = query_pipeline.run({"text_embedder": {"text": query}})

print(result['retriever']['documents'][0])

示例输出将是:

Document(id=cfe93bc1c274908801e6670440bf2bbba54fad792770d57421f85ffa2a4fcc94, content: 'There are over 7,000 languages spoken around the world today.', score: 0.87717235, embedding: vector of size 768)

相关链接

请查看 GitHub 仓库或我们的文档中的 API 参考