diff --git a/dist/baseApi.cjs.map b/dist/baseApi.cjs.map deleted file mode 100644 index 58f01f9..0000000 --- a/dist/baseApi.cjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/baseApi.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;","names":["data"]} \ No newline at end of file diff --git a/dist/baseApi.d.cts b/dist/baseApi.d.cts deleted file mode 100644 index 696701c..0000000 --- a/dist/baseApi.d.cts +++ /dev/null @@ -1,2 +0,0 @@ -export { B as default, m as moreRequestOptions } from './index-hCfUd9Lh.cjs'; -import 'axios'; diff --git a/dist/baseApi.d.ts b/dist/baseApi.d.ts deleted file mode 100644 index a992aee..0000000 --- a/dist/baseApi.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { B as default, m as moreRequestOptions } from './index-hCfUd9Lh.js'; -import 'axios'; diff --git a/dist/baseApi.js.map b/dist/baseApi.js.map deleted file mode 100644 index 3fc572d..0000000 --- a/dist/baseApi.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/baseApi.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAUA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;","names":["data"]} \ No newline at end of file diff --git a/dist/completions.cjs b/dist/capability/completions.cjs similarity index 73% rename from dist/completions.cjs rename to dist/capability/completions.cjs index c2c3aba..5bd30ed 100644 --- a/dist/completions.cjs +++ b/dist/capability/completions.cjs @@ -37,33 +37,54 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/completions.ts +// lib/capability/completions.ts var completions_exports = {}; __export(completions_exports, { default: () => Completions }); module.exports = __toCommonJS(completions_exports); -// lib/baseApi.ts +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/completions.ts +// lib/capability/completions.ts var Completions = class extends BaseApi { create(options) { return __async(this, null, function* () { diff --git a/dist/capability/completions.cjs.map b/dist/capability/completions.cjs.map new file mode 100644 index 0000000..439e033 --- /dev/null +++ b/dist/capability/completions.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/capability/completions.ts","../../lib/core/baseApi.ts"],"sourcesContent":["import BaseApi from \"../core/baseApi.js\";\nimport { CreateCompletionsOptions, CompletionsResponseMessage } from \"../types/index.js\";\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;;;ADpCA,IAAqB,cAArB,cAAyC,QAAQ;AAAA,EAEhC,OAAO,SAAwE;AAAA;AACxF,aAAO,KAAK,KAAK,qBAAqB;AAAA,QAClC,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ;AAAA,QACtB,eAAe,QAAQ;AAAA,QACvB,SAAS,QAAQ;AAAA,QACjB,aAAa,QAAQ;AAAA,QACrB,cAAc,QAAQ;AAAA,QACtB,QAAQ,QAAQ;AAAA,QAChB,YAAY,QAAQ;AAAA,QACpB,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,QAChC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,eAAe,QAAQ;AAAA,MAC3B,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/capability/completions.d.cts b/dist/capability/completions.d.cts new file mode 100644 index 0000000..f85cef1 --- /dev/null +++ b/dist/capability/completions.d.cts @@ -0,0 +1,11 @@ +import BaseApi from '../core/baseApi.cjs'; +import { CreateCompletionsOptions, CompletionsResponseMessage } from '../types/completions.cjs'; +import '../core/request.cjs'; +import 'axios'; +import '../types/baseApi.cjs'; + +declare class Completions extends BaseApi { + create(options: CreateCompletionsOptions): Promise; +} + +export { Completions as default }; diff --git a/dist/capability/completions.d.ts b/dist/capability/completions.d.ts new file mode 100644 index 0000000..84458f0 --- /dev/null +++ b/dist/capability/completions.d.ts @@ -0,0 +1,11 @@ +import BaseApi from '../core/baseApi.js'; +import { CreateCompletionsOptions, CompletionsResponseMessage } from '../types/completions.js'; +import '../core/request.js'; +import 'axios'; +import '../types/baseApi.js'; + +declare class Completions extends BaseApi { + create(options: CreateCompletionsOptions): Promise; +} + +export { Completions as default }; diff --git a/dist/completions.js b/dist/capability/completions.js similarity index 64% rename from dist/completions.js rename to dist/capability/completions.js index 100c26d..eae8d76 100644 --- a/dist/completions.js +++ b/dist/capability/completions.js @@ -19,26 +19,47 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/baseApi.ts +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/completions.ts +// lib/capability/completions.ts var Completions = class extends BaseApi { create(options) { return __async(this, null, function* () { diff --git a/dist/capability/completions.js.map b/dist/capability/completions.js.map new file mode 100644 index 0000000..14b2fd6 --- /dev/null +++ b/dist/capability/completions.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/baseApi.ts","../../lib/capability/completions.ts"],"sourcesContent":["import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateCompletionsOptions, CompletionsResponseMessage } from \"../types/index.js\";\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;;;ACpCA,IAAqB,cAArB,cAAyC,QAAQ;AAAA,EAEhC,OAAO,SAAwE;AAAA;AACxF,aAAO,KAAK,KAAK,qBAAqB;AAAA,QAClC,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ;AAAA,QACtB,eAAe,QAAQ;AAAA,QACvB,SAAS,QAAQ;AAAA,QACjB,aAAa,QAAQ;AAAA,QACrB,cAAc,QAAQ;AAAA,QACtB,QAAQ,QAAQ;AAAA,QAChB,YAAY,QAAQ;AAAA,QACpB,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,QAChC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,eAAe,QAAQ;AAAA,MAC3B,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/embeddings.cjs b/dist/capability/embeddings.cjs similarity index 71% rename from dist/embeddings.cjs rename to dist/capability/embeddings.cjs index 1953d50..dafe10f 100644 --- a/dist/embeddings.cjs +++ b/dist/capability/embeddings.cjs @@ -37,33 +37,54 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/embeddings.ts +// lib/capability/embeddings.ts var embeddings_exports = {}; __export(embeddings_exports, { default: () => Embeddings }); module.exports = __toCommonJS(embeddings_exports); -// lib/baseApi.ts +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/embeddings.ts +// lib/capability/embeddings.ts var Embeddings = class extends BaseApi { create(options) { return __async(this, null, function* () { diff --git a/dist/capability/embeddings.cjs.map b/dist/capability/embeddings.cjs.map new file mode 100644 index 0000000..9cf9b6d --- /dev/null +++ b/dist/capability/embeddings.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/capability/embeddings.ts","../../lib/core/baseApi.ts"],"sourcesContent":["import BaseApi from \"../core/baseApi.js\";\nimport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"../types/index.js\";\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} ","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;;;ADpCA,IAAqB,aAArB,cAAwC,QAAQ;AAAA,EAC/B,OAAO,SAA+D;AAAA;AAC/E,aAAO,KAAK,KAAK,eAAe;AAAA,QAC5B,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,MACpC,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/capability/embeddings.d.cts b/dist/capability/embeddings.d.cts new file mode 100644 index 0000000..dd22ebb --- /dev/null +++ b/dist/capability/embeddings.d.cts @@ -0,0 +1,11 @@ +import BaseApi from '../core/baseApi.cjs'; +import { CreateEmbeddingsOptions, EmbeddingsResponse } from '../types/embeddings.cjs'; +import '../core/request.cjs'; +import 'axios'; +import '../types/baseApi.cjs'; + +declare class Embeddings extends BaseApi { + create(options: CreateEmbeddingsOptions): Promise; +} + +export { Embeddings as default }; diff --git a/dist/capability/embeddings.d.ts b/dist/capability/embeddings.d.ts new file mode 100644 index 0000000..943288f --- /dev/null +++ b/dist/capability/embeddings.d.ts @@ -0,0 +1,11 @@ +import BaseApi from '../core/baseApi.js'; +import { CreateEmbeddingsOptions, EmbeddingsResponse } from '../types/embeddings.js'; +import '../core/request.js'; +import 'axios'; +import '../types/baseApi.js'; + +declare class Embeddings extends BaseApi { + create(options: CreateEmbeddingsOptions): Promise; +} + +export { Embeddings as default }; diff --git a/dist/embeddings.js b/dist/capability/embeddings.js similarity index 60% rename from dist/embeddings.js rename to dist/capability/embeddings.js index dbe98b3..40f18c2 100644 --- a/dist/embeddings.js +++ b/dist/capability/embeddings.js @@ -19,26 +19,47 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/baseApi.ts +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/embeddings.ts +// lib/capability/embeddings.ts var Embeddings = class extends BaseApi { create(options) { return __async(this, null, function* () { diff --git a/dist/capability/embeddings.js.map b/dist/capability/embeddings.js.map new file mode 100644 index 0000000..a3df3b1 --- /dev/null +++ b/dist/capability/embeddings.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/baseApi.ts","../../lib/capability/embeddings.ts"],"sourcesContent":["import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"../types/index.js\";\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;;;ACpCA,IAAqB,aAArB,cAAwC,QAAQ;AAAA,EAC/B,OAAO,SAA+D;AAAA;AAC/E,aAAO,KAAK,KAAK,eAAe;AAAA,QAC5B,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,MACpC,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/capability/files.cjs b/dist/capability/files.cjs new file mode 100644 index 0000000..288fcf9 --- /dev/null +++ b/dist/capability/files.cjs @@ -0,0 +1,108 @@ +"use strict"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var __async = (__this, __arguments, generator) => { + return new Promise((resolve, reject) => { + var fulfilled = (value) => { + try { + step(generator.next(value)); + } catch (e) { + reject(e); + } + }; + var rejected = (value) => { + try { + step(generator.throw(value)); + } catch (e) { + reject(e); + } + }; + var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); + step((generator = generator.apply(__this, __arguments)).next()); + }); +}; + +// lib/capability/files.ts +var files_exports = {}; +__export(files_exports, { + default: () => Files +}); +module.exports = __toCommonJS(files_exports); + +// lib/core/baseApi.ts +var BaseApi = class { + constructor(request) { + this.request = request; + } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } + post(url, data, options) { + return __async(this, null, function* () { + return this.request.post(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } +}; + +// lib/capability/files.ts +var Files = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + const formData = new FormData(); + formData.append("purpose", options.purpose); + formData.append("file", options.file); + return this.postForm("/files", formData, options); + }); + } + findList(options) { + return __async(this, null, function* () { + return this.get("/files", { + "purpose": options.purpose, + "limit": options.limit, + "after": options.after, + "order": options.order + }, options); + }); + } +}; +//# sourceMappingURL=files.cjs.map \ No newline at end of file diff --git a/dist/capability/files.cjs.map b/dist/capability/files.cjs.map new file mode 100644 index 0000000..5ff9dcf --- /dev/null +++ b/dist/capability/files.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/capability/files.ts","../../lib/core/baseApi.ts"],"sourcesContent":["import BaseApi from \"../core/baseApi.js\";\nimport { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from \"../types/index.js\";\n\nexport default class Files extends BaseApi {\n public async create(options: CreateFileOptions): Promise {\n const formData = new FormData()\n formData.append(\"purpose\", options.purpose);\n formData.append(\"file\", options.file);\n return this.postForm(\"/files\", formData, options)\n }\n\n public async findList(options: FindFileListOptions): Promise {\n return this.get(\"/files\", {\n \"purpose\": options.purpose,\n \"limit\": options.limit,\n \"after\": options.after,\n \"order\": options.order,\n }, options)\n }\n} ","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;;;ADpCA,IAAqB,QAArB,cAAmC,QAAQ;AAAA,EAC1B,OAAO,SAAmD;AAAA;AACnE,YAAM,WAAW,IAAI,SAAS;AAC9B,eAAS,OAAO,WAAW,QAAQ,OAAO;AAC1C,eAAS,OAAO,QAAQ,QAAQ,IAAI;AACpC,aAAO,KAAK,SAAS,UAAU,UAAU,OAAO;AAAA,IACpD;AAAA;AAAA,EAEa,SAAS,SAAyD;AAAA;AAC3E,aAAO,KAAK,IAAI,UAAU;AAAA,QACtB,WAAW,QAAQ;AAAA,QACnB,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,MACrB,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/capability/files.d.cts b/dist/capability/files.d.cts new file mode 100644 index 0000000..a4e5dc0 --- /dev/null +++ b/dist/capability/files.d.cts @@ -0,0 +1,12 @@ +import BaseApi from '../core/baseApi.cjs'; +import { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from '../types/files.cjs'; +import '../core/request.cjs'; +import 'axios'; +import '../types/baseApi.cjs'; + +declare class Files extends BaseApi { + create(options: CreateFileOptions): Promise; + findList(options: FindFileListOptions): Promise; +} + +export { Files as default }; diff --git a/dist/capability/files.d.ts b/dist/capability/files.d.ts new file mode 100644 index 0000000..4162163 --- /dev/null +++ b/dist/capability/files.d.ts @@ -0,0 +1,12 @@ +import BaseApi from '../core/baseApi.js'; +import { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from '../types/files.js'; +import '../core/request.js'; +import 'axios'; +import '../types/baseApi.js'; + +declare class Files extends BaseApi { + create(options: CreateFileOptions): Promise; + findList(options: FindFileListOptions): Promise; +} + +export { Files as default }; diff --git a/dist/capability/files.js b/dist/capability/files.js new file mode 100644 index 0000000..8ebb8a6 --- /dev/null +++ b/dist/capability/files.js @@ -0,0 +1,86 @@ +var __async = (__this, __arguments, generator) => { + return new Promise((resolve, reject) => { + var fulfilled = (value) => { + try { + step(generator.next(value)); + } catch (e) { + reject(e); + } + }; + var rejected = (value) => { + try { + step(generator.throw(value)); + } catch (e) { + reject(e); + } + }; + var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); + step((generator = generator.apply(__this, __arguments)).next()); + }); +}; + +// lib/core/baseApi.ts +var BaseApi = class { + constructor(request) { + this.request = request; + } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } + post(url, data, options) { + return __async(this, null, function* () { + return this.request.post(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } +}; + +// lib/capability/files.ts +var Files = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + const formData = new FormData(); + formData.append("purpose", options.purpose); + formData.append("file", options.file); + return this.postForm("/files", formData, options); + }); + } + findList(options) { + return __async(this, null, function* () { + return this.get("/files", { + "purpose": options.purpose, + "limit": options.limit, + "after": options.after, + "order": options.order + }, options); + }); + } +}; +export { + Files as default +}; +//# sourceMappingURL=files.js.map \ No newline at end of file diff --git a/dist/capability/files.js.map b/dist/capability/files.js.map new file mode 100644 index 0000000..d86b341 --- /dev/null +++ b/dist/capability/files.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/baseApi.ts","../../lib/capability/files.ts"],"sourcesContent":["import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from \"../types/index.js\";\n\nexport default class Files extends BaseApi {\n public async create(options: CreateFileOptions): Promise {\n const formData = new FormData()\n formData.append(\"purpose\", options.purpose);\n formData.append(\"file\", options.file);\n return this.postForm(\"/files\", formData, options)\n }\n\n public async findList(options: FindFileListOptions): Promise {\n return this.get(\"/files\", {\n \"purpose\": options.purpose,\n \"limit\": options.limit,\n \"after\": options.after,\n \"order\": options.order,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAE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AS,QAAQ;AAAA,MACrB,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/images.cjs b/dist/capability/images.cjs similarity index 71% rename from dist/images.cjs rename to dist/capability/images.cjs index 3d7b35c..6d1d140 100644 --- a/dist/images.cjs +++ b/dist/capability/images.cjs @@ -37,33 +37,54 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/images.ts +// lib/capability/images.ts var images_exports = {}; __export(images_exports, { default: () => Images }); module.exports = __toCommonJS(images_exports); -// lib/baseApi.ts +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/images.ts +// lib/capability/images.ts var Images = class extends BaseApi { create(options) { return __async(this, null, function* () { diff --git a/dist/capability/images.cjs.map b/dist/capability/images.cjs.map new file mode 100644 index 0000000..3460e18 --- /dev/null +++ b/dist/capability/images.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/capability/images.ts","../../lib/core/baseApi.ts"],"sourcesContent":["import BaseApi from \"../core/baseApi.js\";\nimport { CreateImagesOptions, ImagesResponse } from \"../types/index.js\";\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;;;ADpCA,IAAqB,SAArB,cAAoC,QAAQ;AAAA,EAC3B,OAAO,SAAuD;AAAA;AACvE,aAAO,KAAK,KAAK,uBAAuB;AAAA,QACpC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,KAAK,QAAQ;AAAA,QACb,WAAW,QAAQ;AAAA,QACnB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,SAAS,QAAQ;AAAA,QACjB,QAAQ,QAAQ;AAAA,MACpB,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/capability/images.d.cts b/dist/capability/images.d.cts new file mode 100644 index 0000000..1086ff5 --- /dev/null +++ b/dist/capability/images.d.cts @@ -0,0 +1,11 @@ +import BaseApi from '../core/baseApi.cjs'; +import { CreateImagesOptions, ImagesResponse } from '../types/images.cjs'; +import '../core/request.cjs'; +import 'axios'; +import '../types/baseApi.cjs'; + +declare class Images extends BaseApi { + create(options: CreateImagesOptions): Promise; +} + +export { Images as default }; diff --git a/dist/capability/images.d.ts b/dist/capability/images.d.ts new file mode 100644 index 0000000..494d93a --- /dev/null +++ b/dist/capability/images.d.ts @@ -0,0 +1,11 @@ +import BaseApi from '../core/baseApi.js'; +import { CreateImagesOptions, ImagesResponse } from '../types/images.js'; +import '../core/request.js'; +import 'axios'; +import '../types/baseApi.js'; + +declare class Images extends BaseApi { + create(options: CreateImagesOptions): Promise; +} + +export { Images as default }; diff --git a/dist/images.js b/dist/capability/images.js similarity index 61% rename from dist/images.js rename to dist/capability/images.js index 7d90a80..5c85296 100644 --- a/dist/images.js +++ b/dist/capability/images.js @@ -19,26 +19,47 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/baseApi.ts +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/images.ts +// lib/capability/images.ts var Images = class extends BaseApi { create(options) { return __async(this, null, function* () { diff --git a/dist/capability/images.js.map b/dist/capability/images.js.map new file mode 100644 index 0000000..f371d78 --- /dev/null +++ b/dist/capability/images.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/baseApi.ts","../../lib/capability/images.ts"],"sourcesContent":["import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateImagesOptions, ImagesResponse } from \"../types/index.js\";\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;;;ACpCA,IAAqB,SAArB,cAAoC,QAAQ;AAAA,EAC3B,OAAO,SAAuD;AAAA;AACvE,aAAO,KAAK,KAAK,uBAAuB;AAAA,QACpC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,KAAK,QAAQ;AAAA,QACb,WAAW,QAAQ;AAAA,QACnB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,SAAS,QAAQ;AAAA,QACjB,QAAQ,QAAQ;AAAA,MACpB,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/capability/index.cjs b/dist/capability/index.cjs new file mode 100644 index 0000000..4b963e7 --- /dev/null +++ b/dist/capability/index.cjs @@ -0,0 +1,150 @@ +"use strict"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); +var __async = (__this, __arguments, generator) => { + return new Promise((resolve, reject) => { + var fulfilled = (value) => { + try { + step(generator.next(value)); + } catch (e) { + reject(e); + } + }; + var rejected = (value) => { + try { + step(generator.throw(value)); + } catch (e) { + reject(e); + } + }; + var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); + step((generator = generator.apply(__this, __arguments)).next()); + }); +}; + +// lib/capability/index.ts +var capability_exports = {}; +__export(capability_exports, { + Completions: () => Completions, + Embeddings: () => Embeddings, + Images: () => Images +}); +module.exports = __toCommonJS(capability_exports); + +// lib/core/baseApi.ts +var BaseApi = class { + constructor(request) { + this.request = request; + } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } + post(url, data, options) { + return __async(this, null, function* () { + return this.request.post(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } +}; + +// lib/capability/completions.ts +var Completions = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + return this.post("/chat/completions", { + "model": options.model, + "request_id": options.requestId, + "temperature": options.temperature, + "top_p": options.topP, + "do_sample": options.doSample, + "max_tokens": options.maxTokens, + "seed": options.seed, + "messages": options.messages, + "stop": options.stop, + "sensitive_word_check": options.sensitiveWordCheck, + "stream": options.stream, + "tools": options.tools, + "tool_choice": options.toolChoice + }, options); + }); + } +}; + +// lib/capability/images.ts +var Images = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + return this.post("/images/generations", { + "prompt": options.prompt, + "model": options.model, + "n": options.n, + "quality": options.quality, + "response_format": options.responseFormat, + "size": options.size, + "style": options.style, + "user": options.user + }, options); + }); + } +}; + +// lib/capability/embeddings.ts +var Embeddings = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + return this.post("/embeddings", { + "input": options.input, + "model": options.model, + "encoding_format": options.encodingFormat, + "user": options.user, + "sensitive_word_check": options.sensitiveWordCheck + }, options); + }); + } +}; +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + Completions, + Embeddings, + Images +}); +//# sourceMappingURL=index.cjs.map \ No newline at end of file diff --git a/dist/capability/index.cjs.map b/dist/capability/index.cjs.map new file mode 100644 index 0000000..f7005e6 --- /dev/null +++ b/dist/capability/index.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/capability/index.ts","../../lib/core/baseApi.ts","../../lib/capability/completions.ts","../../lib/capability/images.ts","../../lib/capability/embeddings.ts"],"sourcesContent":["export { default as Completions } from \"./completions.js\"\nexport { default as Images } from \"./images.js\"\nexport { default as Embeddings } from \"./embeddings.js\"","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateCompletionsOptions, CompletionsResponseMessage } from \"../types/index.js\";\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateImagesOptions, ImagesResponse } from \"../types/index.js\";\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"../types/index.js\";\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA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AAAA;AACvE,aAAO,KAAK,KAAK,uBAAuB;AAAA,QACpC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,KAAK,QAAQ;AAAA,QACb,WAAW,QAAQ;AAAA,QACnB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,SAAS,QAAQ;AAAA,QACjB,QAAQ,QAAQ;AAAA,MACpB,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;;;ACbA,IAAqB,aAArB,cAAwC,QAAQ;AAAA,EAC/B,OAAO,SAA+D;AAAA;AAC/E,aAAO,KAAK,KAAK,eAAe;AAAA,QAC5B,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,MACpC,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/capability/index.d.cts b/dist/capability/index.d.cts new file mode 100644 index 0000000..610cb9e --- /dev/null +++ b/dist/capability/index.d.cts @@ -0,0 +1,10 @@ +export { default as Completions } from './completions.cjs'; +export { default as Images } from './images.cjs'; +export { default as Embeddings } from './embeddings.cjs'; +import '../core/baseApi.cjs'; +import '../core/request.cjs'; +import 'axios'; +import '../types/baseApi.cjs'; +import '../types/completions.cjs'; +import '../types/images.cjs'; +import '../types/embeddings.cjs'; diff --git a/dist/capability/index.d.ts b/dist/capability/index.d.ts new file mode 100644 index 0000000..52ed212 --- /dev/null +++ b/dist/capability/index.d.ts @@ -0,0 +1,10 @@ +export { default as Completions } from './completions.js'; +export { default as Images } from './images.js'; +export { default as Embeddings } from './embeddings.js'; +import '../core/baseApi.js'; +import '../core/request.js'; +import 'axios'; +import '../types/baseApi.js'; +import '../types/completions.js'; +import '../types/images.js'; +import '../types/embeddings.js'; diff --git a/dist/capability/index.js b/dist/capability/index.js new file mode 100644 index 0000000..338cd9c --- /dev/null +++ b/dist/capability/index.js @@ -0,0 +1,122 @@ +var __async = (__this, __arguments, generator) => { + return new Promise((resolve, reject) => { + var fulfilled = (value) => { + try { + step(generator.next(value)); + } catch (e) { + reject(e); + } + }; + var rejected = (value) => { + try { + step(generator.throw(value)); + } catch (e) { + reject(e); + } + }; + var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected); + step((generator = generator.apply(__this, __arguments)).next()); + }); +}; + +// lib/core/baseApi.ts +var BaseApi = class { + constructor(request) { + this.request = request; + } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } + post(url, data, options) { + return __async(this, null, function* () { + return this.request.post(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } +}; + +// lib/capability/completions.ts +var Completions = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + return this.post("/chat/completions", { + "model": options.model, + "request_id": options.requestId, + "temperature": options.temperature, + "top_p": options.topP, + "do_sample": options.doSample, + "max_tokens": options.maxTokens, + "seed": options.seed, + "messages": options.messages, + "stop": options.stop, + "sensitive_word_check": options.sensitiveWordCheck, + "stream": options.stream, + "tools": options.tools, + "tool_choice": options.toolChoice + }, options); + }); + } +}; + +// lib/capability/images.ts +var Images = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + return this.post("/images/generations", { + "prompt": options.prompt, + "model": options.model, + "n": options.n, + "quality": options.quality, + "response_format": options.responseFormat, + "size": options.size, + "style": options.style, + "user": options.user + }, options); + }); + } +}; + +// lib/capability/embeddings.ts +var Embeddings = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + return this.post("/embeddings", { + "input": options.input, + "model": options.model, + "encoding_format": options.encodingFormat, + "user": options.user, + "sensitive_word_check": options.sensitiveWordCheck + }, options); + }); + } +}; +export { + Completions, + Embeddings, + Images +}; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/capability/index.js.map b/dist/capability/index.js.map new file mode 100644 index 0000000..4e3b101 --- /dev/null +++ b/dist/capability/index.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/baseApi.ts","../../lib/capability/completions.ts","../../lib/capability/images.ts","../../lib/capability/embeddings.ts"],"sourcesContent":["import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateCompletionsOptions, CompletionsResponseMessage } from \"../types/index.js\";\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateImagesOptions, ImagesResponse } from \"../types/index.js\";\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"../types/index.js\";\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;A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ACbA,IAAqB,aAArB,cAAwC,QAAQ;AAAA,EAC/B,OAAO,SAA+D;AAAA;AAC/E,aAAO,KAAK,KAAK,eAAe;AAAA,QAC5B,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,MACpC,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/completions.cjs.map b/dist/completions.cjs.map deleted file mode 100644 index 7e54831..0000000 --- a/dist/completions.cjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/completions.ts","../lib/baseApi.ts"],"sourcesContent":["import BaseApi from \"./baseApi\";\n\nexport type MessageOptions = {\n role: \"system\" | \"user\" | \"assistant\" | \"function\"\n content: string\n}\n\nexport type CreateCompletionsOptions = {\n model: string,\n messages: Array,\n requestId?: string,\n doSample?: boolean,\n stream?: boolean,\n temperature?: number,\n topP?: number,\n maxTokens?: number,\n seed?: number,\n stop?: Array,\n sensitiveWordCheck?: object,\n tools?: object,\n toolChoice?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type CompletionsResponseMessage = {\n id: string,\n created: number,\n model: string,\n choices: Array<{\n index: number,\n finish_reason: string,\n message: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n },\n delta: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n }\n }>,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number,\n },\n}\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACUA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;;;ADuCA,IAAqB,cAArB,cAAyC,QAA8D;AAAA,EAEtF,OAAO,SAAwE;AAAA;AACxF,aAAO,KAAK,KAAK,qBAAqB;AAAA,QAClC,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ;AAAA,QACtB,eAAe,QAAQ;AAAA,QACvB,SAAS,QAAQ;AAAA,QACjB,aAAa,QAAQ;AAAA,QACrB,cAAc,QAAQ;AAAA,QACtB,QAAQ,QAAQ;AAAA,QAChB,YAAY,QAAQ;AAAA,QACpB,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,QAChC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,eAAe,QAAQ;AAAA,MAC3B,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":["data"]} \ No newline at end of file diff --git a/dist/completions.d.cts b/dist/completions.d.cts deleted file mode 100644 index ad4e84b..0000000 --- a/dist/completions.d.cts +++ /dev/null @@ -1,2 +0,0 @@ -export { b as CompletionsResponseMessage, a as CreateCompletionsOptions, M as MessageOptions, C as default } from './index-hCfUd9Lh.cjs'; -import 'axios'; diff --git a/dist/completions.d.ts b/dist/completions.d.ts deleted file mode 100644 index 4ea5b75..0000000 --- a/dist/completions.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { b as CompletionsResponseMessage, a as CreateCompletionsOptions, M as MessageOptions, C as default } from './index-hCfUd9Lh.js'; -import 'axios'; diff --git a/dist/completions.js.map b/dist/completions.js.map deleted file mode 100644 index 3b90bde..0000000 --- a/dist/completions.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/baseApi.ts","../lib/completions.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type MessageOptions = {\n role: \"system\" | \"user\" | \"assistant\" | \"function\"\n content: string\n}\n\nexport type CreateCompletionsOptions = {\n model: string,\n messages: Array,\n requestId?: string,\n doSample?: boolean,\n stream?: boolean,\n temperature?: number,\n topP?: number,\n maxTokens?: number,\n seed?: number,\n stop?: Array,\n sensitiveWordCheck?: object,\n tools?: object,\n toolChoice?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type CompletionsResponseMessage = {\n id: string,\n created: number,\n model: string,\n choices: Array<{\n index: number,\n finish_reason: string,\n message: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n },\n delta: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n }\n }>,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number,\n },\n}\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAUA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;;;ACuCA,IAAqB,cAArB,cAAyC,QAA8D;AAAA,EAEtF,OAAO,SAAwE;AAAA;AACxF,aAAO,KAAK,KAAK,qBAAqB;AAAA,QAClC,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ;AAAA,QACtB,eAAe,QAAQ;AAAA,QACvB,SAAS,QAAQ;AAAA,QACjB,aAAa,QAAQ;AAAA,QACrB,cAAc,QAAQ;AAAA,QACtB,QAAQ,QAAQ;AAAA,QAChB,YAAY,QAAQ;AAAA,QACpB,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,QAChC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,eAAe,QAAQ;AAAA,MAC3B,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":["data"]} \ No newline at end of file diff --git a/dist/baseApi.cjs b/dist/core/baseApi.cjs similarity index 68% rename from dist/baseApi.cjs rename to dist/core/baseApi.cjs index aef6e4f..6450973 100644 --- a/dist/baseApi.cjs +++ b/dist/core/baseApi.cjs @@ -37,7 +37,7 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/baseApi.ts +// lib/core/baseApi.ts var baseApi_exports = {}; __export(baseApi_exports, { default: () => BaseApi @@ -47,16 +47,37 @@ var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; diff --git a/dist/core/baseApi.cjs.map b/dist/core/baseApi.cjs.map new file mode 100644 index 0000000..4344909 --- /dev/null +++ b/dist/core/baseApi.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/baseApi.ts"],"sourcesContent":["import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/core/baseApi.d.cts b/dist/core/baseApi.d.cts new file mode 100644 index 0000000..0725312 --- /dev/null +++ b/dist/core/baseApi.d.cts @@ -0,0 +1,14 @@ +import Request from './request.cjs'; +import { BaseRequestOptions } from '../types/baseApi.cjs'; +import 'axios'; + +declare class BaseApi { + private readonly request; + constructor(request: Request); + private processError; + get(url: string, params: object, options: Options): Promise; + post(url: string, data: object, options: Options): Promise; + postForm(url: string, data: FormData, options: Options): Promise; +} + +export { BaseApi as default }; diff --git a/dist/core/baseApi.d.ts b/dist/core/baseApi.d.ts new file mode 100644 index 0000000..bc9985a --- /dev/null +++ b/dist/core/baseApi.d.ts @@ -0,0 +1,14 @@ +import Request from './request.js'; +import { BaseRequestOptions } from '../types/baseApi.js'; +import 'axios'; + +declare class BaseApi { + private readonly request; + constructor(request: Request); + private processError; + get(url: string, params: object, options: Options): Promise; + post(url: string, data: object, options: Options): Promise; + postForm(url: string, data: FormData, options: Options): Promise; +} + +export { BaseApi as default }; diff --git a/dist/baseApi.js b/dist/core/baseApi.js similarity index 52% rename from dist/baseApi.js rename to dist/core/baseApi.js index 9060937..cbe9ae1 100644 --- a/dist/baseApi.js +++ b/dist/core/baseApi.js @@ -19,21 +19,42 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/baseApi.ts +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; diff --git a/dist/core/baseApi.js.map b/dist/core/baseApi.js.map new file mode 100644 index 0000000..cdd6bed --- /dev/null +++ b/dist/core/baseApi.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/baseApi.ts"],"sourcesContent":["import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAGA,IAAqB,UAArB,MAA6B;AAAA,EACzB,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEQ,aAAa,KAAU;AAPnC;AAQQ,UAAM,QAAO,sCAAK,aAAL,mBAAe,SAAf,YAAuB;AACpC,WAAO,QAAQ,OAAO,IAAI;AAAA,EAC9B;AAAA,EAEa,IAAkD,KAAa,QAAgB,SAA2C;AAAA;AACnI,aAAO,KAAK,QAAQ,IAAI,KAAK;AAAA,QACzB;AAAA,QACA,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,KAAmD,KAAa,MAAc,SAA2C;AAAA;AAClI,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AAAA,EAEa,SAAuD,KAAa,MAAgB,SAA2C;AAAA;AACxI,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM;AAAA,QACpC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,KAAK,YAAY;AAAA,IAChC;AAAA;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/jwt.cjs b/dist/core/jwt.cjs similarity index 99% rename from dist/jwt.cjs rename to dist/core/jwt.cjs index 79ca73b..0051f44 100644 --- a/dist/jwt.cjs +++ b/dist/core/jwt.cjs @@ -27,7 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); -// lib/jwt.ts +// lib/core/jwt.ts var jwt_exports = {}; __export(jwt_exports, { generateToken: () => generateToken diff --git a/dist/core/jwt.cjs.map b/dist/core/jwt.cjs.map new file mode 100644 index 0000000..65dc1fe --- /dev/null +++ b/dist/core/jwt.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/jwt.ts"],"sourcesContent":["import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAAyB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,oBAAAA,QAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,EAAE,KAAK,SAAS,WAAW,OAAO;AAAA,IAC9C,CAAC;AACD,WAAO;AAAA,EACX,SAAS,GAAG;AACR,UAAM;AAAA,EACV;AACJ;","names":["jsonwebtoken"]} \ No newline at end of file diff --git a/dist/jwt.d.cts b/dist/core/jwt.d.cts similarity index 100% rename from dist/jwt.d.cts rename to dist/core/jwt.d.cts diff --git a/dist/jwt.d.ts b/dist/core/jwt.d.ts similarity index 100% rename from dist/jwt.d.ts rename to dist/core/jwt.d.ts diff --git a/dist/jwt.js b/dist/core/jwt.js similarity index 97% rename from dist/jwt.js rename to dist/core/jwt.js index 7a0aa77..cc0909d 100644 --- a/dist/jwt.js +++ b/dist/core/jwt.js @@ -1,4 +1,4 @@ -// lib/jwt.ts +// lib/core/jwt.ts import jsonwebtoken from "jsonwebtoken"; var API_TOKEN_TTL_SECONDS = 3 * 60; var generateToken = (apiSecretKey) => { diff --git a/dist/core/jwt.js.map b/dist/core/jwt.js.map new file mode 100644 index 0000000..8aa5ac0 --- /dev/null +++ b/dist/core/jwt.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/jwt.ts"],"sourcesContent":["import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}\n"],"mappings":";AAAA,OAAO,kBAAkB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,aAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,EAAE,KAAK,SAAS,WAAW,OAAO;AAAA,IAC9C,CAAC;AACD,WAAO;AAAA,EACX,SAAS,GAAG;AACR,UAAM;AAAA,EACV;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/request.cjs b/dist/core/request.cjs similarity index 92% rename from dist/request.cjs rename to dist/core/request.cjs index 2209192..4dec615 100644 --- a/dist/request.cjs +++ b/dist/core/request.cjs @@ -52,7 +52,7 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/request.ts +// lib/core/request.ts var request_exports = {}; __export(request_exports, { default: () => Request @@ -60,12 +60,11 @@ __export(request_exports, { module.exports = __toCommonJS(request_exports); var import_axios = __toESM(require("axios"), 1); var Request = class { - constructor(app, config) { - this.app = app; + constructor(config, authHeaders) { __publicField(this, "request"); this.request = import_axios.default.create(config); this.request.interceptors.request.use((config2) => { - config2.headers.set(this.app.authHeaders()); + config2.headers.set(authHeaders()); return config2; }); } @@ -74,6 +73,11 @@ var Request = class { return this.request.post(url, data, config).then((res) => res.data); }); } + postForm(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } get(url, config) { return __async(this, null, function* () { return this.request.get(url, config).then((res) => res.data); diff --git a/dist/core/request.cjs.map b/dist/core/request.cjs.map new file mode 100644 index 0000000..ab613cb --- /dev/null +++ b/dist/core/request.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/request.ts"],"sourcesContent":["import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig, AxiosHeaderValue } from \"axios\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(config: CreateAxiosDefaults, authHeaders: () => { [key: string]: AxiosHeaderValue }) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA+G;AAE/G,IAAqB,UAArB,MAA6B;AAAA,EAGzB,YAAY,QAA6B,aAAwD;AAFjG,wBAAO;AAGH,SAAK,UAAU,aAAAA,QAAM,OAAO,MAAM;AAElC,SAAK,QAAQ,aAAa,QAAQ,IAAI,CAACC,YAAW;AAC9C,MAAAA,QAAO,QAAQ,IAAI,YAAY,CAAC;AAChC,aAAOA;AAAA,IACX,CAAC;AAAA,EACL;AAAA,EAEM,KAA6C,KAAa,MAAU,QAA4C;AAAA;AAClH,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACpE;AAAA;AAAA,EAEM,SAAoD,KAAa,MAAS,QAA4C;AAAA;AACxH,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACpE;AAAA;AAAA,EAEM,IAA4C,KAAa,QAA4C;AAAA;AACvG,aAAO,KAAK,QAAQ,IAAI,KAAK,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IAC7D;AAAA;AAAA,EAEM,IAA4C,KAAa,MAAU,QAA4C;AAAA;AACjH,aAAO,KAAK,QAAQ,IAAI,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACnE;AAAA;AAAA,EAEM,OAA+C,KAAa,QAA4C;AAAA;AAC1G,aAAO,KAAK,QAAQ,OAAO,KAAK,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IAChE;AAAA;AACJ;","names":["Axios","config"]} \ No newline at end of file diff --git a/dist/core/request.d.cts b/dist/core/request.d.cts new file mode 100644 index 0000000..a9c7292 --- /dev/null +++ b/dist/core/request.d.cts @@ -0,0 +1,15 @@ +import { AxiosInstance, CreateAxiosDefaults, AxiosHeaderValue, AxiosResponse, AxiosRequestConfig } from 'axios'; + +declare class Request { + request: AxiosInstance; + constructor(config: CreateAxiosDefaults, authHeaders: () => { + [key: string]: AxiosHeaderValue; + }); + post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise; + get, D = any>(url: string, config?: AxiosRequestConfig): Promise; + put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + delete, D = any>(url: string, config?: AxiosRequestConfig): Promise; +} + +export { Request as default }; diff --git a/dist/core/request.d.ts b/dist/core/request.d.ts new file mode 100644 index 0000000..a9c7292 --- /dev/null +++ b/dist/core/request.d.ts @@ -0,0 +1,15 @@ +import { AxiosInstance, CreateAxiosDefaults, AxiosHeaderValue, AxiosResponse, AxiosRequestConfig } from 'axios'; + +declare class Request { + request: AxiosInstance; + constructor(config: CreateAxiosDefaults, authHeaders: () => { + [key: string]: AxiosHeaderValue; + }); + post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise; + get, D = any>(url: string, config?: AxiosRequestConfig): Promise; + put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; + delete, D = any>(url: string, config?: AxiosRequestConfig): Promise; +} + +export { Request as default }; diff --git a/dist/request.js b/dist/core/request.js similarity index 87% rename from dist/request.js rename to dist/core/request.js index 31d8c99..7822a9b 100644 --- a/dist/request.js +++ b/dist/core/request.js @@ -25,15 +25,14 @@ var __async = (__this, __arguments, generator) => { }); }; -// lib/request.ts +// lib/core/request.ts import Axios from "axios"; var Request = class { - constructor(app, config) { - this.app = app; + constructor(config, authHeaders) { __publicField(this, "request"); this.request = Axios.create(config); this.request.interceptors.request.use((config2) => { - config2.headers.set(this.app.authHeaders()); + config2.headers.set(authHeaders()); return config2; }); } @@ -42,6 +41,11 @@ var Request = class { return this.request.post(url, data, config).then((res) => res.data); }); } + postForm(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } get(url, config) { return __async(this, null, function* () { return this.request.get(url, config).then((res) => res.data); diff --git a/dist/core/request.js.map b/dist/core/request.js.map new file mode 100644 index 0000000..cff5d2a --- /dev/null +++ b/dist/core/request.js.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/core/request.ts"],"sourcesContent":["import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig, AxiosHeaderValue } from \"axios\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(config: CreateAxiosDefaults, authHeaders: () => { [key: string]: AxiosHeaderValue }) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,WAAwG;AAE/G,IAAqB,UAArB,MAA6B;AAAA,EAGzB,YAAY,QAA6B,aAAwD;AAFjG,wBAAO;AAGH,SAAK,UAAU,MAAM,OAAO,MAAM;AAElC,SAAK,QAAQ,aAAa,QAAQ,IAAI,CAACA,YAAW;AAC9C,MAAAA,QAAO,QAAQ,IAAI,YAAY,CAAC;AAChC,aAAOA;AAAA,IACX,CAAC;AAAA,EACL;AAAA,EAEM,KAA6C,KAAa,MAAU,QAA4C;AAAA;AAClH,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACpE;AAAA;AAAA,EAEM,SAAoD,KAAa,MAAS,QAA4C;AAAA;AACxH,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACpE;AAAA;AAAA,EAEM,IAA4C,KAAa,QAA4C;AAAA;AACvG,aAAO,KAAK,QAAQ,IAAI,KAAK,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IAC7D;AAAA;AAAA,EAEM,IAA4C,KAAa,MAAU,QAA4C;AAAA;AACjH,aAAO,KAAK,QAAQ,IAAI,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACnE;AAAA;AAAA,EAEM,OAA+C,KAAa,QAA4C;AAAA;AAC1G,aAAO,KAAK,QAAQ,OAAO,KAAK,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IAChE;AAAA;AACJ;","names":["config"]} \ No newline at end of file diff --git a/dist/embeddings.cjs.map b/dist/embeddings.cjs.map deleted file mode 100644 index ffe3e84..0000000 --- a/dist/embeddings.cjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/embeddings.ts","../lib/baseApi.ts"],"sourcesContent":["import BaseApi from \"./baseApi\";\n\nexport type CreateEmbeddingsOptions = {\n input: Array | string,\n model: string,\n encodingFormat: string,\n user: string,\n sensitiveWordCheck: object,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type EmbeddingsResponse = {\n model: string,\n data: Array<{\n index: number,\n object: string,\n embedding: Array\n }>,\n object: string,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number\n }\n}\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} ","import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACUA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;;;ADEA,IAAqB,aAArB,cAAwC,QAAqD;AAAA,EAC5E,OAAO,SAA+D;AAAA;AAC/E,aAAO,KAAK,KAAK,eAAe;AAAA,QAC5B,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,MACpC,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":["data"]} \ No newline at end of file diff --git a/dist/embeddings.d.cts b/dist/embeddings.d.cts deleted file mode 100644 index 1d3d0b9..0000000 --- a/dist/embeddings.d.cts +++ /dev/null @@ -1,2 +0,0 @@ -export { e as CreateEmbeddingsOptions, f as EmbeddingsResponse, E as default } from './index-hCfUd9Lh.cjs'; -import 'axios'; diff --git a/dist/embeddings.d.ts b/dist/embeddings.d.ts deleted file mode 100644 index 767c5e7..0000000 --- a/dist/embeddings.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { e as CreateEmbeddingsOptions, f as EmbeddingsResponse, E as default } from './index-hCfUd9Lh.js'; -import 'axios'; diff --git a/dist/embeddings.js.map b/dist/embeddings.js.map deleted file mode 100644 index d78115d..0000000 --- a/dist/embeddings.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/baseApi.ts","../lib/embeddings.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type CreateEmbeddingsOptions = {\n input: Array | string,\n model: string,\n encodingFormat: string,\n user: string,\n sensitiveWordCheck: object,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type EmbeddingsResponse = {\n model: string,\n data: Array<{\n index: number,\n object: string,\n embedding: Array\n }>,\n object: string,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number\n }\n}\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAUA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;;;ACEA,IAAqB,aAArB,cAAwC,QAAqD;AAAA,EAC5E,OAAO,SAA+D;AAAA;AAC/E,aAAO,KAAK,KAAK,eAAe;AAAA,QAC5B,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,wBAAwB,QAAQ;AAAA,MACpC,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":["data"]} \ No newline at end of file diff --git a/dist/images.cjs.map b/dist/images.cjs.map deleted file mode 100644 index 6bc4f89..0000000 --- a/dist/images.cjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/images.ts","../lib/baseApi.ts"],"sourcesContent":["import BaseApi from \"./baseApi\";\n\nexport type CreateImagesOptions = {\n model: string,\n prompt?: string,\n n?: number,\n quality?: string,\n responseFormat?: string,\n size?: string,\n style?: string,\n user?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type ImagesResponse = {\n created: number,\n data: Array\n}\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACUA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;;;ADLA,IAAqB,SAArB,cAAoC,QAA6C;AAAA,EAChE,OAAO,SAAuD;AAAA;AACvE,aAAO,KAAK,KAAK,uBAAuB;AAAA,QACpC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,KAAK,QAAQ;AAAA,QACb,WAAW,QAAQ;AAAA,QACnB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,SAAS,QAAQ;AAAA,QACjB,QAAQ,QAAQ;AAAA,MACpB,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":["data"]} \ No newline at end of file diff --git a/dist/images.d.cts b/dist/images.d.cts deleted file mode 100644 index 14924e7..0000000 --- a/dist/images.d.cts +++ /dev/null @@ -1,2 +0,0 @@ -export { c as CreateImagesOptions, d as ImagesResponse, I as default } from './index-hCfUd9Lh.cjs'; -import 'axios'; diff --git a/dist/images.d.ts b/dist/images.d.ts deleted file mode 100644 index 9a03af5..0000000 --- a/dist/images.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { c as CreateImagesOptions, d as ImagesResponse, I as default } from './index-hCfUd9Lh.js'; -import 'axios'; diff --git a/dist/images.js.map b/dist/images.js.map deleted file mode 100644 index 2fb5375..0000000 --- a/dist/images.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/baseApi.ts","../lib/images.ts"],"sourcesContent":["import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type CreateImagesOptions = {\n model: string,\n prompt?: string,\n n?: number,\n quality?: string,\n responseFormat?: string,\n size?: string,\n style?: string,\n user?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type ImagesResponse = {\n created: number,\n data: Array\n}\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAUA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;;;ACLA,IAAqB,SAArB,cAAoC,QAA6C;AAAA,EAChE,OAAO,SAAuD;AAAA;AACvE,aAAO,KAAK,KAAK,uBAAuB;AAAA,QACpC,UAAU,QAAQ;AAAA,QAClB,SAAS,QAAQ;AAAA,QACjB,KAAK,QAAQ;AAAA,QACb,WAAW,QAAQ;AAAA,QACnB,mBAAmB,QAAQ;AAAA,QAC3B,QAAQ,QAAQ;AAAA,QAChB,SAAS,QAAQ;AAAA,QACjB,QAAQ,QAAQ;AAAA,MACpB,GAAG,OAAO;AAAA,IACd;AAAA;AACJ;","names":["data"]} \ No newline at end of file diff --git a/dist/index-hCfUd9Lh.d.cts b/dist/index-hCfUd9Lh.d.cts deleted file mode 100644 index d6f9897..0000000 --- a/dist/index-hCfUd9Lh.d.cts +++ /dev/null @@ -1,154 +0,0 @@ -import { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from 'axios'; - -type MessageOptions = { - role: "system" | "user" | "assistant" | "function"; - content: string; -}; -type CreateCompletionsOptions = { - model: string; - messages: Array; - requestId?: string; - doSample?: boolean; - stream?: boolean; - temperature?: number; - topP?: number; - maxTokens?: number; - seed?: number; - stop?: Array; - sensitiveWordCheck?: object; - tools?: object; - toolChoice?: string; - extraHeaders?: object; - timeout?: number; -}; -type CompletionsResponseMessage = { - id: string; - created: number; - model: string; - choices: Array<{ - index: number; - finish_reason: string; - message: { - role: string; - content: string; - tool_calls: Array<{ - id: string; - type: string; - function: { - name: string; - arguments: object; - }; - }>; - }; - delta: { - role: string; - content: string; - tool_calls: Array<{ - id: string; - type: string; - function: { - name: string; - arguments: object; - }; - }>; - }; - }>; - usage: { - prompt_tokens: number; - completion_tokens: number; - total_tokens: number; - }; -}; -declare class Completions extends BaseApi { - create(options: CreateCompletionsOptions): Promise; -} - -type CreateImagesOptions = { - model: string; - prompt?: string; - n?: number; - quality?: string; - responseFormat?: string; - size?: string; - style?: string; - user?: string; - extraHeaders?: object; - timeout?: number; -}; -type ImagesResponse = { - created: number; - data: Array; -}; -declare class Images extends BaseApi { - create(options: CreateImagesOptions): Promise; -} - -type CreateEmbeddingsOptions = { - input: Array | string; - model: string; - encodingFormat: string; - user: string; - sensitiveWordCheck: object; - extraHeaders?: object; - timeout?: number; -}; -type EmbeddingsResponse = { - model: string; - data: Array<{ - index: number; - object: string; - embedding: Array; - }>; - object: string; - usage: { - prompt_tokens: number; - completion_tokens: number; - total_tokens: number; - }; -}; -declare class Embeddings extends BaseApi { - create(options: CreateEmbeddingsOptions): Promise; -} - -type ZhipuAIOptions = { - apiKey: string; - baseUrl?: string; - timeout?: number; - maxRetries?: number; - customHeaders?: object; -}; -declare class ZhipuAI { - private readonly options; - __esModule: boolean; - request: Request; - constructor(options: ZhipuAIOptions); - createCompletions(options: CreateCompletionsOptions): Promise; - createImages(options: CreateImagesOptions): Promise; - createEmbeddings(options: CreateEmbeddingsOptions): Promise; - authHeaders(): { - Authorization: never; - }; -} - -declare class Request { - private readonly app; - request: AxiosInstance; - constructor(app: ZhipuAI, config: CreateAxiosDefaults); - post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; - get, D = any>(url: string, config?: AxiosRequestConfig): Promise; - put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; - delete, D = any>(url: string, config?: AxiosRequestConfig): Promise; -} - -type moreRequestOptions = { - extraHeaders?: object; - timeout?: number; - stream?: boolean; -}; -declare class BaseApi { - private readonly request; - constructor(request: Request); - post(url: string, data: object, options: Options & moreRequestOptions): Promise; -} - -export { BaseApi as B, Completions as C, Embeddings as E, Images as I, type MessageOptions as M, Request as R, ZhipuAI as Z, type CreateCompletionsOptions as a, type CompletionsResponseMessage as b, type CreateImagesOptions as c, type ImagesResponse as d, type CreateEmbeddingsOptions as e, type EmbeddingsResponse as f, type ZhipuAIOptions as g, type moreRequestOptions as m }; diff --git a/dist/index-hCfUd9Lh.d.ts b/dist/index-hCfUd9Lh.d.ts deleted file mode 100644 index d6f9897..0000000 --- a/dist/index-hCfUd9Lh.d.ts +++ /dev/null @@ -1,154 +0,0 @@ -import { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from 'axios'; - -type MessageOptions = { - role: "system" | "user" | "assistant" | "function"; - content: string; -}; -type CreateCompletionsOptions = { - model: string; - messages: Array; - requestId?: string; - doSample?: boolean; - stream?: boolean; - temperature?: number; - topP?: number; - maxTokens?: number; - seed?: number; - stop?: Array; - sensitiveWordCheck?: object; - tools?: object; - toolChoice?: string; - extraHeaders?: object; - timeout?: number; -}; -type CompletionsResponseMessage = { - id: string; - created: number; - model: string; - choices: Array<{ - index: number; - finish_reason: string; - message: { - role: string; - content: string; - tool_calls: Array<{ - id: string; - type: string; - function: { - name: string; - arguments: object; - }; - }>; - }; - delta: { - role: string; - content: string; - tool_calls: Array<{ - id: string; - type: string; - function: { - name: string; - arguments: object; - }; - }>; - }; - }>; - usage: { - prompt_tokens: number; - completion_tokens: number; - total_tokens: number; - }; -}; -declare class Completions extends BaseApi { - create(options: CreateCompletionsOptions): Promise; -} - -type CreateImagesOptions = { - model: string; - prompt?: string; - n?: number; - quality?: string; - responseFormat?: string; - size?: string; - style?: string; - user?: string; - extraHeaders?: object; - timeout?: number; -}; -type ImagesResponse = { - created: number; - data: Array; -}; -declare class Images extends BaseApi { - create(options: CreateImagesOptions): Promise; -} - -type CreateEmbeddingsOptions = { - input: Array | string; - model: string; - encodingFormat: string; - user: string; - sensitiveWordCheck: object; - extraHeaders?: object; - timeout?: number; -}; -type EmbeddingsResponse = { - model: string; - data: Array<{ - index: number; - object: string; - embedding: Array; - }>; - object: string; - usage: { - prompt_tokens: number; - completion_tokens: number; - total_tokens: number; - }; -}; -declare class Embeddings extends BaseApi { - create(options: CreateEmbeddingsOptions): Promise; -} - -type ZhipuAIOptions = { - apiKey: string; - baseUrl?: string; - timeout?: number; - maxRetries?: number; - customHeaders?: object; -}; -declare class ZhipuAI { - private readonly options; - __esModule: boolean; - request: Request; - constructor(options: ZhipuAIOptions); - createCompletions(options: CreateCompletionsOptions): Promise; - createImages(options: CreateImagesOptions): Promise; - createEmbeddings(options: CreateEmbeddingsOptions): Promise; - authHeaders(): { - Authorization: never; - }; -} - -declare class Request { - private readonly app; - request: AxiosInstance; - constructor(app: ZhipuAI, config: CreateAxiosDefaults); - post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; - get, D = any>(url: string, config?: AxiosRequestConfig): Promise; - put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise; - delete, D = any>(url: string, config?: AxiosRequestConfig): Promise; -} - -type moreRequestOptions = { - extraHeaders?: object; - timeout?: number; - stream?: boolean; -}; -declare class BaseApi { - private readonly request; - constructor(request: Request); - post(url: string, data: object, options: Options & moreRequestOptions): Promise; -} - -export { BaseApi as B, Completions as C, Embeddings as E, Images as I, type MessageOptions as M, Request as R, ZhipuAI as Z, type CreateCompletionsOptions as a, type CompletionsResponseMessage as b, type CreateImagesOptions as c, type ImagesResponse as d, type CreateEmbeddingsOptions as e, type EmbeddingsResponse as f, type ZhipuAIOptions as g, type moreRequestOptions as m }; diff --git a/dist/index.cjs b/dist/index.cjs index 1d98bc9..03f33ab 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -66,7 +66,7 @@ module.exports = __toCommonJS(lib_exports); // lib/zhipu-ai.ts var import_assert = __toESM(require("assert"), 1); -// lib/jwt.ts +// lib/core/jwt.ts var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1); var API_TOKEN_TTL_SECONDS = 3 * 60; var generateToken = (apiSecretKey) => { @@ -87,26 +87,85 @@ var generateToken = (apiSecretKey) => { } }; -// lib/baseApi.ts +// lib/core/request.ts +var import_axios = __toESM(require("axios"), 1); +var Request = class { + constructor(config, authHeaders) { + __publicField(this, "request"); + this.request = import_axios.default.create(config); + this.request.interceptors.request.use((config2) => { + config2.headers.set(authHeaders()); + return config2; + }); + } + post(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } + postForm(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } + get(url, config) { + return __async(this, null, function* () { + return this.request.get(url, config).then((res) => res.data); + }); + } + put(url, data, config) { + return __async(this, null, function* () { + return this.request.put(url, data, config).then((res) => res.data); + }); + } + delete(url, config) { + return __async(this, null, function* () { + return this.request.delete(url, config).then((res) => res.data); + }); + } +}; + +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/completions.ts +// lib/capability/completions.ts var Completions = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -129,41 +188,7 @@ var Completions = class extends BaseApi { } }; -// lib/request.ts -var import_axios = __toESM(require("axios"), 1); -var Request = class { - constructor(app, config) { - this.app = app; - __publicField(this, "request"); - this.request = import_axios.default.create(config); - this.request.interceptors.request.use((config2) => { - config2.headers.set(this.app.authHeaders()); - return config2; - }); - } - post(url, data, config) { - return __async(this, null, function* () { - return this.request.post(url, data, config).then((res) => res.data); - }); - } - get(url, config) { - return __async(this, null, function* () { - return this.request.get(url, config).then((res) => res.data); - }); - } - put(url, data, config) { - return __async(this, null, function* () { - return this.request.put(url, data, config).then((res) => res.data); - }); - } - delete(url, config) { - return __async(this, null, function* () { - return this.request.delete(url, config).then((res) => res.data); - }); - } -}; - -// lib/images.ts +// lib/capability/images.ts var Images = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -181,7 +206,7 @@ var Images = class extends BaseApi { } }; -// lib/embeddings.ts +// lib/capability/embeddings.ts var Embeddings = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -196,12 +221,38 @@ var Embeddings = class extends BaseApi { } }; +// lib/capability/files.ts +var Files = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + const formData = new FormData(); + formData.append("purpose", options.purpose); + formData.append("file", options.file); + return this.postForm("/files", formData, options); + }); + } + findList(options) { + return __async(this, null, function* () { + return this.get("/files", { + "purpose": options.purpose, + "limit": options.limit, + "after": options.after, + "order": options.order + }, options); + }); + } +}; + // lib/zhipu-ai.ts var ZhipuAI = class { constructor(options) { this.options = options; __publicField(this, "__esModule", false); __publicField(this, "request"); + __publicField(this, "completions"); + __publicField(this, "images"); + __publicField(this, "embeddings"); + __publicField(this, "files"); if (!options.apiKey) options.apiKey = process.env["ZHIPUAI_API_KEY"] || ""; import_assert.default.ok(options.apiKey, "\u672A\u63D0\u4F9Bapi_key\uFF0C\u8BF7\u901A\u8FC7\u53C2\u6570\u6216\u73AF\u5883\u53D8\u91CF\u63D0\u4F9B"); @@ -209,25 +260,39 @@ var ZhipuAI = class { options.baseUrl = process.env["ZHIPUAI_BASE_URL"] || ""; if (!options.baseUrl) options.baseUrl = "https://open.bigmodel.cn/api/paas/v4"; - this.request = new Request(this, { + this.request = new Request({ timeout: options.timeout, headers: options.customHeaders, baseURL: options.baseUrl - }); + }, this.authHeaders.bind(this)); + this.completions = new Completions(this.request); + this.images = new Images(this.request); + this.embeddings = new Embeddings(this.request); + this.files = new Files(this.request); } createCompletions(options) { return __async(this, null, function* () { - return new Completions(this.request).create(options); + return this.completions.create(options); }); } createImages(options) { return __async(this, null, function* () { - return new Images(this.request).create(options); + return this.images.create(options); }); } createEmbeddings(options) { return __async(this, null, function* () { - return new Embeddings(this.request).create(options); + return this.embeddings.create(options); + }); + } + createFiles(options) { + return __async(this, null, function* () { + return this.files.create(options); + }); + } + findFiles() { + return __async(this, arguments, function* (options = {}) { + return this.files.findList(options); }); } authHeaders() { diff --git a/dist/index.cjs.map b/dist/index.cjs.map index 373bd49..8536279 100644 --- a/dist/index.cjs.map +++ b/dist/index.cjs.map @@ -1 +1 @@ -{"version":3,"sources":["../lib/index.ts","../lib/zhipu-ai.ts","../lib/jwt.ts","../lib/baseApi.ts","../lib/completions.ts","../lib/request.ts","../lib/images.ts","../lib/embeddings.ts"],"sourcesContent":["export { default as ZhipuAI } from \"./zhipu-ai.js\"\nexport { default as Completions } from \"./completions.js\"\nexport { default as Images } from \"./images.js\"\nexport { default as Embeddings } from \"./embeddings.js\"\nexport { default } from \"./zhipu-ai.js\"","import assert from \"assert\"\nimport { generateToken } from \"./jwt.js\"\nimport Completions, { CompletionsResponseMessage, CreateCompletionsOptions } from \"./completions.js\"\nimport Request from \"./request.js\"\nimport Images, { CreateImagesOptions, ImagesResponse } from \"./images.js\"\nimport Embeddings, { CreateEmbeddingsOptions, EmbeddingsResponse } from \"./embeddings.js\"\n\nexport type ZhipuAIOptions = {\n apiKey: string,\n baseUrl?: string,\n timeout?: number,\n maxRetries?: number,\n customHeaders?: object\n}\n\nexport class ZhipuAI {\n public __esModule = false\n public request: Request\n\n constructor(private readonly options: ZhipuAIOptions) {\n if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || ''\n assert.ok(options.apiKey, \"未提供api_key,请通过参数或环境变量提供\")\n if (!options.baseUrl) options.baseUrl = process.env[\"ZHIPUAI_BASE_URL\"] || ''\n if (!options.baseUrl) options.baseUrl = \"https://open.bigmodel.cn/api/paas/v4\"\n this.request = new Request(this, {\n timeout: options.timeout,\n headers: options.customHeaders,\n baseURL: options.baseUrl\n });\n }\n\n public async createCompletions(options: CreateCompletionsOptions): Promise {\n return new Completions(this.request).create(options)\n }\n\n public async createImages(options: CreateImagesOptions): Promise {\n return new Images(this.request).create(options)\n }\n\n public async createEmbeddings(options: CreateEmbeddingsOptions): Promise {\n return new Embeddings(this.request).create(options)\n }\n\n public authHeaders() {\n const token = generateToken(this.options.apiKey)\n return { \"Authorization\": token }\n }\n}\n\nexport default ZhipuAI","import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}","import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type MessageOptions = {\n role: \"system\" | \"user\" | \"assistant\" | \"function\"\n content: string\n}\n\nexport type CreateCompletionsOptions = {\n model: string,\n messages: Array,\n requestId?: string,\n doSample?: boolean,\n stream?: boolean,\n temperature?: number,\n topP?: number,\n maxTokens?: number,\n seed?: number,\n stop?: Array,\n sensitiveWordCheck?: object,\n tools?: object,\n toolChoice?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type CompletionsResponseMessage = {\n id: string,\n created: number,\n model: string,\n choices: Array<{\n index: number,\n finish_reason: string,\n message: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n },\n delta: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n }\n }>,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number,\n },\n}\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from \"axios\";\nimport ZhipuAI from \"./zhipu-ai\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(private readonly app: ZhipuAI, config: CreateAxiosDefaults) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(this.app.authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}","import BaseApi from \"./baseApi\";\n\nexport type CreateImagesOptions = {\n model: string,\n prompt?: string,\n n?: number,\n quality?: string,\n responseFormat?: string,\n size?: string,\n style?: string,\n user?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type ImagesResponse = {\n created: number,\n data: Array\n}\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type CreateEmbeddingsOptions = {\n input: Array | string,\n model: string,\n encodingFormat: string,\n user: string,\n sensitiveWordCheck: object,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type EmbeddingsResponse = {\n model: string,\n data: Array<{\n index: number,\n object: string,\n embedding: Array\n }>,\n object: string,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number\n }\n}\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAmB;;;ACAnB,0BAAyB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,oBAAAA,QAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,EAAE,KAAK,SAAS,WAAW,OAAO;AAAA,IAC9C,CAAC;AACD,WAAO;AAAA,EACX,SAAS,GAAG;AACR,UAAM;AAAA,EACV;AACJ;;;ACZA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ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C3E,QAAI,CAAC,QAAQ;AAAS,cAAQ,UAAU;AACxC,SAAK,UAAU,IAAI,QAAQ,MAAM;AAAA,MAC7B,SAAS,QAAQ;AAAA,MACjB,SAAS,QAAQ;AAAA,MACjB,SAAS,QAAQ;AAAA,IACrB,CAAC;AAAA,EACL;AAAA,EAEa,kBAAkB,SAAwE;AAAA;AACnG,aAAO,IAAI,YAAY,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IACvD;AAAA;AAAA,EAEa,aAAa,SAAuD;AAAA;AAC7E,aAAO,IAAI,OAAO,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IAClD;AAAA;AAAA,EAEa,iBAAiB,SAA+D;AAAA;AACzF,aAAO,IAAI,WAAW,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IACtD;AAAA;AAAA,EAEO,cAAc;AACjB,UAAM,QAAQ,cAAc,KAAK,QAAQ,MAAM;AAC/C,WAAO,EAAE,iBAAiB,MAAM;AAAA,EACpC;AACJ;AAEA,IAAO,mBAAQ;","names":["jsonwebtoken","data","Axios","config","assert"]} \ No newline at end of file +{"version":3,"sources":["../lib/index.ts","../lib/zhipu-ai.ts","../lib/core/jwt.ts","../lib/core/request.ts","../lib/core/baseApi.ts","../lib/capability/completions.ts","../lib/capability/images.ts","../lib/capability/embeddings.ts","../lib/capability/files.ts"],"sourcesContent":["export { default as ZhipuAI } from \"./zhipu-ai.js\"\nexport { default } from \"./zhipu-ai.js\"\nexport { Completions, Images, Embeddings } from \"./capability/index.js\"","import assert from \"assert\"\nimport { generateToken } from \"./core/jwt.js\"\nimport Request from \"./core/request.js\"\n\nimport { Completions, Images, Embeddings } from \"./capability\"\n\nimport {\n CompletionsResponseMessage, CreateCompletionsOptions,\n CreateImagesOptions, ImagesResponse,\n CreateEmbeddingsOptions, EmbeddingsResponse,\n CreateFileOptions, FileResponse, FileListResponse, FindFileListOptions,\n} from \"./types\"\nimport Files from \"./capability/files.js\"\n\nexport type ZhipuAIOptions = {\n apiKey: string,\n baseUrl?: string,\n timeout?: number,\n maxRetries?: number,\n customHeaders?: object\n}\n\nexport class ZhipuAI {\n public __esModule = false\n public request: Request\n public completions: Completions\n public images: Images\n public embeddings: Embeddings\n public files: Files\n\n\n constructor(private readonly options: ZhipuAIOptions) {\n if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || ''\n assert.ok(options.apiKey, \"未提供api_key,请通过参数或环境变量提供\")\n if (!options.baseUrl) options.baseUrl = process.env[\"ZHIPUAI_BASE_URL\"] || ''\n if (!options.baseUrl) options.baseUrl = \"https://open.bigmodel.cn/api/paas/v4\"\n this.request = new Request({\n timeout: options.timeout,\n headers: options.customHeaders,\n baseURL: options.baseUrl\n }, this.authHeaders.bind(this));\n\n this.completions = new Completions(this.request)\n this.images = new Images(this.request)\n this.embeddings = new Embeddings(this.request)\n this.files = new Files(this.request)\n }\n\n public async createCompletions(options: CreateCompletionsOptions): Promise {\n return this.completions.create(options)\n }\n\n public async createImages(options: CreateImagesOptions): Promise {\n return this.images.create(options)\n }\n\n public async createEmbeddings(options: CreateEmbeddingsOptions): Promise {\n return this.embeddings.create(options)\n }\n\n public async createFiles(options: CreateFileOptions): Promise {\n return this.files.create(options)\n }\n\n public async findFiles(options: FindFileListOptions = {}): Promise {\n return this.files.findList(options)\n }\n\n public authHeaders(): { [key: string]: string } {\n const token = generateToken(this.options.apiKey)\n return { \"Authorization\": token }\n }\n}\n\nexport default ZhipuAI","import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}\n","import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig, AxiosHeaderValue } from \"axios\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(config: CreateAxiosDefaults, authHeaders: () => { [key: string]: AxiosHeaderValue }) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateCompletionsOptions, CompletionsResponseMessage } from \"../types/index.js\";\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateImagesOptions, ImagesResponse } from \"../types/index.js\";\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"../types/index.js\";\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from \"../types/index.js\";\n\nexport default class Files extends BaseApi {\n public async create(options: CreateFileOptions): Promise {\n const formData = new FormData()\n formData.append(\"purpose\", options.purpose);\n formData.append(\"file\", options.file);\n return this.postForm(\"/files\", formData, options)\n }\n\n public async findList(options: FindFileListOptions): Promise {\n return this.get(\"/files\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C;AAAA,EAEa,kBAAkB,SAAwE;AAAA;AACnG,aAAO,KAAK,YAAY,OAAO,OAAO;AAAA,IAC1C;AAAA;AAAA,EAEa,aAAa,SAAuD;AAAA;AAC7E,aAAO,KAAK,OAAO,OAAO,OAAO;AAAA,IACrC;AAAA;AAAA,EAEa,iBAAiB,SAA+D;AAAA;AACzF,aAAO,KAAK,WAAW,OAAO,OAAO;AAAA,IACzC;AAAA;AAAA,EAEa,YAAY,SAAmD;AAAA;AACxE,aAAO,KAAK,MAAM,OAAO,OAAO;AAAA,IACpC;AAAA;AAAA,EAEa,YAAwE;AAAA,+CAA9D,UAA+B,CAAC,GAA8B;AACjF,aAAO,KAAK,MAAM,SAAS,OAAO;AAAA,IACtC;AAAA;AAAA,EAEO,cAAyC;AAC5C,UAAM,QAAQ,cAAc,KAAK,QAAQ,MAAM;AAC/C,WAAO,EAAE,iBAAiB,MAAM;AAAA,EACpC;AACJ;AAEA,IAAO,mBAAQ;","names":["jsonwebtoken","Axios","config","assert"]} \ No newline at end of file diff --git a/dist/index.d.cts b/dist/index.d.cts index db25be3..8552b78 100644 --- a/dist/index.d.cts +++ b/dist/index.d.cts @@ -1,2 +1,13 @@ -export { C as Completions, E as Embeddings, I as Images, Z as ZhipuAI, Z as default } from './index-hCfUd9Lh.cjs'; +export { default as ZhipuAI, default } from './zhipu-ai.cjs'; +export { default as Completions } from './capability/completions.cjs'; +export { default as Images } from './capability/images.cjs'; +export { default as Embeddings } from './capability/embeddings.cjs'; +import './core/request.cjs'; import 'axios'; +import './types/completions.cjs'; +import './types/baseApi.cjs'; +import './types/images.cjs'; +import './types/embeddings.cjs'; +import './types/files.cjs'; +import './capability/files.cjs'; +import './core/baseApi.cjs'; diff --git a/dist/index.d.ts b/dist/index.d.ts index 9f2017a..c5a8f57 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,2 +1,13 @@ -export { C as Completions, E as Embeddings, I as Images, Z as ZhipuAI, Z as default } from './index-hCfUd9Lh.js'; +export { default as ZhipuAI, default } from './zhipu-ai.js'; +export { default as Completions } from './capability/completions.js'; +export { default as Images } from './capability/images.js'; +export { default as Embeddings } from './capability/embeddings.js'; +import './core/request.js'; import 'axios'; +import './types/completions.js'; +import './types/baseApi.js'; +import './types/images.js'; +import './types/embeddings.js'; +import './types/files.js'; +import './capability/files.js'; +import './core/baseApi.js'; diff --git a/dist/index.js b/dist/index.js index f33c882..b2624a7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -28,7 +28,7 @@ var __async = (__this, __arguments, generator) => { // lib/zhipu-ai.ts import assert from "assert"; -// lib/jwt.ts +// lib/core/jwt.ts import jsonwebtoken from "jsonwebtoken"; var API_TOKEN_TTL_SECONDS = 3 * 60; var generateToken = (apiSecretKey) => { @@ -49,26 +49,85 @@ var generateToken = (apiSecretKey) => { } }; -// lib/baseApi.ts +// lib/core/request.ts +import Axios from "axios"; +var Request = class { + constructor(config, authHeaders) { + __publicField(this, "request"); + this.request = Axios.create(config); + this.request.interceptors.request.use((config2) => { + config2.headers.set(authHeaders()); + return config2; + }); + } + post(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } + postForm(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } + get(url, config) { + return __async(this, null, function* () { + return this.request.get(url, config).then((res) => res.data); + }); + } + put(url, data, config) { + return __async(this, null, function* () { + return this.request.put(url, data, config).then((res) => res.data); + }); + } + delete(url, config) { + return __async(this, null, function* () { + return this.request.delete(url, config).then((res) => res.data); + }); + } +}; + +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/completions.ts +// lib/capability/completions.ts var Completions = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -91,41 +150,7 @@ var Completions = class extends BaseApi { } }; -// lib/request.ts -import Axios from "axios"; -var Request = class { - constructor(app, config) { - this.app = app; - __publicField(this, "request"); - this.request = Axios.create(config); - this.request.interceptors.request.use((config2) => { - config2.headers.set(this.app.authHeaders()); - return config2; - }); - } - post(url, data, config) { - return __async(this, null, function* () { - return this.request.post(url, data, config).then((res) => res.data); - }); - } - get(url, config) { - return __async(this, null, function* () { - return this.request.get(url, config).then((res) => res.data); - }); - } - put(url, data, config) { - return __async(this, null, function* () { - return this.request.put(url, data, config).then((res) => res.data); - }); - } - delete(url, config) { - return __async(this, null, function* () { - return this.request.delete(url, config).then((res) => res.data); - }); - } -}; - -// lib/images.ts +// lib/capability/images.ts var Images = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -143,7 +168,7 @@ var Images = class extends BaseApi { } }; -// lib/embeddings.ts +// lib/capability/embeddings.ts var Embeddings = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -158,12 +183,38 @@ var Embeddings = class extends BaseApi { } }; +// lib/capability/files.ts +var Files = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + const formData = new FormData(); + formData.append("purpose", options.purpose); + formData.append("file", options.file); + return this.postForm("/files", formData, options); + }); + } + findList(options) { + return __async(this, null, function* () { + return this.get("/files", { + "purpose": options.purpose, + "limit": options.limit, + "after": options.after, + "order": options.order + }, options); + }); + } +}; + // lib/zhipu-ai.ts var ZhipuAI = class { constructor(options) { this.options = options; __publicField(this, "__esModule", false); __publicField(this, "request"); + __publicField(this, "completions"); + __publicField(this, "images"); + __publicField(this, "embeddings"); + __publicField(this, "files"); if (!options.apiKey) options.apiKey = process.env["ZHIPUAI_API_KEY"] || ""; assert.ok(options.apiKey, "\u672A\u63D0\u4F9Bapi_key\uFF0C\u8BF7\u901A\u8FC7\u53C2\u6570\u6216\u73AF\u5883\u53D8\u91CF\u63D0\u4F9B"); @@ -171,25 +222,39 @@ var ZhipuAI = class { options.baseUrl = process.env["ZHIPUAI_BASE_URL"] || ""; if (!options.baseUrl) options.baseUrl = "https://open.bigmodel.cn/api/paas/v4"; - this.request = new Request(this, { + this.request = new Request({ timeout: options.timeout, headers: options.customHeaders, baseURL: options.baseUrl - }); + }, this.authHeaders.bind(this)); + this.completions = new Completions(this.request); + this.images = new Images(this.request); + this.embeddings = new Embeddings(this.request); + this.files = new Files(this.request); } createCompletions(options) { return __async(this, null, function* () { - return new Completions(this.request).create(options); + return this.completions.create(options); }); } createImages(options) { return __async(this, null, function* () { - return new Images(this.request).create(options); + return this.images.create(options); }); } createEmbeddings(options) { return __async(this, null, function* () { - return new Embeddings(this.request).create(options); + return this.embeddings.create(options); + }); + } + createFiles(options) { + return __async(this, null, function* () { + return this.files.create(options); + }); + } + findFiles() { + return __async(this, arguments, function* (options = {}) { + return this.files.findList(options); }); } authHeaders() { diff --git a/dist/index.js.map b/dist/index.js.map index 48210b3..6e1aaa8 100644 --- a/dist/index.js.map +++ b/dist/index.js.map @@ -1 +1 @@ -{"version":3,"sources":["../lib/zhipu-ai.ts","../lib/jwt.ts","../lib/baseApi.ts","../lib/completions.ts","../lib/request.ts","../lib/images.ts","../lib/embeddings.ts"],"sourcesContent":["import assert from \"assert\"\nimport { generateToken } from \"./jwt.js\"\nimport Completions, { CompletionsResponseMessage, CreateCompletionsOptions } from \"./completions.js\"\nimport Request from \"./request.js\"\nimport Images, { CreateImagesOptions, ImagesResponse } from \"./images.js\"\nimport Embeddings, { CreateEmbeddingsOptions, EmbeddingsResponse } from \"./embeddings.js\"\n\nexport type ZhipuAIOptions = {\n apiKey: string,\n baseUrl?: string,\n timeout?: number,\n maxRetries?: number,\n customHeaders?: object\n}\n\nexport class ZhipuAI {\n public __esModule = false\n public request: Request\n\n constructor(private readonly options: ZhipuAIOptions) {\n if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || ''\n assert.ok(options.apiKey, \"未提供api_key,请通过参数或环境变量提供\")\n if (!options.baseUrl) options.baseUrl = process.env[\"ZHIPUAI_BASE_URL\"] || ''\n if (!options.baseUrl) options.baseUrl = \"https://open.bigmodel.cn/api/paas/v4\"\n this.request = new Request(this, {\n timeout: options.timeout,\n headers: options.customHeaders,\n baseURL: options.baseUrl\n });\n }\n\n public async createCompletions(options: CreateCompletionsOptions): Promise {\n return new Completions(this.request).create(options)\n }\n\n public async createImages(options: CreateImagesOptions): Promise {\n return new Images(this.request).create(options)\n }\n\n public async createEmbeddings(options: CreateEmbeddingsOptions): Promise {\n return new Embeddings(this.request).create(options)\n }\n\n public authHeaders() {\n const token = generateToken(this.options.apiKey)\n return { \"Authorization\": token }\n }\n}\n\nexport default ZhipuAI","import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}","import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type MessageOptions = {\n role: \"system\" | \"user\" | \"assistant\" | \"function\"\n content: string\n}\n\nexport type CreateCompletionsOptions = {\n model: string,\n messages: Array,\n requestId?: string,\n doSample?: boolean,\n stream?: boolean,\n temperature?: number,\n topP?: number,\n maxTokens?: number,\n seed?: number,\n stop?: Array,\n sensitiveWordCheck?: object,\n tools?: object,\n toolChoice?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type CompletionsResponseMessage = {\n id: string,\n created: number,\n model: string,\n choices: Array<{\n index: number,\n finish_reason: string,\n message: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n },\n delta: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n }\n }>,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number,\n },\n}\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from \"axios\";\nimport ZhipuAI from \"./zhipu-ai\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(private readonly app: ZhipuAI, config: CreateAxiosDefaults) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(this.app.authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}","import BaseApi from \"./baseApi\";\n\nexport type CreateImagesOptions = {\n model: string,\n prompt?: string,\n n?: number,\n quality?: string,\n responseFormat?: string,\n size?: string,\n style?: string,\n user?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type ImagesResponse = {\n created: number,\n data: Array\n}\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type CreateEmbeddingsOptions = {\n input: Array | string,\n model: string,\n encodingFormat: string,\n user: string,\n sensitiveWordCheck: object,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type EmbeddingsResponse = {\n model: string,\n data: Array<{\n index: number,\n object: string,\n embedding: Array\n }>,\n object: string,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number\n }\n}\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,YAAY;;;ACAnB,OAAO,kBAAkB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,aAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,EAAE,KAAK,SAAS,WAAW,OAAO;AAAA,IAC9C,CAAC;AACD,WAAO;AAAA,EACX,SAAS,GAAG;AACR,UAAM;AAAA,EACV;AACJ;;;ACZA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMA,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;;;ACuCA,IAAqB,cAArB,cAAyC,QAA8D;AAAA,EAEtF,OAAO,SAAwE;AAAA;AACxF,aAAO,KAAK,KAAK,qBAAqB;AAAA,QAClC,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ;AAAA,QACtB,eAAe,Q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D;AAAA;AAAA,EAEa,iBAAiB,SAA+D;AAAA;AACzF,aAAO,IAAI,WAAW,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IACtD;AAAA;AAAA,EAEO,cAAc;AACjB,UAAM,QAAQ,cAAc,KAAK,QAAQ,MAAM;AAC/C,WAAO,EAAE,iBAAiB,MAAM;AAAA,EACpC;AACJ;AAEA,IAAO,mBAAQ;","names":["data","config"]} \ No newline at end of file +{"version":3,"sources":["../lib/zhipu-ai.ts","../lib/core/jwt.ts","../lib/core/request.ts","../lib/core/baseApi.ts","../lib/capability/completions.ts","../lib/capability/images.ts","../lib/capability/embeddings.ts","../lib/capability/files.ts"],"sourcesContent":["import assert from \"assert\"\nimport { generateToken } from \"./core/jwt.js\"\nimport Request from \"./core/request.js\"\n\nimport { Completions, Images, Embeddings } from \"./capability\"\n\nimport {\n CompletionsResponseMessage, CreateCompletionsOptions,\n CreateImagesOptions, ImagesResponse,\n CreateEmbeddingsOptions, EmbeddingsResponse,\n CreateFileOptions, FileResponse, FileListResponse, FindFileListOptions,\n} from \"./types\"\nimport Files from \"./capability/files.js\"\n\nexport type ZhipuAIOptions = {\n apiKey: string,\n baseUrl?: string,\n timeout?: number,\n maxRetries?: number,\n customHeaders?: object\n}\n\nexport class ZhipuAI {\n public __esModule = false\n public request: Request\n public completions: Completions\n public images: Images\n public embeddings: Embeddings\n public files: Files\n\n\n constructor(private readonly options: ZhipuAIOptions) {\n if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || ''\n assert.ok(options.apiKey, \"未提供api_key,请通过参数或环境变量提供\")\n if (!options.baseUrl) options.baseUrl = process.env[\"ZHIPUAI_BASE_URL\"] || ''\n if (!options.baseUrl) options.baseUrl = \"https://open.bigmodel.cn/api/paas/v4\"\n this.request = new Request({\n timeout: options.timeout,\n headers: options.customHeaders,\n baseURL: options.baseUrl\n }, this.authHeaders.bind(this));\n\n this.completions = new Completions(this.request)\n this.images = new Images(this.request)\n this.embeddings = new Embeddings(this.request)\n this.files = new Files(this.request)\n }\n\n public async createCompletions(options: CreateCompletionsOptions): Promise {\n return this.completions.create(options)\n }\n\n public async createImages(options: CreateImagesOptions): Promise {\n return this.images.create(options)\n }\n\n public async createEmbeddings(options: CreateEmbeddingsOptions): Promise {\n return this.embeddings.create(options)\n }\n\n public async createFiles(options: CreateFileOptions): Promise {\n return this.files.create(options)\n }\n\n public async findFiles(options: FindFileListOptions = {}): Promise {\n return this.files.findList(options)\n }\n\n public authHeaders(): { [key: string]: string } {\n const token = generateToken(this.options.apiKey)\n return { \"Authorization\": token }\n }\n}\n\nexport default ZhipuAI","import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}\n","import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig, AxiosHeaderValue } from \"axios\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(config: CreateAxiosDefaults, authHeaders: () => { [key: string]: AxiosHeaderValue }) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateCompletionsOptions, CompletionsResponseMessage } from \"../types/index.js\";\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateImagesOptions, ImagesResponse } from \"../types/index.js\";\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"../types/index.js\";\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from \"../types/index.js\";\n\nexport default class Files extends BaseApi {\n public async create(options: CreateFileOptions): Promise {\n const formData = new FormData()\n formData.append(\"purpose\", options.purpose);\n formData.append(\"file\", options.file);\n return this.postForm(\"/files\", formData, options)\n }\n\n public async findList(options: FindFileListOptions): Promise {\n return this.get(\"/files\", {\n \"purpose\": options.purpose,\n \"limit\": options.limit,\n \"after\": options.after,\n \"order\": options.order,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,YAAY;;;ACAnB,OAAO,kBAAkB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,aAAa,KAAK,SAAS,QAAQ;AAAA,M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end of file diff --git a/dist/jwt.cjs.map b/dist/jwt.cjs.map deleted file mode 100644 index b9ec5aa..0000000 --- a/dist/jwt.cjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/jwt.ts"],"sourcesContent":["import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAAyB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,oBAAAA,QAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,EAAE,KAAK,SAAS,WAAW,OAAO;AAAA,IAC9C,CAAC;AACD,WAAO;AAAA,EACX,SAAS,GAAG;AACR,UAAM;AAAA,EACV;AACJ;","names":["jsonwebtoken"]} \ No newline at end of file diff --git a/dist/jwt.js.map b/dist/jwt.js.map deleted file mode 100644 index 21ed8cc..0000000 --- a/dist/jwt.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/jwt.ts"],"sourcesContent":["import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}"],"mappings":";AAAA,OAAO,kBAAkB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,aAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,EAAE,KAAK,SAAS,WAAW,OAAO;AAAA,IAC9C,CAAC;AACD,WAAO;AAAA,EACX,SAAS,GAAG;AACR,UAAM;AAAA,EACV;AACJ;","names":[]} \ No newline at end of file diff --git a/dist/request.cjs.map b/dist/request.cjs.map deleted file mode 100644 index 51fa1d4..0000000 --- a/dist/request.cjs.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/request.ts"],"sourcesContent":["import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from \"axios\";\nimport ZhipuAI from \"./zhipu-ai\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(private readonly app: ZhipuAI, config: CreateAxiosDefaults) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(this.app.authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA6F;AAG7F,IAAqB,UAArB,MAA6B;AAAA,EAGzB,YAA6B,KAAc,QAA6B;AAA3C;AAF7B,wBAAO;AAGH,SAAK,UAAU,aAAAA,QAAM,OAAO,MAAM;AAElC,SAAK,QAAQ,aAAa,QAAQ,IAAI,CAACC,YAAW;AAC9C,MAAAA,QAAO,QAAQ,IAAI,KAAK,IAAI,YAAY,CAAC;AACzC,aAAOA;AAAA,IACX,CAAC;AAAA,EACL;AAAA,EAEM,KAA6C,KAAa,MAAU,QAA4C;AAAA;AAClH,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACpE;AAAA;AAAA,EAEM,IAA4C,KAAa,QAA4C;AAAA;AACvG,aAAO,KAAK,QAAQ,IAAI,KAAK,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IAC7D;AAAA;AAAA,EAEM,IAA4C,KAAa,MAAU,QAA4C;AAAA;AACjH,aAAO,KAAK,QAAQ,IAAI,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACnE;AAAA;AAAA,EAEM,OAA+C,KAAa,QAA4C;AAAA;AAC1G,aAAO,KAAK,QAAQ,OAAO,KAAK,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IAChE;AAAA;AACJ;","names":["Axios","config"]} \ No newline at end of file diff --git a/dist/request.d.cts b/dist/request.d.cts deleted file mode 100644 index 10aabcc..0000000 --- a/dist/request.d.cts +++ /dev/null @@ -1,2 +0,0 @@ -import 'axios'; -export { R as default } from './index-hCfUd9Lh.cjs'; diff --git a/dist/request.d.ts b/dist/request.d.ts deleted file mode 100644 index 94d5e06..0000000 --- a/dist/request.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -import 'axios'; -export { R as default } from './index-hCfUd9Lh.js'; diff --git a/dist/request.js.map b/dist/request.js.map deleted file mode 100644 index 5ddfb75..0000000 --- a/dist/request.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"sources":["../lib/request.ts"],"sourcesContent":["import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from \"axios\";\nimport ZhipuAI from \"./zhipu-ai\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(private readonly app: ZhipuAI, config: CreateAxiosDefaults) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(this.app.authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,WAAsF;AAG7F,IAAqB,UAArB,MAA6B;AAAA,EAGzB,YAA6B,KAAc,QAA6B;AAA3C;AAF7B,wBAAO;AAGH,SAAK,UAAU,MAAM,OAAO,MAAM;AAElC,SAAK,QAAQ,aAAa,QAAQ,IAAI,CAACA,YAAW;AAC9C,MAAAA,QAAO,QAAQ,IAAI,KAAK,IAAI,YAAY,CAAC;AACzC,aAAOA;AAAA,IACX,CAAC;AAAA,EACL;AAAA,EAEM,KAA6C,KAAa,MAAU,QAA4C;AAAA;AAClH,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACpE;AAAA;AAAA,EAEM,IAA4C,KAAa,QAA4C;AAAA;AACvG,aAAO,KAAK,QAAQ,IAAI,KAAK,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IAC7D;AAAA;AAAA,EAEM,IAA4C,KAAa,MAAU,QAA4C;AAAA;AACjH,aAAO,KAAK,QAAQ,IAAI,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACnE;AAAA;AAAA,EAEM,OAA+C,KAAa,QAA4C;AAAA;AAC1G,aAAO,KAAK,QAAQ,OAAO,KAAK,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IAChE;AAAA;AACJ;","names":["config"]} \ No newline at end of file diff --git a/dist/types/baseApi.cjs b/dist/types/baseApi.cjs new file mode 100644 index 0000000..1d1e2ad --- /dev/null +++ b/dist/types/baseApi.cjs @@ -0,0 +1,19 @@ +"use strict"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// lib/types/baseApi.ts +var baseApi_exports = {}; +module.exports = __toCommonJS(baseApi_exports); +//# sourceMappingURL=baseApi.cjs.map \ No newline at end of file diff --git a/dist/types/baseApi.cjs.map b/dist/types/baseApi.cjs.map new file mode 100644 index 0000000..c112ed8 --- /dev/null +++ b/dist/types/baseApi.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/types/baseApi.ts"],"sourcesContent":["export type BaseRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]} \ No newline at end of file diff --git a/dist/types/baseApi.d.cts b/dist/types/baseApi.d.cts new file mode 100644 index 0000000..e606422 --- /dev/null +++ b/dist/types/baseApi.d.cts @@ -0,0 +1,7 @@ +type BaseRequestOptions = { + extraHeaders?: object; + timeout?: number; + stream?: boolean; +}; + +export type { BaseRequestOptions }; diff --git a/dist/types/baseApi.d.ts b/dist/types/baseApi.d.ts new file mode 100644 index 0000000..e606422 --- /dev/null +++ b/dist/types/baseApi.d.ts @@ -0,0 +1,7 @@ +type BaseRequestOptions = { + extraHeaders?: object; + timeout?: number; + stream?: boolean; +}; + +export type { BaseRequestOptions }; diff --git a/dist/types/baseApi.js b/dist/types/baseApi.js new file mode 100644 index 0000000..c398929 --- /dev/null +++ b/dist/types/baseApi.js @@ -0,0 +1 @@ +//# sourceMappingURL=baseApi.js.map \ No newline at end of file diff --git a/dist/types/baseApi.js.map b/dist/types/baseApi.js.map new file mode 100644 index 0000000..84c51b2 --- /dev/null +++ b/dist/types/baseApi.js.map @@ -0,0 +1 @@ +{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]} \ No newline at end of file diff --git a/dist/types/completions.cjs b/dist/types/completions.cjs new file mode 100644 index 0000000..3719945 --- /dev/null +++ b/dist/types/completions.cjs @@ -0,0 +1,19 @@ +"use strict"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// lib/types/completions.ts +var completions_exports = {}; +module.exports = __toCommonJS(completions_exports); +//# sourceMappingURL=completions.cjs.map \ No newline at end of file diff --git a/dist/types/completions.cjs.map b/dist/types/completions.cjs.map new file mode 100644 index 0000000..1548807 --- /dev/null +++ b/dist/types/completions.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/types/completions.ts"],"sourcesContent":["import { BaseRequestOptions } from './baseApi.js'\n\nexport type MessageOptions = {\n role: \"system\" | \"user\" | \"assistant\" | \"function\"\n content: string\n}\n\nexport type CreateCompletionsOptions = {\n model: string,\n messages: Array,\n requestId?: string,\n doSample?: boolean,\n stream?: boolean,\n temperature?: number,\n topP?: number,\n maxTokens?: number,\n seed?: number,\n stop?: Array,\n sensitiveWordCheck?: object,\n tools?: object,\n toolChoice?: string\n} & BaseRequestOptions\n\nexport type CompletionsResponseMessage = {\n id: string,\n created: number,\n model: string,\n choices: Array<{\n index: number,\n finish_reason: string,\n message: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n },\n delta: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n }\n }>,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number,\n },\n}"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]} \ No newline at end of file diff --git a/dist/types/completions.d.cts b/dist/types/completions.d.cts new file mode 100644 index 0000000..ec3b39c --- /dev/null +++ b/dist/types/completions.d.cts @@ -0,0 +1,61 @@ +import { BaseRequestOptions } from './baseApi.cjs'; + +type MessageOptions = { + role: "system" | "user" | "assistant" | "function"; + content: string; +}; +type CreateCompletionsOptions = { + model: string; + messages: Array; + requestId?: string; + doSample?: boolean; + stream?: boolean; + temperature?: number; + topP?: number; + maxTokens?: number; + seed?: number; + stop?: Array; + sensitiveWordCheck?: object; + tools?: object; + toolChoice?: string; +} & BaseRequestOptions; +type CompletionsResponseMessage = { + id: string; + created: number; + model: string; + choices: Array<{ + index: number; + finish_reason: string; + message: { + role: string; + content: string; + tool_calls: Array<{ + id: string; + type: string; + function: { + name: string; + arguments: object; + }; + }>; + }; + delta: { + role: string; + content: string; + tool_calls: Array<{ + id: string; + type: string; + function: { + name: string; + arguments: object; + }; + }>; + }; + }>; + usage: { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; + }; +}; + +export type { CompletionsResponseMessage, CreateCompletionsOptions, MessageOptions }; diff --git a/dist/types/completions.d.ts b/dist/types/completions.d.ts new file mode 100644 index 0000000..4f43672 --- /dev/null +++ b/dist/types/completions.d.ts @@ -0,0 +1,61 @@ +import { BaseRequestOptions } from './baseApi.js'; + +type MessageOptions = { + role: "system" | "user" | "assistant" | "function"; + content: string; +}; +type CreateCompletionsOptions = { + model: string; + messages: Array; + requestId?: string; + doSample?: boolean; + stream?: boolean; + temperature?: number; + topP?: number; + maxTokens?: number; + seed?: number; + stop?: Array; + sensitiveWordCheck?: object; + tools?: object; + toolChoice?: string; +} & BaseRequestOptions; +type CompletionsResponseMessage = { + id: string; + created: number; + model: string; + choices: Array<{ + index: number; + finish_reason: string; + message: { + role: string; + content: string; + tool_calls: Array<{ + id: string; + type: string; + function: { + name: string; + arguments: object; + }; + }>; + }; + delta: { + role: string; + content: string; + tool_calls: Array<{ + id: string; + type: string; + function: { + name: string; + arguments: object; + }; + }>; + }; + }>; + usage: { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; + }; +}; + +export type { CompletionsResponseMessage, CreateCompletionsOptions, MessageOptions }; diff --git a/dist/types/completions.js b/dist/types/completions.js new file mode 100644 index 0000000..f7f6a81 --- /dev/null +++ b/dist/types/completions.js @@ -0,0 +1 @@ +//# sourceMappingURL=completions.js.map \ No newline at end of file diff --git a/dist/types/completions.js.map b/dist/types/completions.js.map new file mode 100644 index 0000000..84c51b2 --- /dev/null +++ b/dist/types/completions.js.map @@ -0,0 +1 @@ +{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]} \ No newline at end of file diff --git a/dist/types/embeddings.cjs b/dist/types/embeddings.cjs new file mode 100644 index 0000000..3df6f51 --- /dev/null +++ b/dist/types/embeddings.cjs @@ -0,0 +1,19 @@ +"use strict"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// lib/types/embeddings.ts +var embeddings_exports = {}; +module.exports = __toCommonJS(embeddings_exports); +//# sourceMappingURL=embeddings.cjs.map \ No newline at end of file diff --git a/dist/types/embeddings.cjs.map b/dist/types/embeddings.cjs.map new file mode 100644 index 0000000..20005cc --- /dev/null +++ b/dist/types/embeddings.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/types/embeddings.ts"],"sourcesContent":["import { BaseRequestOptions } from './baseApi.js'\n\nexport type CreateEmbeddingsOptions = {\n input: Array | string,\n model: string,\n encodingFormat: string,\n user: string,\n sensitiveWordCheck: object,\n extraHeaders?: object,\n timeout?: number,\n} & BaseRequestOptions\n\nexport type EmbeddingsResponse = {\n model: string,\n data: Array<{\n index: number,\n object: string,\n embedding: Array\n }>,\n object: string,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number\n }\n}"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]} \ No newline at end of file diff --git a/dist/types/embeddings.d.cts b/dist/types/embeddings.d.cts new file mode 100644 index 0000000..e8f1233 --- /dev/null +++ b/dist/types/embeddings.d.cts @@ -0,0 +1,27 @@ +import { BaseRequestOptions } from './baseApi.cjs'; + +type CreateEmbeddingsOptions = { + input: Array | string; + model: string; + encodingFormat: string; + user: string; + sensitiveWordCheck: object; + extraHeaders?: object; + timeout?: number; +} & BaseRequestOptions; +type EmbeddingsResponse = { + model: string; + data: Array<{ + index: number; + object: string; + embedding: Array; + }>; + object: string; + usage: { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; + }; +}; + +export type { CreateEmbeddingsOptions, EmbeddingsResponse }; diff --git a/dist/types/embeddings.d.ts b/dist/types/embeddings.d.ts new file mode 100644 index 0000000..ade8301 --- /dev/null +++ b/dist/types/embeddings.d.ts @@ -0,0 +1,27 @@ +import { BaseRequestOptions } from './baseApi.js'; + +type CreateEmbeddingsOptions = { + input: Array | string; + model: string; + encodingFormat: string; + user: string; + sensitiveWordCheck: object; + extraHeaders?: object; + timeout?: number; +} & BaseRequestOptions; +type EmbeddingsResponse = { + model: string; + data: Array<{ + index: number; + object: string; + embedding: Array; + }>; + object: string; + usage: { + prompt_tokens: number; + completion_tokens: number; + total_tokens: number; + }; +}; + +export type { CreateEmbeddingsOptions, EmbeddingsResponse }; diff --git a/dist/types/embeddings.js b/dist/types/embeddings.js new file mode 100644 index 0000000..4fac560 --- /dev/null +++ b/dist/types/embeddings.js @@ -0,0 +1 @@ +//# sourceMappingURL=embeddings.js.map \ No newline at end of file diff --git a/dist/types/embeddings.js.map b/dist/types/embeddings.js.map new file mode 100644 index 0000000..84c51b2 --- /dev/null +++ b/dist/types/embeddings.js.map @@ -0,0 +1 @@ +{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]} \ No newline at end of file diff --git a/dist/types/files.cjs b/dist/types/files.cjs new file mode 100644 index 0000000..7ac0d25 --- /dev/null +++ b/dist/types/files.cjs @@ -0,0 +1,19 @@ +"use strict"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// lib/types/files.ts +var files_exports = {}; +module.exports = __toCommonJS(files_exports); +//# sourceMappingURL=files.cjs.map \ No newline at end of file diff --git a/dist/types/files.cjs.map b/dist/types/files.cjs.map new file mode 100644 index 0000000..f15f899 --- /dev/null +++ b/dist/types/files.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/types/files.ts"],"sourcesContent":["import { BaseRequestOptions } from './baseApi.js'\n\nexport type CreateFileOptions = {\n file: Blob,\n purpose: string,\n} & BaseRequestOptions\n\nexport type FileResponse = {\n id: string,\n filename: string,\n purpose: string,\n}\n\nexport type FindFileListOrderTypes = \"desc\" | \"asc\"\n\nexport type FindFileListOptions = {\n purpose?: string,\n limit?: number,\n after?: string,\n order?: FindFileListOrderTypes,\n} & BaseRequestOptions\n\nexport type FileListResponse = {\n object: string,\n data: Array<{\n bytes: number,\n created_at: number,\n object: string\n } & FileResponse>\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]} \ No newline at end of file diff --git a/dist/types/files.d.cts b/dist/types/files.d.cts new file mode 100644 index 0000000..dd0324d --- /dev/null +++ b/dist/types/files.d.cts @@ -0,0 +1,28 @@ +import { BaseRequestOptions } from './baseApi.cjs'; + +type CreateFileOptions = { + file: Blob; + purpose: string; +} & BaseRequestOptions; +type FileResponse = { + id: string; + filename: string; + purpose: string; +}; +type FindFileListOrderTypes = "desc" | "asc"; +type FindFileListOptions = { + purpose?: string; + limit?: number; + after?: string; + order?: FindFileListOrderTypes; +} & BaseRequestOptions; +type FileListResponse = { + object: string; + data: Array<{ + bytes: number; + created_at: number; + object: string; + } & FileResponse>; +}; + +export type { CreateFileOptions, FileListResponse, FileResponse, FindFileListOptions, FindFileListOrderTypes }; diff --git a/dist/types/files.d.ts b/dist/types/files.d.ts new file mode 100644 index 0000000..f341d6b --- /dev/null +++ b/dist/types/files.d.ts @@ -0,0 +1,28 @@ +import { BaseRequestOptions } from './baseApi.js'; + +type CreateFileOptions = { + file: Blob; + purpose: string; +} & BaseRequestOptions; +type FileResponse = { + id: string; + filename: string; + purpose: string; +}; +type FindFileListOrderTypes = "desc" | "asc"; +type FindFileListOptions = { + purpose?: string; + limit?: number; + after?: string; + order?: FindFileListOrderTypes; +} & BaseRequestOptions; +type FileListResponse = { + object: string; + data: Array<{ + bytes: number; + created_at: number; + object: string; + } & FileResponse>; +}; + +export type { CreateFileOptions, FileListResponse, FileResponse, FindFileListOptions, FindFileListOrderTypes }; diff --git a/dist/types/files.js b/dist/types/files.js new file mode 100644 index 0000000..6f22115 --- /dev/null +++ b/dist/types/files.js @@ -0,0 +1 @@ +//# sourceMappingURL=files.js.map \ No newline at end of file diff --git a/dist/types/files.js.map b/dist/types/files.js.map new file mode 100644 index 0000000..84c51b2 --- /dev/null +++ b/dist/types/files.js.map @@ -0,0 +1 @@ +{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]} \ No newline at end of file diff --git a/dist/types/images.cjs b/dist/types/images.cjs new file mode 100644 index 0000000..d727a24 --- /dev/null +++ b/dist/types/images.cjs @@ -0,0 +1,19 @@ +"use strict"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// lib/types/images.ts +var images_exports = {}; +module.exports = __toCommonJS(images_exports); +//# sourceMappingURL=images.cjs.map \ No newline at end of file diff --git a/dist/types/images.cjs.map b/dist/types/images.cjs.map new file mode 100644 index 0000000..59a192c --- /dev/null +++ b/dist/types/images.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/types/images.ts"],"sourcesContent":["import { BaseRequestOptions } from './baseApi.js'\n\nexport type CreateImagesOptions = {\n model: string,\n prompt?: string,\n n?: number,\n quality?: string,\n responseFormat?: string,\n size?: string,\n style?: string,\n user?: string,\n extraHeaders?: object,\n timeout?: number,\n} & BaseRequestOptions\n\nexport type ImagesResponse = {\n created: number,\n data: Array\n}"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]} \ No newline at end of file diff --git a/dist/types/images.d.cts b/dist/types/images.d.cts new file mode 100644 index 0000000..5e72901 --- /dev/null +++ b/dist/types/images.d.cts @@ -0,0 +1,20 @@ +import { BaseRequestOptions } from './baseApi.cjs'; + +type CreateImagesOptions = { + model: string; + prompt?: string; + n?: number; + quality?: string; + responseFormat?: string; + size?: string; + style?: string; + user?: string; + extraHeaders?: object; + timeout?: number; +} & BaseRequestOptions; +type ImagesResponse = { + created: number; + data: Array; +}; + +export type { CreateImagesOptions, ImagesResponse }; diff --git a/dist/types/images.d.ts b/dist/types/images.d.ts new file mode 100644 index 0000000..e559cf2 --- /dev/null +++ b/dist/types/images.d.ts @@ -0,0 +1,20 @@ +import { BaseRequestOptions } from './baseApi.js'; + +type CreateImagesOptions = { + model: string; + prompt?: string; + n?: number; + quality?: string; + responseFormat?: string; + size?: string; + style?: string; + user?: string; + extraHeaders?: object; + timeout?: number; +} & BaseRequestOptions; +type ImagesResponse = { + created: number; + data: Array; +}; + +export type { CreateImagesOptions, ImagesResponse }; diff --git a/dist/types/images.js b/dist/types/images.js new file mode 100644 index 0000000..64032ba --- /dev/null +++ b/dist/types/images.js @@ -0,0 +1 @@ +//# sourceMappingURL=images.js.map \ No newline at end of file diff --git a/dist/types/images.js.map b/dist/types/images.js.map new file mode 100644 index 0000000..84c51b2 --- /dev/null +++ b/dist/types/images.js.map @@ -0,0 +1 @@ +{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]} \ No newline at end of file diff --git a/dist/types/index.cjs b/dist/types/index.cjs new file mode 100644 index 0000000..e12331a --- /dev/null +++ b/dist/types/index.cjs @@ -0,0 +1,19 @@ +"use strict"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); + +// lib/types/index.ts +var types_exports = {}; +module.exports = __toCommonJS(types_exports); +//# sourceMappingURL=index.cjs.map \ No newline at end of file diff --git a/dist/types/index.cjs.map b/dist/types/index.cjs.map new file mode 100644 index 0000000..6d7d51c --- /dev/null +++ b/dist/types/index.cjs.map @@ -0,0 +1 @@ +{"version":3,"sources":["../../lib/types/index.ts"],"sourcesContent":["export { BaseRequestOptions } from './baseApi.js'\nexport { CreateCompletionsOptions, CompletionsResponseMessage, MessageOptions } from \"./completions.js\"\nexport { CreateImagesOptions, ImagesResponse } from \"./images.js\"\nexport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"./embeddings.js\"\nexport { CreateFileOptions, FileResponse, FindFileListOrderTypes, FindFileListOptions, FileListResponse } from \"./files.js\"\n\n"],"mappings":";;;;;;;;;;;;;;;;AAAA;AAAA;","names":[]} \ No newline at end of file diff --git a/dist/types/index.d.cts b/dist/types/index.d.cts new file mode 100644 index 0000000..c77eace --- /dev/null +++ b/dist/types/index.d.cts @@ -0,0 +1,5 @@ +export { BaseRequestOptions } from './baseApi.cjs'; +export { CompletionsResponseMessage, CreateCompletionsOptions, MessageOptions } from './completions.cjs'; +export { CreateImagesOptions, ImagesResponse } from './images.cjs'; +export { CreateEmbeddingsOptions, EmbeddingsResponse } from './embeddings.cjs'; +export { CreateFileOptions, FileListResponse, FileResponse, FindFileListOptions, FindFileListOrderTypes } from './files.cjs'; diff --git a/dist/types/index.d.ts b/dist/types/index.d.ts new file mode 100644 index 0000000..06e8b3b --- /dev/null +++ b/dist/types/index.d.ts @@ -0,0 +1,5 @@ +export { BaseRequestOptions } from './baseApi.js'; +export { CompletionsResponseMessage, CreateCompletionsOptions, MessageOptions } from './completions.js'; +export { CreateImagesOptions, ImagesResponse } from './images.js'; +export { CreateEmbeddingsOptions, EmbeddingsResponse } from './embeddings.js'; +export { CreateFileOptions, FileListResponse, FileResponse, FindFileListOptions, FindFileListOrderTypes } from './files.js'; diff --git a/dist/types/index.js b/dist/types/index.js new file mode 100644 index 0000000..8332f84 --- /dev/null +++ b/dist/types/index.js @@ -0,0 +1 @@ +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/dist/types/index.js.map b/dist/types/index.js.map new file mode 100644 index 0000000..84c51b2 --- /dev/null +++ b/dist/types/index.js.map @@ -0,0 +1 @@ +{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]} \ No newline at end of file diff --git a/dist/zhipu-ai.cjs b/dist/zhipu-ai.cjs index ffbc085..1b48356 100644 --- a/dist/zhipu-ai.cjs +++ b/dist/zhipu-ai.cjs @@ -61,7 +61,7 @@ __export(zhipu_ai_exports, { module.exports = __toCommonJS(zhipu_ai_exports); var import_assert = __toESM(require("assert"), 1); -// lib/jwt.ts +// lib/core/jwt.ts var import_jsonwebtoken = __toESM(require("jsonwebtoken"), 1); var API_TOKEN_TTL_SECONDS = 3 * 60; var generateToken = (apiSecretKey) => { @@ -82,26 +82,85 @@ var generateToken = (apiSecretKey) => { } }; -// lib/baseApi.ts +// lib/core/request.ts +var import_axios = __toESM(require("axios"), 1); +var Request = class { + constructor(config, authHeaders) { + __publicField(this, "request"); + this.request = import_axios.default.create(config); + this.request.interceptors.request.use((config2) => { + config2.headers.set(authHeaders()); + return config2; + }); + } + post(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } + postForm(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } + get(url, config) { + return __async(this, null, function* () { + return this.request.get(url, config).then((res) => res.data); + }); + } + put(url, data, config) { + return __async(this, null, function* () { + return this.request.put(url, data, config).then((res) => res.data); + }); + } + delete(url, config) { + return __async(this, null, function* () { + return this.request.delete(url, config).then((res) => res.data); + }); + } +}; + +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/completions.ts +// lib/capability/completions.ts var Completions = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -124,41 +183,7 @@ var Completions = class extends BaseApi { } }; -// lib/request.ts -var import_axios = __toESM(require("axios"), 1); -var Request = class { - constructor(app, config) { - this.app = app; - __publicField(this, "request"); - this.request = import_axios.default.create(config); - this.request.interceptors.request.use((config2) => { - config2.headers.set(this.app.authHeaders()); - return config2; - }); - } - post(url, data, config) { - return __async(this, null, function* () { - return this.request.post(url, data, config).then((res) => res.data); - }); - } - get(url, config) { - return __async(this, null, function* () { - return this.request.get(url, config).then((res) => res.data); - }); - } - put(url, data, config) { - return __async(this, null, function* () { - return this.request.put(url, data, config).then((res) => res.data); - }); - } - delete(url, config) { - return __async(this, null, function* () { - return this.request.delete(url, config).then((res) => res.data); - }); - } -}; - -// lib/images.ts +// lib/capability/images.ts var Images = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -176,7 +201,7 @@ var Images = class extends BaseApi { } }; -// lib/embeddings.ts +// lib/capability/embeddings.ts var Embeddings = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -191,12 +216,38 @@ var Embeddings = class extends BaseApi { } }; +// lib/capability/files.ts +var Files = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + const formData = new FormData(); + formData.append("purpose", options.purpose); + formData.append("file", options.file); + return this.postForm("/files", formData, options); + }); + } + findList(options) { + return __async(this, null, function* () { + return this.get("/files", { + "purpose": options.purpose, + "limit": options.limit, + "after": options.after, + "order": options.order + }, options); + }); + } +}; + // lib/zhipu-ai.ts var ZhipuAI = class { constructor(options) { this.options = options; __publicField(this, "__esModule", false); __publicField(this, "request"); + __publicField(this, "completions"); + __publicField(this, "images"); + __publicField(this, "embeddings"); + __publicField(this, "files"); if (!options.apiKey) options.apiKey = process.env["ZHIPUAI_API_KEY"] || ""; import_assert.default.ok(options.apiKey, "\u672A\u63D0\u4F9Bapi_key\uFF0C\u8BF7\u901A\u8FC7\u53C2\u6570\u6216\u73AF\u5883\u53D8\u91CF\u63D0\u4F9B"); @@ -204,25 +255,39 @@ var ZhipuAI = class { options.baseUrl = process.env["ZHIPUAI_BASE_URL"] || ""; if (!options.baseUrl) options.baseUrl = "https://open.bigmodel.cn/api/paas/v4"; - this.request = new Request(this, { + this.request = new Request({ timeout: options.timeout, headers: options.customHeaders, baseURL: options.baseUrl - }); + }, this.authHeaders.bind(this)); + this.completions = new Completions(this.request); + this.images = new Images(this.request); + this.embeddings = new Embeddings(this.request); + this.files = new Files(this.request); } createCompletions(options) { return __async(this, null, function* () { - return new Completions(this.request).create(options); + return this.completions.create(options); }); } createImages(options) { return __async(this, null, function* () { - return new Images(this.request).create(options); + return this.images.create(options); }); } createEmbeddings(options) { return __async(this, null, function* () { - return new Embeddings(this.request).create(options); + return this.embeddings.create(options); + }); + } + createFiles(options) { + return __async(this, null, function* () { + return this.files.create(options); + }); + } + findFiles() { + return __async(this, arguments, function* (options = {}) { + return this.files.findList(options); }); } authHeaders() { diff --git a/dist/zhipu-ai.cjs.map b/dist/zhipu-ai.cjs.map index 6163516..21fab0e 100644 --- a/dist/zhipu-ai.cjs.map +++ b/dist/zhipu-ai.cjs.map @@ -1 +1 @@ -{"version":3,"sources":["../lib/zhipu-ai.ts","../lib/jwt.ts","../lib/baseApi.ts","../lib/completions.ts","../lib/request.ts","../lib/images.ts","../lib/embeddings.ts"],"sourcesContent":["import assert from \"assert\"\nimport { generateToken } from \"./jwt.js\"\nimport Completions, { CompletionsResponseMessage, CreateCompletionsOptions } from \"./completions.js\"\nimport Request from \"./request.js\"\nimport Images, { CreateImagesOptions, ImagesResponse } from \"./images.js\"\nimport Embeddings, { CreateEmbeddingsOptions, EmbeddingsResponse } from \"./embeddings.js\"\n\nexport type ZhipuAIOptions = {\n apiKey: string,\n baseUrl?: string,\n timeout?: number,\n maxRetries?: number,\n customHeaders?: object\n}\n\nexport class ZhipuAI {\n public __esModule = false\n public request: Request\n\n constructor(private readonly options: ZhipuAIOptions) {\n if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || ''\n assert.ok(options.apiKey, \"未提供api_key,请通过参数或环境变量提供\")\n if (!options.baseUrl) options.baseUrl = process.env[\"ZHIPUAI_BASE_URL\"] || ''\n if (!options.baseUrl) options.baseUrl = \"https://open.bigmodel.cn/api/paas/v4\"\n this.request = new Request(this, {\n timeout: options.timeout,\n headers: options.customHeaders,\n baseURL: options.baseUrl\n });\n }\n\n public async createCompletions(options: CreateCompletionsOptions): Promise {\n return new Completions(this.request).create(options)\n }\n\n public async createImages(options: CreateImagesOptions): Promise {\n return new Images(this.request).create(options)\n }\n\n public async createEmbeddings(options: CreateEmbeddingsOptions): Promise {\n return new Embeddings(this.request).create(options)\n }\n\n public authHeaders() {\n const token = generateToken(this.options.apiKey)\n return { \"Authorization\": token }\n }\n}\n\nexport default ZhipuAI","import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}","import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type MessageOptions = {\n role: \"system\" | \"user\" | \"assistant\" | \"function\"\n content: string\n}\n\nexport type CreateCompletionsOptions = {\n model: string,\n messages: Array,\n requestId?: string,\n doSample?: boolean,\n stream?: boolean,\n temperature?: number,\n topP?: number,\n maxTokens?: number,\n seed?: number,\n stop?: Array,\n sensitiveWordCheck?: object,\n tools?: object,\n toolChoice?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type CompletionsResponseMessage = {\n id: string,\n created: number,\n model: string,\n choices: Array<{\n index: number,\n finish_reason: string,\n message: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n },\n delta: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n }\n }>,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number,\n },\n}\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from \"axios\";\nimport ZhipuAI from \"./zhipu-ai\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(private readonly app: ZhipuAI, config: CreateAxiosDefaults) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(this.app.authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}","import BaseApi from \"./baseApi\";\n\nexport type CreateImagesOptions = {\n model: string,\n prompt?: string,\n n?: number,\n quality?: string,\n responseFormat?: string,\n size?: string,\n style?: string,\n user?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type ImagesResponse = {\n created: number,\n data: Array\n}\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type CreateEmbeddingsOptions = {\n input: Array | string,\n model: string,\n encodingFormat: string,\n user: string,\n sensitiveWordCheck: object,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type EmbeddingsResponse = {\n model: string,\n data: Array<{\n index: number,\n object: string,\n embedding: Array\n }>,\n object: string,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number\n }\n}\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAmB;;;ACAnB,0BAAyB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,oBAAAA,QAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,EAAE,KAAK,SAAS,WAAW,OAAO;AAAA,IAC9C,CAAC;AACD,WAAO;AAAA,EACX,SAAS,GAAG;AACR,UAAM;AAAA,EACV;AACJ;;;ACZA,IAAqB,UAArB,MAAgD;AAAA,EAC5C,YAA6B,SAAkB;AAAlB;AAAA,EAC7B;AAAA,EAEa,KAAK,KAAa,MAAc,SAAgE;AAAA;AACzG,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM;AAAA,QAChC,SAAS,QAAQ;AAAA,QACjB,SAAS,QAAQ;AAAA,QACjB,cAAc,QAAQ,SAAS,WAAW;AAAA,MAC9C,CAAC,EACI,MAAM,SAAO;AACV,cAAMC,QAAO,IAAI,SAAS;AAC1B,eAAO,QAAQ,OAAOA,KAAI;AAAA,MAC9B,CAAC;AAAA,IACT;AAAA;AACJ;;;ACuCA,IAAqB,cAArB,cAAyC,QAA8D;AAAA,EAEtF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,EAAE,OAAO,OAAO;AAAA,IACvD;AAAA;AAAA,EAEa,aAAa,SAAuD;AAAA;AAC7E,aAAO,IAAI,OAAO,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IAClD;AAAA;AAAA,EAEa,iBAAiB,SAA+D;AAAA;AACzF,aAAO,IAAI,WAAW,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IACtD;AAAA;AAAA,EAEO,cAAc;AACjB,UAAM,QAAQ,cAAc,KAAK,QAAQ,MAAM;AAC/C,WAAO,EAAE,iBAAiB,MAAM;AAAA,EACpC;AACJ;AAEA,IAAO,mBAAQ;","names":["jsonwebtoken","data","Axios","config","assert"]} \ No newline at end of file +{"version":3,"sources":["../lib/zhipu-ai.ts","../lib/core/jwt.ts","../lib/core/request.ts","../lib/core/baseApi.ts","../lib/capability/completions.ts","../lib/capability/images.ts","../lib/capability/embeddings.ts","../lib/capability/files.ts"],"sourcesContent":["import assert from \"assert\"\nimport { generateToken } from \"./core/jwt.js\"\nimport Request from \"./core/request.js\"\n\nimport { Completions, Images, Embeddings } from \"./capability\"\n\nimport {\n CompletionsResponseMessage, CreateCompletionsOptions,\n CreateImagesOptions, ImagesResponse,\n CreateEmbeddingsOptions, EmbeddingsResponse,\n CreateFileOptions, FileResponse, FileListResponse, FindFileListOptions,\n} from \"./types\"\nimport Files from \"./capability/files.js\"\n\nexport type ZhipuAIOptions = {\n apiKey: string,\n baseUrl?: string,\n timeout?: number,\n maxRetries?: number,\n customHeaders?: object\n}\n\nexport class ZhipuAI {\n public __esModule = false\n public request: Request\n public completions: Completions\n public images: Images\n public embeddings: Embeddings\n public files: Files\n\n\n constructor(private readonly options: ZhipuAIOptions) {\n if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || ''\n assert.ok(options.apiKey, \"未提供api_key,请通过参数或环境变量提供\")\n if (!options.baseUrl) options.baseUrl = process.env[\"ZHIPUAI_BASE_URL\"] || ''\n if (!options.baseUrl) options.baseUrl = \"https://open.bigmodel.cn/api/paas/v4\"\n this.request = new Request({\n timeout: options.timeout,\n headers: options.customHeaders,\n baseURL: options.baseUrl\n }, this.authHeaders.bind(this));\n\n this.completions = new Completions(this.request)\n this.images = new Images(this.request)\n this.embeddings = new Embeddings(this.request)\n this.files = new Files(this.request)\n }\n\n public async createCompletions(options: CreateCompletionsOptions): Promise {\n return this.completions.create(options)\n }\n\n public async createImages(options: CreateImagesOptions): Promise {\n return this.images.create(options)\n }\n\n public async createEmbeddings(options: CreateEmbeddingsOptions): Promise {\n return this.embeddings.create(options)\n }\n\n public async createFiles(options: CreateFileOptions): Promise {\n return this.files.create(options)\n }\n\n public async findFiles(options: FindFileListOptions = {}): Promise {\n return this.files.findList(options)\n }\n\n public authHeaders(): { [key: string]: string } {\n const token = generateToken(this.options.apiKey)\n return { \"Authorization\": token }\n }\n}\n\nexport default ZhipuAI","import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}\n","import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig, AxiosHeaderValue } from \"axios\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(config: CreateAxiosDefaults, authHeaders: () => { [key: string]: AxiosHeaderValue }) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateCompletionsOptions, CompletionsResponseMessage } from \"../types/index.js\";\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateImagesOptions, ImagesResponse } from \"../types/index.js\";\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"../types/index.js\";\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from \"../types/index.js\";\n\nexport default class Files extends BaseApi {\n public async create(options: CreateFileOptions): Promise {\n const formData = new FormData()\n formData.append(\"purpose\", options.purpose);\n formData.append(\"file\", options.file);\n return this.postForm(\"/files\", formData, options)\n }\n\n public async findList(options: FindFileListOptions): Promise {\n return this.get(\"/files\", {\n \"purpose\": options.purpose,\n \"limit\": options.limit,\n \"after\": options.after,\n \"order\": options.order,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAmB;;;ACAnB,0BAAyB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM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D,UAA+B,CAAC,GAA8B;AACjF,aAAO,KAAK,MAAM,SAAS,OAAO;AAAA,IACtC;AAAA;AAAA,EAEO,cAAyC;AAC5C,UAAM,QAAQ,cAAc,KAAK,QAAQ,MAAM;AAC/C,WAAO,EAAE,iBAAiB,MAAM;AAAA,EACpC;AACJ;AAEA,IAAO,mBAAQ;","names":["jsonwebtoken","Axios","config","assert"]} \ No newline at end of file diff --git a/dist/zhipu-ai.d.cts b/dist/zhipu-ai.d.cts index b48e8b6..74f8326 100644 --- a/dist/zhipu-ai.d.cts +++ b/dist/zhipu-ai.d.cts @@ -1,2 +1,40 @@ -export { Z as ZhipuAI, g as ZhipuAIOptions, Z as default } from './index-hCfUd9Lh.cjs'; +import Request from './core/request.cjs'; +import Completions from './capability/completions.cjs'; +import Images from './capability/images.cjs'; +import Embeddings from './capability/embeddings.cjs'; +import { CreateCompletionsOptions, CompletionsResponseMessage } from './types/completions.cjs'; +import { CreateImagesOptions, ImagesResponse } from './types/images.cjs'; +import { CreateEmbeddingsOptions, EmbeddingsResponse } from './types/embeddings.cjs'; +import { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from './types/files.cjs'; +import Files from './capability/files.cjs'; import 'axios'; +import './core/baseApi.cjs'; +import './types/baseApi.cjs'; + +type ZhipuAIOptions = { + apiKey: string; + baseUrl?: string; + timeout?: number; + maxRetries?: number; + customHeaders?: object; +}; +declare class ZhipuAI { + private readonly options; + __esModule: boolean; + request: Request; + completions: Completions; + images: Images; + embeddings: Embeddings; + files: Files; + constructor(options: ZhipuAIOptions); + createCompletions(options: CreateCompletionsOptions): Promise; + createImages(options: CreateImagesOptions): Promise; + createEmbeddings(options: CreateEmbeddingsOptions): Promise; + createFiles(options: CreateFileOptions): Promise; + findFiles(options?: FindFileListOptions): Promise; + authHeaders(): { + [key: string]: string; + }; +} + +export { ZhipuAI, type ZhipuAIOptions, ZhipuAI as default }; diff --git a/dist/zhipu-ai.d.ts b/dist/zhipu-ai.d.ts index e3eddd0..6e9b8e1 100644 --- a/dist/zhipu-ai.d.ts +++ b/dist/zhipu-ai.d.ts @@ -1,2 +1,40 @@ -export { Z as ZhipuAI, g as ZhipuAIOptions, Z as default } from './index-hCfUd9Lh.js'; +import Request from './core/request.js'; +import Completions from './capability/completions.js'; +import Images from './capability/images.js'; +import Embeddings from './capability/embeddings.js'; +import { CreateCompletionsOptions, CompletionsResponseMessage } from './types/completions.js'; +import { CreateImagesOptions, ImagesResponse } from './types/images.js'; +import { CreateEmbeddingsOptions, EmbeddingsResponse } from './types/embeddings.js'; +import { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from './types/files.js'; +import Files from './capability/files.js'; import 'axios'; +import './core/baseApi.js'; +import './types/baseApi.js'; + +type ZhipuAIOptions = { + apiKey: string; + baseUrl?: string; + timeout?: number; + maxRetries?: number; + customHeaders?: object; +}; +declare class ZhipuAI { + private readonly options; + __esModule: boolean; + request: Request; + completions: Completions; + images: Images; + embeddings: Embeddings; + files: Files; + constructor(options: ZhipuAIOptions); + createCompletions(options: CreateCompletionsOptions): Promise; + createImages(options: CreateImagesOptions): Promise; + createEmbeddings(options: CreateEmbeddingsOptions): Promise; + createFiles(options: CreateFileOptions): Promise; + findFiles(options?: FindFileListOptions): Promise; + authHeaders(): { + [key: string]: string; + }; +} + +export { ZhipuAI, type ZhipuAIOptions, ZhipuAI as default }; diff --git a/dist/zhipu-ai.js b/dist/zhipu-ai.js index 89325d3..295b7a1 100644 --- a/dist/zhipu-ai.js +++ b/dist/zhipu-ai.js @@ -28,7 +28,7 @@ var __async = (__this, __arguments, generator) => { // lib/zhipu-ai.ts import assert from "assert"; -// lib/jwt.ts +// lib/core/jwt.ts import jsonwebtoken from "jsonwebtoken"; var API_TOKEN_TTL_SECONDS = 3 * 60; var generateToken = (apiSecretKey) => { @@ -49,26 +49,85 @@ var generateToken = (apiSecretKey) => { } }; -// lib/baseApi.ts +// lib/core/request.ts +import Axios from "axios"; +var Request = class { + constructor(config, authHeaders) { + __publicField(this, "request"); + this.request = Axios.create(config); + this.request.interceptors.request.use((config2) => { + config2.headers.set(authHeaders()); + return config2; + }); + } + post(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } + postForm(url, data, config) { + return __async(this, null, function* () { + return this.request.post(url, data, config).then((res) => res.data); + }); + } + get(url, config) { + return __async(this, null, function* () { + return this.request.get(url, config).then((res) => res.data); + }); + } + put(url, data, config) { + return __async(this, null, function* () { + return this.request.put(url, data, config).then((res) => res.data); + }); + } + delete(url, config) { + return __async(this, null, function* () { + return this.request.delete(url, config).then((res) => res.data); + }); + } +}; + +// lib/core/baseApi.ts var BaseApi = class { constructor(request) { this.request = request; } + processError(err) { + var _a, _b; + const data = (_b = (_a = err == null ? void 0 : err.response) == null ? void 0 : _a.data) != null ? _b : err; + return Promise.reject(data); + } + get(url, params, options) { + return __async(this, null, function* () { + return this.request.get(url, { + params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); + }); + } post(url, data, options) { return __async(this, null, function* () { return this.request.post(url, data, { headers: options.extraHeaders, timeout: options.timeout, responseType: options.stream ? "stream" : "json" - }).catch((err) => { - const data2 = err.response.data; - return Promise.reject(data2); - }); + }).catch(this.processError); + }); + } + postForm(url, data, options) { + return __async(this, null, function* () { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? "stream" : "json" + }).catch(this.processError); }); } }; -// lib/completions.ts +// lib/capability/completions.ts var Completions = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -91,41 +150,7 @@ var Completions = class extends BaseApi { } }; -// lib/request.ts -import Axios from "axios"; -var Request = class { - constructor(app, config) { - this.app = app; - __publicField(this, "request"); - this.request = Axios.create(config); - this.request.interceptors.request.use((config2) => { - config2.headers.set(this.app.authHeaders()); - return config2; - }); - } - post(url, data, config) { - return __async(this, null, function* () { - return this.request.post(url, data, config).then((res) => res.data); - }); - } - get(url, config) { - return __async(this, null, function* () { - return this.request.get(url, config).then((res) => res.data); - }); - } - put(url, data, config) { - return __async(this, null, function* () { - return this.request.put(url, data, config).then((res) => res.data); - }); - } - delete(url, config) { - return __async(this, null, function* () { - return this.request.delete(url, config).then((res) => res.data); - }); - } -}; - -// lib/images.ts +// lib/capability/images.ts var Images = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -143,7 +168,7 @@ var Images = class extends BaseApi { } }; -// lib/embeddings.ts +// lib/capability/embeddings.ts var Embeddings = class extends BaseApi { create(options) { return __async(this, null, function* () { @@ -158,12 +183,38 @@ var Embeddings = class extends BaseApi { } }; +// lib/capability/files.ts +var Files = class extends BaseApi { + create(options) { + return __async(this, null, function* () { + const formData = new FormData(); + formData.append("purpose", options.purpose); + formData.append("file", options.file); + return this.postForm("/files", formData, options); + }); + } + findList(options) { + return __async(this, null, function* () { + return this.get("/files", { + "purpose": options.purpose, + "limit": options.limit, + "after": options.after, + "order": options.order + }, options); + }); + } +}; + // lib/zhipu-ai.ts var ZhipuAI = class { constructor(options) { this.options = options; __publicField(this, "__esModule", false); __publicField(this, "request"); + __publicField(this, "completions"); + __publicField(this, "images"); + __publicField(this, "embeddings"); + __publicField(this, "files"); if (!options.apiKey) options.apiKey = process.env["ZHIPUAI_API_KEY"] || ""; assert.ok(options.apiKey, "\u672A\u63D0\u4F9Bapi_key\uFF0C\u8BF7\u901A\u8FC7\u53C2\u6570\u6216\u73AF\u5883\u53D8\u91CF\u63D0\u4F9B"); @@ -171,25 +222,39 @@ var ZhipuAI = class { options.baseUrl = process.env["ZHIPUAI_BASE_URL"] || ""; if (!options.baseUrl) options.baseUrl = "https://open.bigmodel.cn/api/paas/v4"; - this.request = new Request(this, { + this.request = new Request({ timeout: options.timeout, headers: options.customHeaders, baseURL: options.baseUrl - }); + }, this.authHeaders.bind(this)); + this.completions = new Completions(this.request); + this.images = new Images(this.request); + this.embeddings = new Embeddings(this.request); + this.files = new Files(this.request); } createCompletions(options) { return __async(this, null, function* () { - return new Completions(this.request).create(options); + return this.completions.create(options); }); } createImages(options) { return __async(this, null, function* () { - return new Images(this.request).create(options); + return this.images.create(options); }); } createEmbeddings(options) { return __async(this, null, function* () { - return new Embeddings(this.request).create(options); + return this.embeddings.create(options); + }); + } + createFiles(options) { + return __async(this, null, function* () { + return this.files.create(options); + }); + } + findFiles() { + return __async(this, arguments, function* (options = {}) { + return this.files.findList(options); }); } authHeaders() { diff --git a/dist/zhipu-ai.js.map b/dist/zhipu-ai.js.map index 48210b3..6e1aaa8 100644 --- a/dist/zhipu-ai.js.map +++ b/dist/zhipu-ai.js.map @@ -1 +1 @@ -{"version":3,"sources":["../lib/zhipu-ai.ts","../lib/jwt.ts","../lib/baseApi.ts","../lib/completions.ts","../lib/request.ts","../lib/images.ts","../lib/embeddings.ts"],"sourcesContent":["import assert from \"assert\"\nimport { generateToken } from \"./jwt.js\"\nimport Completions, { CompletionsResponseMessage, CreateCompletionsOptions } from \"./completions.js\"\nimport Request from \"./request.js\"\nimport Images, { CreateImagesOptions, ImagesResponse } from \"./images.js\"\nimport Embeddings, { CreateEmbeddingsOptions, EmbeddingsResponse } from \"./embeddings.js\"\n\nexport type ZhipuAIOptions = {\n apiKey: string,\n baseUrl?: string,\n timeout?: number,\n maxRetries?: number,\n customHeaders?: object\n}\n\nexport class ZhipuAI {\n public __esModule = false\n public request: Request\n\n constructor(private readonly options: ZhipuAIOptions) {\n if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || ''\n assert.ok(options.apiKey, \"未提供api_key,请通过参数或环境变量提供\")\n if (!options.baseUrl) options.baseUrl = process.env[\"ZHIPUAI_BASE_URL\"] || ''\n if (!options.baseUrl) options.baseUrl = \"https://open.bigmodel.cn/api/paas/v4\"\n this.request = new Request(this, {\n timeout: options.timeout,\n headers: options.customHeaders,\n baseURL: options.baseUrl\n });\n }\n\n public async createCompletions(options: CreateCompletionsOptions): Promise {\n return new Completions(this.request).create(options)\n }\n\n public async createImages(options: CreateImagesOptions): Promise {\n return new Images(this.request).create(options)\n }\n\n public async createEmbeddings(options: CreateEmbeddingsOptions): Promise {\n return new Embeddings(this.request).create(options)\n }\n\n public authHeaders() {\n const token = generateToken(this.options.apiKey)\n return { \"Authorization\": token }\n }\n}\n\nexport default ZhipuAI","import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}","import { AxiosResponse } from \"axios\";\nimport Request from \"./request\";\n\n\nexport type moreRequestOptions = {\n extraHeaders?: object,\n timeout?: number,\n stream?: boolean\n}\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n public async post(url: string, data: object, options: Options & moreRequestOptions): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(err => {\n const data = err.response.data as Response\n return Promise.reject(data)\n })\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type MessageOptions = {\n role: \"system\" | \"user\" | \"assistant\" | \"function\"\n content: string\n}\n\nexport type CreateCompletionsOptions = {\n model: string,\n messages: Array,\n requestId?: string,\n doSample?: boolean,\n stream?: boolean,\n temperature?: number,\n topP?: number,\n maxTokens?: number,\n seed?: number,\n stop?: Array,\n sensitiveWordCheck?: object,\n tools?: object,\n toolChoice?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type CompletionsResponseMessage = {\n id: string,\n created: number,\n model: string,\n choices: Array<{\n index: number,\n finish_reason: string,\n message: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n },\n delta: {\n role: string,\n content: string,\n tool_calls: Array<{\n id: string,\n type: string,\n function: {\n name: string,\n arguments: object,\n }\n }>\n }\n }>,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number,\n },\n}\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from \"axios\";\nimport ZhipuAI from \"./zhipu-ai\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(private readonly app: ZhipuAI, config: CreateAxiosDefaults) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(this.app.authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}","import BaseApi from \"./baseApi\";\n\nexport type CreateImagesOptions = {\n model: string,\n prompt?: string,\n n?: number,\n quality?: string,\n responseFormat?: string,\n size?: string,\n style?: string,\n user?: string,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type ImagesResponse = {\n created: number,\n data: Array\n}\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"./baseApi\";\n\nexport type CreateEmbeddingsOptions = {\n input: Array | string,\n model: string,\n encodingFormat: string,\n user: string,\n sensitiveWordCheck: object,\n extraHeaders?: object,\n timeout?: number,\n}\n\nexport type EmbeddingsResponse = {\n model: string,\n data: Array<{\n index: number,\n object: string,\n embedding: Array\n }>,\n object: string,\n usage: {\n prompt_tokens: number,\n completion_tokens: number,\n total_tokens: number\n }\n}\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,YAAY;;;ACAnB,OAAO,kBAAkB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,aAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,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;;;ANtBO,IAAM,UAAN,MAAc;AAAA,EAIjB,YAA6B,SAAyB;AAAzB;AAH7B,wBAAO,cAAa;AACpB,wBAAO;AAGH,QAAI,CAAC,QAAQ;AAAQ,cAAQ,SAAS,QAAQ,IAAI,iBAAiB,KAAK;AACxE,WAAO,GAAG,QAAQ,QAAQ,yGAAyB;AACnD,QAAI,CAAC,QAAQ;AAAS,cAAQ,UAAU,QAAQ,IAAI,kBAAkB,KAAK;AAC3E,QAAI,CAAC,QAAQ;AAAS,cAAQ,UAAU;AACxC,SAAK,UAAU,IAAI,QAAQ,MAAM;AAAA,MAC7B,SAAS,QAAQ;AAAA,MACjB,SAAS,QAAQ;AAAA,MACjB,SAAS,QAAQ;AAAA,IACrB,CAAC;AAAA,EACL;AAAA,EAEa,kBAAkB,SAAwE;AAAA;AACnG,aAAO,IAAI,YAAY,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IACvD;AAAA;AAAA,EAEa,aAAa,SAAuD;AAAA;AAC7E,aAAO,IAAI,OAAO,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IAClD;AAAA;AAAA,EAEa,iBAAiB,SAA+D;AAAA;AACzF,aAAO,IAAI,WAAW,KAAK,OAAO,EAAE,OAAO,OAAO;AAAA,IACtD;AAAA;AAAA,EAEO,cAAc;AACjB,UAAM,QAAQ,cAAc,KAAK,QAAQ,MAAM;AAC/C,WAAO,EAAE,iBAAiB,MAAM;AAAA,EACpC;AACJ;AAEA,IAAO,mBAAQ;","names":["data","config"]} \ No newline at end of file +{"version":3,"sources":["../lib/zhipu-ai.ts","../lib/core/jwt.ts","../lib/core/request.ts","../lib/core/baseApi.ts","../lib/capability/completions.ts","../lib/capability/images.ts","../lib/capability/embeddings.ts","../lib/capability/files.ts"],"sourcesContent":["import assert from \"assert\"\nimport { generateToken } from \"./core/jwt.js\"\nimport Request from \"./core/request.js\"\n\nimport { Completions, Images, Embeddings } from \"./capability\"\n\nimport {\n CompletionsResponseMessage, CreateCompletionsOptions,\n CreateImagesOptions, ImagesResponse,\n CreateEmbeddingsOptions, EmbeddingsResponse,\n CreateFileOptions, FileResponse, FileListResponse, FindFileListOptions,\n} from \"./types\"\nimport Files from \"./capability/files.js\"\n\nexport type ZhipuAIOptions = {\n apiKey: string,\n baseUrl?: string,\n timeout?: number,\n maxRetries?: number,\n customHeaders?: object\n}\n\nexport class ZhipuAI {\n public __esModule = false\n public request: Request\n public completions: Completions\n public images: Images\n public embeddings: Embeddings\n public files: Files\n\n\n constructor(private readonly options: ZhipuAIOptions) {\n if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || ''\n assert.ok(options.apiKey, \"未提供api_key,请通过参数或环境变量提供\")\n if (!options.baseUrl) options.baseUrl = process.env[\"ZHIPUAI_BASE_URL\"] || ''\n if (!options.baseUrl) options.baseUrl = \"https://open.bigmodel.cn/api/paas/v4\"\n this.request = new Request({\n timeout: options.timeout,\n headers: options.customHeaders,\n baseURL: options.baseUrl\n }, this.authHeaders.bind(this));\n\n this.completions = new Completions(this.request)\n this.images = new Images(this.request)\n this.embeddings = new Embeddings(this.request)\n this.files = new Files(this.request)\n }\n\n public async createCompletions(options: CreateCompletionsOptions): Promise {\n return this.completions.create(options)\n }\n\n public async createImages(options: CreateImagesOptions): Promise {\n return this.images.create(options)\n }\n\n public async createEmbeddings(options: CreateEmbeddingsOptions): Promise {\n return this.embeddings.create(options)\n }\n\n public async createFiles(options: CreateFileOptions): Promise {\n return this.files.create(options)\n }\n\n public async findFiles(options: FindFileListOptions = {}): Promise {\n return this.files.findList(options)\n }\n\n public authHeaders(): { [key: string]: string } {\n const token = generateToken(this.options.apiKey)\n return { \"Authorization\": token }\n }\n}\n\nexport default ZhipuAI","import jsonwebtoken from \"jsonwebtoken\"\n\nconst API_TOKEN_TTL_SECONDS = 3 * 60\n\nexport const generateToken = (apiSecretKey: string) => {\n try {\n const [apiKey, secret] = apiSecretKey.split(\".\")\n const payload = {\n \"api_key\": apiKey,\n \"exp\": Math.round(Date.now() * 1000) + API_TOKEN_TTL_SECONDS * 1000,\n \"timestamp\": Math.round(Date.now() * 1000),\n }\n // algorithm = \"HS256\", headers = { \"alg\": \"HS256\", \"sign_type\": \"SIGN\" }\n //@ts-ignore 不用管\n const ret = jsonwebtoken.sign(payload, secret, {\n algorithm: \"HS256\",\n header: { alg: \"HS256\", sign_type: \"SIGN\" }\n })\n return ret\n } catch (e) {\n throw \"invalid api_key\"\n }\n}\n","import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig, AxiosHeaderValue } from \"axios\";\n\nexport default class Request {\n public request: AxiosInstance\n\n constructor(config: CreateAxiosDefaults, authHeaders: () => { [key: string]: AxiosHeaderValue }) {\n this.request = Axios.create(config)\n\n this.request.interceptors.request.use((config) => {\n config.headers.set(authHeaders())\n return config;\n })\n }\n\n async post, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise {\n return this.request.post(url, data, config).then(res => res.data)\n }\n\n async get, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.get(url, config).then(res => res.data)\n }\n\n async put, D = any>(url: string, data?: D, config?: AxiosRequestConfig): Promise {\n return this.request.put(url, data, config).then(res => res.data)\n }\n\n async delete, D = any>(url: string, config?: AxiosRequestConfig): Promise {\n return this.request.delete(url, config).then(res => res.data)\n }\n}","import Request from \"./request.js\";\nimport { BaseRequestOptions } from \"../types/index.js\";\n\nexport default class BaseApi {\n constructor(private readonly request: Request) {\n }\n\n private processError(err: any) {\n const data = err?.response?.data ?? err\n return Promise.reject(data)\n }\n\n public async get(url: string, params: object, options: Options): Promise {\n return this.request.get(url, {\n params: params,\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async post(url: string, data: object, options: Options): Promise {\n return this.request.post(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n\n public async postForm(url: string, data: FormData, options: Options): Promise {\n return this.request.postForm(url, data, {\n headers: options.extraHeaders,\n timeout: options.timeout,\n responseType: options.stream ? 'stream' : 'json'\n })\n .catch(this.processError)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateCompletionsOptions, CompletionsResponseMessage } from \"../types/index.js\";\n\nexport default class Completions extends BaseApi {\n\n public async create(options: CreateCompletionsOptions): Promise {\n return this.post(\"/chat/completions\", {\n \"model\": options.model,\n \"request_id\": options.requestId,\n \"temperature\": options.temperature,\n \"top_p\": options.topP,\n \"do_sample\": options.doSample,\n \"max_tokens\": options.maxTokens,\n \"seed\": options.seed,\n \"messages\": options.messages,\n \"stop\": options.stop,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n \"stream\": options.stream,\n \"tools\": options.tools,\n \"tool_choice\": options.toolChoice,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateImagesOptions, ImagesResponse } from \"../types/index.js\";\n\nexport default class Images extends BaseApi {\n public async create(options: CreateImagesOptions): Promise {\n return this.post(\"/images/generations\", {\n \"prompt\": options.prompt,\n \"model\": options.model,\n \"n\": options.n,\n \"quality\": options.quality,\n \"response_format\": options.responseFormat,\n \"size\": options.size,\n \"style\": options.style,\n \"user\": options.user,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateEmbeddingsOptions, EmbeddingsResponse } from \"../types/index.js\";\n\nexport default class Embeddings extends BaseApi {\n public async create(options: CreateEmbeddingsOptions): Promise {\n return this.post(\"/embeddings\", {\n \"input\": options.input,\n \"model\": options.model,\n \"encoding_format\": options.encodingFormat,\n \"user\": options.user,\n \"sensitive_word_check\": options.sensitiveWordCheck,\n }, options)\n }\n} ","import BaseApi from \"../core/baseApi.js\";\nimport { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from \"../types/index.js\";\n\nexport default class Files extends BaseApi {\n public async create(options: CreateFileOptions): Promise {\n const formData = new FormData()\n formData.append(\"purpose\", options.purpose);\n formData.append(\"file\", options.file);\n return this.postForm(\"/files\", formData, options)\n }\n\n public async findList(options: FindFileListOptions): Promise {\n return this.get(\"/files\", {\n \"purpose\": options.purpose,\n \"limit\": options.limit,\n \"after\": options.after,\n \"order\": options.order,\n }, options)\n }\n} "],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,YAAY;;;ACAnB,OAAO,kBAAkB;AAEzB,IAAM,wBAAwB,IAAI;AAE3B,IAAM,gBAAgB,CAAC,iBAAyB;AACnD,MAAI;AACA,UAAM,CAAC,QAAQ,MAAM,IAAI,aAAa,MAAM,GAAG;AAC/C,UAAM,UAAU;AAAA,MACZ,WAAW;AAAA,MACX,OAAO,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI,IAAI,wBAAwB;AAAA,MAC/D,aAAa,KAAK,MAAM,KAAK,IAAI,IAAI,GAAI;AAAA,IAC7C;AAGA,UAAM,MAAM,aAAa,KAAK,SAAS,QAAQ;AAAA,MAC3C,WAAW;AAAA,MACX,QAAQ,EAAE,KAAK,SAAS,WAAW,OAAO;AAAA,IAC9C,CAAC;AACD,WAAO;AAAA,EACX,SAAS,GAAG;AACR,UAAM;AAAA,EACV;AACJ;;;ACtBA,OAAO,WAAwG;AAE/G,IAAqB,UAArB,MAA6B;AAAA,EAGzB,YAAY,QAA6B,aAAwD;AAFjG,wBAAO;AAGH,SAAK,UAAU,MAAM,OAAO,MAAM;AAElC,SAAK,QAAQ,aAAa,QAAQ,IAAI,CAACA,YAAW;AAC9C,MAAAA,QAAO,QAAQ,IAAI,YAAY,CAAC;AAChC,aAAOA;AAAA,IACX,CAAC;AAAA,EACL;AAAA,EAEM,KAA6C,KAAa,MAAU,QAA4C;AAAA;AAClH,aAAO,KAAK,QAAQ,KAAK,KAAK,MAAM,MAAM,EAAE,KAAK,SAAO,IAAI,IAAI;AAAA,IACpE;AAAA;A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K,OAAO;AAC7C,SAAK,QAAQ,IAAI,MAAM,KAAK,OAAO;AAAA,EACvC;AAAA,EAEa,kBAAkB,SAAwE;AAAA;AACnG,aAAO,KAAK,YAAY,OAAO,OAAO;AAAA,IAC1C;AAAA;AAAA,EAEa,aAAa,SAAuD;AAAA;AAC7E,aAAO,KAAK,OAAO,OAAO,OAAO;AAAA,IACrC;AAAA;AAAA,EAEa,iBAAiB,SAA+D;AAAA;AACzF,aAAO,KAAK,WAAW,OAAO,OAAO;AAAA,IACzC;AAAA;AAAA,EAEa,YAAY,SAAmD;AAAA;AACxE,aAAO,KAAK,MAAM,OAAO,OAAO;AAAA,IACpC;AAAA;AAAA,EAEa,YAAwE;AAAA,+CAA9D,UAA+B,CAAC,GAA8B;AACjF,aAAO,KAAK,MAAM,SAAS,OAAO;AAAA,IACtC;AAAA;AAAA,EAEO,cAAyC;AAC5C,UAAM,QAAQ,cAAc,KAAK,QAAQ,MAAM;AAC/C,WAAO,EAAE,iBAAiB,MAAM;AAAA,EACpC;AACJ;AAEA,IAAO,mBAAQ;","names":["config"]} \ No newline at end of file diff --git a/index.js b/index.js index 639283e..af6430b 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -export { default as ZhipuAI } from "./zhipu-ai.js" -export { default as Completions } from "./completions.js" -export { default as Images } from "./images.js" -export { default } from "./zhipu-ai.js" \ No newline at end of file +export { default as ZhipuAI } from "./dist/zhipu-ai.js" +export { default } from "./dist/zhipu-ai.js" +export { Completions, Images, Embeddings } from "./dist/capability/index.js" \ No newline at end of file diff --git a/lib/baseApi.ts b/lib/baseApi.ts deleted file mode 100644 index 2f3164a..0000000 --- a/lib/baseApi.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { AxiosResponse } from "axios"; -import Request from "./request"; - - -export type moreRequestOptions = { - extraHeaders?: object, - timeout?: number, - stream?: boolean -} - -export default class BaseApi { - constructor(private readonly request: Request) { - } - - public async post(url: string, data: object, options: Options & moreRequestOptions): Promise { - return this.request.post(url, data, { - headers: options.extraHeaders, - timeout: options.timeout, - responseType: options.stream ? 'stream' : 'json' - }) - .catch(err => { - const data = err.response.data as Response - return Promise.reject(data) - }) - } -} \ No newline at end of file diff --git a/lib/capability/completions.ts b/lib/capability/completions.ts new file mode 100644 index 0000000..037e5b1 --- /dev/null +++ b/lib/capability/completions.ts @@ -0,0 +1,23 @@ +import BaseApi from "../core/baseApi.js"; +import { CreateCompletionsOptions, CompletionsResponseMessage } from "../types/index.js"; + +export default class Completions extends BaseApi { + + public async create(options: CreateCompletionsOptions): Promise { + return this.post("/chat/completions", { + "model": options.model, + "request_id": options.requestId, + "temperature": options.temperature, + "top_p": options.topP, + "do_sample": options.doSample, + "max_tokens": options.maxTokens, + "seed": options.seed, + "messages": options.messages, + "stop": options.stop, + "sensitive_word_check": options.sensitiveWordCheck, + "stream": options.stream, + "tools": options.tools, + "tool_choice": options.toolChoice, + }, options) + } +} \ No newline at end of file diff --git a/lib/capability/embeddings.ts b/lib/capability/embeddings.ts new file mode 100644 index 0000000..780571c --- /dev/null +++ b/lib/capability/embeddings.ts @@ -0,0 +1,14 @@ +import BaseApi from "../core/baseApi.js"; +import { CreateEmbeddingsOptions, EmbeddingsResponse } from "../types/index.js"; + +export default class Embeddings extends BaseApi { + public async create(options: CreateEmbeddingsOptions): Promise { + return this.post("/embeddings", { + "input": options.input, + "model": options.model, + "encoding_format": options.encodingFormat, + "user": options.user, + "sensitive_word_check": options.sensitiveWordCheck, + }, options) + } +} \ No newline at end of file diff --git a/lib/capability/files.ts b/lib/capability/files.ts new file mode 100644 index 0000000..0980ce8 --- /dev/null +++ b/lib/capability/files.ts @@ -0,0 +1,20 @@ +import BaseApi from "../core/baseApi.js"; +import { CreateFileOptions, FileResponse, FindFileListOptions, FileListResponse } from "../types/index.js"; + +export default class Files extends BaseApi { + public async create(options: CreateFileOptions): Promise { + const formData = new FormData() + formData.append("purpose", options.purpose); + formData.append("file", options.file); + return this.postForm("/files", formData, options) + } + + public async findList(options: FindFileListOptions): Promise { + return this.get("/files", { + "purpose": options.purpose, + "limit": options.limit, + "after": options.after, + "order": options.order, + }, options) + } +} \ No newline at end of file diff --git a/lib/images.ts b/lib/capability/images.ts similarity index 50% rename from lib/images.ts rename to lib/capability/images.ts index daf7a2e..f61ad6b 100644 --- a/lib/images.ts +++ b/lib/capability/images.ts @@ -1,24 +1,7 @@ -import BaseApi from "./baseApi"; +import BaseApi from "../core/baseApi.js"; +import { CreateImagesOptions, ImagesResponse } from "../types/index.js"; -export type CreateImagesOptions = { - model: string, - prompt?: string, - n?: number, - quality?: string, - responseFormat?: string, - size?: string, - style?: string, - user?: string, - extraHeaders?: object, - timeout?: number, -} - -export type ImagesResponse = { - created: number, - data: Array -} - -export default class Images extends BaseApi { +export default class Images extends BaseApi { public async create(options: CreateImagesOptions): Promise { return this.post("/images/generations", { "prompt": options.prompt, diff --git a/lib/capability/index.ts b/lib/capability/index.ts new file mode 100644 index 0000000..a747dc6 --- /dev/null +++ b/lib/capability/index.ts @@ -0,0 +1,3 @@ +export { default as Completions } from "./completions.js" +export { default as Images } from "./images.js" +export { default as Embeddings } from "./embeddings.js" \ No newline at end of file diff --git a/lib/core/baseApi.ts b/lib/core/baseApi.ts new file mode 100644 index 0000000..862e0f3 --- /dev/null +++ b/lib/core/baseApi.ts @@ -0,0 +1,40 @@ +import Request from "./request.js"; +import { BaseRequestOptions } from "../types/index.js"; + +export default class BaseApi { + constructor(private readonly request: Request) { + } + + private processError(err: any) { + const data = err?.response?.data ?? err + return Promise.reject(data) + } + + public async get(url: string, params: object, options: Options): Promise { + return this.request.get(url, { + params: params, + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? 'stream' : 'json' + }) + .catch(this.processError) + } + + public async post(url: string, data: object, options: Options): Promise { + return this.request.post(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? 'stream' : 'json' + }) + .catch(this.processError) + } + + public async postForm(url: string, data: FormData, options: Options): Promise { + return this.request.postForm(url, data, { + headers: options.extraHeaders, + timeout: options.timeout, + responseType: options.stream ? 'stream' : 'json' + }) + .catch(this.processError) + } +} \ No newline at end of file diff --git a/lib/jwt.ts b/lib/core/jwt.ts similarity index 99% rename from lib/jwt.ts rename to lib/core/jwt.ts index f8dd6fa..b577d8d 100644 --- a/lib/jwt.ts +++ b/lib/core/jwt.ts @@ -20,4 +20,4 @@ export const generateToken = (apiSecretKey: string) => { } catch (e) { throw "invalid api_key" } -} \ No newline at end of file +} diff --git a/lib/request.ts b/lib/core/request.ts similarity index 71% rename from lib/request.ts rename to lib/core/request.ts index 21438be..6f80d44 100644 --- a/lib/request.ts +++ b/lib/core/request.ts @@ -1,14 +1,13 @@ -import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig } from "axios"; -import ZhipuAI from "./zhipu-ai"; +import Axios, { AxiosInstance, CreateAxiosDefaults, AxiosResponse, AxiosRequestConfig, AxiosHeaderValue } from "axios"; export default class Request { public request: AxiosInstance - constructor(private readonly app: ZhipuAI, config: CreateAxiosDefaults) { + constructor(config: CreateAxiosDefaults, authHeaders: () => { [key: string]: AxiosHeaderValue }) { this.request = Axios.create(config) this.request.interceptors.request.use((config) => { - config.headers.set(this.app.authHeaders()) + config.headers.set(authHeaders()) return config; }) } @@ -17,6 +16,10 @@ export default class Request { return this.request.post(url, data, config).then(res => res.data) } + async postForm, D = object>(url: string, data: D, config?: AxiosRequestConfig): Promise { + return this.request.post(url, data, config).then(res => res.data) + } + async get, D = any>(url: string, config?: AxiosRequestConfig): Promise { return this.request.get(url, config).then(res => res.data) } diff --git a/lib/embeddings.ts b/lib/embeddings.ts deleted file mode 100644 index f3b1c24..0000000 --- a/lib/embeddings.ts +++ /dev/null @@ -1,38 +0,0 @@ -import BaseApi from "./baseApi"; - -export type CreateEmbeddingsOptions = { - input: Array | string, - model: string, - encodingFormat: string, - user: string, - sensitiveWordCheck: object, - extraHeaders?: object, - timeout?: number, -} - -export type EmbeddingsResponse = { - model: string, - data: Array<{ - index: number, - object: string, - embedding: Array - }>, - object: string, - usage: { - prompt_tokens: number, - completion_tokens: number, - total_tokens: number - } -} - -export default class Embeddings extends BaseApi { - public async create(options: CreateEmbeddingsOptions): Promise { - return this.post("/embeddings", { - "input": options.input, - "model": options.model, - "encoding_format": options.encodingFormat, - "user": options.user, - "sensitive_word_check": options.sensitiveWordCheck, - }, options) - } -} \ No newline at end of file diff --git a/lib/index.ts b/lib/index.ts index 592c92f..2a758e5 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -1,5 +1,3 @@ export { default as ZhipuAI } from "./zhipu-ai.js" -export { default as Completions } from "./completions.js" -export { default as Images } from "./images.js" -export { default as Embeddings } from "./embeddings.js" -export { default } from "./zhipu-ai.js" \ No newline at end of file +export { default } from "./zhipu-ai.js" +export { Completions, Images, Embeddings } from "./capability/index.js" \ No newline at end of file diff --git a/lib/types/baseApi.ts b/lib/types/baseApi.ts new file mode 100644 index 0000000..7b2ba24 --- /dev/null +++ b/lib/types/baseApi.ts @@ -0,0 +1,5 @@ +export type BaseRequestOptions = { + extraHeaders?: object, + timeout?: number, + stream?: boolean +} \ No newline at end of file diff --git a/lib/completions.ts b/lib/types/completions.ts similarity index 59% rename from lib/completions.ts rename to lib/types/completions.ts index cf2e849..1f45f22 100644 --- a/lib/completions.ts +++ b/lib/types/completions.ts @@ -1,4 +1,4 @@ -import BaseApi from "./baseApi"; +import { BaseRequestOptions } from './baseApi.js' export type MessageOptions = { role: "system" | "user" | "assistant" | "function" @@ -18,10 +18,8 @@ export type CreateCompletionsOptions = { stop?: Array, sensitiveWordCheck?: object, tools?: object, - toolChoice?: string, - extraHeaders?: object, - timeout?: number, -} + toolChoice?: string +} & BaseRequestOptions export type CompletionsResponseMessage = { id: string, @@ -60,25 +58,4 @@ export type CompletionsResponseMessage = { completion_tokens: number, total_tokens: number, }, -} - -export default class Completions extends BaseApi { - - public async create(options: CreateCompletionsOptions): Promise { - return this.post("/chat/completions", { - "model": options.model, - "request_id": options.requestId, - "temperature": options.temperature, - "top_p": options.topP, - "do_sample": options.doSample, - "max_tokens": options.maxTokens, - "seed": options.seed, - "messages": options.messages, - "stop": options.stop, - "sensitive_word_check": options.sensitiveWordCheck, - "stream": options.stream, - "tools": options.tools, - "tool_choice": options.toolChoice, - }, options) - } -} \ No newline at end of file +} \ No newline at end of file diff --git a/lib/types/embeddings.ts b/lib/types/embeddings.ts new file mode 100644 index 0000000..190b4e3 --- /dev/null +++ b/lib/types/embeddings.ts @@ -0,0 +1,26 @@ +import { BaseRequestOptions } from './baseApi.js' + +export type CreateEmbeddingsOptions = { + input: Array | string, + model: string, + encodingFormat: string, + user: string, + sensitiveWordCheck: object, + extraHeaders?: object, + timeout?: number, +} & BaseRequestOptions + +export type EmbeddingsResponse = { + model: string, + data: Array<{ + index: number, + object: string, + embedding: Array + }>, + object: string, + usage: { + prompt_tokens: number, + completion_tokens: number, + total_tokens: number + } +} \ No newline at end of file diff --git a/lib/types/files.ts b/lib/types/files.ts new file mode 100644 index 0000000..7478467 --- /dev/null +++ b/lib/types/files.ts @@ -0,0 +1,30 @@ +import { BaseRequestOptions } from './baseApi.js' + +export type CreateFileOptions = { + file: Blob, + purpose: string, +} & BaseRequestOptions + +export type FileResponse = { + id: string, + filename: string, + purpose: string, +} + +export type FindFileListOrderTypes = "desc" | "asc" + +export type FindFileListOptions = { + purpose?: string, + limit?: number, + after?: string, + order?: FindFileListOrderTypes, +} & BaseRequestOptions + +export type FileListResponse = { + object: string, + data: Array<{ + bytes: number, + created_at: number, + object: string + } & FileResponse> +} diff --git a/lib/types/images.ts b/lib/types/images.ts new file mode 100644 index 0000000..9be1a9b --- /dev/null +++ b/lib/types/images.ts @@ -0,0 +1,19 @@ +import { BaseRequestOptions } from './baseApi.js' + +export type CreateImagesOptions = { + model: string, + prompt?: string, + n?: number, + quality?: string, + responseFormat?: string, + size?: string, + style?: string, + user?: string, + extraHeaders?: object, + timeout?: number, +} & BaseRequestOptions + +export type ImagesResponse = { + created: number, + data: Array +} \ No newline at end of file diff --git a/lib/types/index.ts b/lib/types/index.ts new file mode 100644 index 0000000..7aaf56a --- /dev/null +++ b/lib/types/index.ts @@ -0,0 +1,6 @@ +export { BaseRequestOptions } from './baseApi.js' +export { CreateCompletionsOptions, CompletionsResponseMessage, MessageOptions } from "./completions.js" +export { CreateImagesOptions, ImagesResponse } from "./images.js" +export { CreateEmbeddingsOptions, EmbeddingsResponse } from "./embeddings.js" +export { CreateFileOptions, FileResponse, FindFileListOrderTypes, FindFileListOptions, FileListResponse } from "./files.js" + diff --git a/lib/zhipu-ai.ts b/lib/zhipu-ai.ts index 365b03a..abb9cfa 100644 --- a/lib/zhipu-ai.ts +++ b/lib/zhipu-ai.ts @@ -1,9 +1,16 @@ import assert from "assert" -import { generateToken } from "./jwt.js" -import Completions, { CompletionsResponseMessage, CreateCompletionsOptions } from "./completions.js" -import Request from "./request.js" -import Images, { CreateImagesOptions, ImagesResponse } from "./images.js" -import Embeddings, { CreateEmbeddingsOptions, EmbeddingsResponse } from "./embeddings.js" +import { generateToken } from "./core/jwt.js" +import Request from "./core/request.js" + +import { Completions, Images, Embeddings } from "./capability" + +import { + CompletionsResponseMessage, CreateCompletionsOptions, + CreateImagesOptions, ImagesResponse, + CreateEmbeddingsOptions, EmbeddingsResponse, + CreateFileOptions, FileResponse, FileListResponse, FindFileListOptions, +} from "./types" +import Files from "./capability/files.js" export type ZhipuAIOptions = { apiKey: string, @@ -16,32 +23,50 @@ export type ZhipuAIOptions = { export class ZhipuAI { public __esModule = false public request: Request + public completions: Completions + public images: Images + public embeddings: Embeddings + public files: Files + constructor(private readonly options: ZhipuAIOptions) { if (!options.apiKey) options.apiKey = process.env['ZHIPUAI_API_KEY'] || '' assert.ok(options.apiKey, "未提供api_key,请通过参数或环境变量提供") if (!options.baseUrl) options.baseUrl = process.env["ZHIPUAI_BASE_URL"] || '' if (!options.baseUrl) options.baseUrl = "https://open.bigmodel.cn/api/paas/v4" - this.request = new Request(this, { + this.request = new Request({ timeout: options.timeout, headers: options.customHeaders, baseURL: options.baseUrl - }); + }, this.authHeaders.bind(this)); + + this.completions = new Completions(this.request) + this.images = new Images(this.request) + this.embeddings = new Embeddings(this.request) + this.files = new Files(this.request) } public async createCompletions(options: CreateCompletionsOptions): Promise { - return new Completions(this.request).create(options) + return this.completions.create(options) } public async createImages(options: CreateImagesOptions): Promise { - return new Images(this.request).create(options) + return this.images.create(options) } public async createEmbeddings(options: CreateEmbeddingsOptions): Promise { - return new Embeddings(this.request).create(options) + return this.embeddings.create(options) + } + + public async createFiles(options: CreateFileOptions): Promise { + return this.files.create(options) + } + + public async findFiles(options: FindFileListOptions = {}): Promise { + return this.files.findList(options) } - public authHeaders() { + public authHeaders(): { [key: string]: string } { const token = generateToken(this.options.apiKey) return { "Authorization": token } } diff --git a/package.json b/package.json index 7860813..38597a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zhipuai-sdk-nodejs-v4", - "version": "0.1.7", + "version": "0.1.8", "license": "MIT", "main": "./index.js", "types": "dist/index.d.ts",