@huggingface/inference 4.5.2 → 4.6.0
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/package.d.ts +1 -1
- package/dist/commonjs/package.js +1 -1
- package/dist/commonjs/providers/replicate.d.ts.map +1 -1
- package/dist/commonjs/providers/replicate.js +3 -0
- package/dist/commonjs/snippets/getInferenceSnippets.d.ts.map +1 -1
- package/dist/commonjs/snippets/getInferenceSnippets.js +36 -6
- package/dist/commonjs/snippets/templates.exported.d.ts.map +1 -1
- package/dist/commonjs/snippets/templates.exported.js +5 -4
- package/dist/esm/package.d.ts +1 -1
- package/dist/esm/package.js +1 -1
- package/dist/esm/providers/replicate.d.ts.map +1 -1
- package/dist/esm/providers/replicate.js +3 -0
- package/dist/esm/snippets/getInferenceSnippets.d.ts.map +1 -1
- package/dist/esm/snippets/getInferenceSnippets.js +36 -6
- package/dist/esm/snippets/templates.exported.d.ts.map +1 -1
- package/dist/esm/snippets/templates.exported.js +5 -4
- package/package.json +3 -3
- package/src/package.ts +1 -1
- package/src/providers/replicate.ts +4 -0
- package/src/snippets/getInferenceSnippets.ts +42 -6
- package/src/snippets/templates.exported.ts +5 -4
package/dist/commonjs/package.js
CHANGED
|
@@ -2,5 +2,5 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PACKAGE_NAME = 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.
|
|
5
|
+
exports.PACKAGE_VERSION = "4.6.0";
|
|
6
6
|
exports.PACKAGE_NAME = "@huggingface/inference";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replicate.d.ts","sourceRoot":"","sources":["../../../src/providers/replicate.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,UAAU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E,OAAO,CAAC;IAerD,WAAW,CACzB,GAAG,EAAE,eAAe,GAAG,IAAI,EAC3B,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;CAsBnD;AAED,qBAAa,yBAA0B,SAAQ,aAAa;IAClD,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAarD,WAAW,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC;CAiBpE;AAED,qBAAa,wBAAyB,SAAQ,aAAc,YAAW,qBAAqB;IAC5E,WAAW,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC;CAcpE;AAED,qBAAa,yBAA0B,SAAQ,aAAc,YAAW,sBAAsB;IACpF,cAAc,CAAC,MAAM,EAAE,UAAU,CAAC,gBAAgB,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAehF,mBAAmB,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,WAAW,CAAC;IAcxD,WAAW,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC;CA0BpE"}
|
|
@@ -144,6 +144,9 @@ class ReplicateImageToImageTask extends ReplicateTask {
|
|
|
144
144
|
...(0, omit_js_1.omit)(params.args, ["inputs", "parameters"]),
|
|
145
145
|
...params.args.parameters,
|
|
146
146
|
input_image: params.args.inputs, // This will be processed in preparePayloadAsync
|
|
147
|
+
lora_weights: params.mapping?.adapter === "lora" && params.mapping.adapterWeightsPath
|
|
148
|
+
? `https://huggingface.co/${params.mapping.hfModelId}`
|
|
149
|
+
: undefined,
|
|
147
150
|
},
|
|
148
151
|
version: params.model.includes(":") ? params.model.split(":")[1] : undefined,
|
|
149
152
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAK5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAKxH,MAAM,MAAM,uBAAuB,GAAG;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAK5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAKxH,MAAM,MAAM,uBAAuB,GAAG;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAkY5B,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,yBAAyB,EACnC,wBAAwB,CAAC,EAAE,6BAA6B,EACxD,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CAIpB"}
|
|
@@ -16,9 +16,11 @@ const CLIENTS = {
|
|
|
16
16
|
python: [...PYTHON_CLIENTS],
|
|
17
17
|
sh: [...SH_CLIENTS],
|
|
18
18
|
};
|
|
19
|
-
|
|
19
|
+
// The "auto"-provider policy is only available through the HF SDKs (huggingface.js / huggingface_hub)
|
|
20
|
+
// except for conversational tasks for which we have https://router.huggingface.co/v1/chat/completions
|
|
21
|
+
const CLIENTS_NON_CONVERSATIONAL_AUTO_POLICY = {
|
|
20
22
|
js: ["huggingface.js"],
|
|
21
|
-
python: ["huggingface_hub"
|
|
23
|
+
python: ["huggingface_hub"],
|
|
22
24
|
};
|
|
23
25
|
// Helpers to find + load templates
|
|
24
26
|
const hasTemplate = (language, client, templateName) => templates_exported_js_1.templates[language]?.[client]?.[templateName] !== undefined;
|
|
@@ -134,12 +136,29 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
134
136
|
logger.error("Failed to parse body as JSON", e);
|
|
135
137
|
}
|
|
136
138
|
}
|
|
139
|
+
// Inputs for the "auto" route is strictly the same as "inputs", except the model includes the provider
|
|
140
|
+
// If not "auto" route, use the providerInputs
|
|
141
|
+
const autoInputs = !opts?.endpointUrl && !opts?.directRequest
|
|
142
|
+
? provider !== "auto"
|
|
143
|
+
? {
|
|
144
|
+
...inputs,
|
|
145
|
+
model: `${model.id}:${provider}`,
|
|
146
|
+
}
|
|
147
|
+
: {
|
|
148
|
+
...inputs,
|
|
149
|
+
model: `${model.id}`, // if no :provider => auto
|
|
150
|
+
}
|
|
151
|
+
: providerInputs;
|
|
137
152
|
/// Prepare template injection data
|
|
138
153
|
const params = {
|
|
139
154
|
accessToken: accessTokenOrPlaceholder,
|
|
140
155
|
authorizationHeader: request.info.headers?.Authorization,
|
|
141
|
-
baseUrl:
|
|
142
|
-
|
|
156
|
+
baseUrl: task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
157
|
+
? config_js_1.HF_ROUTER_AUTO_ENDPOINT
|
|
158
|
+
: removeSuffix(request.url, "/chat/completions"),
|
|
159
|
+
fullUrl: task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
160
|
+
? config_js_1.HF_ROUTER_AUTO_ENDPOINT + "/chat/completions"
|
|
161
|
+
: request.url,
|
|
143
162
|
inputs: {
|
|
144
163
|
asObj: inputs,
|
|
145
164
|
asCurlString: formatBody(inputs, "curl"),
|
|
@@ -154,14 +173,25 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
154
173
|
asPythonString: formatBody(providerInputs, "python"),
|
|
155
174
|
asTsString: formatBody(providerInputs, "ts"),
|
|
156
175
|
},
|
|
176
|
+
autoInputs: {
|
|
177
|
+
asObj: autoInputs,
|
|
178
|
+
asCurlString: formatBody(autoInputs, "curl"),
|
|
179
|
+
asJsonString: formatBody(autoInputs, "json"),
|
|
180
|
+
asPythonString: formatBody(autoInputs, "python"),
|
|
181
|
+
asTsString: formatBody(autoInputs, "ts"),
|
|
182
|
+
},
|
|
157
183
|
model,
|
|
158
184
|
provider,
|
|
159
|
-
providerModelId:
|
|
185
|
+
providerModelId: task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
186
|
+
? provider !== "auto"
|
|
187
|
+
? `${model.id}:${provider}` // e.g. "moonshotai/Kimi-K2-Instruct:groq"
|
|
188
|
+
: model.id
|
|
189
|
+
: providerModelId ?? model.id,
|
|
160
190
|
billTo: opts?.billTo,
|
|
161
191
|
endpointUrl: opts?.endpointUrl,
|
|
162
192
|
};
|
|
163
193
|
/// Iterate over clients => check if a snippet exists => generate
|
|
164
|
-
const clients = provider === "auto" ?
|
|
194
|
+
const clients = provider === "auto" && task !== "conversational" ? CLIENTS_NON_CONVERSATIONAL_AUTO_POLICY : CLIENTS;
|
|
165
195
|
return tasks_1.inferenceSnippetLanguages
|
|
166
196
|
.map((language) => {
|
|
167
197
|
const langClients = clients[language] ?? [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"templates.exported.d.ts","sourceRoot":"","sources":["../../../src/snippets/templates.exported.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"templates.exported.d.ts","sourceRoot":"","sources":["../../../src/snippets/templates.exported.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAgFnE,CAAC"}
|
|
@@ -8,6 +8,7 @@ exports.templates = {
|
|
|
8
8
|
"basic": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
9
9
|
"basicAudio": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"audio/flac\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
10
10
|
"basicImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
11
|
+
"conversational": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ \n{{ autoInputs.asTsString }}\n}).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
11
12
|
"imageToImage": "const image = fs.readFileSync(\"{{inputs.asObj.inputs}}\");\n\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: {\n\t\t\t\t\"inputs\": `data:image/png;base64,${data.inputs.encode(\"base64\")}`,\n\t\t\t\t\"parameters\": data.parameters,\n\t\t\t}\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ \n\tinputs: image,\n\tparameters: {\n\t\tprompt: \"{{ inputs.asObj.parameters.prompt }}\",\n\t}\n}).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
12
13
|
"textToAudio": "{% if model.library_name == \"transformers\" %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n // Returns a byte object of the Audio wavform. Use it directly!\n});\n{% else %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});\n{% endif %} ",
|
|
13
14
|
"textToImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n\treturn result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});",
|
|
@@ -58,8 +59,8 @@ exports.templates = {
|
|
|
58
59
|
"basic": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {{ providerInputs.asObj.inputs }},\n}) ",
|
|
59
60
|
"basicAudio": "def query(filename):\n with open(filename, \"rb\") as f:\n data = f.read()\n response = requests.post(API_URL, headers={\"Content-Type\": \"audio/flac\", **headers}, data=data)\n return response.json()\n\noutput = query({{ providerInputs.asObj.inputs }})",
|
|
60
61
|
"basicImage": "def query(filename):\n with open(filename, \"rb\") as f:\n data = f.read()\n response = requests.post(API_URL, headers={\"Content-Type\": \"image/jpeg\", **headers}, data=data)\n return response.json()\n\noutput = query({{ providerInputs.asObj.inputs }})",
|
|
61
|
-
"conversational": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\nresponse = query({\n{{
|
|
62
|
-
"conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{
|
|
62
|
+
"conversational": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\nresponse = query({\n{{ autoInputs.asJsonString }}\n})\n\nprint(response[\"choices\"][0][\"message\"])",
|
|
63
|
+
"conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{ autoInputs.asJsonString }},\n \"stream\": True,\n})\n\nfor chunk in chunks:\n print(chunk[\"choices\"][0][\"delta\"][\"content\"], end=\"\")",
|
|
63
64
|
"documentQuestionAnswering": "def query(payload):\n with open(payload[\"image\"], \"rb\") as f:\n img = f.read()\n payload[\"image\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {\n \"image\": \"{{ inputs.asObj.image }}\",\n \"question\": \"{{ inputs.asObj.question }}\",\n },\n}) ",
|
|
64
65
|
"imageToImage": "with open(\"{{inputs.asObj.inputs}}\", \"rb\") as image_file:\n image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')\n\ndef query(payload):\n with open(payload[\"inputs\"], \"rb\") as f:\n img = f.read()\n payload[\"inputs\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nimage_bytes = query({\n{{ providerInputs.asJsonString }}\n})\n\n# You can access the image with PIL.Image for example\nimport io\nfrom PIL import Image\nimage = Image.open(io.BytesIO(image_bytes)) ",
|
|
65
66
|
"importRequests": "{% if importBase64 %}\nimport base64\n{% endif %}\n{% if importJson %}\nimport json\n{% endif %}\nimport requests\n\nAPI_URL = \"{{ fullUrl }}\"\nheaders = {\n \"Authorization\": \"{{ authorizationHeader }}\",\n{% if billTo %}\n \"X-HF-Bill-To\": \"{{ billTo }}\"\n{% endif %}\n}",
|
|
@@ -76,8 +77,8 @@ exports.templates = {
|
|
|
76
77
|
"basic": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }}\n }'",
|
|
77
78
|
"basicAudio": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: audio/flac' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
|
|
78
79
|
"basicImage": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: image/jpeg' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
|
|
79
|
-
"conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{
|
|
80
|
-
"conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{
|
|
80
|
+
"conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ autoInputs.asCurlString }},\n \"stream\": false\n }'",
|
|
81
|
+
"conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ autoInputs.asCurlString }},\n \"stream\": true\n }'",
|
|
81
82
|
"zeroShotClassification": "curl {{ fullUrl }} \\\n -X POST \\\n -d '{\"inputs\": {{ providerInputs.asObj.inputs }}, \"parameters\": {\"candidate_labels\": [\"refund\", \"legal\", \"faq\"]}}' \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: {{ authorizationHeader }}'\n{% if billTo %} \\\n -H 'X-HF-Bill-To: {{ billTo }}'\n{% endif %}"
|
|
82
83
|
}
|
|
83
84
|
}
|
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.
|
|
2
|
+
export const PACKAGE_VERSION = "4.6.0";
|
|
3
3
|
export const PACKAGE_NAME = "@huggingface/inference";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"replicate.d.ts","sourceRoot":"","sources":["../../../src/providers/replicate.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAEpF,OAAO,EACN,kBAAkB,EAClB,KAAK,sBAAsB,EAC3B,KAAK,qBAAqB,EAC1B,KAAK,qBAAqB,EAC1B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEpE,MAAM,WAAW,eAAe;IAC/B,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CAC3B;AAED,uBAAe,aAAc,SAAQ,kBAAkB;gBAC1C,GAAG,CAAC,EAAE,MAAM;IAIxB,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM;IAMpC,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAUlD,cAAc,CAAC,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAQ7E,OAAO,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM;CAO3C;AAED,qBAAa,wBAAy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,gBAAgB,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAehF,mBAAmB,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,WAAW,CAAC;IAcxD,WAAW,CAAC,QAAQ,EAAE,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC;CA0BpE"}
|
|
@@ -138,6 +138,9 @@ export class ReplicateImageToImageTask extends ReplicateTask {
|
|
|
138
138
|
...omit(params.args, ["inputs", "parameters"]),
|
|
139
139
|
...params.args.parameters,
|
|
140
140
|
input_image: params.args.inputs, // This will be processed in preparePayloadAsync
|
|
141
|
+
lora_weights: params.mapping?.adapter === "lora" && params.mapping.adapterWeightsPath
|
|
142
|
+
? `https://huggingface.co/${params.mapping.hfModelId}`
|
|
143
|
+
: undefined,
|
|
141
144
|
},
|
|
142
145
|
version: params.model.includes(":") ? params.model.split(":")[1] : undefined,
|
|
143
146
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAK5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAKxH,MAAM,MAAM,uBAAuB,GAAG;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAK5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAKxH,MAAM,MAAM,uBAAuB,GAAG;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACjC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAkY5B,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,yBAAyB,EACnC,wBAAwB,CAAC,EAAE,6BAA6B,EACxD,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CAIpB"}
|
|
@@ -13,9 +13,11 @@ const CLIENTS = {
|
|
|
13
13
|
python: [...PYTHON_CLIENTS],
|
|
14
14
|
sh: [...SH_CLIENTS],
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
// The "auto"-provider policy is only available through the HF SDKs (huggingface.js / huggingface_hub)
|
|
17
|
+
// except for conversational tasks for which we have https://router.huggingface.co/v1/chat/completions
|
|
18
|
+
const CLIENTS_NON_CONVERSATIONAL_AUTO_POLICY = {
|
|
17
19
|
js: ["huggingface.js"],
|
|
18
|
-
python: ["huggingface_hub"
|
|
20
|
+
python: ["huggingface_hub"],
|
|
19
21
|
};
|
|
20
22
|
// Helpers to find + load templates
|
|
21
23
|
const hasTemplate = (language, client, templateName) => templates[language]?.[client]?.[templateName] !== undefined;
|
|
@@ -131,12 +133,29 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
131
133
|
logger.error("Failed to parse body as JSON", e);
|
|
132
134
|
}
|
|
133
135
|
}
|
|
136
|
+
// Inputs for the "auto" route is strictly the same as "inputs", except the model includes the provider
|
|
137
|
+
// If not "auto" route, use the providerInputs
|
|
138
|
+
const autoInputs = !opts?.endpointUrl && !opts?.directRequest
|
|
139
|
+
? provider !== "auto"
|
|
140
|
+
? {
|
|
141
|
+
...inputs,
|
|
142
|
+
model: `${model.id}:${provider}`,
|
|
143
|
+
}
|
|
144
|
+
: {
|
|
145
|
+
...inputs,
|
|
146
|
+
model: `${model.id}`, // if no :provider => auto
|
|
147
|
+
}
|
|
148
|
+
: providerInputs;
|
|
134
149
|
/// Prepare template injection data
|
|
135
150
|
const params = {
|
|
136
151
|
accessToken: accessTokenOrPlaceholder,
|
|
137
152
|
authorizationHeader: request.info.headers?.Authorization,
|
|
138
|
-
baseUrl:
|
|
139
|
-
|
|
153
|
+
baseUrl: task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
154
|
+
? HF_ROUTER_AUTO_ENDPOINT
|
|
155
|
+
: removeSuffix(request.url, "/chat/completions"),
|
|
156
|
+
fullUrl: task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
157
|
+
? HF_ROUTER_AUTO_ENDPOINT + "/chat/completions"
|
|
158
|
+
: request.url,
|
|
140
159
|
inputs: {
|
|
141
160
|
asObj: inputs,
|
|
142
161
|
asCurlString: formatBody(inputs, "curl"),
|
|
@@ -151,14 +170,25 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
|
|
|
151
170
|
asPythonString: formatBody(providerInputs, "python"),
|
|
152
171
|
asTsString: formatBody(providerInputs, "ts"),
|
|
153
172
|
},
|
|
173
|
+
autoInputs: {
|
|
174
|
+
asObj: autoInputs,
|
|
175
|
+
asCurlString: formatBody(autoInputs, "curl"),
|
|
176
|
+
asJsonString: formatBody(autoInputs, "json"),
|
|
177
|
+
asPythonString: formatBody(autoInputs, "python"),
|
|
178
|
+
asTsString: formatBody(autoInputs, "ts"),
|
|
179
|
+
},
|
|
154
180
|
model,
|
|
155
181
|
provider,
|
|
156
|
-
providerModelId:
|
|
182
|
+
providerModelId: task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
183
|
+
? provider !== "auto"
|
|
184
|
+
? `${model.id}:${provider}` // e.g. "moonshotai/Kimi-K2-Instruct:groq"
|
|
185
|
+
: model.id
|
|
186
|
+
: providerModelId ?? model.id,
|
|
157
187
|
billTo: opts?.billTo,
|
|
158
188
|
endpointUrl: opts?.endpointUrl,
|
|
159
189
|
};
|
|
160
190
|
/// Iterate over clients => check if a snippet exists => generate
|
|
161
|
-
const clients = provider === "auto" ?
|
|
191
|
+
const clients = provider === "auto" && task !== "conversational" ? CLIENTS_NON_CONVERSATIONAL_AUTO_POLICY : CLIENTS;
|
|
162
192
|
return inferenceSnippetLanguages
|
|
163
193
|
.map((language) => {
|
|
164
194
|
const langClients = clients[language] ?? [];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"templates.exported.d.ts","sourceRoot":"","sources":["../../../src/snippets/templates.exported.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"templates.exported.d.ts","sourceRoot":"","sources":["../../../src/snippets/templates.exported.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAgFnE,CAAC"}
|
|
@@ -5,6 +5,7 @@ export const templates = {
|
|
|
5
5
|
"basic": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
6
6
|
"basicAudio": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"audio/flac\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
7
7
|
"basicImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
8
|
+
"conversational": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ \n{{ autoInputs.asTsString }}\n}).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
8
9
|
"imageToImage": "const image = fs.readFileSync(\"{{inputs.asObj.inputs}}\");\n\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: {\n\t\t\t\t\"inputs\": `data:image/png;base64,${data.inputs.encode(\"base64\")}`,\n\t\t\t\t\"parameters\": data.parameters,\n\t\t\t}\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ \n\tinputs: image,\n\tparameters: {\n\t\tprompt: \"{{ inputs.asObj.parameters.prompt }}\",\n\t}\n}).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
9
10
|
"textToAudio": "{% if model.library_name == \"transformers\" %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n // Returns a byte object of the Audio wavform. Use it directly!\n});\n{% else %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});\n{% endif %} ",
|
|
10
11
|
"textToImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n\treturn result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});",
|
|
@@ -55,8 +56,8 @@ export const templates = {
|
|
|
55
56
|
"basic": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {{ providerInputs.asObj.inputs }},\n}) ",
|
|
56
57
|
"basicAudio": "def query(filename):\n with open(filename, \"rb\") as f:\n data = f.read()\n response = requests.post(API_URL, headers={\"Content-Type\": \"audio/flac\", **headers}, data=data)\n return response.json()\n\noutput = query({{ providerInputs.asObj.inputs }})",
|
|
57
58
|
"basicImage": "def query(filename):\n with open(filename, \"rb\") as f:\n data = f.read()\n response = requests.post(API_URL, headers={\"Content-Type\": \"image/jpeg\", **headers}, data=data)\n return response.json()\n\noutput = query({{ providerInputs.asObj.inputs }})",
|
|
58
|
-
"conversational": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\nresponse = query({\n{{
|
|
59
|
-
"conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{
|
|
59
|
+
"conversational": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\nresponse = query({\n{{ autoInputs.asJsonString }}\n})\n\nprint(response[\"choices\"][0][\"message\"])",
|
|
60
|
+
"conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{ autoInputs.asJsonString }},\n \"stream\": True,\n})\n\nfor chunk in chunks:\n print(chunk[\"choices\"][0][\"delta\"][\"content\"], end=\"\")",
|
|
60
61
|
"documentQuestionAnswering": "def query(payload):\n with open(payload[\"image\"], \"rb\") as f:\n img = f.read()\n payload[\"image\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {\n \"image\": \"{{ inputs.asObj.image }}\",\n \"question\": \"{{ inputs.asObj.question }}\",\n },\n}) ",
|
|
61
62
|
"imageToImage": "with open(\"{{inputs.asObj.inputs}}\", \"rb\") as image_file:\n image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')\n\ndef query(payload):\n with open(payload[\"inputs\"], \"rb\") as f:\n img = f.read()\n payload[\"inputs\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nimage_bytes = query({\n{{ providerInputs.asJsonString }}\n})\n\n# You can access the image with PIL.Image for example\nimport io\nfrom PIL import Image\nimage = Image.open(io.BytesIO(image_bytes)) ",
|
|
62
63
|
"importRequests": "{% if importBase64 %}\nimport base64\n{% endif %}\n{% if importJson %}\nimport json\n{% endif %}\nimport requests\n\nAPI_URL = \"{{ fullUrl }}\"\nheaders = {\n \"Authorization\": \"{{ authorizationHeader }}\",\n{% if billTo %}\n \"X-HF-Bill-To\": \"{{ billTo }}\"\n{% endif %}\n}",
|
|
@@ -73,8 +74,8 @@ export const templates = {
|
|
|
73
74
|
"basic": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }}\n }'",
|
|
74
75
|
"basicAudio": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: audio/flac' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
|
|
75
76
|
"basicImage": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: image/jpeg' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
|
|
76
|
-
"conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{
|
|
77
|
-
"conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{
|
|
77
|
+
"conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ autoInputs.asCurlString }},\n \"stream\": false\n }'",
|
|
78
|
+
"conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ autoInputs.asCurlString }},\n \"stream\": true\n }'",
|
|
78
79
|
"zeroShotClassification": "curl {{ fullUrl }} \\\n -X POST \\\n -d '{\"inputs\": {{ providerInputs.asObj.inputs }}, \"parameters\": {\"candidate_labels\": [\"refund\", \"legal\", \"faq\"]}}' \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: {{ authorizationHeader }}'\n{% if billTo %} \\\n -H 'X-HF-Bill-To: {{ billTo }}'\n{% endif %}"
|
|
79
80
|
}
|
|
80
81
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huggingface/inference",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
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,8 +40,8 @@
|
|
|
40
40
|
},
|
|
41
41
|
"type": "module",
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@huggingface/
|
|
44
|
-
"@huggingface/
|
|
43
|
+
"@huggingface/jinja": "^0.5.1",
|
|
44
|
+
"@huggingface/tasks": "^0.19.32"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@types/node": "18.13.0"
|
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.
|
|
2
|
+
export const PACKAGE_VERSION = "4.6.0";
|
|
3
3
|
export const PACKAGE_NAME = "@huggingface/inference";
|
|
@@ -170,6 +170,10 @@ export class ReplicateImageToImageTask extends ReplicateTask implements ImageToI
|
|
|
170
170
|
...omit(params.args, ["inputs", "parameters"]),
|
|
171
171
|
...params.args.parameters,
|
|
172
172
|
input_image: params.args.inputs, // This will be processed in preparePayloadAsync
|
|
173
|
+
lora_weights:
|
|
174
|
+
params.mapping?.adapter === "lora" && params.mapping.adapterWeightsPath
|
|
175
|
+
? `https://huggingface.co/${params.mapping.hfModelId}`
|
|
176
|
+
: undefined,
|
|
173
177
|
},
|
|
174
178
|
version: params.model.includes(":") ? params.model.split(":")[1] : undefined,
|
|
175
179
|
};
|
|
@@ -36,9 +36,11 @@ const CLIENTS: Record<InferenceSnippetLanguage, Client[]> = {
|
|
|
36
36
|
sh: [...SH_CLIENTS],
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
// The "auto"-provider policy is only available through the HF SDKs (huggingface.js / huggingface_hub)
|
|
40
|
+
// except for conversational tasks for which we have https://router.huggingface.co/v1/chat/completions
|
|
41
|
+
const CLIENTS_NON_CONVERSATIONAL_AUTO_POLICY: Partial<Record<InferenceSnippetLanguage, Client[]>> = {
|
|
40
42
|
js: ["huggingface.js"],
|
|
41
|
-
python: ["huggingface_hub"
|
|
43
|
+
python: ["huggingface_hub"],
|
|
42
44
|
};
|
|
43
45
|
|
|
44
46
|
type InputPreparationFn = (model: ModelDataMinimal, opts?: Record<string, unknown>) => object;
|
|
@@ -49,6 +51,7 @@ interface TemplateParams {
|
|
|
49
51
|
fullUrl?: string;
|
|
50
52
|
inputs?: object;
|
|
51
53
|
providerInputs?: object;
|
|
54
|
+
autoInputs?: object;
|
|
52
55
|
model?: ModelDataMinimal;
|
|
53
56
|
provider?: InferenceProviderOrPolicy;
|
|
54
57
|
providerModelId?: string;
|
|
@@ -202,12 +205,33 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
|
|
|
202
205
|
}
|
|
203
206
|
}
|
|
204
207
|
|
|
208
|
+
// Inputs for the "auto" route is strictly the same as "inputs", except the model includes the provider
|
|
209
|
+
// If not "auto" route, use the providerInputs
|
|
210
|
+
const autoInputs =
|
|
211
|
+
!opts?.endpointUrl && !opts?.directRequest
|
|
212
|
+
? provider !== "auto"
|
|
213
|
+
? {
|
|
214
|
+
...inputs,
|
|
215
|
+
model: `${model.id}:${provider}`,
|
|
216
|
+
}
|
|
217
|
+
: {
|
|
218
|
+
...inputs,
|
|
219
|
+
model: `${model.id}`, // if no :provider => auto
|
|
220
|
+
}
|
|
221
|
+
: providerInputs;
|
|
222
|
+
|
|
205
223
|
/// Prepare template injection data
|
|
206
224
|
const params: TemplateParams = {
|
|
207
225
|
accessToken: accessTokenOrPlaceholder,
|
|
208
226
|
authorizationHeader: (request.info.headers as Record<string, string>)?.Authorization,
|
|
209
|
-
baseUrl:
|
|
210
|
-
|
|
227
|
+
baseUrl:
|
|
228
|
+
task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
229
|
+
? HF_ROUTER_AUTO_ENDPOINT
|
|
230
|
+
: removeSuffix(request.url, "/chat/completions"),
|
|
231
|
+
fullUrl:
|
|
232
|
+
task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
233
|
+
? HF_ROUTER_AUTO_ENDPOINT + "/chat/completions"
|
|
234
|
+
: request.url,
|
|
211
235
|
inputs: {
|
|
212
236
|
asObj: inputs,
|
|
213
237
|
asCurlString: formatBody(inputs, "curl"),
|
|
@@ -222,15 +246,27 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
|
|
|
222
246
|
asPythonString: formatBody(providerInputs, "python"),
|
|
223
247
|
asTsString: formatBody(providerInputs, "ts"),
|
|
224
248
|
},
|
|
249
|
+
autoInputs: {
|
|
250
|
+
asObj: autoInputs,
|
|
251
|
+
asCurlString: formatBody(autoInputs, "curl"),
|
|
252
|
+
asJsonString: formatBody(autoInputs, "json"),
|
|
253
|
+
asPythonString: formatBody(autoInputs, "python"),
|
|
254
|
+
asTsString: formatBody(autoInputs, "ts"),
|
|
255
|
+
},
|
|
225
256
|
model,
|
|
226
257
|
provider,
|
|
227
|
-
providerModelId:
|
|
258
|
+
providerModelId:
|
|
259
|
+
task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
|
|
260
|
+
? provider !== "auto"
|
|
261
|
+
? `${model.id}:${provider}` // e.g. "moonshotai/Kimi-K2-Instruct:groq"
|
|
262
|
+
: model.id
|
|
263
|
+
: providerModelId ?? model.id,
|
|
228
264
|
billTo: opts?.billTo,
|
|
229
265
|
endpointUrl: opts?.endpointUrl,
|
|
230
266
|
};
|
|
231
267
|
|
|
232
268
|
/// Iterate over clients => check if a snippet exists => generate
|
|
233
|
-
const clients = provider === "auto" ?
|
|
269
|
+
const clients = provider === "auto" && task !== "conversational" ? CLIENTS_NON_CONVERSATIONAL_AUTO_POLICY : CLIENTS;
|
|
234
270
|
return inferenceSnippetLanguages
|
|
235
271
|
.map((language) => {
|
|
236
272
|
const langClients = clients[language] ?? [];
|
|
@@ -5,6 +5,7 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
|
|
|
5
5
|
"basic": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
6
6
|
"basicAudio": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"audio/flac\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
7
7
|
"basicImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
8
|
+
"conversational": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ \n{{ autoInputs.asTsString }}\n}).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
8
9
|
"imageToImage": "const image = fs.readFileSync(\"{{inputs.asObj.inputs}}\");\n\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: {\n\t\t\t\t\"inputs\": `data:image/png;base64,${data.inputs.encode(\"base64\")}`,\n\t\t\t\t\"parameters\": data.parameters,\n\t\t\t}\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ \n\tinputs: image,\n\tparameters: {\n\t\tprompt: \"{{ inputs.asObj.parameters.prompt }}\",\n\t}\n}).then((response) => {\n console.log(JSON.stringify(response));\n});",
|
|
9
10
|
"textToAudio": "{% if model.library_name == \"transformers\" %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n // Returns a byte object of the Audio wavform. Use it directly!\n});\n{% else %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});\n{% endif %} ",
|
|
10
11
|
"textToImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n\treturn result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});",
|
|
@@ -55,8 +56,8 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
|
|
|
55
56
|
"basic": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {{ providerInputs.asObj.inputs }},\n}) ",
|
|
56
57
|
"basicAudio": "def query(filename):\n with open(filename, \"rb\") as f:\n data = f.read()\n response = requests.post(API_URL, headers={\"Content-Type\": \"audio/flac\", **headers}, data=data)\n return response.json()\n\noutput = query({{ providerInputs.asObj.inputs }})",
|
|
57
58
|
"basicImage": "def query(filename):\n with open(filename, \"rb\") as f:\n data = f.read()\n response = requests.post(API_URL, headers={\"Content-Type\": \"image/jpeg\", **headers}, data=data)\n return response.json()\n\noutput = query({{ providerInputs.asObj.inputs }})",
|
|
58
|
-
"conversational": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\nresponse = query({\n{{
|
|
59
|
-
"conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{
|
|
59
|
+
"conversational": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\nresponse = query({\n{{ autoInputs.asJsonString }}\n})\n\nprint(response[\"choices\"][0][\"message\"])",
|
|
60
|
+
"conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{ autoInputs.asJsonString }},\n \"stream\": True,\n})\n\nfor chunk in chunks:\n print(chunk[\"choices\"][0][\"delta\"][\"content\"], end=\"\")",
|
|
60
61
|
"documentQuestionAnswering": "def query(payload):\n with open(payload[\"image\"], \"rb\") as f:\n img = f.read()\n payload[\"image\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {\n \"image\": \"{{ inputs.asObj.image }}\",\n \"question\": \"{{ inputs.asObj.question }}\",\n },\n}) ",
|
|
61
62
|
"imageToImage": "with open(\"{{inputs.asObj.inputs}}\", \"rb\") as image_file:\n image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')\n\ndef query(payload):\n with open(payload[\"inputs\"], \"rb\") as f:\n img = f.read()\n payload[\"inputs\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nimage_bytes = query({\n{{ providerInputs.asJsonString }}\n})\n\n# You can access the image with PIL.Image for example\nimport io\nfrom PIL import Image\nimage = Image.open(io.BytesIO(image_bytes)) ",
|
|
62
63
|
"importRequests": "{% if importBase64 %}\nimport base64\n{% endif %}\n{% if importJson %}\nimport json\n{% endif %}\nimport requests\n\nAPI_URL = \"{{ fullUrl }}\"\nheaders = {\n \"Authorization\": \"{{ authorizationHeader }}\",\n{% if billTo %}\n \"X-HF-Bill-To\": \"{{ billTo }}\"\n{% endif %}\n}",
|
|
@@ -73,8 +74,8 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
|
|
|
73
74
|
"basic": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }}\n }'",
|
|
74
75
|
"basicAudio": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: audio/flac' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
|
|
75
76
|
"basicImage": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: image/jpeg' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
|
|
76
|
-
"conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{
|
|
77
|
-
"conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{
|
|
77
|
+
"conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ autoInputs.asCurlString }},\n \"stream\": false\n }'",
|
|
78
|
+
"conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ autoInputs.asCurlString }},\n \"stream\": true\n }'",
|
|
78
79
|
"zeroShotClassification": "curl {{ fullUrl }} \\\n -X POST \\\n -d '{\"inputs\": {{ providerInputs.asObj.inputs }}, \"parameters\": {\"candidate_labels\": [\"refund\", \"legal\", \"faq\"]}}' \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: {{ authorizationHeader }}'\n{% if billTo %} \\\n -H 'X-HF-Bill-To: {{ billTo }}'\n{% endif %}"
|
|
79
80
|
}
|
|
80
81
|
}
|