nucliadb-utils 5.0.0.post645__py3-none-any.whl → 5.0.0.post646__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.
@@ -0,0 +1,19 @@
1
+ # Copyright (C) 2021 Bosutech XXI S.L.
2
+ #
3
+ # nucliadb is offered under the AGPL v3.0 and as commercial software.
4
+ # For commercial licensing, contact us at info@nuclia.com.
5
+ #
6
+ # AGPL:
7
+ # This program is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Affero General Public License as
9
+ # published by the Free Software Foundation, either version 3 of the
10
+ # License, or (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Affero General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Affero General Public License
18
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+ #
@@ -0,0 +1,84 @@
1
+ # Copyright (C) 2021 Bosutech XXI S.L.
2
+ #
3
+ # nucliadb is offered under the AGPL v3.0 and as commercial software.
4
+ # For commercial licensing, contact us at info@nuclia.com.
5
+ #
6
+ # AGPL:
7
+ # This program is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Affero General Public License as
9
+ # published by the Free Software Foundation, either version 3 of the
10
+ # License, or (at your option) any later version.
11
+ #
12
+ # This program is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Affero General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Affero General Public License
18
+ # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
+ #
20
+ import logging
21
+
22
+ import httpx
23
+
24
+ from nucliadb_telemetry.metrics import Observer
25
+
26
+ logger = logging.getLogger(__name__)
27
+
28
+ pinecone_observer = Observer(
29
+ "pinecone_client",
30
+ labels={"type": ""},
31
+ )
32
+
33
+ BASE_URL = "https://api.pinecone.io/"
34
+
35
+
36
+ class PineconeClient:
37
+ def __init__(self, api_key: str, http_session: httpx.AsyncClient):
38
+ self.api_key = api_key
39
+ self.session = http_session
40
+
41
+ @pinecone_observer.wrap({"type": "create_index"})
42
+ async def create_index(self, name: str, dimension: int) -> str:
43
+ payload = {
44
+ "name": name,
45
+ "dimension": dimension,
46
+ "metric": "dotproduct",
47
+ "spec": {"serverless": {"cloud": "aws", "region": "us-east-1"}},
48
+ }
49
+ headers = {"Api-Key": self.api_key}
50
+ response = await self.session.post("/indexes", json=payload, headers=headers)
51
+ response.raise_for_status()
52
+ response_json = response.json()
53
+ return response_json["host"]
54
+
55
+ @pinecone_observer.wrap({"type": "delete_index"})
56
+ async def delete_index(self, name: str) -> None:
57
+ headers = {"Api-Key": self.api_key}
58
+ response = await self.session.delete(f"/indexes/{name}", headers=headers)
59
+ if response.status_code == 404:
60
+ logger.warning("Pinecone index not found.", extra={"index_name": name})
61
+ return
62
+ response.raise_for_status()
63
+ return
64
+
65
+
66
+ class PineconeSession:
67
+ """
68
+ Wrapper that manages the singletone session around all Pinecone http api interactions.
69
+ """
70
+
71
+ def __init__(self):
72
+ self.headers = {
73
+ "Content-Type": "application/json",
74
+ "Accept": "application/json",
75
+ }
76
+ self.http_session = httpx.AsyncClient(base_url=BASE_URL, headers=self.headers)
77
+
78
+ async def finalize(self):
79
+ if self.http_session.is_closed:
80
+ return
81
+ await self.http_session.aclose()
82
+
83
+ def get_client(self, api_key: str) -> PineconeClient:
84
+ return PineconeClient(api_key=api_key, http_session=self.http_session)
@@ -28,6 +28,7 @@ from typing import TYPE_CHECKING, Any, List, Optional, Union, cast
28
28
 
29
29
  from nucliadb_protos.writer_pb2_grpc import WriterStub
30
30
  from nucliadb_utils import featureflagging
31
+ from nucliadb_utils.aiopynecone.client import PineconeSession
31
32
  from nucliadb_utils.audit.audit import AuditStorage
32
33
  from nucliadb_utils.audit.basic import BasicAuditStorage
33
34
  from nucliadb_utils.audit.stream import StreamAuditStorage
@@ -81,6 +82,7 @@ class Utility(str, Enum):
81
82
  NUCLIA_STORAGE = "nuclia_storage"
82
83
  MAINDB_DRIVER = "driver"
83
84
  ENDECRYPTOR = "endecryptor"
85
+ PINECONE_SESSION = "pinecone_session"
84
86
 
85
87
 
86
88
  def get_utility(ident: Union[Utility, str]):
@@ -425,3 +427,12 @@ def get_endecryptor() -> EndecryptorUtility:
425
427
  ) from ex
426
428
  set_utility(Utility.ENDECRYPTOR, util)
427
429
  return util
430
+
431
+
432
+ def get_pinecone_session() -> Any:
433
+ util = get_utility(Utility.PINECONE_SESSION)
434
+ if util is not None:
435
+ return util
436
+ util = PineconeSession()
437
+ set_utility(Utility.PINECONE_SESSION, util)
438
+ return util
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: nucliadb_utils
3
- Version: 5.0.0.post645
3
+ Version: 5.0.0.post646
4
4
  Home-page: https://nuclia.com
