first commit
This commit is contained in:
214
sdk/javascript/README.md
Normal file
214
sdk/javascript/README.md
Normal file
@@ -0,0 +1,214 @@
|
||||
# 智能算法展示平台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
|
||||
28
sdk/javascript/package.json
Normal file
28
sdk/javascript/package.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "algorithm-showcase-sdk",
|
||||
"version": "1.0.0",
|
||||
"description": "智能算法展示平台JavaScript SDK",
|
||||
"main": "src/index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [
|
||||
"algorithm",
|
||||
"showcase",
|
||||
"SDK",
|
||||
"API"
|
||||
],
|
||||
"author": "Algorithm Showcase Team",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"axios": "^1.6.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/algorithm-showcase/sdk"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/algorithm-showcase/sdk/issues"
|
||||
},
|
||||
"homepage": "https://github.com/algorithm-showcase/sdk#readme"
|
||||
}
|
||||
247
sdk/javascript/src/index.js
Normal file
247
sdk/javascript/src/index.js
Normal file
@@ -0,0 +1,247 @@
|
||||
/**
|
||||
* 智能算法展示平台JavaScript SDK
|
||||
*/
|
||||
|
||||
const axios = require('axios');
|
||||
|
||||
/**
|
||||
* 智能算法展示平台客户端类
|
||||
*/
|
||||
class AlgorithmShowcaseClient {
|
||||
/**
|
||||
* 初始化客户端
|
||||
* @param {string} baseUrl - API基础URL
|
||||
* @param {string} apiKey - API密钥
|
||||
*/
|
||||
constructor(baseUrl = 'http://localhost:8000/api/v1', apiKey = null) {
|
||||
this.baseUrl = baseUrl.replace(/\/$/, '');
|
||||
this.apiKey = apiKey;
|
||||
|
||||
// 创建axios实例
|
||||
this.client = axios.create({
|
||||
baseURL: this.baseUrl,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
// 如果提供了API密钥,设置认证头
|
||||
if (this.apiKey) {
|
||||
this.client.defaults.headers.common['Authorization'] = `Bearer ${this.apiKey}`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取算法列表
|
||||
* @param {string} type - 算法类型
|
||||
* @returns {Promise<Array>} 算法列表
|
||||
*/
|
||||
async getAlgorithms(type = null) {
|
||||
try {
|
||||
const params = {};
|
||||
if (type) {
|
||||
params.type = type;
|
||||
}
|
||||
|
||||
const response = await this.client.get('/algorithms', { params });
|
||||
return response.data.algorithms;
|
||||
} catch (error) {
|
||||
throw new Error(`获取算法列表失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取算法详情
|
||||
* @param {string} algorithmId - 算法ID
|
||||
* @returns {Promise<Object>} 算法详情
|
||||
*/
|
||||
async getAlgorithm(algorithmId) {
|
||||
try {
|
||||
const response = await this.client.get(`/algorithms/${algorithmId}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`获取算法详情失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取算法版本列表
|
||||
* @param {string} algorithmId - 算法ID
|
||||
* @returns {Promise<Array>} 版本列表
|
||||
*/
|
||||
async getAlgorithmVersions(algorithmId) {
|
||||
try {
|
||||
const response = await this.client.get(`/algorithms/${algorithmId}/versions`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`获取算法版本列表失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用算法
|
||||
* @param {string} algorithmId - 算法ID
|
||||
* @param {string} versionId - 版本ID
|
||||
* @param {Object} inputData - 输入数据
|
||||
* @param {Object} params - 算法参数
|
||||
* @returns {Promise<Object>} 调用结果
|
||||
*/
|
||||
async callAlgorithm(algorithmId, versionId, inputData, params = {}) {
|
||||
try {
|
||||
const response = await this.client.post('/algorithms/call', {
|
||||
algorithm_id: algorithmId,
|
||||
version_id: versionId,
|
||||
input_data: inputData,
|
||||
params: params
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`调用算法失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取调用结果
|
||||
* @param {string} callId - 调用ID
|
||||
* @returns {Promise<Object>} 调用结果
|
||||
*/
|
||||
async getCallResult(callId) {
|
||||
try {
|
||||
const response = await this.client.get(`/algorithms/calls/${callId}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`获取调用结果失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成仿真输入数据
|
||||
* @param {string} prompt - 描述
|
||||
* @param {string} dataType - 数据类型
|
||||
* @returns {Promise<Object>} 生成的数据
|
||||
*/
|
||||
async generateSimulationData(prompt, dataType = 'text') {
|
||||
try {
|
||||
const response = await this.client.post('/openai/generate-data', {
|
||||
prompt: prompt,
|
||||
data_type: dataType
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`生成仿真数据失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取调用历史
|
||||
* @param {string} algorithmId - 算法ID
|
||||
* @param {string} status - 状态
|
||||
* @param {string} startDate - 开始日期
|
||||
* @param {string} endDate - 结束日期
|
||||
* @returns {Promise<Array>} 调用历史
|
||||
*/
|
||||
async getCallHistory(algorithmId = null, status = null, startDate = null, endDate = null) {
|
||||
try {
|
||||
const params = {};
|
||||
if (algorithmId) params.algorithm_id = algorithmId;
|
||||
if (status) params.status = status;
|
||||
if (startDate) params.start_date = startDate;
|
||||
if (endDate) params.end_date = endDate;
|
||||
|
||||
const response = await this.client.get('/history/user-calls', { params });
|
||||
return response.data.history;
|
||||
} catch (error) {
|
||||
throw new Error(`获取调用历史失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取调用统计
|
||||
* @param {string} algorithmId - 算法ID
|
||||
* @returns {Promise<Object>} 统计信息
|
||||
*/
|
||||
async getCallStatistics(algorithmId = null) {
|
||||
try {
|
||||
const params = {};
|
||||
if (algorithmId) params.algorithm_id = algorithmId;
|
||||
|
||||
const response = await this.client.get('/history/statistics', { params });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`获取调用统计失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出历史记录
|
||||
* @param {string} algorithmId - 算法ID
|
||||
* @param {string} startDate - 开始日期
|
||||
* @param {string} endDate - 结束日期
|
||||
* @param {string} formatType - 导出格式
|
||||
* @returns {Promise<Object>} 导出结果
|
||||
*/
|
||||
async exportHistory(algorithmId = null, startDate = null, endDate = null, formatType = 'json') {
|
||||
try {
|
||||
const params = {
|
||||
format_type: formatType
|
||||
};
|
||||
if (algorithmId) params.algorithm_id = algorithmId;
|
||||
if (startDate) params.start_date = startDate;
|
||||
if (endDate) params.end_date = endDate;
|
||||
|
||||
const response = await this.client.get('/history/export', { params });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`导出历史记录失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取算法性能指标
|
||||
* @param {string} algorithmId - 算法ID
|
||||
* @param {number} days - 统计天数
|
||||
* @returns {Promise<Object>} 性能指标
|
||||
*/
|
||||
async getAlgorithmPerformance(algorithmId, days = 7) {
|
||||
try {
|
||||
const params = { days };
|
||||
const response = await this.client.get(`/monitoring/performance/algorithm/${algorithmId}`, { params });
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`获取算法性能指标失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统健康状况
|
||||
* @returns {Promise<Object>} 系统健康状况
|
||||
*/
|
||||
async getSystemHealth() {
|
||||
try {
|
||||
const response = await this.client.get('/monitoring/health');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw new Error(`获取系统健康状况失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查权限
|
||||
* @param {string} algorithmId - 算法ID
|
||||
* @param {string} permissionType - 权限类型
|
||||
* @returns {Promise<boolean>} 是否有权限
|
||||
*/
|
||||
async checkPermission(algorithmId, permissionType = 'execute') {
|
||||
try {
|
||||
const response = await this.client.post('/permissions/check', {
|
||||
algorithm_id: algorithmId,
|
||||
permission_type: permissionType
|
||||
});
|
||||
return response.data.has_permission;
|
||||
} catch (error) {
|
||||
throw new Error(`检查权限失败: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AlgorithmShowcaseClient;
|
||||
Reference in New Issue
Block a user