pinecone-haystack 6.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- haystack_integrations/components/retrievers/pinecone/__init__.py +3 -0
- haystack_integrations/components/retrievers/pinecone/embedding_retriever.py +173 -0
- haystack_integrations/components/retrievers/py.typed +0 -0
- haystack_integrations/document_stores/pinecone/__init__.py +6 -0
- haystack_integrations/document_stores/pinecone/document_store.py +1102 -0
- haystack_integrations/document_stores/pinecone/filters.py +191 -0
- haystack_integrations/document_stores/py.typed +0 -0
- pinecone_haystack-6.2.0.dist-info/METADATA +38 -0
- pinecone_haystack-6.2.0.dist-info/RECORD +10 -0
- pinecone_haystack-6.2.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai>
|
|
2
|
+
#
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
from haystack import component, default_from_dict, default_to_dict
|
|
7
|
+
from haystack.dataclasses import Document
|
|
8
|
+
from haystack.document_stores.types import FilterPolicy
|
|
9
|
+
from haystack.document_stores.types.filter_policy import apply_filter_policy
|
|
10
|
+
|
|
11
|
+
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@component
|
|
15
|
+
class PineconeEmbeddingRetriever:
|
|
16
|
+
"""
|
|
17
|
+
Retrieves documents from the `PineconeDocumentStore`, based on their dense embeddings.
|
|
18
|
+
|
|
19
|
+
Usage example:
|
|
20
|
+
```python
|
|
21
|
+
import os
|
|
22
|
+
from haystack.document_stores.types import DuplicatePolicy
|
|
23
|
+
from haystack import Document
|
|
24
|
+
from haystack import Pipeline
|
|
25
|
+
from haystack.components.embedders import SentenceTransformersTextEmbedder, SentenceTransformersDocumentEmbedder
|
|
26
|
+
from haystack_integrations.components.retrievers.pinecone import PineconeEmbeddingRetriever
|
|
27
|
+
from haystack_integrations.document_stores.pinecone import PineconeDocumentStore
|
|
28
|
+
|
|
29
|
+
os.environ["PINECONE_API_KEY"] = "YOUR_PINECONE_API_KEY"
|
|
30
|
+
document_store = PineconeDocumentStore(index="my_index", namespace="my_namespace", dimension=768)
|
|
31
|
+
|
|
32
|
+
documents = [Document(content="There are over 7,000 languages spoken around the world today."),
|
|
33
|
+
Document(content="Elephants have been observed to behave in a way that indicates..."),
|
|
34
|
+
Document(content="In certain places, you can witness the phenomenon of bioluminescent waves.")]
|
|
35
|
+
|
|
36
|
+
document_embedder = SentenceTransformersDocumentEmbedder()
|
|
37
|
+
document_embedder.warm_up()
|
|
38
|
+
documents_with_embeddings = document_embedder.run(documents)
|
|
39
|
+
|
|
40
|
+
document_store.write_documents(documents_with_embeddings.get("documents"), policy=DuplicatePolicy.OVERWRITE)
|
|
41
|
+
|
|
42
|
+
query_pipeline = Pipeline()
|
|
43
|
+
query_pipeline.add_component("text_embedder", SentenceTransformersTextEmbedder())
|
|
44
|
+
query_pipeline.add_component("retriever", PineconeEmbeddingRetriever(document_store=document_store))
|
|
45
|
+
query_pipeline.connect("text_embedder.embedding", "retriever.query_embedding")
|
|
46
|
+
|
|
47
|
+
query = "How many languages are there?"
|
|
48
|
+
|
|
49
|
+
res = query_pipeline.run({"text_embedder": {"text": query}})
|
|
50
|
+
assert res['retriever']['documents'][0].content == "There are over 7,000 languages spoken around the world today."
|
|
51
|
+
```
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
def __init__(
|
|
55
|
+
self,
|
|
56
|
+
*,
|
|
57
|
+
document_store: PineconeDocumentStore,
|
|
58
|
+
filters: dict[str, Any] | None = None,
|
|
59
|
+
top_k: int = 10,
|
|
60
|
+
filter_policy: str | FilterPolicy = FilterPolicy.REPLACE,
|
|
61
|
+
) -> None:
|
|
62
|
+
"""
|
|
63
|
+
Initialize the PineconeEmbeddingRetriever.
|
|
64
|
+
|
|
65
|
+
:param document_store: The Pinecone Document Store.
|
|
66
|
+
:param filters: Filters applied to the retrieved Documents.
|
|
67
|
+
:param top_k: Maximum number of Documents to return.
|
|
68
|
+
:param filter_policy: Policy to determine how filters are applied.
|
|
69
|
+
|
|
70
|
+
:raises ValueError: If `document_store` is not an instance of `PineconeDocumentStore`.
|
|
71
|
+
"""
|
|
72
|
+
if not isinstance(document_store, PineconeDocumentStore):
|
|
73
|
+
msg = "document_store must be an instance of PineconeDocumentStore"
|
|
74
|
+
raise ValueError(msg)
|
|
75
|
+
|
|
76
|
+
self.document_store = document_store
|
|
77
|
+
self.filters = filters or {}
|
|
78
|
+
self.top_k = top_k
|
|
79
|
+
self.filter_policy = (
|
|
80
|
+
filter_policy if isinstance(filter_policy, FilterPolicy) else FilterPolicy.from_str(filter_policy)
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
def to_dict(self) -> dict[str, Any]:
|
|
84
|
+
"""
|
|
85
|
+
Serializes the component to a dictionary.
|
|
86
|
+
|
|
87
|
+
:returns:
|
|
88
|
+
Dictionary with serialized data.
|
|
89
|
+
"""
|
|
90
|
+
return default_to_dict(
|
|
91
|
+
self,
|
|
92
|
+
filters=self.filters,
|
|
93
|
+
top_k=self.top_k,
|
|
94
|
+
filter_policy=self.filter_policy.value,
|
|
95
|
+
document_store=self.document_store.to_dict(),
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
@classmethod
|
|
99
|
+
def from_dict(cls, data: dict[str, Any]) -> "PineconeEmbeddingRetriever":
|
|
100
|
+
"""
|
|
101
|
+
Deserializes the component from a dictionary.
|
|
102
|
+
|
|
103
|
+
:param data:
|
|
104
|
+
Dictionary to deserialize from.
|
|
105
|
+
:returns:
|
|
106
|
+
Deserialized component.
|
|
107
|
+
"""
|
|
108
|
+
data["init_parameters"]["document_store"] = PineconeDocumentStore.from_dict(
|
|
109
|
+
data["init_parameters"]["document_store"]
|
|
110
|
+
)
|
|
111
|
+
# Pipelines serialized with old versions of the component might not
|
|
112
|
+
# have the filter_policy field.
|
|
113
|
+
if filter_policy := data["init_parameters"].get("filter_policy"):
|
|
114
|
+
data["init_parameters"]["filter_policy"] = FilterPolicy.from_str(filter_policy)
|
|
115
|
+
return default_from_dict(cls, data)
|
|
116
|
+
|
|
117
|
+
@component.output_types(documents=list[Document])
|
|
118
|
+
def run(
|
|
119
|
+
self,
|
|
120
|
+
query_embedding: list[float],
|
|
121
|
+
filters: dict[str, Any] | None = None,
|
|
122
|
+
top_k: int | None = None,
|
|
123
|
+
) -> dict[str, list[Document]]:
|
|
124
|
+
"""
|
|
125
|
+
Retrieve documents from the `PineconeDocumentStore`, based on their dense embeddings.
|
|
126
|
+
|
|
127
|
+
:param query_embedding: Embedding of the query.
|
|
128
|
+
:param filters: Filters applied to the retrieved Documents. The way runtime filters are applied depends on
|
|
129
|
+
the `filter_policy` chosen at retriever initialization. See init method docstring for more
|
|
130
|
+
details.
|
|
131
|
+
:param top_k: Maximum number of `Document`s to return.
|
|
132
|
+
|
|
133
|
+
:returns: List of Document similar to `query_embedding`.
|
|
134
|
+
"""
|
|
135
|
+
filters = apply_filter_policy(self.filter_policy, self.filters, filters)
|
|
136
|
+
|
|
137
|
+
top_k = top_k or self.top_k
|
|
138
|
+
|
|
139
|
+
docs = self.document_store._embedding_retrieval(
|
|
140
|
+
query_embedding=query_embedding,
|
|
141
|
+
filters=filters,
|
|
142
|
+
top_k=top_k,
|
|
143
|
+
)
|
|
144
|
+
return {"documents": docs}
|
|
145
|
+
|
|
146
|
+
@component.output_types(documents=list[Document])
|
|
147
|
+
async def run_async(
|
|
148
|
+
self,
|
|
149
|
+
query_embedding: list[float],
|
|
150
|
+
filters: dict[str, Any] | None = None,
|
|
151
|
+
top_k: int | None = None,
|
|
152
|
+
) -> dict[str, list[Document]]:
|
|
153
|
+
"""
|
|
154
|
+
Asynchronously retrieve documents from the `PineconeDocumentStore`, based on their dense embeddings.
|
|
155
|
+
|
|
156
|
+
:param query_embedding: Embedding of the query.
|
|
157
|
+
:param filters: Filters applied to the retrieved Documents. The way runtime filters are applied depends on
|
|
158
|
+
the `filter_policy` chosen at retriever initialization. See init method docstring for more
|
|
159
|
+
details.
|
|
160
|
+
:param top_k: Maximum number of `Document`s to return.
|
|
161
|
+
|
|
162
|
+
:returns: List of Document similar to `query_embedding`.
|
|
163
|
+
"""
|
|
164
|
+
filters = apply_filter_policy(self.filter_policy, self.filters, filters)
|
|
165
|
+
|
|
166
|
+
top_k = top_k or self.top_k
|
|
167
|
+
|
|
168
|
+
docs = await self.document_store._embedding_retrieval_async(
|
|
169
|
+
query_embedding=query_embedding,
|
|
170
|
+
filters=filters,
|
|
171
|
+
top_k=top_k,
|
|
172
|
+
)
|
|
173
|
+
return {"documents": docs}
|
|
File without changes
|