5
5
  License: BSD
6
6
  Classifier: Development Status :: 4 - Beta
@@ -23,8 +23,8 @@ Requires-Dist: PyNaCl
23
23
  Requires-Dist: pyjwt >=2.4.0
24
24
  Requires-Dist: memorylru >=1.1.2
25
25
  Requires-Dist: mrflagly
26
- Requires-Dist: nucliadb-protos >=5.0.0.post645
27
- Requires-Dist: nucliadb-telemetry >=5.0.0.post645
26
+ Requires-Dist: nucliadb-protos >=5.0.0.post646
27
+ Requires-Dist: nucliadb-telemetry >=5.0.0.post646
28
28
  Provides-Extra: cache
29
29
  Requires-Dist: redis >=4.3.4 ; extra == 'cache'
30
30
  Requires-Dist: orjson >=3.6.7 ; extra == 'cache'
@@ -16,7 +16,9 @@ nucliadb_utils/settings.py,sha256=AaOtQZVRqRcMnUyN1l1MpR10lANaDT2uPrbhmTyn6uk,76
16
16
  nucliadb_utils/signals.py,sha256=JRNv2y9zLtBjOANBf7krGfDGfOc9qcoXZ6N1nKWS2FE,2674
17
17
  nucliadb_utils/store.py,sha256=kQ35HemE0v4_Qg6xVqNIJi8vSFAYQtwI3rDtMsNy62Y,890
18
18
  nucliadb_utils/transaction.py,sha256=mwcI3aIHAvU5KOGqd_Uz_d1XQzXhk_-NWY8NqU1lfb0,7307
19
- nucliadb_utils/utilities.py,sha256=ABcv4PwRYlrFOFOJF-1TU8PVbmW548g7scs-dsEJDp0,14955
19
+ nucliadb_utils/utilities.py,sha256=-oZne4Y8AqtN2-2ciWcLagtB6vbl-2HhEqzO_fHbIOU,15283
20
+ nucliadb_utils/aiopynecone/__init__.py,sha256=cp15ZcFnHvpcu_5-aK2A4uUyvuZVV_MJn4bIXMa20ks,835
21
+ nucliadb_utils/aiopynecone/client.py,sha256=td21RcCkiI3TZxtNoLZc70rP3FDoawZZE0Z_u8HEn9w,2870
20
22
  nucliadb_utils/audit/__init__.py,sha256=cp15ZcFnHvpcu_5-aK2A4uUyvuZVV_MJn4bIXMa20ks,835
21
23
  nucliadb_utils/audit/audit.py,sha256=dn5ZnCVQUlCcvdjzaORghbrjk9QgVGrtkfIftq30Bp8,2819
22
24
  nucliadb_utils/audit/basic.py,sha256=NViey6mKbCXqRTLDBX2xNTcCg9I-2e4oB2xkekuhDvM,3392
@@ -60,8 +62,8 @@ nucliadb_utils/tests/indexing.py,sha256=YW2QhkhO9Q_8A4kKWJaWSvXvyQ_AiAwY1VylcfVQ
60
62
  nucliadb_utils/tests/local.py,sha256=c3gZJJWmvOftruJkIQIwB3q_hh3uxEhqGIAVWim1Bbk,1343
61
63
  nucliadb_utils/tests/nats.py,sha256=Tosonm9A9cusImyji80G4pgdXEHNVPaCLT5TbFK_ra0,7543
62
64
  nucliadb_utils/tests/s3.py,sha256=YB8QqDaBXxyhHonEHmeBbRRDmvB7sTOaKBSi8KBGokg,2330
63
- nucliadb_utils-5.0.0.post645.dist-info/METADATA,sha256=uaEkng18pvQi3A1bqfVt3bJfGDyQNxIU9fx-xcq_S-k,2078
64
- nucliadb_utils-5.0.0.post645.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
65
- nucliadb_utils-5.0.0.post645.dist-info/top_level.txt,sha256=fE3vJtALTfgh7bcAWcNhcfXkNPp_eVVpbKK-2IYua3E,15
66
- nucliadb_utils-5.0.0.post645.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
67
- nucliadb_utils-5.0.0.post645.dist-info/RECORD,,
65
+ nucliadb_utils-5.0.0.post646.dist-info/METADATA,sha256=ZAoYGu2Cfe1IS9EfS7XPt3Jhr6ACMed4Ifuvm_QMmpM,2078
66
+ nucliadb_utils-5.0.0.post646.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
67
+ nucliadb_utils-5.0.0.post646.dist-info/top_level.txt,sha256=fE3vJtALTfgh7bcAWcNhcfXkNPp_eVVpbKK-2IYua3E,15
68
+ nucliadb_utils-5.0.0.post646.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
69
+ nucliadb_utils-5.0.0.post646.dist-info/RECORD,,