@huggingface/inference 4.13.5 → 4.13.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/lib/getProviderHelper.d.ts.map +1 -1
- package/dist/commonjs/lib/getProviderHelper.js +1 -0
- package/dist/commonjs/package.d.ts +1 -1
- package/dist/commonjs/package.js +1 -1
- package/dist/commonjs/providers/zai-org.d.ts +16 -18
- package/dist/commonjs/providers/zai-org.d.ts.map +1 -1
- package/dist/commonjs/providers/zai-org.js +90 -1
- package/dist/esm/lib/getProviderHelper.d.ts.map +1 -1
- package/dist/esm/lib/getProviderHelper.js +1 -0
- package/dist/esm/package.d.ts +1 -1
- package/dist/esm/package.js +1 -1
- package/dist/esm/providers/zai-org.d.ts +16 -18
- package/dist/esm/providers/zai-org.d.ts.map +1 -1
- package/dist/esm/providers/zai-org.js +89 -1
- package/package.json +2 -2
- package/src/lib/getProviderHelper.ts +1 -0
- package/src/package.ts +1 -1
- package/src/providers/zai-org.ts +147 -2
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getProviderHelper.d.ts","sourceRoot":"","sources":["../../../src/lib/getProviderHelper.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EACX,6BAA6B,EAC7B,sBAAsB,EACtB,oCAAoC,EACpC,wBAAwB,EACxB,mCAAmC,EACnC,2BAA2B,EAC3B,kBAAkB,EAClB,6BAA6B,EAC7B,2BAA2B,EAC3B,sBAAsB,EACtB,qBAAqB,EACrB,sBAAsB,EACtB,0BAA0B,EAC1B,0BAA0B,EAC1B,yBAAyB,EACzB,2BAA2B,EAC3B,4BAA4B,EAC5B,uBAAuB,EACvB,gCAAgC,EAChC,+BAA+B,EAC/B,2BAA2B,EAC3B,kBAAkB,EAClB,4BAA4B,EAC5B,wBAAwB,EACxB,qBAAqB,EACrB,qBAAqB,EACrB,sBAAsB,EACtB,qBAAqB,EACrB,6BAA6B,EAC7B,qBAAqB,EACrB,iCAAi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= exports.PACKAGE_VERSION = void 0;
|
|
4
4
|
// Generated file from package.json. Issues importing JSON directly when publishing on commonjs/ESM - see https://github.com/microsoft/TypeScript/issues/51783
|
|
5
|
-
exports.PACKAGE_VERSION = "4.13.
|
|
5
|
+
exports.PACKAGE_VERSION = "4.13.6";
|
|
6
6
|
exports.PACKAGE_NAME = "@huggingface/inference";
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* https://huggingface.co/api/partners/zai-org/models
|
|
5
|
-
*
|
|
6
|
-
* This is a publicly available mapping.
|
|
7
|
-
*
|
|
8
|
-
* If you want to try to run inference for a new model locally before it's registered on huggingface.co,
|
|
9
|
-
* you can add it to the dictionary "HARDCODED_MODEL_ID_MAPPING" in consts.ts, for dev purposes.
|
|
10
|
-
*
|
|
11
|
-
* - If you work at zai and want to update this mapping, please use the model mapping API we provide on huggingface.co
|
|
12
|
-
* - If you're a community member and want to add a new supported HF model to zai, please open an issue on the present repo
|
|
13
|
-
* and we will tag zai team members.
|
|
14
|
-
*
|
|
15
|
-
* Thanks!
|
|
16
|
-
*/
|
|
17
|
-
import { BaseConversationalTask } from "./providerHelper.js";
|
|
18
|
-
import type { HeaderParams } from "../types.js";
|
|
1
|
+
import type { BodyParams, HeaderParams } from "../types.js";
|
|
2
|
+
import { BaseConversationalTask, TaskProviderHelper, type TextToImageTaskHelper } from "./providerHelper.js";
|
|
19
3
|
export declare class ZaiConversationalTask extends BaseConversationalTask {
|
|
20
4
|
constructor();
|
|
21
5
|
prepareHeaders(params: HeaderParams, binary: boolean): Record<string, string>;
|
|
22
6
|
makeRoute(): string;
|
|
23
7
|
}
|
|
8
|
+
interface ZaiTextToImageResponse {
|
|
9
|
+
model: string;
|
|
10
|
+
id: string;
|
|
11
|
+
request_id: string;
|
|
12
|
+
task_status: "PROCESSING" | "SUCCESS" | "FAIL";
|
|
13
|
+
}
|
|
14
|
+
export declare class ZaiTextToImageTask extends TaskProviderHelper implements TextToImageTaskHelper {
|
|
15
|
+
constructor();
|
|
16
|
+
prepareHeaders(params: HeaderParams, binary: boolean): Record<string, string>;
|
|
17
|
+
makeRoute(): string;
|
|
18
|
+
preparePayload(params: BodyParams): Record<string, unknown>;
|
|
19
|
+
getResponse(response: ZaiTextToImageResponse, url?: string, headers?: Record<string, string>, outputType?: "url" | "blob" | "json"): Promise<string | Blob | Record<string, unknown>>;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
24
22
|
//# sourceMappingURL=zai-org.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zai-org.d.ts","sourceRoot":"","sources":["../../../src/providers/zai-org.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"zai-org.d.ts","sourceRoot":"","sources":["../../../src/providers/zai-org.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG5D,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,KAAK,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAI7G,qBAAa,qBAAsB,SAAQ,sBAAsB;;IAKvD,cAAc,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAO7E,SAAS,IAAI,MAAM;CAG5B;AAED,UAAU,sBAAsB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,YAAY,GAAG,SAAS,GAAG,MAAM,CAAC;CAC/C;AAaD,qBAAa,kBAAmB,SAAQ,kBAAmB,YAAW,qBAAqB;;IAKjF,cAAc,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAYtF,SAAS,IAAI,MAAM;IAInB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IASrD,WAAW,CAChB,QAAQ,EAAE,sBAAsB,EAChC,GAAG,CAAC,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,UAAU,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,GAClC,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAsFnD"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ZaiConversationalTask = void 0;
|
|
3
|
+
exports.ZaiTextToImageTask = exports.ZaiConversationalTask = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* See the registered mapping of HF model ID => ZAI model ID here:
|
|
6
6
|
*
|
|
@@ -17,6 +17,10 @@ exports.ZaiConversationalTask = void 0;
|
|
|
17
17
|
*
|
|
18
18
|
* Thanks!
|
|
19
19
|
*/
|
|
20
|
+
const errors_js_1 = require("../errors.js");
|
|
21
|
+
const isUrl_js_1 = require("../lib/isUrl.js");
|
|
22
|
+
const delay_js_1 = require("../utils/delay.js");
|
|
23
|
+
const omit_js_1 = require("../utils/omit.js");
|
|
20
24
|
const providerHelper_js_1 = require("./providerHelper.js");
|
|
21
25
|
const ZAI_API_BASE_URL = "https://api.z.ai";
|
|
22
26
|
class ZaiConversationalTask extends providerHelper_js_1.BaseConversationalTask {
|
|
@@ -34,3 +38,88 @@ class ZaiConversationalTask extends providerHelper_js_1.BaseConversationalTask {
|
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
exports.ZaiConversationalTask = ZaiConversationalTask;
|
|
41
|
+
const MAX_POLL_ATTEMPTS = 60;
|
|
42
|
+
const POLL_INTERVAL_MS = 5000;
|
|
43
|
+
class ZaiTextToImageTask extends providerHelper_js_1.TaskProviderHelper {
|
|
44
|
+
constructor() {
|
|
45
|
+
super("zai-org", ZAI_API_BASE_URL);
|
|
46
|
+
}
|
|
47
|
+
prepareHeaders(params, binary) {
|
|
48
|
+
const headers = {
|
|
49
|
+
Authorization: `Bearer ${params.accessToken}`,
|
|
50
|
+
"x-source-channel": "hugging_face",
|
|
51
|
+
"accept-language": "en-US,en",
|
|
52
|
+
};
|
|
53
|
+
if (!binary) {
|
|
54
|
+
headers["Content-Type"] = "application/json";
|
|
55
|
+
}
|
|
56
|
+
return headers;
|
|
57
|
+
}
|
|
58
|
+
makeRoute() {
|
|
59
|
+
return "/api/paas/v4/async/images/generations";
|
|
60
|
+
}
|
|
61
|
+
preparePayload(params) {
|
|
62
|
+
return {
|
|
63
|
+
...(0, omit_js_1.omit)(params.args, ["inputs", "parameters"]),
|
|
64
|
+
...params.args.parameters,
|
|
65
|
+
model: params.model,
|
|
66
|
+
prompt: params.args.inputs,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
async getResponse(response, url, headers, outputType) {
|
|
70
|
+
void url;
|
|
71
|
+
if (typeof response !== "object" ||
|
|
72
|
+
!response ||
|
|
73
|
+
!("task_status" in response) ||
|
|
74
|
+
!("id" in response) ||
|
|
75
|
+
typeof response.id !== "string") {
|
|
76
|
+
throw new errors_js_1.InferenceClientProviderOutputError(`Received malformed response from ZAI text-to-image API: expected { id: string, task_status: string }, got: ${JSON.stringify(response)}`);
|
|
77
|
+
}
|
|
78
|
+
if (response.task_status === "FAIL") {
|
|
79
|
+
throw new errors_js_1.InferenceClientProviderOutputError("ZAI API returned task status: FAIL");
|
|
80
|
+
}
|
|
81
|
+
const taskId = response.id;
|
|
82
|
+
const pollUrl = `${ZAI_API_BASE_URL}/api/paas/v4/async-result/${taskId}`;
|
|
83
|
+
const pollHeaders = {
|
|
84
|
+
"x-source-channel": "hugging_face",
|
|
85
|
+
"accept-language": "en-US,en",
|
|
86
|
+
};
|
|
87
|
+
if (headers && headers["Authorization"]) {
|
|
88
|
+
pollHeaders["Authorization"] = headers["Authorization"];
|
|
89
|
+
}
|
|
90
|
+
for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt++) {
|
|
91
|
+
await (0, delay_js_1.delay)(POLL_INTERVAL_MS);
|
|
92
|
+
const resp = await fetch(pollUrl, {
|
|
93
|
+
method: "GET",
|
|
94
|
+
headers: pollHeaders,
|
|
95
|
+
});
|
|
96
|
+
if (!resp.ok) {
|
|
97
|
+
throw new errors_js_1.InferenceClientProviderApiError(`Failed to fetch result from ZAI text-to-image API: ${resp.status}`, { url: pollUrl, method: "GET" }, { requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: await resp.text() });
|
|
98
|
+
}
|
|
99
|
+
const result = await resp.json();
|
|
100
|
+
if (result.task_status === "FAIL") {
|
|
101
|
+
throw new errors_js_1.InferenceClientProviderOutputError("ZAI text-to-image API task failed");
|
|
102
|
+
}
|
|
103
|
+
if (result.task_status === "SUCCESS") {
|
|
104
|
+
if (!result.image_result ||
|
|
105
|
+
!Array.isArray(result.image_result) ||
|
|
106
|
+
result.image_result.length === 0 ||
|
|
107
|
+
typeof result.image_result[0]?.url !== "string" ||
|
|
108
|
+
!(0, isUrl_js_1.isUrl)(result.image_result[0].url)) {
|
|
109
|
+
throw new errors_js_1.InferenceClientProviderOutputError(`Received malformed response from ZAI text-to-image API: expected { image_result: Array<{ url: string }> }, got: ${JSON.stringify(result)}`);
|
|
110
|
+
}
|
|
111
|
+
const imageUrl = result.image_result[0].url;
|
|
112
|
+
if (outputType === "json") {
|
|
113
|
+
return { ...result };
|
|
114
|
+
}
|
|
115
|
+
if (outputType === "url") {
|
|
116
|
+
return imageUrl;
|
|
117
|
+
}
|
|
118
|
+
const imageResponse = await fetch(imageUrl);
|
|
119
|
+
return await imageResponse.blob();
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
throw new errors_js_1.InferenceClientProviderOutputError(`Timed out while waiting for the result from ZAI API - aborting after ${MAX_POLL_ATTEMPTS} attempts`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
exports.ZaiTextToImageTask = ZaiTextToImageTask;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getProviderHelper.d.ts","sourceRoot":"","sources":["../../../src/lib/getProviderHelper.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EACX,6BAA6B,EAC7B,sBAAsB,EACtB,oCAAoC,EACpC,wBAAwB,EACxB,mCAAmC,EACnC,2BAA2B,EAC3B,kBAAkB,EAClB,6BAA6B,EAC7B,2BAA2B,EAC3B,sBAAsB,EACtB,qBAAqB,EACrB,sBAAsB,EACtB,0BAA0B,EAC1B,0BAA0B,EAC1B,yBAAyB,EACzB,2BAA2B,EAC3B,4BAA4B,EAC5B,uBAAuB,EACvB,gCAAgC,EAChC,+BAA+B,EAC/B,2BAA2B,EAC3B,kBAAkB,EAClB,4BAA4B,EAC5B,wBAAwB,EACxB,qBAAqB,EACrB,qBAAqB,EACrB,sBAAsB,EACtB,qBAAqB,EACrB,6BAA6B,EAC7B,qBAAqB,EACrB,iCAAiC,EACjC,gCAAgC,EAChC,qCAAqC,EACrC,MAAM,gCAAgC,CAAC;AAOxC,OAAO,KAAK,EAAE,iBAAiB,EAAE,yBAAyB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAG/F,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,iBAAiB,EAAE,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,kBAAkB,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"getProviderHelper.d.ts","sourceRoot":"","sources":["../../../src/lib/getProviderHelper.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EACX,6BAA6B,EAC7B,sBAAsB,EACtB,oCAAoC,EACpC,wBAAwB,EACxB,mCAAmC,EACnC,2BAA2B,EAC3B,kBAAkB,EAClB,6BAA6B,EAC7B,2BAA2B,EAC3B,sBAAsB,EACtB,qBAAqB,EACrB,sBAAsB,EACtB,0BAA0B,EA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AAgB,iBAAiB,CAChC,QAAQ,EAAE,yBAAyB,EACnC,IAAI,EAAE,sBAAsB,GAC1B,6BAA6B,GAAG,kBAAkB,CAAC;AACtD,wBAAgB,iBAAiB,CAChC,QAAQ,EAAE,yBAAyB,EACnC,IAAI,EAAE,aAAa,GACjB,qBAAqB,GAAG,kBAAkB,CAAC;AAC9C,wBAAgB,iBAAiB,CAChC,QAAQ,EAAE,yBAAyB,EACnC,IAAI,EAAE,eAAe,GACnB,uBAAuB,GAAG,kBAAkB,CAAC;AAChD,wBAAgB,iBAAiB,CAChC,QAAQ,EAAE,yBAAyB,EACnC,IAAI,EAAE,2BAA2B,GAC/B,iCAAiC,GAAG,kBAAkB,CAAC;AAC1D,wBAAgB,iBAAiB,CAChC,QAAQ,EAAE,yBAAyB,EACnC,IAAI,EAAE,aAAa,GAAG,SAAS,GAC7B,kBAAkB,CAAC"}
|
package/dist/esm/package.d.ts
CHANGED
package/dist/esm/package.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// Generated file from package.json. Issues importing JSON directly when publishing on commonjs/ESM - see https://github.com/microsoft/TypeScript/issues/51783
|
|
2
|
-
export const PACKAGE_VERSION = "4.13.
|
|
2
|
+
export const PACKAGE_VERSION = "4.13.6";
|
|
3
3
|
export const PACKAGE_NAME = "@huggingface/inference";
|
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* https://huggingface.co/api/partners/zai-org/models
|
|
5
|
-
*
|
|
6
|
-
* This is a publicly available mapping.
|
|
7
|
-
*
|
|
8
|
-
* If you want to try to run inference for a new model locally before it's registered on huggingface.co,
|
|
9
|
-
* you can add it to the dictionary "HARDCODED_MODEL_ID_MAPPING" in consts.ts, for dev purposes.
|
|
10
|
-
*
|
|
11
|
-
* - If you work at zai and want to update this mapping, please use the model mapping API we provide on huggingface.co
|
|
12
|
-
* - If you're a community member and want to add a new supported HF model to zai, please open an issue on the present repo
|
|
13
|
-
* and we will tag zai team members.
|
|
14
|
-
*
|
|
15
|
-
* Thanks!
|
|
16
|
-
*/
|
|
17
|
-
import { BaseConversationalTask } from "./providerHelper.js";
|
|
18
|
-
import type { HeaderParams } from "../types.js";
|
|
1
|
+
import type { BodyParams, HeaderParams } from "../types.js";
|
|
2
|
+
import { BaseConversationalTask, TaskProviderHelper, type TextToImageTaskHelper } from "./providerHelper.js";
|
|
19
3
|
export declare class ZaiConversationalTask extends BaseConversationalTask {
|
|
20
4
|
constructor();
|
|
21
5
|
prepareHeaders(params: HeaderParams, binary: boolean): Record<string, string>;
|
|
22
6
|
makeRoute(): string;
|
|
23
7
|
}
|
|
8
|
+
interface ZaiTextToImageResponse {
|
|
9
|
+
model: string;
|
|
10
|
+
id: string;
|
|
11
|
+
request_id: string;
|
|
12
|
+
task_status: "PROCESSING" | "SUCCESS" | "FAIL";
|
|
13
|
+
}
|
|
14
|
+
export declare class ZaiTextToImageTask extends TaskProviderHelper implements TextToImageTaskHelper {
|
|
15
|
+
constructor();
|
|
16
|
+
prepareHeaders(params: HeaderParams, binary: boolean): Record<string, string>;
|
|
17
|
+
makeRoute(): string;
|
|
18
|
+
preparePayload(params: BodyParams): Record<string, unknown>;
|
|
19
|
+
getResponse(response: ZaiTextToImageResponse, url?: string, headers?: Record<string, string>, outputType?: "url" | "blob" | "json"): Promise<string | Blob | Record<string, unknown>>;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
24
22
|
//# sourceMappingURL=zai-org.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"zai-org.d.ts","sourceRoot":"","sources":["../../../src/providers/zai-org.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"zai-org.d.ts","sourceRoot":"","sources":["../../../src/providers/zai-org.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAG5D,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,KAAK,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAI7G,qBAAa,qBAAsB,SAAQ,sBAAsB;;IAKvD,cAAc,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAO7E,SAAS,IAAI,MAAM;CAG5B;AAED,UAAU,sBAAsB;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,YAAY,GAAG,SAAS,GAAG,MAAM,CAAC;CAC/C;AAaD,qBAAa,kBAAmB,SAAQ,kBAAmB,YAAW,qBAAqB;;IAKjF,cAAc,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAYtF,SAAS,IAAI,MAAM;IAInB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IASrD,WAAW,CAChB,QAAQ,EAAE,sBAAsB,EAChC,GAAG,CAAC,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAChC,UAAU,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,GAClC,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAsFnD"}
|
|
@@ -14,7 +14,11 @@
|
|
|
14
14
|
*
|
|
15
15
|
* Thanks!
|
|
16
16
|
*/
|
|
17
|
-
import {
|
|
17
|
+
import { InferenceClientProviderApiError, InferenceClientProviderOutputError } from "../errors.js";
|
|
18
|
+
import { isUrl } from "../lib/isUrl.js";
|
|
19
|
+
import { delay } from "../utils/delay.js";
|
|
20
|
+
import { omit } from "../utils/omit.js";
|
|
21
|
+
import { BaseConversationalTask, TaskProviderHelper } from "./providerHelper.js";
|
|
18
22
|
const ZAI_API_BASE_URL = "https://api.z.ai";
|
|
19
23
|
export class ZaiConversationalTask extends BaseConversationalTask {
|
|
20
24
|
constructor() {
|
|
@@ -30,3 +34,87 @@ export class ZaiConversationalTask extends BaseConversationalTask {
|
|
|
30
34
|
return "/api/paas/v4/chat/completions";
|
|
31
35
|
}
|
|
32
36
|
}
|
|
37
|
+
const MAX_POLL_ATTEMPTS = 60;
|
|
38
|
+
const POLL_INTERVAL_MS = 5000;
|
|
39
|
+
export class ZaiTextToImageTask extends TaskProviderHelper {
|
|
40
|
+
constructor() {
|
|
41
|
+
super("zai-org", ZAI_API_BASE_URL);
|
|
42
|
+
}
|
|
43
|
+
prepareHeaders(params, binary) {
|
|
44
|
+
const headers = {
|
|
45
|
+
Authorization: `Bearer ${params.accessToken}`,
|
|
46
|
+
"x-source-channel": "hugging_face",
|
|
47
|
+
"accept-language": "en-US,en",
|
|
48
|
+
};
|
|
49
|
+
if (!binary) {
|
|
50
|
+
headers["Content-Type"] = "application/json";
|
|
51
|
+
}
|
|
52
|
+
return headers;
|
|
53
|
+
}
|
|
54
|
+
makeRoute() {
|
|
55
|
+
return "/api/paas/v4/async/images/generations";
|
|
56
|
+
}
|
|
57
|
+
preparePayload(params) {
|
|
58
|
+
return {
|
|
59
|
+
...omit(params.args, ["inputs", "parameters"]),
|
|
60
|
+
...params.args.parameters,
|
|
61
|
+
model: params.model,
|
|
62
|
+
prompt: params.args.inputs,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async getResponse(response, url, headers, outputType) {
|
|
66
|
+
void url;
|
|
67
|
+
if (typeof response !== "object" ||
|
|
68
|
+
!response ||
|
|
69
|
+
!("task_status" in response) ||
|
|
70
|
+
!("id" in response) ||
|
|
71
|
+
typeof response.id !== "string") {
|
|
72
|
+
throw new InferenceClientProviderOutputError(`Received malformed response from ZAI text-to-image API: expected { id: string, task_status: string }, got: ${JSON.stringify(response)}`);
|
|
73
|
+
}
|
|
74
|
+
if (response.task_status === "FAIL") {
|
|
75
|
+
throw new InferenceClientProviderOutputError("ZAI API returned task status: FAIL");
|
|
76
|
+
}
|
|
77
|
+
const taskId = response.id;
|
|
78
|
+
const pollUrl = `${ZAI_API_BASE_URL}/api/paas/v4/async-result/${taskId}`;
|
|
79
|
+
const pollHeaders = {
|
|
80
|
+
"x-source-channel": "hugging_face",
|
|
81
|
+
"accept-language": "en-US,en",
|
|
82
|
+
};
|
|
83
|
+
if (headers && headers["Authorization"]) {
|
|
84
|
+
pollHeaders["Authorization"] = headers["Authorization"];
|
|
85
|
+
}
|
|
86
|
+
for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt++) {
|
|
87
|
+
await delay(POLL_INTERVAL_MS);
|
|
88
|
+
const resp = await fetch(pollUrl, {
|
|
89
|
+
method: "GET",
|
|
90
|
+
headers: pollHeaders,
|
|
91
|
+
});
|
|
92
|
+
if (!resp.ok) {
|
|
93
|
+
throw new InferenceClientProviderApiError(`Failed to fetch result from ZAI text-to-image API: ${resp.status}`, { url: pollUrl, method: "GET" }, { requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: await resp.text() });
|
|
94
|
+
}
|
|
95
|
+
const result = await resp.json();
|
|
96
|
+
if (result.task_status === "FAIL") {
|
|
97
|
+
throw new InferenceClientProviderOutputError("ZAI text-to-image API task failed");
|
|
98
|
+
}
|
|
99
|
+
if (result.task_status === "SUCCESS") {
|
|
100
|
+
if (!result.image_result ||
|
|
101
|
+
!Array.isArray(result.image_result) ||
|
|
102
|
+
result.image_result.length === 0 ||
|
|
103
|
+
typeof result.image_result[0]?.url !== "string" ||
|
|
104
|
+
!isUrl(result.image_result[0].url)) {
|
|
105
|
+
throw new InferenceClientProviderOutputError(`Received malformed response from ZAI text-to-image API: expected { image_result: Array<{ url: string }> }, got: ${JSON.stringify(result)}`);
|
|
106
|
+
}
|
|
107
|
+
const imageUrl = result.image_result[0].url;
|
|
108
|
+
if (outputType === "json") {
|
|
109
|
+
return { ...result };
|
|
110
|
+
}
|
|
111
|
+
if (outputType === "url") {
|
|
112
|
+
return imageUrl;
|
|
113
|
+
}
|
|
114
|
+
const imageResponse = await fetch(imageUrl);
|
|
115
|
+
return await imageResponse.blob();
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
throw new InferenceClientProviderOutputError(`Timed out while waiting for the result from ZAI API - aborting after ${MAX_POLL_ATTEMPTS} attempts`);
|
|
119
|
+
}
|
|
120
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huggingface/inference",
|
|
3
|
-
"version": "4.13.
|
|
3
|
+
"version": "4.13.6",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Hugging Face and Tim Mikeladze <tim.mikeladze@gmail.com>",
|
|
6
6
|
"description": "Typescript client for the Hugging Face Inference Providers and Inference Endpoints",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"type": "module",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@huggingface/tasks": "^0.19.
|
|
43
|
+
"@huggingface/tasks": "^0.19.76",
|
|
44
44
|
"@huggingface/jinja": "^0.5.3"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
package/src/package.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
// Generated file from package.json. Issues importing JSON directly when publishing on commonjs/ESM - see https://github.com/microsoft/TypeScript/issues/51783
|
|
2
|
-
export const PACKAGE_VERSION = "4.13.
|
|
2
|
+
export const PACKAGE_VERSION = "4.13.6";
|
|
3
3
|
export const PACKAGE_NAME = "@huggingface/inference";
|
package/src/providers/zai-org.ts
CHANGED
|
@@ -14,8 +14,12 @@
|
|
|
14
14
|
*
|
|
15
15
|
* Thanks!
|
|
16
16
|
*/
|
|
17
|
-
import {
|
|
18
|
-
import
|
|
17
|
+
import { InferenceClientProviderApiError, InferenceClientProviderOutputError } from "../errors.js";
|
|
18
|
+
import { isUrl } from "../lib/isUrl.js";
|
|
19
|
+
import type { BodyParams, HeaderParams } from "../types.js";
|
|
20
|
+
import { delay } from "../utils/delay.js";
|
|
21
|
+
import { omit } from "../utils/omit.js";
|
|
22
|
+
import { BaseConversationalTask, TaskProviderHelper, type TextToImageTaskHelper } from "./providerHelper.js";
|
|
19
23
|
|
|
20
24
|
const ZAI_API_BASE_URL = "https://api.z.ai";
|
|
21
25
|
|
|
@@ -35,3 +39,144 @@ export class ZaiConversationalTask extends BaseConversationalTask {
|
|
|
35
39
|
return "/api/paas/v4/chat/completions";
|
|
36
40
|
}
|
|
37
41
|
}
|
|
42
|
+
|
|
43
|
+
interface ZaiTextToImageResponse {
|
|
44
|
+
model: string;
|
|
45
|
+
id: string;
|
|
46
|
+
request_id: string;
|
|
47
|
+
task_status: "PROCESSING" | "SUCCESS" | "FAIL";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface ZaiAsyncResultResponse {
|
|
51
|
+
image_result?: Array<{ url: string }>;
|
|
52
|
+
model: string;
|
|
53
|
+
id: string;
|
|
54
|
+
request_id: string;
|
|
55
|
+
task_status: "PROCESSING" | "SUCCESS" | "FAIL";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const MAX_POLL_ATTEMPTS = 60;
|
|
59
|
+
const POLL_INTERVAL_MS = 5000;
|
|
60
|
+
|
|
61
|
+
export class ZaiTextToImageTask extends TaskProviderHelper implements TextToImageTaskHelper {
|
|
62
|
+
constructor() {
|
|
63
|
+
super("zai-org", ZAI_API_BASE_URL);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
override prepareHeaders(params: HeaderParams, binary: boolean): Record<string, string> {
|
|
67
|
+
const headers: Record<string, string> = {
|
|
68
|
+
Authorization: `Bearer ${params.accessToken}`,
|
|
69
|
+
"x-source-channel": "hugging_face",
|
|
70
|
+
"accept-language": "en-US,en",
|
|
71
|
+
};
|
|
72
|
+
if (!binary) {
|
|
73
|
+
headers["Content-Type"] = "application/json";
|
|
74
|
+
}
|
|
75
|
+
return headers;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
makeRoute(): string {
|
|
79
|
+
return "/api/paas/v4/async/images/generations";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
preparePayload(params: BodyParams): Record<string, unknown> {
|
|
83
|
+
return {
|
|
84
|
+
...omit(params.args, ["inputs", "parameters"]),
|
|
85
|
+
...(params.args.parameters as Record<string, unknown>),
|
|
86
|
+
model: params.model,
|
|
87
|
+
prompt: params.args.inputs,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async getResponse(
|
|
92
|
+
response: ZaiTextToImageResponse,
|
|
93
|
+
url?: string,
|
|
94
|
+
headers?: Record<string, string>,
|
|
95
|
+
outputType?: "url" | "blob" | "json"
|
|
96
|
+
): Promise<string | Blob | Record<string, unknown>> {
|
|
97
|
+
void url;
|
|
98
|
+
if (
|
|
99
|
+
typeof response !== "object" ||
|
|
100
|
+
!response ||
|
|
101
|
+
!("task_status" in response) ||
|
|
102
|
+
!("id" in response) ||
|
|
103
|
+
typeof response.id !== "string"
|
|
104
|
+
) {
|
|
105
|
+
throw new InferenceClientProviderOutputError(
|
|
106
|
+
`Received malformed response from ZAI text-to-image API: expected { id: string, task_status: string }, got: ${JSON.stringify(
|
|
107
|
+
response
|
|
108
|
+
)}`
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (response.task_status === "FAIL") {
|
|
113
|
+
throw new InferenceClientProviderOutputError("ZAI API returned task status: FAIL");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const taskId = response.id;
|
|
117
|
+
const pollUrl = `${ZAI_API_BASE_URL}/api/paas/v4/async-result/${taskId}`;
|
|
118
|
+
|
|
119
|
+
const pollHeaders: Record<string, string> = {
|
|
120
|
+
"x-source-channel": "hugging_face",
|
|
121
|
+
"accept-language": "en-US,en",
|
|
122
|
+
};
|
|
123
|
+
if (headers && headers["Authorization"]) {
|
|
124
|
+
pollHeaders["Authorization"] = headers["Authorization"];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
for (let attempt = 0; attempt < MAX_POLL_ATTEMPTS; attempt++) {
|
|
128
|
+
await delay(POLL_INTERVAL_MS);
|
|
129
|
+
|
|
130
|
+
const resp = await fetch(pollUrl, {
|
|
131
|
+
method: "GET",
|
|
132
|
+
headers: pollHeaders,
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
if (!resp.ok) {
|
|
136
|
+
throw new InferenceClientProviderApiError(
|
|
137
|
+
`Failed to fetch result from ZAI text-to-image API: ${resp.status}`,
|
|
138
|
+
{ url: pollUrl, method: "GET" },
|
|
139
|
+
{ requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: await resp.text() }
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const result: ZaiAsyncResultResponse = await resp.json();
|
|
144
|
+
|
|
145
|
+
if (result.task_status === "FAIL") {
|
|
146
|
+
throw new InferenceClientProviderOutputError("ZAI text-to-image API task failed");
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (result.task_status === "SUCCESS") {
|
|
150
|
+
if (
|
|
151
|
+
!result.image_result ||
|
|
152
|
+
!Array.isArray(result.image_result) ||
|
|
153
|
+
result.image_result.length === 0 ||
|
|
154
|
+
typeof result.image_result[0]?.url !== "string" ||
|
|
155
|
+
!isUrl(result.image_result[0].url)
|
|
156
|
+
) {
|
|
157
|
+
throw new InferenceClientProviderOutputError(
|
|
158
|
+
`Received malformed response from ZAI text-to-image API: expected { image_result: Array<{ url: string }> }, got: ${JSON.stringify(
|
|
159
|
+
result
|
|
160
|
+
)}`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const imageUrl = result.image_result[0].url;
|
|
165
|
+
|
|
166
|
+
if (outputType === "json") {
|
|
167
|
+
return { ...result };
|
|
168
|
+
}
|
|
169
|
+
if (outputType === "url") {
|
|
170
|
+
return imageUrl;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const imageResponse = await fetch(imageUrl);
|
|
174
|
+
return await imageResponse.blob();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
throw new InferenceClientProviderOutputError(
|
|
179
|
+
`Timed out while waiting for the result from ZAI API - aborting after ${MAX_POLL_ATTEMPTS} attempts`
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
}
|