personal_knowledge_library 3.0.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.

Potentially problematic release.


This version of personal_knowledge_library might be problematic. Click here for more details.

Files changed (42) hide show
  1. knowledge/__init__.py +91 -0
  2. knowledge/base/__init__.py +22 -0
  3. knowledge/base/access.py +167 -0
  4. knowledge/base/entity.py +267 -0
  5. knowledge/base/language.py +27 -0
  6. knowledge/base/ontology.py +2734 -0
  7. knowledge/base/search.py +473 -0
  8. knowledge/base/tenant.py +192 -0
  9. knowledge/nel/__init__.py +11 -0
  10. knowledge/nel/base.py +495 -0
  11. knowledge/nel/engine.py +123 -0
  12. knowledge/ontomapping/__init__.py +667 -0
  13. knowledge/ontomapping/manager.py +320 -0
  14. knowledge/public/__init__.py +27 -0
  15. knowledge/public/cache.py +115 -0
  16. knowledge/public/helper.py +373 -0
  17. knowledge/public/relations.py +128 -0
  18. knowledge/public/wikidata.py +1324 -0
  19. knowledge/services/__init__.py +128 -0
  20. knowledge/services/asyncio/__init__.py +7 -0
  21. knowledge/services/asyncio/base.py +458 -0
  22. knowledge/services/asyncio/graph.py +1420 -0
  23. knowledge/services/asyncio/group.py +450 -0
  24. knowledge/services/asyncio/search.py +439 -0
  25. knowledge/services/asyncio/users.py +270 -0
  26. knowledge/services/base.py +533 -0
  27. knowledge/services/graph.py +1897 -0
  28. knowledge/services/group.py +819 -0
  29. knowledge/services/helper.py +142 -0
  30. knowledge/services/ontology.py +1234 -0
  31. knowledge/services/search.py +488 -0
  32. knowledge/services/session.py +444 -0
  33. knowledge/services/tenant.py +281 -0
  34. knowledge/services/users.py +445 -0
  35. knowledge/utils/__init__.py +10 -0
  36. knowledge/utils/graph.py +417 -0
  37. knowledge/utils/wikidata.py +197 -0
  38. knowledge/utils/wikipedia.py +175 -0
  39. personal_knowledge_library-3.0.0.dist-info/LICENSE +201 -0
  40. personal_knowledge_library-3.0.0.dist-info/METADATA +1163 -0
  41. personal_knowledge_library-3.0.0.dist-info/RECORD +42 -0
  42. personal_knowledge_library-3.0.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,281 @@
