-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathpinecone.py
353 lines (296 loc) · 12 KB
/
pinecone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import time
import logging
from typing import Optional, Dict, Union
from multiprocessing import cpu_count
from .index_host_store import IndexHostStore
from .pinecone_interface import PineconeDBControlInterface
from pinecone.config import PineconeConfig, ConfigBuilder
from pinecone.core.openapi.db_control.api.manage_indexes_api import ManageIndexesApi
from pinecone.openapi_support.api_client import ApiClient
from pinecone.utils import normalize_host, setup_openapi_client, PluginAware
from pinecone.core.openapi.db_control import API_VERSION
from pinecone.models import (
ServerlessSpec,
PodSpec,
IndexModel,
IndexList,
CollectionList,
IndexEmbed,
)
from .langchain_import_warnings import _build_langchain_attribute_error_message
from pinecone.utils import docslinks
from pinecone.data import _Index, _Inference, _IndexAsyncio
from pinecone.enums import (
Metric,
VectorType,
DeletionProtection,
PodType,
CloudProvider,
AwsRegion,
GcpRegion,
AzureRegion,
)
from .types import CreateIndexForModelEmbedTypedDict
from .request_factory import PineconeDBControlRequestFactory
logger = logging.getLogger(__name__)
""" @private """
class Pinecone(PluginAware, PineconeDBControlInterface):
"""
A client for interacting with Pinecone's vector database.
This class implements methods for managing and interacting with Pinecone resources
such as collections and indexes.
"""
def __init__(
self,
api_key: Optional[str] = None,
host: Optional[str] = None,
proxy_url: Optional[str] = None,
proxy_headers: Optional[Dict[str, str]] = None,
ssl_ca_certs: Optional[str] = None,
ssl_verify: Optional[bool] = None,
additional_headers: Optional[Dict[str, str]] = {},
pool_threads: Optional[int] = None,
**kwargs,
):
for deprecated_kwarg in {"config", "openapi_config", "index_api"}:
if deprecated_kwarg in kwargs:
raise NotImplementedError(
f"Passing {deprecated_kwarg} is no longer supported. Please pass individual settings such as proxy_url, proxy_headers, ssl_ca_certs, and ssl_verify directly to the Pinecone constructor as keyword arguments. See the README at {docslinks['README']} for examples."
)
self.config = PineconeConfig.build(
api_key=api_key,
host=host,
additional_headers=additional_headers,
proxy_url=proxy_url,
proxy_headers=proxy_headers,
ssl_ca_certs=ssl_ca_certs,
ssl_verify=ssl_verify,
**kwargs,
)
""" @private """
self.openapi_config = ConfigBuilder.build_openapi_config(self.config, **kwargs)
""" @private """
if pool_threads is None:
self.pool_threads = 5 * cpu_count()
""" @private """
else:
self.pool_threads = pool_threads
""" @private """
self._inference = None # Lazy initialization
""" @private """
self.index_api = setup_openapi_client(
api_client_klass=ApiClient,
api_klass=ManageIndexesApi,
config=self.config,
openapi_config=self.openapi_config,
pool_threads=pool_threads,
api_version=API_VERSION,
)
""" @private """
self.index_host_store = IndexHostStore()
""" @private """
# Initialize PluginAware first, which will then call PineconeDBControlInterface.__init__
super().__init__()
@property
def inference(self):
"""
Inference is a namespace where an instance of the `pinecone.data.features.inference.inference.Inference` class is lazily created and cached.
"""
if self._inference is None:
self._inference = _Inference(config=self.config, openapi_config=self.openapi_config)
return self._inference
def create_index(
self,
name: str,
spec: Union[Dict, ServerlessSpec, PodSpec],
dimension: Optional[int] = None,
metric: Optional[Union[Metric, str]] = Metric.COSINE,
timeout: Optional[int] = None,
deletion_protection: Optional[Union[DeletionProtection, str]] = DeletionProtection.DISABLED,
vector_type: Optional[Union[VectorType, str]] = VectorType.DENSE,
tags: Optional[Dict[str, str]] = None,
) -> IndexModel:
req = PineconeDBControlRequestFactory.create_index_request(
name=name,
spec=spec,
dimension=dimension,
metric=metric,
deletion_protection=deletion_protection,
vector_type=vector_type,
tags=tags,
)
resp = self.index_api.create_index(create_index_request=req)
if timeout == -1:
return IndexModel(resp)
return self.__poll_describe_index_until_ready(name, timeout)
def create_index_for_model(
self,
name: str,
cloud: Union[CloudProvider, str],
region: Union[AwsRegion, GcpRegion, AzureRegion, str],
embed: Union[IndexEmbed, CreateIndexForModelEmbedTypedDict],
tags: Optional[Dict[str, str]] = None,
deletion_protection: Optional[Union[DeletionProtection, str]] = DeletionProtection.DISABLED,
timeout: Optional[int] = None,
) -> IndexModel:
req = PineconeDBControlRequestFactory.create_index_for_model_request(
name=name,
cloud=cloud,
region=region,
embed=embed,
tags=tags,
deletion_protection=deletion_protection,
)
resp = self.index_api.create_index_for_model(req)
if timeout == -1:
return IndexModel(resp)
return self.__poll_describe_index_until_ready(name, timeout)
def __poll_describe_index_until_ready(self, name: str, timeout: Optional[int] = None):
description = None
def is_ready() -> bool:
nonlocal description
description = self.describe_index(name=name)
return description.status.ready
total_wait_time = 0
if timeout is None:
# Wait indefinitely
while not is_ready():
logger.debug(
f"Waiting for index {name} to be ready. Total wait time {total_wait_time} seconds."
)
total_wait_time += 5
time.sleep(5)
else:
# Wait for a maximum of timeout seconds
while not is_ready():
if timeout < 0:
logger.error(f"Index {name} is not ready. Timeout reached.")
link = docslinks["API_DESCRIBE_INDEX"]
timeout_msg = (
f"Please call describe_index() to confirm index status. See docs at {link}"
)
raise TimeoutError(timeout_msg)
logger.debug(
f"Waiting for index {name} to be ready. Total wait time: {total_wait_time}"
)
total_wait_time += 5
time.sleep(5)
timeout -= 5
return description
def delete_index(self, name: str, timeout: Optional[int] = None):
self.index_api.delete_index(name)
self.index_host_store.delete_host(self.config, name)
if timeout == -1:
return
if timeout is None:
while self.has_index(name):
time.sleep(5)
else:
while self.has_index(name) and timeout >= 0:
time.sleep(5)
timeout -= 5
if timeout and timeout < 0:
raise (
TimeoutError(
"Please call the list_indexes API ({}) to confirm if index is deleted".format(
"https://www.pinecone.io/docs/api/operation/list_indexes/"
)
)
)
def list_indexes(self) -> IndexList:
response = self.index_api.list_indexes()
return IndexList(response)
def describe_index(self, name: str) -> IndexModel:
api_instance = self.index_api
description = api_instance.describe_index(name)
host = description.host
self.index_host_store.set_host(self.config, name, host)
return IndexModel(description)
def has_index(self, name: str) -> bool:
if name in self.list_indexes().names():
return True
else:
return False
def configure_index(
self,
name: str,
replicas: Optional[int] = None,
pod_type: Optional[Union[PodType, str]] = None,
deletion_protection: Optional[Union[DeletionProtection, str]] = None,
tags: Optional[Dict[str, str]] = None,
):
api_instance = self.index_api
description = self.describe_index(name=name)
req = PineconeDBControlRequestFactory.configure_index_request(
description=description,
replicas=replicas,
pod_type=pod_type,
deletion_protection=deletion_protection,
tags=tags,
)
api_instance.configure_index(name, configure_index_request=req)
def create_collection(self, name: str, source: str) -> None:
req = PineconeDBControlRequestFactory.create_collection_request(name=name, source=source)
self.index_api.create_collection(create_collection_request=req)
def list_collections(self) -> CollectionList:
response = self.index_api.list_collections()
return CollectionList(response)
def delete_collection(self, name: str) -> None:
self.index_api.delete_collection(name)
def describe_collection(self, name: str):
return self.index_api.describe_collection(name).to_dict()
@staticmethod
def from_texts(*args, **kwargs):
"""@private"""
raise AttributeError(_build_langchain_attribute_error_message("from_texts"))
@staticmethod
def from_documents(*args, **kwargs):
"""@private"""
raise AttributeError(_build_langchain_attribute_error_message("from_documents"))
def Index(self, name: str = "", host: str = "", **kwargs):
if name == "" and host == "":
raise ValueError("Either name or host must be specified")
pt = kwargs.pop("pool_threads", None) or self.pool_threads
api_key = self.config.api_key
openapi_config = self.openapi_config
if host != "":
check_realistic_host(host)
# Use host url if it is provided
index_host = normalize_host(host)
else:
# Otherwise, get host url from describe_index using the index name
index_host = self.index_host_store.get_host(self.index_api, self.config, name)
return _Index(
host=index_host,
api_key=api_key,
pool_threads=pt,
openapi_config=openapi_config,
source_tag=self.config.source_tag,
**kwargs,
)
def IndexAsyncio(self, host: str, **kwargs):
api_key = self.config.api_key
openapi_config = self.openapi_config
if host is None or host == "":
raise ValueError("A host must be specified")
check_realistic_host(host)
index_host = normalize_host(host)
return _IndexAsyncio(
host=index_host,
api_key=api_key,
openapi_config=openapi_config,
source_tag=self.config.source_tag,
**kwargs,
)
def check_realistic_host(host: str) -> None:
"""@private
Checks whether a user-provided host string seems plausible.
Someone could erroneously pass an index name as the host by
mistake, and if they have done that we'd like to give them a
simple error message as feedback rather than attempting to
call the url and getting a more cryptic DNS resolution error.
"""
if "." not in host and "localhost" not in host:
raise ValueError(
f"You passed '{host}' as the host but this does not appear to be valid. Call describe_index() to confirm the host of the index."
)