deepsights-api 0.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.
- deepsights/__init__.py +23 -0
- deepsights/answers/__init__.py +26 -0
- deepsights/answers/answer.py +106 -0
- deepsights/answers/answer_v1.py +55 -0
- deepsights/answers/model.py +107 -0
- deepsights/api/__init__.py +22 -0
- deepsights/api/api.py +231 -0
- deepsights/api/model.py +96 -0
- deepsights/api/quota.py +54 -0
- deepsights/contentstore/__init__.py +24 -0
- deepsights/contentstore/_search.py +229 -0
- deepsights/contentstore/model.py +76 -0
- deepsights/contentstore/news.py +139 -0
- deepsights/contentstore/secondary.py +139 -0
- deepsights/documents/__init__.py +44 -0
- deepsights/documents/_cache.py +39 -0
- deepsights/documents/_segmenter.py +113 -0
- deepsights/documents/delete.py +82 -0
- deepsights/documents/download.py +63 -0
- deepsights/documents/load.py +172 -0
- deepsights/documents/model.py +161 -0
- deepsights/documents/search.py +182 -0
- deepsights/documents/upload.py +130 -0
- deepsights/minions/__init__.py +0 -0
- deepsights/minions/_minions.py +59 -0
- deepsights/reports/__init__.py +24 -0
- deepsights/reports/model.py +141 -0
- deepsights/reports/report.py +95 -0
- deepsights/utils/__init__.py +31 -0
- deepsights/utils/_cache.py +63 -0
- deepsights/utils/_ranking.py +201 -0
- deepsights/utils/_utils.py +45 -0
- deepsights/utils/model.py +91 -0
- deepsights_api-0.2.0.dist-info/LICENSE +201 -0
- deepsights_api-0.2.0.dist-info/METADATA +92 -0
- deepsights_api-0.2.0.dist-info/RECORD +39 -0
- deepsights_api-0.2.0.dist-info/WHEEL +5 -0
- deepsights_api-0.2.0.dist-info/top_level.txt +1 -0
- src/deepsights/__init__.py +23 -0
deepsights/__init__.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Copyright 2024 Market Logic Software AG. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
This module contains the client library to interact with the Market Logic DeepSights and ContentStore APIs.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from deepsights.api import *
|
|
20
|
+
from deepsights.documents import *
|
|
21
|
+
from deepsights.contentstore import *
|
|
22
|
+
from deepsights.answers import *
|
|
23
|
+
from deepsights.reports import *
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Copyright 2024 Market Logic Software AG. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
This module contains the functions to retrieve answers from the DeepSights API.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from deepsights.answers.model import DocumentAnswer, DocumentAnswerPageReference
|
|
20
|
+
from deepsights.answers.answer import (
|
|
21
|
+
answer_set_create,
|
|
22
|
+
answer_set_wait_for_completion,
|
|
23
|
+
answer_set_get,
|
|
24
|
+
answer_set_get_sync,
|
|
25
|
+
)
|
|
26
|
+
from deepsights.answers.answer_v1 import answers_get
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# Copyright 2024 Market Logic Software AG. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
This module contains the functions to retrieve answers from the DeepSights API.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from deepsights.api import DeepSights
|
|
20
|
+
from deepsights.minions._minions import minion_wait_for_completion
|
|
21
|
+
from deepsights.answers.model import DocumentAnswerSet, DocumentAnswer
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
#################################################
|
|
25
|
+
def answer_set_create(api: DeepSights, question: str) -> str:
|
|
26
|
+
"""
|
|
27
|
+
Creates a new answer set by submitting a question to the DeepSights API.
|
|
28
|
+
|
|
29
|
+
Args:
|
|
30
|
+
|
|
31
|
+
api (DeepSights): An instance of the DeepSights API client.
|
|
32
|
+
question (str): The question to be submitted for the answers.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
|
|
36
|
+
str: The ID of the created answer's minion job.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
body = {"input": question}
|
|
40
|
+
response = api.post("/minion-commander-service/answer-sets", body=body, timeout=5)
|
|
41
|
+
|
|
42
|
+
return response["minion_job"]["id"]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
#################################################
|
|
46
|
+
def answer_set_wait_for_completion(api: DeepSights, answer_set_id: str, timeout=30):
|
|
47
|
+
"""
|
|
48
|
+
Waits for the completion of an answer set.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
|
|
52
|
+
api (DeepSights): The DeepSights API instance.
|
|
53
|
+
answer_set_id (str): The ID of the answer set.
|
|
54
|
+
timeout (int, optional): The maximum time to wait for the answer set to complete, in seconds.
|
|
55
|
+
Defaults to 30.
|
|
56
|
+
|
|
57
|
+
Raises:
|
|
58
|
+
|
|
59
|
+
ValueError: If the answer set fails to complete.
|
|
60
|
+
"""
|
|
61
|
+
return minion_wait_for_completion(api, "answer-sets", answer_set_id, timeout)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
#################################################
|
|
65
|
+
def answer_set_get(api: DeepSights, answer_set_id: str) -> DocumentAnswerSet:
|
|
66
|
+
"""
|
|
67
|
+
Loads an answer set from the DeepSights API.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
|
|
71
|
+
api (DeepSights): An instance of the DeepSights API client.
|
|
72
|
+
answer_set_id (str): The ID of the answer set to load.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
|
|
76
|
+
DocumentAnswerSet: The answer set.
|
|
77
|
+
"""
|
|
78
|
+
response = api.get(f"/minion-commander-service/answer-sets/{answer_set_id}")
|
|
79
|
+
|
|
80
|
+
return DocumentAnswerSet(
|
|
81
|
+
answers=[
|
|
82
|
+
DocumentAnswer.model_validate(answer)
|
|
83
|
+
for answer in response["context"]["summarized_search_results"]
|
|
84
|
+
]
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
#################################################
|
|
89
|
+
def answer_set_get_sync(api: DeepSights, question: str) -> DocumentAnswerSet:
|
|
90
|
+
"""
|
|
91
|
+
Submits a question to the DeepSights API and waits for the answer set to complete.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
|
|
95
|
+
api (DeepSights): An instance of the DeepSights API client.
|
|
96
|
+
question (str): The question to be submitted for the answers.
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
|
|
100
|
+
DocumentAnswerSet: The answer set.
|
|
101
|
+
"""
|
|
102
|
+
answer_set_id = answer_set_create(api, question)
|
|
103
|
+
|
|
104
|
+
answer_set_wait_for_completion(api, answer_set_id)
|
|
105
|
+
|
|
106
|
+
return answer_set_get(api, answer_set_id)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Copyright 2024 Market Logic Software AG. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
This module contains the functions to retrieve answers v1 from the DeepSights API.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import logging
|
|
20
|
+
from typing import List
|
|
21
|
+
from deepsights.api import DeepSights
|
|
22
|
+
from deepsights.answers.model import DocumentAnswer
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
#################################################
|
|
26
|
+
def answers_get(api: DeepSights, question: str, timeout=30) -> List[DocumentAnswer]:
|
|
27
|
+
"""
|
|
28
|
+
Retrieves answers for a given question.
|
|
29
|
+
|
|
30
|
+
This function is deprecated and will be removed in a future release.
|
|
31
|
+
Please use the answer_set_create, answer_set_wait_for_completion,
|
|
32
|
+
and answer_set_get functions instead.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
|
|
36
|
+
api (DeepSights): The DeepSights API client.
|
|
37
|
+
question (str): The question to retrieve answers for.
|
|
38
|
+
timeout (int, optional): The timeout for the request. Defaults to 30.
|
|
39
|
+
|
|
40
|
+
Returns:
|
|
41
|
+
|
|
42
|
+
List[DocumentAnswer]: The list of answers for the question.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
# deprecation warning
|
|
46
|
+
logging.warning(
|
|
47
|
+
"===== DEPRECATION WARNING =====\n"
|
|
48
|
+
"The answers_get function is deprecated and will be removed in a future release. "
|
|
49
|
+
"Please use the answer_set_create, answer_set_wait_for_completion, and answer_set_get functions instead."
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
body = {"search_term": question}
|
|
53
|
+
response = api.post("/answer-service/answer-sets", body=body, timeout=timeout)
|
|
54
|
+
|
|
55
|
+
return [DocumentAnswer.model_validate(answer) for answer in response["answers"]]
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Copyright 2024 Market Logic Software AG. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
This module contains the models for answers in the DeepSights API.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from typing import Optional, List
|
|
20
|
+
from datetime import datetime
|
|
21
|
+
from pydantic import Field, BaseModel, AliasChoices
|
|
22
|
+
from deepsights.utils import DeepSightsIdModel, DeepSightsIdTitleModel
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
#################################################
|
|
26
|
+
class BaseAnswer(DeepSightsIdTitleModel):
|
|
27
|
+
"""
|
|
28
|
+
Represents a base answer object.
|
|
29
|
+
|
|
30
|
+
Attributes:
|
|
31
|
+
|
|
32
|
+
answer (str): The summary of the answer.
|
|
33
|
+
artifact_id (str): The ID of the artifact.
|
|
34
|
+
artifact_type (str): The type of the artifact.
|
|
35
|
+
artifact_description (Optional[str]): The human-readable summary of the artifact.
|
|
36
|
+
timestamp (datetime, optional): The publication date of the answer. Defaults to None.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
answer: str = Field(alias="summary", description="The answer from the artifact.")
|
|
40
|
+
artifact_id: str = Field(
|
|
41
|
+
description="The ID of the artifact from which the answer is derived."
|
|
42
|
+
)
|
|
43
|
+
artifact_type: str = Field(
|
|
44
|
+
description="The type of the artifact from which the answer is derived."
|
|
45
|
+
)
|
|
46
|
+
artifact_description: Optional[str] = Field(
|
|
47
|
+
alias="artifact_summary",
|
|
48
|
+
default=None,
|
|
49
|
+
description="The human-readable summary of the artifact.",
|
|
50
|
+
)
|
|
51
|
+
timestamp: Optional[datetime] = Field(
|
|
52
|
+
alias="publication_date",
|
|
53
|
+
default=None,
|
|
54
|
+
description="The publication date of the artifact from which the answer is derived.",
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
#################################################
|
|
59
|
+
class DocumentAnswerPageReference(DeepSightsIdModel):
|
|
60
|
+
"""
|
|
61
|
+
Represents a reference to a specific page in a document.
|
|
62
|
+
|
|
63
|
+
Attributes:
|
|
64
|
+
|
|
65
|
+
page_number (int): The page number in the document.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
page_number: int = Field(
|
|
69
|
+
validation_alias=AliasChoices("page_number", "number"),
|
|
70
|
+
description="The page number in the document."
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
#################################################
|
|
75
|
+
class DocumentAnswer(BaseAnswer):
|
|
76
|
+
"""
|
|
77
|
+
Represents an answer that is a document.
|
|
78
|
+
|
|
79
|
+
Attributes:
|
|
80
|
+
|
|
81
|
+
artifact_type (str): The type of the artifact, which is set to "DOCUMENT".
|
|
82
|
+
pages (Optional[int]): The total number of pages in the document.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
artifact_type: str = Field(
|
|
86
|
+
default="DOCUMENT",
|
|
87
|
+
description="The type of the artifact from which the answer is derived.",
|
|
88
|
+
)
|
|
89
|
+
pages: List[DocumentAnswerPageReference] = Field(
|
|
90
|
+
alias="page_references",
|
|
91
|
+
description="The references to the pages in the document.",
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
#################################################
|
|
96
|
+
class DocumentAnswerSet(BaseModel):
|
|
97
|
+
"""
|
|
98
|
+
Represents an answer set that contains document answers.
|
|
99
|
+
|
|
100
|
+
Attributes:
|
|
101
|
+
|
|
102
|
+
answers (List[DocumentAnswer]): The list of document answers in the set.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
answers: List[DocumentAnswer] = Field(
|
|
106
|
+
description="The list of document answers in the set."
|
|
107
|
+
)
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Copyright 2024 Market Logic Software AG. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
"""
|
|
17
|
+
This module contains the base functions to interact with the DeepSights API.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from deepsights.api.api import DeepSights, ContentStore
|
|
21
|
+
from deepsights.api.model import APIProfile, QuotaInfo, QuotaStatus
|
|
22
|
+
from deepsights.api.quota import quota_get_status, api_get_profile
|
deepsights/api/api.py
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
# Copyright 2024 Market Logic Software AG. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
This module contains the base functions to interact with the DeepSights API.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
import os
|
|
20
|
+
import logging
|
|
21
|
+
from typing import Dict
|
|
22
|
+
from tenacity import (
|
|
23
|
+
retry,
|
|
24
|
+
stop_after_attempt,
|
|
25
|
+
wait_random_exponential,
|
|
26
|
+
retry_if_exception_type,
|
|
27
|
+
)
|
|
28
|
+
from requests import Session
|
|
29
|
+
from requests.exceptions import Timeout
|
|
30
|
+
from ratelimit import limits, sleep_and_retry
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
#################################################
|
|
34
|
+
class API:
|
|
35
|
+
"""
|
|
36
|
+
Represents an API client for interacting with the DeepSights APIs.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
#######################################
|
|
40
|
+
def __init__(
|
|
41
|
+
self, endpoint_base: str, api_key: str, api_key_env_var: str = None
|
|
42
|
+
) -> None:
|
|
43
|
+
"""
|
|
44
|
+
Initializes the API client.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
|
|
48
|
+
endpoint_base (str): The base URL of the API endpoint.
|
|
49
|
+
api_key (str): The API key to be used for authentication.
|
|
50
|
+
api_key_env_var (str, optional): The name of the environment variable that contains the API key.
|
|
51
|
+
If not provided, the API key must be passed directly as an argument. Defaults to None.
|
|
52
|
+
|
|
53
|
+
Raises:
|
|
54
|
+
|
|
55
|
+
AssertionError: If neither API key nor environment variable is provided.
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
# fall back to environment variable
|
|
59
|
+
assert (
|
|
60
|
+
api_key or api_key_env_var
|
|
61
|
+
), "Must provide either API key or environment variable"
|
|
62
|
+
if not api_key:
|
|
63
|
+
api_key = os.environ.get(api_key_env_var)
|
|
64
|
+
self._api_key = api_key
|
|
65
|
+
|
|
66
|
+
# record endpoint base
|
|
67
|
+
self._endpoint_base = endpoint_base
|
|
68
|
+
if not self._endpoint_base.endswith("/"):
|
|
69
|
+
self._endpoint_base += "/"
|
|
70
|
+
# prepare session
|
|
71
|
+
self._session = Session()
|
|
72
|
+
self._session.headers.update({"X-Api-Key": self._api_key})
|
|
73
|
+
|
|
74
|
+
#######################################
|
|
75
|
+
def _endpoint(self, path: str) -> str:
|
|
76
|
+
"""
|
|
77
|
+
Constructs the full endpoint URL by appending the given path to the base endpoint.
|
|
78
|
+
|
|
79
|
+
Args:
|
|
80
|
+
|
|
81
|
+
path (str): The path to be appended to the base endpoint.
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
|
|
85
|
+
str: The full endpoint URL.
|
|
86
|
+
"""
|
|
87
|
+
return self._endpoint_base + path
|
|
88
|
+
|
|
89
|
+
#######################################
|
|
90
|
+
@retry(
|
|
91
|
+
stop=stop_after_attempt(3),
|
|
92
|
+
wait=wait_random_exponential(max=5),
|
|
93
|
+
retry=retry_if_exception_type(Timeout),
|
|
94
|
+
)
|
|
95
|
+
@sleep_and_retry
|
|
96
|
+
@limits(calls=1000, period=60)
|
|
97
|
+
def get(self, path: str, params: Dict = None, timeout=15) -> Dict:
|
|
98
|
+
"""
|
|
99
|
+
Sends a GET request to the specified path with optional parameters.
|
|
100
|
+
|
|
101
|
+
Args:
|
|
102
|
+
|
|
103
|
+
path (str): The path to send the GET request to.
|
|
104
|
+
params (Dict, optional): Optional parameters to include in the request. Defaults to None
|
|
105
|
+
timeout (int, optional): The timeout in seconds for the request. Defaults to 15.
|
|
106
|
+
|
|
107
|
+
Returns:
|
|
108
|
+
|
|
109
|
+
The JSON body of the server's response to the request.
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
response = self._session.get(
|
|
113
|
+
self._endpoint(path), params=params, timeout=timeout
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
if response.status_code != 200:
|
|
117
|
+
logging.error(
|
|
118
|
+
"GET %s failed with status code %s", path, response.status_code
|
|
119
|
+
)
|
|
120
|
+
response.raise_for_status()
|
|
121
|
+
|
|
122
|
+
return response.json()
|
|
123
|
+
|
|
124
|
+
#######################################
|
|
125
|
+
@retry(
|
|
126
|
+
stop=stop_after_attempt(3),
|
|
127
|
+
wait=wait_random_exponential(max=5),
|
|
128
|
+
retry=retry_if_exception_type(Timeout),
|
|
129
|
+
)
|
|
130
|
+
@sleep_and_retry
|
|
131
|
+
@limits(calls=100, period=60)
|
|
132
|
+
def post(self, path: str, body: Dict, params: Dict = None, timeout=15) -> Dict:
|
|
133
|
+
"""
|
|
134
|
+
Sends a POST request to the specified path with optional parameters.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
|
|
138
|
+
path (str): The path to send the POST request to.
|
|
139
|
+
body (Dict): The JSON body to include in the request.
|
|
140
|
+
params (Dict, optional): Optional parameters to include in the request. Defaults to None.
|
|
141
|
+
timeout (int, optional): The timeout in seconds for the request. Defaults to 15.
|
|
142
|
+
|
|
143
|
+
Returns:
|
|
144
|
+
|
|
145
|
+
Dict: The JSON body of the server's response to the request.
|
|
146
|
+
"""
|
|
147
|
+
|
|
148
|
+
response = self._session.post(
|
|
149
|
+
self._endpoint(path), params=params, json=body, timeout=timeout
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
if response.status_code != 200:
|
|
153
|
+
logging.error(
|
|
154
|
+
"POST %s failed with status code %s", path, response.status_code
|
|
155
|
+
)
|
|
156
|
+
response.raise_for_status()
|
|
157
|
+
|
|
158
|
+
return response.json()
|
|
159
|
+
|
|
160
|
+
#######################################
|
|
161
|
+
@retry(
|
|
162
|
+
stop=stop_after_attempt(3),
|
|
163
|
+
wait=wait_random_exponential(max=5),
|
|
164
|
+
retry=retry_if_exception_type(Timeout),
|
|
165
|
+
)
|
|
166
|
+
@sleep_and_retry
|
|
167
|
+
@limits(calls=1000, period=60)
|
|
168
|
+
def delete(self, path: str, timeout=5):
|
|
169
|
+
"""
|
|
170
|
+
Sends a DELETE request to the specified path.
|
|
171
|
+
|
|
172
|
+
Args:
|
|
173
|
+
|
|
174
|
+
path (str): The path to send the DELETE request to.
|
|
175
|
+
timeout (int, optional): The timeout for the request in seconds. Defaults to 5.
|
|
176
|
+
|
|
177
|
+
Raises:
|
|
178
|
+
|
|
179
|
+
HTTPError: If the DELETE request fails with a non-200 status code.
|
|
180
|
+
"""
|
|
181
|
+
response = self._session.delete(self._endpoint(path), timeout=timeout)
|
|
182
|
+
|
|
183
|
+
if response.status_code != 200:
|
|
184
|
+
logging.error(
|
|
185
|
+
"DELETE %s failed with status code %s", path, response.status_code
|
|
186
|
+
)
|
|
187
|
+
response.raise_for_status()
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
#################################################
|
|
191
|
+
class ContentStore(API):
|
|
192
|
+
"""
|
|
193
|
+
This class provides methods to interact with the ContentStore API.
|
|
194
|
+
"""
|
|
195
|
+
|
|
196
|
+
#######################################
|
|
197
|
+
def __init__(self, api_key: str = None) -> None:
|
|
198
|
+
"""
|
|
199
|
+
Initializes the API client.
|
|
200
|
+
|
|
201
|
+
Args:
|
|
202
|
+
|
|
203
|
+
api_key (str, optional): The API key to be used for authentication. If not provided, it will be fetched from the environment variable CONTENTSTORE_API_KEY.
|
|
204
|
+
"""
|
|
205
|
+
super().__init__(
|
|
206
|
+
endpoint_base="https://apigee.mlsdevcloud.com/secondary-content/api/",
|
|
207
|
+
api_key=api_key,
|
|
208
|
+
api_key_env_var="CONTENTSTORE_API_KEY",
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
#################################################
|
|
213
|
+
class DeepSights(API):
|
|
214
|
+
"""
|
|
215
|
+
This class provides methods to interact with the DeepSights API.
|
|
216
|
+
"""
|
|
217
|
+
|
|
218
|
+
#######################################
|
|
219
|
+
def __init__(self, api_key: str = None) -> None:
|
|
220
|
+
"""
|
|
221
|
+
Initializes the API client.
|
|
222
|
+
|
|
223
|
+
Args:
|
|
224
|
+
|
|
225
|
+
api_key (str, optional): The API key to be used for authentication. If not provided, it will be fetched from the environment variable DEEPSIGHTS_API_KEY.
|
|
226
|
+
"""
|
|
227
|
+
super().__init__(
|
|
228
|
+
endpoint_base="https://api.deepsights.ai/ds/v1/",
|
|
229
|
+
api_key=api_key,
|
|
230
|
+
api_key_env_var="DEEPSIGHTS_API_KEY",
|
|
231
|
+
)
|
deepsights/api/model.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# Copyright 2024 Market Logic Software AG. All Rights Reserved.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""
|
|
16
|
+
This module contains the models for the DeepSights API.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from typing import Optional
|
|
20
|
+
from datetime import datetime
|
|
21
|
+
from pydantic import Field
|
|
22
|
+
from deepsights.utils import DeepSightsBaseModel
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
#################################################
|
|
26
|
+
class APIProfile(DeepSightsBaseModel):
|
|
27
|
+
"""
|
|
28
|
+
Represents the profile of an API key.
|
|
29
|
+
|
|
30
|
+
Attributes:
|
|
31
|
+
|
|
32
|
+
app (str): The name of the application associated with the API key.
|
|
33
|
+
tenant (str): The name of the tenant associated with the API key.
|
|
34
|
+
user (Optional[str]): The user ID associated with the API key.
|
|
35
|
+
day_quota (Optional[int]): The daily request quota limit for the API key.
|
|
36
|
+
minute_quota (Optional[int]): The minute request quota limit for the API key.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
app: str = Field(
|
|
40
|
+
description="The name of the application associated with the API key."
|
|
41
|
+
)
|
|
42
|
+
tenant: str = Field(
|
|
43
|
+
description="The name of the tenant associated with the API key."
|
|
44
|
+
)
|
|
45
|
+
user: Optional[str] = Field(description="The user ID associated with the API key.")
|
|
46
|
+
day_quota: Optional[int] = Field(
|
|
47
|
+
alias="daily_quota_limit",
|
|
48
|
+
description="The daily request quota limit for the API key; if None, unlimited.",
|
|
49
|
+
)
|
|
50
|
+
minute_quota: Optional[int] = Field(
|
|
51
|
+
alias="minute_quota_limit",
|
|
52
|
+
description="The minute request quota limit for the API key; if None, unlimited.",
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
#################################################
|
|
57
|
+
class QuotaInfo(DeepSightsBaseModel):
|
|
58
|
+
"""
|
|
59
|
+
Represents information about the quota for API requests.
|
|
60
|
+
|
|
61
|
+
Attributes:
|
|
62
|
+
|
|
63
|
+
quota (Optional[int]): The request quota limit.
|
|
64
|
+
quota_used (Optional[int]): The number of requests used.
|
|
65
|
+
quota_reset_at (datetime): The time at which the quota will be reset.
|
|
66
|
+
"""
|
|
67
|
+
|
|
68
|
+
quota: Optional[int] = Field(
|
|
69
|
+
alias="quota_limit",
|
|
70
|
+
description="The request quota limit per time period; if None, unlimited.",
|
|
71
|
+
)
|
|
72
|
+
quota_used: Optional[int] = Field(
|
|
73
|
+
description="The number of requests used in this time period."
|
|
74
|
+
)
|
|
75
|
+
quota_reset_at: datetime = Field(
|
|
76
|
+
description="The time at which the quota will be reset."
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
#################################################
|
|
81
|
+
class QuotaStatus(DeepSightsBaseModel):
|
|
82
|
+
"""
|
|
83
|
+
Represents the quota status for the API.
|
|
84
|
+
|
|
85
|
+
Attributes:
|
|
86
|
+
|
|
87
|
+
day_quota (QuotaInfo): The daily quota limit and status.
|
|
88
|
+
minute_quota (QuotaInfo): The minute quota limit and status.
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
day_quota: QuotaInfo = Field(
|
|
92
|
+
alias="daily", description="The daily quota limit and status."
|
|
93
|
+
)
|
|
94
|
+
minute_quota: QuotaInfo = Field(
|
|
95
|
+
alias="minute", description="The minute quota limit and status."
|
|
96
|
+
)
|