1
+ # -*- coding: utf-8 -*-
2
+ # Copyright © 2021-present Wacom. All rights reserved.
3
+ from typing import List, Dict, Optional
4
+
5
+ import requests
6
+ from requests import Response
7
+ from requests.adapters import HTTPAdapter
8
+ from urllib3 import Retry
9
+
10
+ from knowledge.base.tenant import TenantConfiguration
11
+ from knowledge.services import DEFAULT_TIMEOUT, DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_FACTOR, STATUS_FORCE_LIST
12
+ from knowledge.services.base import (
13
+ WacomServiceAPIClient,
14
+ USER_AGENT_HEADER_FLAG,
15
+ CONTENT_TYPE_HEADER_FLAG,
16
+ handle_error,
17
+ )
18
+ from knowledge.services.graph import AUTHORIZATION_HEADER_FLAG
19
+
20
+
21
+ class TenantManagementServiceAPI(WacomServiceAPIClient):
22
+ """
23
+ Tenant Management Service API
24
+ -----------------------------
25
+
26
+ Functionality:
27
+ - List all tenants
28
+ - Create tenants
29
+
30
+ This is service is used to manage tenants. Only admins can use this service, as it requires the secret key for
31
+ tenant administration.
32
+
33
+ Parameters
34
+ ----------
35
+ tenant_token: str
36
+ Tenant Management token
37
+ service_url: str
38
+ URL of the service
39
+ service_endpoint: str
40
+ Base endpoint
41
+ """
42
+
43
+ TENANT_ENDPOINT: str = "tenant"
44
+ USER_DETAILS_ENDPOINT: str = f"{WacomServiceAPIClient.USER_ENDPOINT}/users"
45
+
46
+ def __init__(
47
+ self,
48
+ tenant_token: str,
49
+ service_url: str = WacomServiceAPIClient.SERVICE_URL,
50
+ service_endpoint: str = "graph/v1",
51
+ ):
52
+ self.__tenant_management_token: str = tenant_token
53
+ super().__init__("TenantManagementServiceAPI", service_url=service_url, service_endpoint=service_endpoint)
54
+
55
+ @property
56
+ def tenant_management_token(self) -> str:
57
+ """Tenant Management token."""
58
+ return self.__tenant_management_token
59
+
60
+ @tenant_management_token.setter
61
+ def tenant_management_token(self, value: str):
62
+ self.__tenant_management_token = value
63
+
64
+ # ------------------------------------------ Tenants handling ------------------------------------------------------
65
+
66
+ def create_tenant(
67
+ self,
68
+ name: str,
69
+ create_and_apply_onto: bool = True,
70
+ rights: Optional[List[str]] = None,
71
+ vector_search_data_properties: Optional[List[str]] = None,
72
+ vector_search_object_properties: Optional[List[str]] = None,
73
+ content_data_property_name: str = "",
74
+ timeout: int = DEFAULT_TIMEOUT,
75
+ max_retries: int = DEFAULT_MAX_RETRIES,
76
+ backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
77
+ ) -> Dict[str, str]:
78
+ """
79
+ Creates a tenant.
80
+
81
+ Parameters
82
+ ----------
83
+ name: str -
84
+ Name of the tenant
85
+ create_and_apply_onto: bool
86
+ Creates and applies the ontology.
87
+ rights: List[str]
88
+ List of rights for the tenant. They are encoded in the user token, e.g., "ink-to-text"
89
+ vector_search_data_properties: List[str]
90
+ List of data properties that are automatically added to meta-data of the vector search index documents.
91
+ vector_search_object_properties: List[str]
92
+ List of object properties that are automatically added to meta-data of the vector search index documents.
93
+ content_data_property_name: str
94
+ The data property that is used to indexing its content to the document index.
95
+ timeout: int
96
+ Timeout for the request (default: 60 seconds)
97
+ max_retries: int
98
+ Maximum number of retries
99
+ backoff_factor: float
100
+ A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
101
+ second try without a delay)
102
+
103
+ Returns
104
+ -------
105
+ tenant_dict: Dict[str, str]
106
+ Newly created tenant structure.
107
+ >>> {
108
+ >>> "id": "<Tenant-ID>",
109
+ >>> "apiKey": "<Tenant-API-Key>",
110
+ >>> "name": "<Tenant-Name>"
111
+ >>> }
112
+
113
+ Raises
114
+ ------
115
+ WacomServiceException
116
+ If the tenant service returns an error code.
117
+ """
118
+ url: str = f"{self.service_base_url}{TenantManagementServiceAPI.TENANT_ENDPOINT}"
119
+ headers: dict = {
120
+ USER_AGENT_HEADER_FLAG: self.user_agent,
121
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {self.__tenant_management_token}",
122
+ CONTENT_TYPE_HEADER_FLAG: "application/json",
123
+ }
124
+ payload: dict = {
125
+ "name": name,
126
+ "rights": rights if rights else [],
127
+ "vectorSearchDataProperties": vector_search_data_properties if vector_search_object_properties else [],
128
+ "vectorSearchObjectProperties": vector_search_object_properties if vector_search_object_properties else [],
129
+ "contentDataPropertyName": content_data_property_name,
130
+ }
131
+ params: dict = {"createAndApplyOnto": create_and_apply_onto}
132
+ mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
133
+ with requests.Session() as session:
134
+ retries: Retry = Retry(total=max_retries, backoff_factor=backoff_factor, status_forcelist=STATUS_FORCE_LIST)
135
+ session.mount(mount_point, HTTPAdapter(max_retries=retries))
136
+ response: Response = session.post(
137
+ url, headers=headers, json=payload, params=params, timeout=timeout, verify=self.verify_calls
138
+ )
139
+ if response.ok:
140
+ return response.json()
141
+ raise handle_error("Creation of tenant failed.", response)
142
+
143
+ def listing_tenant(
144
+ self,
145
+ timeout: int = DEFAULT_TIMEOUT,
146
+ max_retries: int = DEFAULT_MAX_RETRIES,
147
+ backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
148
+ ) -> List[TenantConfiguration]:
149
+ """
150
+ Listing all tenants configured for this instance.
151
+
152
+ Parameters
153
+ ----------
154
+ timeout: int
155
+ Timeout for the request (default: 60 seconds)
156
+ max_retries: int
157
+ Maximum number of retries
158
+ backoff_factor: float
159
+ A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
160
+ second try without a delay)
161
+
162
+ Returns
163
+ -------
164
+ tenants: List[TenantConfiguration]
165
+ List of tenants
166
+
167
+ Raises
168
+ ------
169
+ WacomServiceException
170
+ If the tenant service returns an error code.
171
+ """
172
+ url: str = f"{self.service_base_url}{TenantManagementServiceAPI.TENANT_ENDPOINT}"
173
+ headers: dict = {
174
+ USER_AGENT_HEADER_FLAG: self.user_agent,
175
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {self.__tenant_management_token}",
176
+ }
177
+ mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
178
+ with requests.Session() as session:
179
+ retries: Retry = Retry(total=max_retries, backoff_factor=backoff_factor, status_forcelist=STATUS_FORCE_LIST)
180
+ session.mount(mount_point, HTTPAdapter(max_retries=retries))
181
+ response: Response = session.get(url, headers=headers, data={}, timeout=timeout, verify=self.verify_calls)
182
+ if response.ok:
183
+ return [TenantConfiguration.from_dict(tenant) for tenant in response.json()]
184
+ raise handle_error("Listing of tenant failed.", response)
185
+
186
+ def update_tenant_configuration(
187
+ self,
188
+ identifier: str,
189
+ rights: List[str],
190
+ vector_search_data_properties: List[str],
191
+ vector_search_object_properties: List[str],
192
+ content_data_property_name: str,
193
+ timeout: int = DEFAULT_TIMEOUT,
194
+ max_retries: int = DEFAULT_MAX_RETRIES,
195
+ backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
196
+ ):
197
+ """
198
+ Update the configuration of a tenant.
199
+
200
+ Parameters
201
+ ----------
202
+ identifier: str
203
+ Tenant identifier.
204
+ rights: List[str]
205
+ List of rights for the tenant. They are encoded in the user token, e.g., "ink-to-text"
206
+ vector_search_data_properties: List[str]
207
+ List of data properties that are automatically added to meta-data of the vector search index documents.
208
+ vector_search_object_properties: List[str]
209
+ List of object properties that are automatically added to meta-data of the vector search index documents.
210
+ content_data_property_name: str
211
+ The data property that is used to indexing its content to the document index.
212
+ timeout: int
213
+ Timeout for the request (default: 60 seconds)
214
+ max_retries: int
215
+ Maximum number of retries
216
+ backoff_factor: float
217
+ A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
218
+ second try without a delay)
219
+ """
220
+ url: str = f"{self.service_base_url}{TenantManagementServiceAPI.TENANT_ENDPOINT}/{identifier}/rights"
221
+ headers: dict = {
222
+ USER_AGENT_HEADER_FLAG: self.user_agent,
223
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {self.__tenant_management_token}",
224
+ CONTENT_TYPE_HEADER_FLAG: "application/json",
225
+ }
226
+ payload: dict = {
227
+ "rights": rights,
228
+ "vectorSearchDataProperties": vector_search_data_properties,
229
+ "vectorSearchObjectProperties": vector_search_object_properties,
230
+ "contentDataPropertyName": content_data_property_name,
231
+ }
232
+ mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
233
+ with requests.Session() as session:
234
+ retries: Retry = Retry(total=max_retries, backoff_factor=backoff_factor, status_forcelist=STATUS_FORCE_LIST)
235
+ session.mount(mount_point, HTTPAdapter(max_retries=retries))
236
+ response: Response = session.patch(
237
+ url, headers=headers, json=payload, timeout=timeout, verify=self.verify_calls
238
+ )
239
+ if not response.ok:
240
+ raise handle_error("Update of tenant failed.", response)
241
+
242
+ def delete_tenant(
243
+ self,
244
+ identifier: str,
245
+ timeout: int = DEFAULT_TIMEOUT,
246
+ max_retries: int = DEFAULT_MAX_RETRIES,
247
+ backoff_factor: float = DEFAULT_BACKOFF_FACTOR,
248
+ ):
249
+ """
250
+ Delete a tenant.
251
+ Parameters
252
+ ----------
253
+ identifier: str
254
+ Tenant identifier.
255
+ timeout: int
256
+ Timeout for the request (default: 60 seconds)
257
+ max_retries: int
258
+ Maximum number of retries
259
+ backoff_factor: float
260
+ A backoff factor to apply between attempts after the second try (most errors are resolved immediately by a
261
+ second try without a delay)
262
+
263
+ Raises
264
+ ------
265
+ WacomServiceException
266
+ If the tenant service returns an error code.
267
+
268
+ """
269
+ url: str = f"{self.service_base_url}{TenantManagementServiceAPI.TENANT_ENDPOINT}/{identifier}"
270
+ headers: dict = {
271
+ USER_AGENT_HEADER_FLAG: self.user_agent,
272
+ AUTHORIZATION_HEADER_FLAG: f"Bearer {self.__tenant_management_token}",
273
+ CONTENT_TYPE_HEADER_FLAG: "application/json",
274
+ }
275
+ mount_point: str = "https://" if self.service_url.startswith("https") else "http://"
276
+ with requests.Session() as session:
277
+ retries: Retry = Retry(total=max_retries, backoff_factor=backoff_factor, status_forcelist=STATUS_FORCE_LIST)
278
+ session.mount(mount_point, HTTPAdapter(max_retries=retries))
279
+ response: Response = session.delete(url, headers=headers, timeout=timeout, verify=self.verify_calls)
280
+ if not response.ok:
281
+ raise handle_error("Creation of tenant failed.", response)