215 lines
5.0 KiB
Markdown
215 lines
5.0 KiB
Markdown
# 智能算法展示平台JavaScript SDK
|
||
|
||
本SDK提供了与智能算法展示平台API交互的JavaScript客户端,支持算法调用、仿真数据生成等功能。
|
||
|
||
## 安装
|
||
|
||
### 从源码安装
|
||
|
||
```bash
|
||
cd sdk/javascript
|
||
npm install
|
||
npm link
|
||
```
|
||
|
||
### 从npm安装
|
||
|
||
```bash
|
||
npm install algorithm-showcase-sdk
|
||
```
|
||
|
||
## 使用示例
|
||
|
||
### 1. 初始化客户端
|
||
|
||
```javascript
|
||
const AlgorithmShowcaseClient = require('algorithm-showcase-sdk');
|
||
|
||
// 初始化客户端
|
||
const client = new AlgorithmShowcaseClient(
|
||
'http://localhost:8000/api/v1', // API基础URL
|
||
'your-api-key' // 可选,API密钥
|
||
);
|
||
```
|
||
|
||
### 2. 获取算法列表
|
||
|
||
```javascript
|
||
// 获取所有算法
|
||
async function getAlgorithms() {
|
||
try {
|
||
const algorithms = await client.getAlgorithms();
|
||
|
||
// 打印算法信息
|
||
algorithms.forEach(algo => {
|
||
console.log(`算法ID: ${algo.id}`);
|
||
console.log(`算法名称: ${algo.name}`);
|
||
console.log(`算法描述: ${algo.description}`);
|
||
console.log(`算法类型: ${algo.type}`);
|
||
console.log('---');
|
||
});
|
||
} catch (error) {
|
||
console.error('获取算法列表失败:', error.message);
|
||
}
|
||
}
|
||
|
||
// 获取特定类型的算法
|
||
async function getAlgorithmsByType() {
|
||
try {
|
||
const algorithms = await client.getAlgorithms('computer_vision');
|
||
console.log('计算机视觉算法:', algorithms);
|
||
} catch (error) {
|
||
console.error('获取算法列表失败:', error.message);
|
||
}
|
||
}
|
||
|
||
getAlgorithms();
|
||
```
|
||
|
||
### 3. 获取算法详情
|
||
|
||
```javascript
|
||
// 获取算法详情
|
||
async function getAlgorithmDetail() {
|
||
try {
|
||
const algorithm = await client.getAlgorithm('algorithm-123456');
|
||
|
||
console.log(`算法ID: ${algorithm.id}`);
|
||
console.log(`算法名称: ${algorithm.name}`);
|
||
console.log(`算法描述: ${algorithm.description}`);
|
||
console.log(`算法类型: ${algorithm.type}`);
|
||
console.log('版本信息:');
|
||
algorithm.versions.forEach(version => {
|
||
console.log(` - 版本: ${version.version} (默认: ${version.is_default})`);
|
||
console.log(` URL: ${version.url}`);
|
||
console.log(` 参数: ${JSON.stringify(version.params)}`);
|
||
});
|
||
} catch (error) {
|
||
console.error('获取算法详情失败:', error.message);
|
||
}
|
||
}
|
||
|
||
getAlgorithmDetail();
|
||
```
|
||
|
||
### 4. 调用算法
|
||
|
||
```javascript
|
||
// 调用算法
|
||
async function callAlgorithm() {
|
||
try {
|
||
// 准备输入数据
|
||
const inputData = {
|
||
text: '这是一段测试文本'
|
||
};
|
||
|
||
// 准备算法参数
|
||
const params = {
|
||
confidence_threshold: 0.5,
|
||
model_name: 'resnet50'
|
||
};
|
||
|
||
// 调用算法
|
||
const result = await client.callAlgorithm(
|
||
'algorithm-123456',
|
||
'version-123456',
|
||
inputData,
|
||
params
|
||
);
|
||
|
||
console.log(`调用ID: ${result.id}`);
|
||
console.log(`状态: ${result.status}`);
|
||
console.log(`响应时间: ${result.response_time} 秒`);
|
||
console.log(`输出数据: ${JSON.stringify(result.output_data)}`);
|
||
} catch (error) {
|
||
console.error('调用算法失败:', error.message);
|
||
}
|
||
}
|
||
|
||
callAlgorithm();
|
||
```
|
||
|
||
### 5. 获取调用结果
|
||
|
||
```javascript
|
||
// 获取调用结果
|
||
async function getCallResult() {
|
||
try {
|
||
const result = await client.getCallResult('call-123456');
|
||
|
||
console.log(`调用ID: ${result.id}`);
|
||
console.log(`状态: ${result.status}`);
|
||
console.log(`响应时间: ${result.response_time} 秒`);
|
||
console.log(`输出数据: ${JSON.stringify(result.output_data)}`);
|
||
} catch (error) {
|
||
console.error('获取调用结果失败:', error.message);
|
||
}
|
||
}
|
||
|
||
getCallResult();
|
||
```
|
||
|
||
### 6. 生成仿真输入数据
|
||
|
||
```javascript
|
||
// 生成仿真输入数据
|
||
async function generateSimulationData() {
|
||
try {
|
||
// 生成文本数据
|
||
const textData = await client.generateSimulationData(
|
||
'生成一段关于人工智能的新闻文本',
|
||
'text'
|
||
);
|
||
console.log('生成的文本:', textData.data);
|
||
|
||
// 生成结构化数据
|
||
const structuredData = await client.generateSimulationData(
|
||
'生成一个包含姓名、年龄、职业的用户信息',
|
||
'structured'
|
||
);
|
||
console.log('生成的结构化数据:', structuredData.data);
|
||
} catch (error) {
|
||
console.error('生成仿真数据失败:', error.message);
|
||
}
|
||
}
|
||
|
||
generateSimulationData();
|
||
```
|
||
|
||
## API文档
|
||
|
||
### 核心方法
|
||
|
||
- `getAlgorithms(type)`: 获取算法列表
|
||
- `getAlgorithm(algorithmId)`: 获取算法详情
|
||
- `getAlgorithmVersions(algorithmId)`: 获取算法版本列表
|
||
- `callAlgorithm(algorithmId, versionId, inputData, params)`: 调用算法
|
||
- `getCallResult(callId)`: 获取调用结果
|
||
- `generateSimulationData(prompt, dataType)`: 生成仿真输入数据
|
||
|
||
## 错误处理
|
||
|
||
SDK会捕获API请求中的错误,并抛出异常。建议在使用时添加异常处理:
|
||
|
||
```javascript
|
||
try {
|
||
const result = await client.callAlgorithm(...);
|
||
console.log('调用成功:', result);
|
||
} catch (error) {
|
||
console.error('调用失败:', error.message);
|
||
}
|
||
```
|
||
|
||
## 版本兼容性
|
||
|
||
- Node.js 12+
|
||
- axios 1.6.0+
|
||
|
||
## 浏览器兼容性
|
||
|
||
本SDK基于axios,支持现代浏览器。在浏览器环境中使用时,需要注意跨域问题。
|
||
|
||
## 许可证
|
||
|
||
MIT License
|