scijob 0.1.0a1__tar.gz
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.
- scijob-0.1.0a1/.gitignore +14 -0
- scijob-0.1.0a1/LICENSE +21 -0
- scijob-0.1.0a1/PKG-INFO +91 -0
- scijob-0.1.0a1/README.md +65 -0
- scijob-0.1.0a1/pyproject.toml +54 -0
- scijob-0.1.0a1/scijob/__init__.py +20 -0
- scijob-0.1.0a1/scijob/__version__.py +1 -0
- scijob-0.1.0a1/scijob/_client.py +396 -0
- scijob-0.1.0a1/scijob/_config.py +3 -0
- scijob-0.1.0a1/scijob/_exceptions.py +55 -0
- scijob-0.1.0a1/scijob/_logger.py +12 -0
- scijob-0.1.0a1/scijob/_models.py +141 -0
- scijob-0.1.0a1/scijob/_proxy.py +459 -0
- scijob-0.1.0a1/scijob/_types.py +43 -0
- scijob-0.1.0a1/scijob/_utils.py +20 -0
- scijob-0.1.0a1/scijob/py.typed +0 -0
- scijob-0.1.0a1/scijob/requests.py +382 -0
scijob-0.1.0a1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 wjk376
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
scijob-0.1.0a1/PKG-INFO
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: scijob
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: SciJob platform SDK for Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/wjk376/scijob-sdk-python
|
|
6
|
+
Author-email: Jiankun Wang <wangjiankun376@163.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Framework :: Pydantic :: 2
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Requires-Dist: httpx>=0.28.1
|
|
21
|
+
Requires-Dist: pycryptodome>=3.23.0
|
|
22
|
+
Requires-Dist: pydantic>=2.11.0
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: dotenv>=0.9.9; extra == 'dev'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# SciJob SDK
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
## Usages
|
|
32
|
+
|
|
33
|
+
You may first initialize a SciJob client:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from scijob import Client
|
|
37
|
+
|
|
38
|
+
cli = Client(api_key="your api key", api_base_url="your api base url")
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Or go with the async version:
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from scijob import AsyncClient
|
|
45
|
+
|
|
46
|
+
cli = AsyncClient(api_key="your api key", api_base_url="your api base url")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Register Functions
|
|
50
|
+
|
|
51
|
+
You may register a new function which sources from GitLab and with VolcEngine specifications as below:
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from scijob.requests import *
|
|
55
|
+
|
|
56
|
+
resp = cli.register_function(
|
|
57
|
+
request=RegisterFunctionRequestBuilder()
|
|
58
|
+
.identity(key="func_key", name="some_func", module="some_module", version="0.0.1")
|
|
59
|
+
.description("This is a test function...")
|
|
60
|
+
.repo_source("gitlab")
|
|
61
|
+
.gitlab_repo_info(...)
|
|
62
|
+
.envs([("SOME_ENVIRONMENT_VARIABLE", "hello world")])
|
|
63
|
+
.volcengine_specs(specs=VolcEngineSpecsBuilder()
|
|
64
|
+
.credentials(ak="your access key", sk="your secret key")
|
|
65
|
+
.region("cn-beijing")
|
|
66
|
+
.image(...)
|
|
67
|
+
.num_cpus(4) # use 'num_gpus' instead if the function requires GPU resource
|
|
68
|
+
.vepfs(...)
|
|
69
|
+
.build()
|
|
70
|
+
)
|
|
71
|
+
.build()
|
|
72
|
+
)
|
|
73
|
+
print(resp.function_id)
|
|
74
|
+
#> 6a1b48027b0e2e9c9d3ad8c7
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Call Functions
|
|
78
|
+
|
|
79
|
+
Below shows an example of calling a registered function by its ID, using with statements:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
async with AsyncClient(api_key="your api key", api_base_url="your api base url") as cli:
|
|
83
|
+
job = await cli.call_function(
|
|
84
|
+
request=CallFunctionRequestBuilder()
|
|
85
|
+
.function_id("6a1b48027b0e2e9c9d3ad8c7")
|
|
86
|
+
.function_args(...)
|
|
87
|
+
.backend("volcengine_ml_platform")
|
|
88
|
+
.build()
|
|
89
|
+
)
|
|
90
|
+
res = await job.get_result()
|
|
91
|
+
```
|
scijob-0.1.0a1/README.md
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# SciJob SDK
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
## Usages
|
|
6
|
+
|
|
7
|
+
You may first initialize a SciJob client:
|
|
8
|
+
|
|
9
|
+
```python
|
|
10
|
+
from scijob import Client
|
|
11
|
+
|
|
12
|
+
cli = Client(api_key="your api key", api_base_url="your api base url")
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or go with the async version:
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from scijob import AsyncClient
|
|
19
|
+
|
|
20
|
+
cli = AsyncClient(api_key="your api key", api_base_url="your api base url")
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Register Functions
|
|
24
|
+
|
|
25
|
+
You may register a new function which sources from GitLab and with VolcEngine specifications as below:
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from scijob.requests import *
|
|
29
|
+
|
|
30
|
+
resp = cli.register_function(
|
|
31
|
+
request=RegisterFunctionRequestBuilder()
|
|
32
|
+
.identity(key="func_key", name="some_func", module="some_module", version="0.0.1")
|
|
33
|
+
.description("This is a test function...")
|
|
34
|
+
.repo_source("gitlab")
|
|
35
|
+
.gitlab_repo_info(...)
|
|
36
|
+
.envs([("SOME_ENVIRONMENT_VARIABLE", "hello world")])
|
|
37
|
+
.volcengine_specs(specs=VolcEngineSpecsBuilder()
|
|
38
|
+
.credentials(ak="your access key", sk="your secret key")
|
|
39
|
+
.region("cn-beijing")
|
|
40
|
+
.image(...)
|
|
41
|
+
.num_cpus(4) # use 'num_gpus' instead if the function requires GPU resource
|
|
42
|
+
.vepfs(...)
|
|
43
|
+
.build()
|
|
44
|
+
)
|
|
45
|
+
.build()
|
|
46
|
+
)
|
|
47
|
+
print(resp.function_id)
|
|
48
|
+
#> 6a1b48027b0e2e9c9d3ad8c7
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Call Functions
|
|
52
|
+
|
|
53
|
+
Below shows an example of calling a registered function by its ID, using with statements:
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
async with AsyncClient(api_key="your api key", api_base_url="your api base url") as cli:
|
|
57
|
+
job = await cli.call_function(
|
|
58
|
+
request=CallFunctionRequestBuilder()
|
|
59
|
+
.function_id("6a1b48027b0e2e9c9d3ad8c7")
|
|
60
|
+
.function_args(...)
|
|
61
|
+
.backend("volcengine_ml_platform")
|
|
62
|
+
.build()
|
|
63
|
+
)
|
|
64
|
+
res = await job.get_result()
|
|
65
|
+
```
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "scijob"
|
|
7
|
+
description = "SciJob platform SDK for Python"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
requires-python = ">=3.9"
|
|
10
|
+
authors = [
|
|
11
|
+
{ name = "Jiankun Wang", email = "wangjiankun376@163.com" },
|
|
12
|
+
]
|
|
13
|
+
license = "MIT"
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Framework :: Pydantic :: 2",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Programming Language :: Python :: 3.9",
|
|
23
|
+
"Programming Language :: Python :: 3.10",
|
|
24
|
+
"Programming Language :: Python :: 3.11",
|
|
25
|
+
"Programming Language :: Python :: 3.12",
|
|
26
|
+
]
|
|
27
|
+
dependencies = [
|
|
28
|
+
"httpx>=0.28.1",
|
|
29
|
+
"pycryptodome>=3.23.0",
|
|
30
|
+
"pydantic>=2.11.0",
|
|
31
|
+
]
|
|
32
|
+
dynamic = [
|
|
33
|
+
"version",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[project.optional-dependencies]
|
|
37
|
+
dev = [
|
|
38
|
+
"dotenv>=0.9.9",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://github.com/wjk376/scijob-sdk-python"
|
|
43
|
+
|
|
44
|
+
[tool.hatch.version]
|
|
45
|
+
path = "scijob/__version__.py"
|
|
46
|
+
|
|
47
|
+
[tool.hatch.build.targets.wheel]
|
|
48
|
+
packages = ["scijob"]
|
|
49
|
+
|
|
50
|
+
[tool.hatch.build.targets.sdist]
|
|
51
|
+
include = ["/scijob"]
|
|
52
|
+
|
|
53
|
+
[[tool.uv.index]]
|
|
54
|
+
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from ._client import AsyncClient, Client
|
|
2
|
+
from ._exceptions import *
|
|
3
|
+
from .__version__ import __version__
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"__version__",
|
|
7
|
+
"AsyncClient",
|
|
8
|
+
"Client",
|
|
9
|
+
"HttpRequestError",
|
|
10
|
+
"GetJobResultError",
|
|
11
|
+
"JobCancelledError",
|
|
12
|
+
"JobStateFailedError",
|
|
13
|
+
"JobStateMissingError",
|
|
14
|
+
"JobStateStoppedError",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
__locals = locals()
|
|
18
|
+
for __name in __all__:
|
|
19
|
+
if not __name.startswith("__"):
|
|
20
|
+
setattr(__locals[__name], "__module__", "scijob") # noqa
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.1.0a1"
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
from types import TracebackType
|
|
2
|
+
from typing import Any, Optional, Type
|
|
3
|
+
|
|
4
|
+
import httpx
|
|
5
|
+
|
|
6
|
+
from ._config import *
|
|
7
|
+
from ._exceptions import *
|
|
8
|
+
from ._proxy import AsyncJobProxy, JobProxy
|
|
9
|
+
from ._logger import logger
|
|
10
|
+
from ._models import *
|
|
11
|
+
from ._types import Numeric, QueryFunctionsResponse
|
|
12
|
+
from .requests import *
|
|
13
|
+
|
|
14
|
+
__all__ = ["AsyncClient", "Client"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class _BaseClient:
|
|
18
|
+
def __init__(
|
|
19
|
+
self,
|
|
20
|
+
*,
|
|
21
|
+
api_key: str,
|
|
22
|
+
api_base_url: str,
|
|
23
|
+
poll_interval: Numeric = DEFAULT_POLL_INTERVAL,
|
|
24
|
+
):
|
|
25
|
+
self._api_base_url = f"{api_base_url}/{API_VERSION}"
|
|
26
|
+
self._headers = {API_KEY_HEADER: api_key}
|
|
27
|
+
self._poll_interval = poll_interval
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class Client(_BaseClient):
|
|
31
|
+
def __init__(
|
|
32
|
+
self,
|
|
33
|
+
*,
|
|
34
|
+
api_key: str,
|
|
35
|
+
api_base_url: str,
|
|
36
|
+
poll_interval: Numeric = DEFAULT_POLL_INTERVAL,
|
|
37
|
+
):
|
|
38
|
+
super().__init__(
|
|
39
|
+
api_key=api_key,
|
|
40
|
+
api_base_url=api_base_url,
|
|
41
|
+
poll_interval=poll_interval,
|
|
42
|
+
)
|
|
43
|
+
self._cli = httpx.Client(base_url=self._api_base_url, headers=self._headers)
|
|
44
|
+
|
|
45
|
+
def __del__(self):
|
|
46
|
+
self.close()
|
|
47
|
+
|
|
48
|
+
def close(self):
|
|
49
|
+
self._cli.close()
|
|
50
|
+
|
|
51
|
+
def __enter__(self):
|
|
52
|
+
return self
|
|
53
|
+
|
|
54
|
+
def __exit__(
|
|
55
|
+
self,
|
|
56
|
+
exc_type: Optional[Type[BaseException]],
|
|
57
|
+
exc_value: Optional[BaseException],
|
|
58
|
+
traceback: Optional[TracebackType],
|
|
59
|
+
):
|
|
60
|
+
self.close()
|
|
61
|
+
|
|
62
|
+
def _get(self, url: str):
|
|
63
|
+
resp = self._cli.get(url)
|
|
64
|
+
if not resp.is_success:
|
|
65
|
+
raise HttpRequestError(resp)
|
|
66
|
+
return resp
|
|
67
|
+
|
|
68
|
+
def _post(self, url: str, json: Any):
|
|
69
|
+
resp = self._cli.post(url, json=json)
|
|
70
|
+
if not resp.is_success:
|
|
71
|
+
raise HttpRequestError(resp)
|
|
72
|
+
return resp
|
|
73
|
+
|
|
74
|
+
def get_function(self, function_id: str):
|
|
75
|
+
"""
|
|
76
|
+
Get function details by function ID.
|
|
77
|
+
"""
|
|
78
|
+
resp = self._get(url=f"/function/{function_id}")
|
|
79
|
+
return Function.model_validate_json(resp.content, strict=True)
|
|
80
|
+
|
|
81
|
+
def query_functions(self, request: QueryFunctionsRequest):
|
|
82
|
+
"""
|
|
83
|
+
Query matched functions by identity information.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
# Fetch all matched functions by pagination
|
|
87
|
+
json = {**request.json(), "page": 1}
|
|
88
|
+
funcs = []
|
|
89
|
+
|
|
90
|
+
while True:
|
|
91
|
+
resp = self._post(url=f"/function/query", json=json)
|
|
92
|
+
payload: QueryFunctionsResponse = resp.json()
|
|
93
|
+
funcs.extend(payload["functions"])
|
|
94
|
+
if payload["has_more"]:
|
|
95
|
+
json["page"] += 1
|
|
96
|
+
else:
|
|
97
|
+
break
|
|
98
|
+
|
|
99
|
+
return [Function.model_validate(fn, strict=True) for fn in funcs]
|
|
100
|
+
|
|
101
|
+
def register_function(self, request: RegisterFunctionRequest):
|
|
102
|
+
"""
|
|
103
|
+
Register a new function to job platform.
|
|
104
|
+
"""
|
|
105
|
+
resp = self._post(url=f"/function/register", json=request.json())
|
|
106
|
+
payload = RegisterFunctionResponse.model_validate_json(resp.content, strict=True)
|
|
107
|
+
logger.info(f"Registered function '{payload.function_id}'")
|
|
108
|
+
return payload
|
|
109
|
+
|
|
110
|
+
def delete_job(self, job_id: str):
|
|
111
|
+
"""
|
|
112
|
+
Delete job by job ID (for admin users only).
|
|
113
|
+
"""
|
|
114
|
+
self._post(url=f"/job/delete", json={"job_id": job_id})
|
|
115
|
+
|
|
116
|
+
def delete_function(self, function_id: str):
|
|
117
|
+
"""
|
|
118
|
+
Delete function by function ID (for admin users only).
|
|
119
|
+
"""
|
|
120
|
+
self._post(url=f"/function/delete", json={"id": function_id})
|
|
121
|
+
logger.info(f"Deleted function '{function_id}'")
|
|
122
|
+
|
|
123
|
+
def call_function(
|
|
124
|
+
self,
|
|
125
|
+
request: CallFunctionRequest,
|
|
126
|
+
verbose: bool = True,
|
|
127
|
+
):
|
|
128
|
+
"""
|
|
129
|
+
Call function either by function ID or identity (key, name, module, version).
|
|
130
|
+
|
|
131
|
+
Usage:
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
>>> from scijob import SciJobClient
|
|
135
|
+
>>> from scijob.requests import CallFunctionRequestBuilder
|
|
136
|
+
>>>
|
|
137
|
+
>>> client = SciJobClient(...)
|
|
138
|
+
>>> job = client.call_function(
|
|
139
|
+
>>> request=CallFunctionRequestBuilder()
|
|
140
|
+
>>> .function_id("your-function-id")
|
|
141
|
+
>>> .function_args(...)
|
|
142
|
+
>>> .backend(...)
|
|
143
|
+
>>> .build()
|
|
144
|
+
>>> )
|
|
145
|
+
>>> result = job.get_result()
|
|
146
|
+
```
|
|
147
|
+
"""
|
|
148
|
+
resp = self._post(url=request.api_url, json=request.json())
|
|
149
|
+
payload = CallFunctionResponse.model_validate_json(resp.content, strict=True)
|
|
150
|
+
|
|
151
|
+
if verbose:
|
|
152
|
+
logger.info(
|
|
153
|
+
f"Created job '{payload.job_id}' | backend={request.backend} | "
|
|
154
|
+
f"function_id={payload.function_id}"
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
return JobProxy(
|
|
158
|
+
id=payload.job_id,
|
|
159
|
+
backend=request.backend,
|
|
160
|
+
api_base_url=self._api_base_url,
|
|
161
|
+
headers=self._headers,
|
|
162
|
+
poll_interval=self._poll_interval,
|
|
163
|
+
verbose=verbose,
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
def submit_volc_job(
|
|
167
|
+
self,
|
|
168
|
+
request: SubmitVolcJobRequest,
|
|
169
|
+
verbose: bool = True,
|
|
170
|
+
):
|
|
171
|
+
"""
|
|
172
|
+
Submit a job to VolcEngine.
|
|
173
|
+
"""
|
|
174
|
+
resp = self._post(url=f"/job/volc/submit", json=request.json())
|
|
175
|
+
payload = SubmitVolcJobResponse.model_validate_json(resp.content, strict=True)
|
|
176
|
+
|
|
177
|
+
if verbose:
|
|
178
|
+
logger.info(f"Submitted job '{payload.job_id}' | volc_job_id={payload.volc_job_id}")
|
|
179
|
+
|
|
180
|
+
return JobProxy(
|
|
181
|
+
id=payload.job_id,
|
|
182
|
+
backend="volcengine_ml_platform",
|
|
183
|
+
api_base_url=self._api_base_url,
|
|
184
|
+
headers=self._headers,
|
|
185
|
+
poll_interval=self._poll_interval,
|
|
186
|
+
verbose=verbose,
|
|
187
|
+
ignore_result=True,
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
def submit_volc_dry_run_job(
|
|
191
|
+
self,
|
|
192
|
+
request: SubmitVolcDryRunJobRequest,
|
|
193
|
+
verbose: bool = True,
|
|
194
|
+
):
|
|
195
|
+
"""
|
|
196
|
+
Submit a dry run job to VolcEngine.
|
|
197
|
+
"""
|
|
198
|
+
resp = self._post(url=f"/job/volc/submit-dry-run", json=request.json())
|
|
199
|
+
payload = SubmitVolcJobResponse.model_validate_json(resp.content, strict=True)
|
|
200
|
+
|
|
201
|
+
if verbose:
|
|
202
|
+
logger.info(
|
|
203
|
+
f"Submitted dry run job '{payload.job_id}' | volc_job_id={payload.volc_job_id}"
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
return JobProxy(
|
|
207
|
+
id=payload.job_id,
|
|
208
|
+
backend="volcengine_ml_platform",
|
|
209
|
+
api_base_url=self._api_base_url,
|
|
210
|
+
headers=self._headers,
|
|
211
|
+
poll_interval=self._poll_interval,
|
|
212
|
+
verbose=verbose,
|
|
213
|
+
)
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
class AsyncClient(_BaseClient):
|
|
217
|
+
def __init__(
|
|
218
|
+
self,
|
|
219
|
+
*,
|
|
220
|
+
api_key: str,
|
|
221
|
+
api_base_url: str,
|
|
222
|
+
poll_interval: Numeric = DEFAULT_POLL_INTERVAL,
|
|
223
|
+
):
|
|
224
|
+
super().__init__(
|
|
225
|
+
api_key=api_key,
|
|
226
|
+
api_base_url=api_base_url,
|
|
227
|
+
poll_interval=poll_interval,
|
|
228
|
+
)
|
|
229
|
+
self._cli = httpx.AsyncClient(base_url=self._api_base_url, headers=self._headers)
|
|
230
|
+
|
|
231
|
+
async def aclose(self):
|
|
232
|
+
await self._cli.aclose()
|
|
233
|
+
|
|
234
|
+
async def __aenter__(self):
|
|
235
|
+
return self
|
|
236
|
+
|
|
237
|
+
async def __aexit__(
|
|
238
|
+
self,
|
|
239
|
+
exc_type: Optional[Type[BaseException]],
|
|
240
|
+
exc_value: Optional[BaseException],
|
|
241
|
+
traceback: Optional[TracebackType],
|
|
242
|
+
):
|
|
243
|
+
await self.aclose()
|
|
244
|
+
|
|
245
|
+
async def _get(self, url: str):
|
|
246
|
+
resp = await self._cli.get(url)
|
|
247
|
+
if not resp.is_success:
|
|
248
|
+
raise HttpRequestError(resp)
|
|
249
|
+
return resp
|
|
250
|
+
|
|
251
|
+
async def _post(self, url: str, json: Any):
|
|
252
|
+
resp = await self._cli.post(url, json=json)
|
|
253
|
+
if not resp.is_success:
|
|
254
|
+
raise HttpRequestError(resp)
|
|
255
|
+
return resp
|
|
256
|
+
|
|
257
|
+
async def get_function(self, function_id: str):
|
|
258
|
+
"""
|
|
259
|
+
Get function details by function ID.
|
|
260
|
+
"""
|
|
261
|
+
resp = await self._get(url=f"/function/{function_id}")
|
|
262
|
+
return Function.model_validate_json(resp.content, strict=True)
|
|
263
|
+
|
|
264
|
+
async def query_functions(self, request: QueryFunctionsRequest):
|
|
265
|
+
"""
|
|
266
|
+
Query matched functions by identity information.
|
|
267
|
+
"""
|
|
268
|
+
|
|
269
|
+
# Fetch all matched functions by pagination
|
|
270
|
+
json = {**request.json(), "page": 1}
|
|
271
|
+
funcs = []
|
|
272
|
+
|
|
273
|
+
while True:
|
|
274
|
+
resp = await self._post(url=f"/function/query", json=json)
|
|
275
|
+
payload: QueryFunctionsResponse = resp.json()
|
|
276
|
+
funcs.extend(payload["functions"])
|
|
277
|
+
if payload["has_more"]:
|
|
278
|
+
json["page"] += 1
|
|
279
|
+
else:
|
|
280
|
+
break
|
|
281
|
+
|
|
282
|
+
return [Function.model_validate(fn, strict=True) for fn in funcs]
|
|
283
|
+
|
|
284
|
+
async def register_function(self, request: RegisterFunctionRequest):
|
|
285
|
+
"""
|
|
286
|
+
Register a new function to job platform.
|
|
287
|
+
"""
|
|
288
|
+
resp = await self._post(url=f"/function/register", json=request.json())
|
|
289
|
+
payload = RegisterFunctionResponse.model_validate_json(resp.content, strict=True)
|
|
290
|
+
logger.info(f"Registered function '{payload.function_id}'")
|
|
291
|
+
return payload
|
|
292
|
+
|
|
293
|
+
async def delete_function(self, function_id: str):
|
|
294
|
+
"""
|
|
295
|
+
Delete function by function ID (for admin users only).
|
|
296
|
+
"""
|
|
297
|
+
await self._post(url=f"/function/delete", json={"id": function_id})
|
|
298
|
+
logger.info(f"Deleted function '{function_id}'")
|
|
299
|
+
|
|
300
|
+
async def delete_job(self, job_id: str):
|
|
301
|
+
"""
|
|
302
|
+
Delete job by job ID (for admin users only).
|
|
303
|
+
"""
|
|
304
|
+
await self._post(url=f"/job/delete", json={"job_id": job_id})
|
|
305
|
+
|
|
306
|
+
async def call_function(
|
|
307
|
+
self,
|
|
308
|
+
request: CallFunctionRequest,
|
|
309
|
+
verbose: bool = True,
|
|
310
|
+
):
|
|
311
|
+
"""
|
|
312
|
+
Call function either by function ID or identity (key, name, module, version).
|
|
313
|
+
|
|
314
|
+
Usage:
|
|
315
|
+
|
|
316
|
+
```python
|
|
317
|
+
>>> from scijob import AsyncSciJobClient
|
|
318
|
+
>>> from scijob.requests import CallFunctionRequestBuilder
|
|
319
|
+
>>>
|
|
320
|
+
>>> client = AsyncSciJobClient(...)
|
|
321
|
+
>>> job = await client.call_function(
|
|
322
|
+
>>> request=CallFunctionRequestBuilder()
|
|
323
|
+
>>> .function_id("your-function-id")
|
|
324
|
+
>>> .function_args(...)
|
|
325
|
+
>>> .backend(...)
|
|
326
|
+
>>> .build()
|
|
327
|
+
>>> )
|
|
328
|
+
>>> result = await job.get_result()
|
|
329
|
+
```
|
|
330
|
+
"""
|
|
331
|
+
resp = await self._post(url=request.api_url, json=request.json())
|
|
332
|
+
payload = CallFunctionResponse.model_validate_json(resp.content, strict=True)
|
|
333
|
+
|
|
334
|
+
if verbose:
|
|
335
|
+
logger.info(
|
|
336
|
+
f"Created job '{payload.job_id}' | backend={request.backend} | "
|
|
337
|
+
f"function_id={payload.function_id}"
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
return AsyncJobProxy(
|
|
341
|
+
id=payload.job_id,
|
|
342
|
+
backend=request.backend,
|
|
343
|
+
api_base_url=self._api_base_url,
|
|
344
|
+
headers=self._headers,
|
|
345
|
+
poll_interval=self._poll_interval,
|
|
346
|
+
verbose=verbose,
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
async def submit_volc_job(
|
|
350
|
+
self,
|
|
351
|
+
request: SubmitVolcJobRequest,
|
|
352
|
+
verbose: bool = True,
|
|
353
|
+
):
|
|
354
|
+
"""
|
|
355
|
+
Submit a job to VolcEngine.
|
|
356
|
+
"""
|
|
357
|
+
resp = await self._post(url=f"/job/volc/submit", json=request.json())
|
|
358
|
+
payload = SubmitVolcJobResponse.model_validate_json(resp.content, strict=True)
|
|
359
|
+
|
|
360
|
+
if verbose:
|
|
361
|
+
logger.info(f"Submitted job '{payload.job_id}' | volc_job_id={payload.volc_job_id}")
|
|
362
|
+
|
|
363
|
+
return AsyncJobProxy(
|
|
364
|
+
id=payload.job_id,
|
|
365
|
+
backend="volcengine_ml_platform",
|
|
366
|
+
api_base_url=self._api_base_url,
|
|
367
|
+
headers=self._headers,
|
|
368
|
+
poll_interval=self._poll_interval,
|
|
369
|
+
verbose=verbose,
|
|
370
|
+
ignore_result=True,
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
async def submit_volc_dry_run_job(
|
|
374
|
+
self,
|
|
375
|
+
request: SubmitVolcDryRunJobRequest,
|
|
376
|
+
verbose: bool = True,
|
|
377
|
+
):
|
|
378
|
+
"""
|
|
379
|
+
Submit a dry run job to VolcEngine.
|
|
380
|
+
"""
|
|
381
|
+
resp = await self._post(url=f"/job/volc/submit-dry-run", json=request.json())
|
|
382
|
+
payload = SubmitVolcJobResponse.model_validate_json(resp.content, strict=True)
|
|
383
|
+
|
|
384
|
+
if verbose:
|
|
385
|
+
logger.info(
|
|
386
|
+
f"Submitted dry run job '{payload.job_id}' | volc_job_id={payload.volc_job_id}"
|
|
387
|
+
)
|
|
388
|
+
|
|
389
|
+
return AsyncJobProxy(
|
|
390
|
+
id=payload.job_id,
|
|
391
|
+
backend="volcengine_ml_platform",
|
|
392
|
+
api_base_url=self._api_base_url,
|
|
393
|
+
headers=self._headers,
|
|
394
|
+
poll_interval=self._poll_interval,
|
|
395
|
+
verbose=verbose,
|
|
396
|
+
)
|