Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add javascript call console action #722

Merged
merged 10 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/javascript/Proxy.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

1. 浏览器使用需要添加base url,
2. 需要启动 proxy, 可以在本地package.json 的scripts下面加入: "proxy": "cd bce-qianfan-sdk/javascript/proxyServer && node proxy.js" 运行 命令
或者进入nodemodules里面的千帆sdk下面的javascript里面直接运行命令: npm run proxy -- ak sk。 ak 和sk对应的位置分别输入你的access key 和secret key。
或者进入nodemodules里面的千帆sdk下面的javascript里面直接运行命令: npm run proxy -- ak sk。 ak 和sk对应的位置分别输入你的access key 和secret key。
3. proxy启动后,用浏览器打开html文件,刷新,即可在console里面查看返回结果。
![browserproxy](../docs/imgs/browserproxy.jpeg)

Expand All @@ -23,4 +23,4 @@ async function main() {
}

main();
```
```
46 changes: 46 additions & 0 deletions docs/javascript/console.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 平台功能API吧

## API列表

百度智能云千帆平台提供了丰富的平台功能OpenAPI能力,包括TPM配额管理、私有资源池服务付费、Prompt工程、模型服务、模型管理、模型调优、数据管理、系统记忆等,详情请查看平台功能OpenAPI列表。

