Skip to content

Commit d863543

Browse files
liuqidiv_suiyufei
authored andcommitted
aihc-dev-13405 [Task] AIHC 添加python sdk版本
Change-Id: I7866d15f0528c63541d5a443a698fefd4b730df6
2 parents afa2c31 + 1654059 commit d863543

44 files changed

Lines changed: 8060 additions & 4 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ build_submitter*
55
.DS_Store
66
bce_python_sdk.egg-info
77
build
8-
dist
8+
dist

baidubce/auth/bce_v1_signer.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
This module provides authentication functions for bce services.
1515
"""
1616
from __future__ import absolute_import
17-
from builtins import str
18-
from builtins import bytes
1917
import hashlib
2018
import hmac
2119
import logging
@@ -74,7 +72,7 @@ def sign(credentials, http_method, path, headers, params,
7472

7573
canonical_headers = _get_canonical_headers(headers, headers_to_sign)
7674
string_to_sign = (b'\n').join([
77-
http_method, canonical_uri,
75+
http_method, canonical_uri,
7876
canonical_querystring, canonical_headers
7977
])
8078
sign_result = hmac.new(compat.convert_to_bytes(sign_key), string_to_sign, hashlib.sha256).hexdigest()
@@ -90,3 +88,54 @@ def sign(credentials, http_method, path, headers, params,
9088
(sign_key, len(string_to_sign), string_to_sign))
9189
_logger.debug('result=%s' % result)
9290
return result
91+
92+
93+
def resource_pool_sign(credentials, http_method, path, headers, params,
94+
timestamp=0, expiration_in_seconds=1800, headers_to_sign=None):
95+
"""
96+
资源池接口签名适配方法,与普通接口签名不同的是,资源池接口需要额外处理host头
97+
"""
98+
99+
_logger.debug('Sign params: %s %s %s %s %d %d %s' % (
100+
http_method, path, headers, params, timestamp, expiration_in_seconds, headers_to_sign))
101+
102+
headers = headers or {}
103+
params = params or {}
104+
105+
sign_key_info = b'bce-auth-v1/%s/%s/%d' % (
106+
credentials.access_key_id,
107+
utils.get_canonical_time(timestamp),
108+
expiration_in_seconds)
109+
sign_key = hmac.new(
110+
credentials.secret_access_key,
111+
sign_key_info,
112+
hashlib.sha256).hexdigest()
113+
114+
canonical_uri = path
115+
canonical_querystring = utils.get_canonical_querystring(params, True)
116+
117+
# 从headers中提取host
118+
host_header = None
119+
for key, value in headers.items():
120+
if key.lower() == b'host':
121+
host_header = utils.convert_to_standard_string(value).strip()
122+
break
123+
124+
canonical_headers = b'host:' + utils.normalize_string(host_header)
125+
string_to_sign = (b'\n').join([
126+
http_method, canonical_uri,
127+
canonical_querystring, canonical_headers
128+
])
129+
sign_result = hmac.new(compat.convert_to_bytes(sign_key), string_to_sign, hashlib.sha256).hexdigest()
130+
# convert to bytes
131+
sign_result = compat.convert_to_bytes(sign_result)
132+
133+
if headers_to_sign:
134+
result = b'%s/%s/%s' % (sign_key_info, (b';').join(headers_to_sign), sign_result)
135+
else:
136+
result = b'%s/host/%s' % (sign_key_info, sign_result)
137+
138+
_logger.debug('sign_key=[%s] sign_string=[%d bytes][ %s ]' %
139+
(sign_key, len(string_to_sign), string_to_sign))
140+
_logger.debug('result=%s' % result)
141+
return result

baidubce/services/aihc/README.md

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
# AIHC SDK 开发说明
2+
3+
## 概述
4+
5+
AIHC SDK采用模块化结构,提高了代码的可维护性和可扩展性。最新版本修复了IDE类型提示问题,确保开发体验更加友好。
6+
7+
### 项目结构
8+
```
9+
baidubce/services/aihc/
10+
├── base/ # 基础模块
11+
│ ├── __init__.py
12+
│ ├── aihc_base_client.py # 基础客户端类
13+
│ └── aihc_common.py # 公共工具函数
14+
├── modules/ # 业务模块目录
15+
│ ├── __init__.py
16+
│ ├── job/ # 任务模块
17+
│ │ ├── __init__.py
18+
│ │ └── job_client.py # 任务相关接口
19+
│ ├── dataset/ # 数据集模块
20+
│ │ ├── __init__.py
21+
│ │ └── dataset_client.py # 数据集相关接口
22+
│ ├── model/ # 模型模块
23+
│ │ ├── __init__.py
24+
│ │ └── model_client.py # 模型相关接口
25+
│ ├── service/ # 在线服务模块
26+
│ │ ├── __init__.py
27+
│ │ └── service_client.py # 服务相关接口
28+
│ └── dev_instance/ # 开发机模块
29+
│ ├── __init__.py
30+
│ └── dev_instance_client.py # 开发机相关接口
31+
├── aihc_client.py # 重构后的主客户端文件
32+
├── aihc_model.py # 保留原有模型文件
33+
├── aihc_handler.py # 保留原有处理器文件
34+
├── aihc_client_original.py # 原始文件备份
35+
└── __init__.py # 主入口文件
36+
```
37+
38+
## 模块说明
39+
40+
### 1. 基础模块 (base/)
41+
42+
#### aihc_base_client.py
43+
- 提供基础客户端类 `AIHCBaseClient`
44+
- 包含公共的请求发送方法
45+
- 所有业务模块客户端都继承自此类
46+
47+
#### aihc_common.py
48+
- 提供公共工具函数
49+
- 包含请求参数和请求体构建的通用方法
50+
51+
### 2. 业务模块 (modules/)
52+
53+
#### 任务模块 (job/)
54+
- **文件**: `job_client.py`
55+
- ****: `JobClient`
56+
- **功能**: 训练任务相关接口
57+
- **主要方法**:
58+
- `DescribeJobs()` - 查询训练任务列表
59+
- `DescribeJob()` - 查询训练任务详情
60+
- `CreateJob()` - 创建训练任务
61+
- `DeleteJob()` - 删除训练任务
62+
- `StopJob()` - 停止训练任务
63+
- `UpdateJob()` - 更新训练任务
64+
- `DescribeJobEvents()` - 查询训练任务事件
65+
- `DescribeJobLogs()` - 查询训练任务日志
66+
- `DescribeJobPodEvents()` - 查询训练任务Pod事件
67+
- `DescribeJobNodeNames()` - 查询训练任务所在节点列表
68+
- `GetJobWebTerminalUrl()` - 获取训练任务WebTerminal地址
69+
70+
#### 数据集模块 (dataset/)
71+
- **文件**: `dataset_client.py`
72+
- ****: `DatasetClient`
73+
- **功能**: 数据集相关接口
74+
- **主要方法**:
75+
- `DescribeDatasets()` - 获取数据集列表
76+
- `DescribeDataset()` - 获取数据集详情
77+
- `CreateDataset()` - 创建数据集
78+
- `ModifyDataset()` - 修改数据集
79+
- `DeleteDataset()` - 删除数据集
80+
- `DescribeDatasetVersions()` - 获取数据集版本列表
81+
- `DescribeDatasetVersion()` - 获取数据集版本详情
82+
- `CreateDatasetVersion()` - 创建数据集版本
83+
- `DeleteDatasetVersion()` - 删除数据集版本
84+
85+
#### 模型模块 (model/)
86+
- **文件**: `model_client.py`
87+
- ****: `ModelClient`
88+
- **功能**: 模型相关接口
89+
- **主要方法**:
90+
- `DescribeModels()` - 获取模型列表
91+
- `CreateModel()` - 创建模型
92+
- `DeleteModel()` - 删除模型
93+
- `ModifyModel()` - 修改模型
94+
- `DescribeModel()` - 获取模型详情
95+
- `DescribeModelVersions()` - 获取模型版本列表
96+
- `DescribeModelVersion()` - 获取模型版本详情
97+
- `CreateModelVersion()` - 新建模型版本
98+
- `DeleteModelVersion()` - 删除模型版本
99+
100+
#### 在线服务模块 (service/)
101+
- **文件**: `service_client.py`
102+
- ****: `ServiceClient`
103+
- **功能**: 在线服务部署相关接口
104+
- **主要方法**:
105+
- `DescribeServices()` - 拉取服务列表
106+
- `DescribeService()` - 查询服务详情
107+
- `DescribeServiceStatus()` - 获取服务状态
108+
109+
#### 开发机模块 (dev_instance/)
110+
- **文件**: `dev_instance_client.py`
111+
- ****: `DevInstanceClient`
112+
- **功能**: 开发机相关接口
113+
- **主要方法**:
114+
- `DescribeDevInstances()` - 查询开发机列表
115+
- `DescribeDevInstance()` - 查询开发机详情
116+
- `StartDevInstance()` - 开启开发机实例
117+
- `StopDevInstance()` - 停止开发机实例
118+
119+
## 使用方式
120+
121+
### 1. 使用主客户端(推荐)
122+
```python
123+
from baidubce.services.aihc import AihcClient
124+
from baidubce.bce_client_configuration import BceClientConfiguration
125+
126+
# 创建配置
127+
config = BceClientConfiguration()
128+
config.endpoint = 'https://aihc.bj.baidubce.com'
129+
130+
# 创建客户端
131+
client = AihcClient(config)
132+
133+
# 使用各种接口
134+
client.DescribeJobs(resourcePoolId='your-pool-id')
135+
client.DescribeDatasets()
136+
client.DescribeModels()
137+
```
138+
139+
### 2. 使用独立模块客户端
140+
```python
141+
from baidubce.services.aihc.modules.job import JobClient
142+
from baidubce.services.aihc.modules.dataset import DatasetClient
143+
144+
# 创建配置
145+
config = BceClientConfiguration()
146+
config.endpoint = 'https://aihc.bj.baidubce.com'
147+
148+
# 使用特定模块
149+
job_client = JobClient(config)
150+
dataset_client = DatasetClient(config)
151+
152+
# 调用模块特定方法
153+
job_client.DescribeJobs(resourcePoolId='your-pool-id')
154+
dataset_client.DescribeDatasets()
155+
```
156+
157+
### 3. 直接访问子模块(新特性)
158+
```python
159+
from baidubce.services.aihc import AihcClient
160+
161+
# 创建客户端
162+
client = AihcClient(config)
163+
164+
# 直接访问子模块
165+
client.job.DescribeJobs(resourcePoolId='your-pool-id')
166+
client.dataset.DescribeDatasets()
167+
client.model.DescribeModels()
168+
client.service.DescribeServices()
169+
client.dev_instance.DescribeDevInstances()
170+
```
171+
172+
## 最新改进
173+
174+
### 1. IDE类型提示优化
175+
- **问题**: 之前IDE中方法参数显示为 `any`
176+
- **解决方案**: 使用 `create_typed_proxy_method` 函数确保完整的类型信息传递
177+
- **效果**: 现在IDE正确显示方法签名,如 `DescribeDataset(datasetId: str)`
178+
179+
### 2. 属性名称优化
180+
- **变更**: 子模块属性名称更简洁
181+
- `job_client``job`
182+
- `dataset_client``dataset`
183+
- `model_client``model`
184+
- `service_client``service`
185+
- `dev_instance_client``dev_instance`
186+
187+
### 3. 避免重复注释
188+
- **改进**: 使用代理方法自动继承子模块的完整注释和类型注解
189+
- **优势**: 避免在 `AihcClient` 中重复子模块的注释,维护更简单
190+
191+
## 优势
192+
193+
### 1. 模块化
194+
- 不同业务模块独立开发,降低耦合
195+
- 单一职责,代码结构清晰
196+
197+
### 2. 可维护性
198+
- 每个模块文件大小适中,易于维护
199+
- 问题定位更精确
200+
201+
### 3. 可扩展性
202+
- 新增模块只需添加新的子模块
203+
- 不影响现有模块
204+
205+
### 4. 团队协作
206+
- 不同团队可以并行开发不同模块
207+
- 减少代码冲突
208+
209+
### 5. 可测试性
210+
- 每个模块可以独立测试
211+
- 测试覆盖更全面
212+
213+
### 6. 开发体验
214+
- IDE类型提示完整准确
215+
- 代码补全和错误检查更有效
216+
- 文档字符串自动继承
217+
218+
## 开发规范
219+
220+
### 1. 新增模块
221+
1.`modules/` 目录下创建新的模块目录
222+
2. 创建 `__init__.py` 和模块客户端文件
223+
3.`aihc_client.py` 中添加代理方法
224+
4. 更新相关文档
225+
226+
### 2. 类型注解规范
227+
- 所有公共方法必须包含完整的类型注解
228+
- 使用 `typing` 模块的类型提示
229+
- 确保文档字符串与类型注解一致
230+
231+
### 3. 代理方法规范
232+
- 使用 `create_typed_proxy_method` 函数创建代理方法
233+
- 确保类型信息和文档字符串正确传递
234+
- 避免重复注释
235+
236+
### 4. 版本管理
237+
- 每个模块可以独立版本管理
238+
- 主客户端版本号反映所有模块的兼容性
239+
240+
## 故障排除
241+
242+
### IDE类型提示问题
243+
如果IDE仍然显示 `any`,请尝试:
244+
1. 重启IDE
245+
2. 清除IDE缓存
246+
3. 重新加载项目
247+
4. 检查Python语言服务器状态
248+
249+
### 模块导入问题
250+
确保正确导入模块:
251+
```python
252+
# 正确的导入方式
253+
from baidubce.services.aihc import AihcClient
254+
```
255+
256+
## 单元测试
257+
258+
运行测试脚本验证模块化改造:
259+
```bash
260+
python test_modular_aihc.py
261+
```
262+
263+
## 更新日志
264+
265+
### v2.1.0 (最新)
266+
- ✅ 修复IDE类型提示问题
267+
- ✅ 优化子模块属性名称
268+
- ✅ 避免重复注释
269+
- ✅ 改进代理方法实现
270+
- ✅ 更新文档和示例
271+
272+
### v2.0.0
273+
- ✅ 完成模块化重构
274+
- ✅ 实现代理方法机制
275+
- ✅ 添加完整文档

baidubce/services/aihc/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2014 Baidu, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
4+
# except in compliance with the License. You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software distributed under the
9+
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10+
# either express or implied. See the License for the specific language governing permissions
11+
# and limitations under the License.
12+
13+
"""
14+
AIHC V2 service module.
15+
"""
16+
from baidubce.services.aihc.aihc_client import AihcClient
17+
18+
__all__ = ['AihcClient']

0 commit comments

Comments
 (0)