Files
algorithm/sdk/python/README.md
2026-02-18 23:39:39 +08:00

164 lines
3.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 智能算法展示平台Python SDK
本SDK提供了与智能算法展示平台API交互的Python客户端支持算法调用、仿真数据生成等功能。
## 安装
### 从源码安装
```bash
cd sdk/python
pip install -e .
```
### 从PyPI安装
```bash
pip install algorithm-showcase-sdk
```
## 使用示例
### 1. 初始化客户端
```python
from algorithm_showcase import AlgorithmShowcaseClient
# 初始化客户端
client = AlgorithmShowcaseClient(
base_url="http://localhost:8001/api/v1", # API基础URL
api_key="your-api-key" # 可选API密钥
)
```
### 2. 获取算法列表
```python
# 获取所有算法
algorithms = client.get_algorithms()
# 获取特定类型的算法
algorithms = client.get_algorithms(algorithm_type="computer_vision")
# 打印算法信息
for algo in algorithms:
print(f"算法ID: {algo.id}")
print(f"算法名称: {algo.name}")
print(f"算法描述: {algo.description}")
print(f"算法类型: {algo.type}")
print("---")
```
### 3. 获取算法详情
```python
# 获取算法详情
algorithm = client.get_algorithm(algorithm_id="algorithm-123456")
print(f"算法ID: {algorithm.id}")
print(f"算法名称: {algorithm.name}")
print(f"算法描述: {algorithm.description}")
print(f"算法类型: {algorithm.type}")
print("版本信息:")
for version in algorithm.versions:
print(f" - 版本: {version.version} (默认: {version.is_default})")
print(f" URL: {version.url}")
print(f" 参数: {version.params}")
```
### 4. 调用算法
```python
# 准备输入数据
input_data = {
"text": "这是一段测试文本"
}
# 准备算法参数
params = {
"confidence_threshold": 0.5,
"model_name": "resnet50"
}
# 调用算法
result = client.call_algorithm(
algorithm_id="algorithm-123456",
version_id="version-123456",
input_data=input_data,
params=params
)
print(f"调用ID: {result.id}")
print(f"状态: {result.status}")
print(f"响应时间: {result.response_time} 秒")
print(f"输出数据: {result.output_data}")
```
### 5. 获取调用结果
```python
# 获取调用结果
result = client.get_call_result(call_id="call-123456")
print(f"调用ID: {result.id}")
print(f"状态: {result.status}")
print(f"响应时间: {result.response_time} 秒")
print(f"输出数据: {result.output_data}")
```
### 6. 生成仿真输入数据
```python
# 生成文本数据
text_data = client.generate_simulation_data(
prompt="生成一段关于人工智能的新闻文本",
data_type="text"
)
print(f"生成的文本: {text_data['data']}")
# 生成结构化数据
structured_data = client.generate_simulation_data(
prompt="生成一个包含姓名、年龄、职业的用户信息",
data_type="structured"
)
print(f"生成的结构化数据: {structured_data['data']}")
```
## API文档
### 核心方法
- `get_algorithms(algorithm_type=None)`: 获取算法列表
- `get_algorithm(algorithm_id)`: 获取算法详情
- `get_algorithm_versions(algorithm_id)`: 获取算法版本列表
- `call_algorithm(algorithm_id, version_id, input_data, params=None)`: 调用算法
- `get_call_result(call_id)`: 获取调用结果
- `generate_simulation_data(prompt, data_type="text")`: 生成仿真输入数据
### 数据模型
- `Algorithm`: 算法信息
- `AlgorithmVersion`: 算法版本信息
- `AlgorithmCallRequest`: 算法调用请求
- `AlgorithmCallResult`: 算法调用结果
## 错误处理
SDK会捕获API请求中的错误并抛出异常。建议在使用时添加异常处理
```python
try:
result = client.call_algorithm(...)
except Exception as e:
print(f"调用失败: {e}")
```
## 版本兼容性
- Python 3.7+
- requests 2.31.0+
## 许可证
MIT License