- TPM配额管理:提供了购买TPM配额、查询配额信息等能力。
- 私有资源池服务付费:提供了购买算力单元实例、查询算力单元实例列表或信息等能力。
- 模型服务:提供创建服务、获取服务详情等API能力。
- 模型管理:提供获取模型、模型版本详情,获取用户/预置模型及将训练任务发布为模型等API能力。
- 模型调优:提供创建训练任务、任务运行、停止任务运行及获取任务运行详情等API能力。
- 数据管理:提供创建数据集等数据集管理、导入导出数据集任务、数据清洗任务管理等API能力。
- Prompt工程:提供模板管理、Prompt优化任务、评估等API能力。
- 插件应用:提供知识库、智慧图问、天气等API能力。
- 系统记忆:提供创建系统记忆、查询系统记忆等能力。
[平台功能OpenAPI介绍](https://cloud.baidu.com/doc/WENXINWORKSHOP/s/oly8ar9ai)

## 如何安装

```bash
npm install @baiducloud/qianfan
# or
yarn add @baiducloud/qianfan
```

## 快速使用

### 创建模型精调作业

```ts
import {consoleAction, setEnvVariable} from "@baiducloud/qianfan";

const client = new ChatCompletion({QIANFAN_BASE_URL: 'http://localhost:3001'});

async function consoleApi() {
const res4 = await consoleAction({base_api_route: 'v2/finetuning', action: 'CreateFineTuningJob', body: {"name":"test_name",
"description":"test_description",
"model":"ERNIE-Lite-8K-0922",
"trainMode":"SFT"}});

// 注意:name自定义,不可重名
console.log(res);
}

consoleApi();
```
2 changes: 1 addition & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@baiducloud/qianfan",
"version": "0.1.6-beta.0",
"version": "0.1.7",
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org/"
Expand Down
2 changes: 1 addition & 1 deletion javascript/src/Fetch/nodeFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class Fetch {
if (response.name === 'AbortError') {
throw new Error('Request timed out.');
}
throw new Error('Request timed out.' + response.message);
throw new Error('Request timed out.' + (response?.message || response));
}

if (!response.ok) {
Expand Down
27 changes: 19 additions & 8 deletions javascript/src/HttpClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class HttpClient {
[H.X_BCE_DATE]: new Date().toISOString().replace(/\.\d+Z$/, 'Z'),
};

constructor(private config: any, fetchConfig?: FetchConfig) {
constructor(
private config: any,
fetchConfig?: FetchConfig
) {
this.fetchInstance = new Fetch(fetchConfig);
}

Expand All @@ -40,7 +43,7 @@ class HttpClient {
async getSignature(config): Promise<any> {
const {httpMethod, path, body, headers, params, signFunction} = config;
const method = httpMethod.toUpperCase();
const requestUrl = this._getRequestUrl(path, params);
const requestUrl = this._getRequestUrl(path);
const _headers = Object.assign({}, this.defaultHeaders, headers);
if (!_headers.hasOwnProperty(H.CONTENT_LENGTH)) {
let contentLength = this._guessContentLength(body);
Expand All @@ -51,7 +54,7 @@ class HttpClient {
const url = new URLClass(requestUrl) as any;
_headers[H.HOST] = url.host;
const options = urlObjectToPlainObject(url, method, _headers);
const reqHeaders = await this.setAuthorizationHeader(signFunction, _headers, options);
const reqHeaders = await this.setAuthorizationHeader(signFunction, _headers, options, params);
const fetchOptions = {
url: options.href,
method: options.method,
Expand All @@ -66,7 +69,8 @@ class HttpClient {
| ((credentials: any, method: any, path: any, headers: any) => [string, string] | string)
| undefined,
headers: Record<string, any>,
options: any
options: any,
params: Record<string, any>
): Promise<Record<string, any>> {
if (typeof signFunction === 'function') {
const result = signFunction(this.config.credentials, options.method!, options.path!, headers);
Expand All @@ -89,13 +93,14 @@ class HttpClient {
this.config.credentials,
options.method!,
options.path!,
headers
headers,
params
);
}
return headers;
}

private _getRequestUrl(path: string, params: Record<string, any>): string {
private _getRequestUrl(path: string, params?: Record<string, any>): string {
let uri = path;
const qs = this.buildQueryString(params);
if (qs) {
Expand Down Expand Up @@ -147,9 +152,15 @@ class HttpClient {
throw new Error('No Content-Length is specified.');
}

private createSignature(credentials: any, httpMethod: string, path: string, headers: Record<string, any>): string {
private createSignature(
credentials: any,
httpMethod: string,
path: string,
headers?: Record<string, any>,
params?: Record<string, any>
): string {
const auth = new Auth(credentials.ak, credentials.sk);
return auth.generateAuthorization(httpMethod, path, {}, headers);
return auth.generateAuthorization(httpMethod, path, params, headers);
}
}

Expand Down
3 changes: 2 additions & 1 deletion javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Embedding from './Embedding';
import Plugin from './Plugin';
import {Text2Image, Image2Text} from './Images';
import Reranker from './Reranker';
import {setEnvVariable, setBrowserVariable} from './utils';
import {setEnvVariable, setBrowserVariable, consoleAction} from './utils';

export {
ChatCompletion,
Expand All @@ -30,4 +30,5 @@ export {
Reranker,
setEnvVariable,
setBrowserVariable,
consoleAction,
};
63 changes: 62 additions & 1 deletion javascript/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {BASE_PATH, DEFAULT_CONFIG} from './constant';
import HttpClient from './HttpClient';
import Fetch from './Fetch';
import {BASE_PATH, DEFAULT_CONFIG, DEFAULT_HEADERS} from './constant';
import {IAMConfig, QfLLMInfoMap, ReqBody, DefaultConfig} from './interface';
import * as packageJson from '../package.json';

Expand Down Expand Up @@ -301,4 +303,63 @@ export function setBrowserVariable(variables: Variables): void {
Object.entries(variables).forEach(([key, value]) => {
DEFAULT_CONFIG[key] = value;
});
}

function baseActionUrl(route: string, action: string): string {
if (action === '') {
return route;
}
return `${route}?Action=${action}`;
}


interface ConsoleActionParams {
base_api_route: string;
data?: Record<string, any>,
action?: string;
}

/**
* consoleApi 开放入口
*
* @param base_api_route 基础API路由,类型为字符串
* @param body 查询参数,类型为任意类型
* @param action 可选参数,方法名称,类型为字符串
* @returns 返回任意类型
*/
export async function consoleAction({
base_api_route,
data,
action,
}: ConsoleActionParams): Promise<any> {
const config = getDefaultConfig();
// IAM鉴权,先判断是否有IAM的key
if (!(config.QIANFAN_ACCESS_KEY && config.QIANFAN_SECRET_KEY)) {
throw new Error('请设置QIANFAN_ACCESS_KEY/QIANFAN_SECRET_KEY');
}
// 鉴权
const httpClientConfig = getIAMConfig(
config.QIANFAN_ACCESS_KEY,
config.QIANFAN_SECRET_KEY,
config.QIANFAN_CONSOLE_API_BASE_URL
);
const client = new HttpClient(httpClientConfig);
const fetchOptions = await client.getSignature({
httpMethod: 'POST',
path: `${config.QIANFAN_CONSOLE_API_BASE_URL}/${base_api_route}`,
body: data && JSON.stringify(data),
headers: {
...DEFAULT_HEADERS,
},
params: {'Action': action},
});
const fetchInstance = new Fetch();
try {
const {url, ...rest} = fetchOptions;
const resp = await fetchInstance.makeRequest(baseActionUrl(url, action), rest);
return resp;
}
catch (error) {
throw error;
}
}
23 changes: 21 additions & 2 deletions javascript/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ bser@2.1.1:
dependencies:
node-int64 "^0.4.0"

buffer-es6@^4.9.2:
buffer-es6@^4.9.2, buffer-es6@^4.9.3:
version "4.9.3"
resolved "https://registry.yarnpkg.com/buffer-es6/-/buffer-es6-4.9.3.tgz#f26347b82df76fd37e18bcb5288c4970cfd5c404"
integrity sha512-Ibt+oXxhmeYJSsCkODPqNpPmyegefiD8rfutH1NYGhMZQhSp95Rz7haemgnJ6dxa6LT+JLLbtgOMORRluwKktw==
Expand Down Expand Up @@ -3883,6 +3883,13 @@ ltgt@^2.1.2:
resolved "https://registry.yarnpkg.com/ltgt/-/ltgt-2.2.1.tgz#f35ca91c493f7b73da0e07495304f17b31f87ee5"
integrity sha512-AI2r85+4MquTw9ZYqabu4nMwy9Oftlfa/e/52t9IjtfG+mGBbTNdAoZ3RQKLHR6r0wQnwZnPIEh/Ya6XTWAKNA==

magic-string@^0.22.5:
version "0.22.5"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.22.5.tgz#8e9cf5afddf44385c1da5bc2a6a0dbd10b03657e"
integrity sha512-oreip9rJZkzvA8Qzk9HFs8fZGF/u7H/gtrE8EN6RjKJ9kh2HlC+yQ2QezifqTZfGyiuAV0dRv5a+y/8gBb1m9w==
dependencies:
vlq "^0.2.2"

magic-string@^0.25.3:
version "0.25.9"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.9.tgz#de7f9faf91ef8a1c91d02c2e5314c8277dbcdd1c"
Expand Down Expand Up @@ -4279,7 +4286,7 @@ pretty-format@^29.0.0, pretty-format@^29.7.0:
ansi-styles "^5.0.0"
react-is "^18.0.0"

process-es6@^0.11.2:
process-es6@^0.11.2, process-es6@^0.11.6:
version "0.11.6"
resolved "https://registry.yarnpkg.com/process-es6/-/process-es6-0.11.6.tgz#c6bb389f9a951f82bd4eb169600105bd2ff9c778"
integrity sha512-GYBRQtL4v3wgigq10Pv58jmTbFXlIiTbSfgnNqZLY0ldUPqy1rRxDI5fCjoCpnM6TqmHQI8ydzTBXW86OYc0gA==
Expand Down Expand Up @@ -4549,6 +4556,18 @@ rollup-plugin-node-builtins@^2.1.2:
crypto-browserify "^3.11.0"
process-es6 "^0.11.2"

rollup-plugin-node-globals@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/rollup-plugin-node-globals/-/rollup-plugin-node-globals-1.4.0.tgz#5e1f24a9bb97c0ef51249f625e16c7e61b7c020b"
integrity sha512-xRkB+W/m1KLIzPUmG0ofvR+CPNcvuCuNdjVBVS7ALKSxr3EDhnzNceGkGi1m8MToSli13AzKFYH4ie9w3I5L3g==
dependencies:
acorn "^5.7.3"
buffer-es6 "^4.9.3"
estree-walker "^0.5.2"
magic-string "^0.22.5"
process-es6 "^0.11.6"
rollup-pluginutils "^2.3.1"

rollup-plugin-node-polyfills@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/rollup-plugin-node-polyfills/-/rollup-plugin-node-polyfills-0.2.1.tgz#53092a2744837164d5b8a28812ba5f3ff61109fd"
Expand Down