@huggingface/inference 3.7.1 → 3.8.1

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.
Files changed (76) hide show
  1. package/dist/index.cjs +247 -132
  2. package/dist/index.js +247 -132
  3. package/dist/src/lib/getInferenceProviderMapping.d.ts +21 -0
  4. package/dist/src/lib/getInferenceProviderMapping.d.ts.map +1 -0
  5. package/dist/src/lib/makeRequestOptions.d.ts +5 -3
  6. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  7. package/dist/src/providers/consts.d.ts +2 -3
  8. package/dist/src/providers/consts.d.ts.map +1 -1
  9. package/dist/src/providers/fal-ai.d.ts.map +1 -1
  10. package/dist/src/providers/hf-inference.d.ts +1 -0
  11. package/dist/src/providers/hf-inference.d.ts.map +1 -1
  12. package/dist/src/snippets/getInferenceSnippets.d.ts +6 -1
  13. package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -1
  14. package/dist/src/snippets/index.d.ts +1 -1
  15. package/dist/src/snippets/index.d.ts.map +1 -1
  16. package/dist/src/tasks/custom/request.d.ts.map +1 -1
  17. package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
  18. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  19. package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts.map +1 -1
  20. package/dist/src/tasks/nlp/chatCompletionStream.d.ts.map +1 -1
  21. package/dist/src/tasks/nlp/questionAnswering.d.ts.map +1 -1
  22. package/dist/src/tasks/nlp/tableQuestionAnswering.d.ts.map +1 -1
  23. package/dist/src/tasks/nlp/textGeneration.d.ts.map +1 -1
  24. package/dist/src/tasks/nlp/textGenerationStream.d.ts.map +1 -1
  25. package/dist/src/tasks/nlp/tokenClassification.d.ts.map +1 -1
  26. package/dist/src/tasks/nlp/zeroShotClassification.d.ts.map +1 -1
  27. package/dist/src/types.d.ts +2 -0
  28. package/dist/src/types.d.ts.map +1 -1
  29. package/dist/src/utils/request.d.ts +3 -2
  30. package/dist/src/utils/request.d.ts.map +1 -1
  31. package/package.json +3 -3
  32. package/src/lib/getInferenceProviderMapping.ts +96 -0
  33. package/src/lib/makeRequestOptions.ts +50 -12
  34. package/src/providers/consts.ts +5 -2
  35. package/src/providers/fal-ai.ts +31 -2
  36. package/src/providers/hf-inference.ts +8 -6
  37. package/src/snippets/getInferenceSnippets.ts +27 -10
  38. package/src/snippets/index.ts +1 -1
  39. package/src/snippets/templates.exported.ts +25 -25
  40. package/src/tasks/audio/audioClassification.ts +1 -1
  41. package/src/tasks/audio/audioToAudio.ts +1 -1
  42. package/src/tasks/audio/automaticSpeechRecognition.ts +1 -1
  43. package/src/tasks/audio/textToSpeech.ts +1 -1
  44. package/src/tasks/custom/request.ts +3 -1
  45. package/src/tasks/custom/streamingRequest.ts +4 -1
  46. package/src/tasks/cv/imageClassification.ts +1 -1
  47. package/src/tasks/cv/imageSegmentation.ts +1 -1
  48. package/src/tasks/cv/imageToImage.ts +1 -1
  49. package/src/tasks/cv/imageToText.ts +1 -1
  50. package/src/tasks/cv/objectDetection.ts +1 -1
  51. package/src/tasks/cv/textToImage.ts +2 -2
  52. package/src/tasks/cv/textToVideo.ts +9 -5
  53. package/src/tasks/cv/zeroShotImageClassification.ts +1 -1
  54. package/src/tasks/multimodal/documentQuestionAnswering.ts +1 -0
  55. package/src/tasks/multimodal/visualQuestionAnswering.ts +1 -1
  56. package/src/tasks/nlp/chatCompletion.ts +1 -1
  57. package/src/tasks/nlp/chatCompletionStream.ts +3 -1
  58. package/src/tasks/nlp/featureExtraction.ts +1 -1
  59. package/src/tasks/nlp/fillMask.ts +1 -1
  60. package/src/tasks/nlp/questionAnswering.ts +8 -4
  61. package/src/tasks/nlp/sentenceSimilarity.ts +1 -1
  62. package/src/tasks/nlp/summarization.ts +1 -1
  63. package/src/tasks/nlp/tableQuestionAnswering.ts +8 -4
  64. package/src/tasks/nlp/textClassification.ts +1 -1
  65. package/src/tasks/nlp/textGeneration.ts +2 -3
  66. package/src/tasks/nlp/textGenerationStream.ts +3 -1
  67. package/src/tasks/nlp/tokenClassification.ts +8 -5
  68. package/src/tasks/nlp/translation.ts +1 -1
  69. package/src/tasks/nlp/zeroShotClassification.ts +8 -5
  70. package/src/tasks/tabular/tabularClassification.ts +1 -1
  71. package/src/tasks/tabular/tabularRegression.ts +1 -1
  72. package/src/types.ts +2 -0
  73. package/src/utils/request.ts +7 -4
  74. package/dist/src/lib/getProviderModelId.d.ts +0 -10
  75. package/dist/src/lib/getProviderModelId.d.ts.map +0 -1
  76. package/src/lib/getProviderModelId.ts +0 -74
package/dist/index.cjs CHANGED
@@ -98,15 +98,6 @@ __export(tasks_exports, {
98
98
  zeroShotImageClassification: () => zeroShotImageClassification
99
99
  });
100
100
 
101
- // package.json
102
- var name = "@huggingface/inference";
103
- var version = "3.7.1";
104
-
105
- // src/config.ts
106
- var HF_HUB_URL = "https://huggingface.co";
107
- var HF_ROUTER_URL = "https://router.huggingface.co";
108
- var HF_HEADER_X_BILL_TO = "X-HF-Bill-To";
109
-
110
101
  // src/lib/InferenceOutputError.ts
111
102
  var InferenceOutputError = class extends TypeError {
112
103
  constructor(message) {
@@ -148,6 +139,11 @@ function omit(o, props) {
148
139
  return pick(o, letsKeep);
149
140
  }
150
141
 
142
+ // src/config.ts
143
+ var HF_HUB_URL = "https://huggingface.co";
144
+ var HF_ROUTER_URL = "https://router.huggingface.co";
145
+ var HF_HEADER_X_BILL_TO = "X-HF-Bill-To";
146
+
151
147
  // src/utils/toArray.ts
152
148
  function toArray(obj) {
153
149
  if (Array.isArray(obj)) {
@@ -337,14 +333,37 @@ var FalAITask = class extends TaskProviderHelper {
337
333
  return headers;
338
334
  }
339
335
  };
336
+ function buildLoraPath(modelId, adapterWeightsPath) {
337
+ return `${HF_HUB_URL}/${modelId}/resolve/main/${adapterWeightsPath}`;
338
+ }
340
339
  var FalAITextToImageTask = class extends FalAITask {
341
340
  preparePayload(params) {
342
- return {
341
+ const payload = {
343
342
  ...omit(params.args, ["inputs", "parameters"]),
344
343
  ...params.args.parameters,
345
344
  sync_mode: true,
346
- prompt: params.args.inputs
345
+ prompt: params.args.inputs,
346
+ ...params.mapping?.adapter === "lora" && params.mapping.adapterWeightsPath ? {
347
+ loras: [
348
+ {
349
+ path: buildLoraPath(params.mapping.hfModelId, params.mapping.adapterWeightsPath),
350
+ scale: 1
351
+ }
352
+ ]
353
+ } : void 0
347
354
  };
355
+ if (params.mapping?.adapter === "lora" && params.mapping.adapterWeightsPath) {
356
+ payload.loras = [
357
+ {
358
+ path: buildLoraPath(params.mapping.hfModelId, params.mapping.adapterWeightsPath),
359
+ scale: 1
360
+ }
361
+ ];
362
+ if (params.mapping.providerId === "fal-ai/lora") {
363
+ payload.model_name = "stabilityai/stable-diffusion-xl-base-1.0";
364
+ }
365
+ }
366
+ return payload;
348
367
  }
349
368
  async getResponse(response, outputType) {
350
369
  if (typeof response === "object" && "images" in response && Array.isArray(response.images) && response.images.length > 0 && "url" in response.images[0] && typeof response.images[0].url === "string") {
@@ -474,6 +493,7 @@ var FireworksConversationalTask = class extends BaseConversationalTask {
474
493
  };
475
494
 
476
495
  // src/providers/hf-inference.ts
496
+ var EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS = ["feature-extraction", "sentence-similarity"];
477
497
  var HFInferenceTask = class extends TaskProviderHelper {
478
498
  constructor() {
479
499
  super("hf-inference", `${HF_ROUTER_URL}/hf-inference`);
@@ -1181,8 +1201,12 @@ function getProviderHelper(provider, task) {
1181
1201
  return providerTasks[task];
1182
1202
  }
1183
1203
 
1204
+ // package.json
1205
+ var name = "@huggingface/inference";
1206
+ var version = "3.8.1";
1207
+
1184
1208
  // src/providers/consts.ts
1185
- var HARDCODED_MODEL_ID_MAPPING = {
1209
+ var HARDCODED_MODEL_INFERENCE_MAPPING = {
1186
1210
  /**
1187
1211
  * "HF model ID" => "Model ID on Inference Provider's side"
1188
1212
  *
@@ -1204,53 +1228,67 @@ var HARDCODED_MODEL_ID_MAPPING = {
1204
1228
  together: {}
1205
1229
  };
1206
1230
 
1207
- // src/lib/getProviderModelId.ts
1231
+ // src/lib/getInferenceProviderMapping.ts
1208
1232
  var inferenceProviderMappingCache = /* @__PURE__ */ new Map();
1209
- async function getProviderModelId(params, args, options = {}) {
1210
- if (params.provider === "hf-inference") {
1211
- return params.model;
1212
- }
1213
- if (!options.task) {
1214
- throw new Error("task must be specified when using a third-party provider");
1215
- }
1216
- const task = options.task === "text-generation" && options.chatCompletion ? "conversational" : options.task;
1217
- if (HARDCODED_MODEL_ID_MAPPING[params.provider]?.[params.model]) {
1218
- return HARDCODED_MODEL_ID_MAPPING[params.provider][params.model];
1233
+ async function getInferenceProviderMapping(params, options) {
1234
+ if (HARDCODED_MODEL_INFERENCE_MAPPING[params.provider][params.modelId]) {
1235
+ return HARDCODED_MODEL_INFERENCE_MAPPING[params.provider][params.modelId];
1219
1236
  }
1220
1237
  let inferenceProviderMapping;
1221
- if (inferenceProviderMappingCache.has(params.model)) {
1222
- inferenceProviderMapping = inferenceProviderMappingCache.get(params.model);
1238
+ if (inferenceProviderMappingCache.has(params.modelId)) {
1239
+ inferenceProviderMapping = inferenceProviderMappingCache.get(params.modelId);
1223
1240
  } else {
1224
- inferenceProviderMapping = await (options?.fetch ?? fetch)(
1225
- `${HF_HUB_URL}/api/models/${params.model}?expand[]=inferenceProviderMapping`,
1241
+ const resp = await (options?.fetch ?? fetch)(
1242
+ `${HF_HUB_URL}/api/models/${params.modelId}?expand[]=inferenceProviderMapping`,
1226
1243
  {
1227
- headers: args.accessToken?.startsWith("hf_") ? { Authorization: `Bearer ${args.accessToken}` } : {}
1244
+ headers: params.accessToken?.startsWith("hf_") ? { Authorization: `Bearer ${params.accessToken}` } : {}
1228
1245
  }
1229
- ).then((resp) => resp.json()).then((json) => json.inferenceProviderMapping).catch(() => null);
1246
+ );
1247
+ if (resp.status === 404) {
1248
+ throw new Error(`Model ${params.modelId} does not exist`);
1249
+ }
1250
+ inferenceProviderMapping = await resp.json().then((json) => json.inferenceProviderMapping).catch(() => null);
1230
1251
  }
1231
1252
  if (!inferenceProviderMapping) {
1232
- throw new Error(`We have not been able to find inference provider information for model ${params.model}.`);
1253
+ throw new Error(`We have not been able to find inference provider information for model ${params.modelId}.`);
1233
1254
  }
1234
1255
  const providerMapping = inferenceProviderMapping[params.provider];
1235
1256
  if (providerMapping) {
1236
- if (providerMapping.task !== task) {
1257
+ const equivalentTasks = params.provider === "hf-inference" && typedInclude(EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS, params.task) ? EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS : [params.task];
1258
+ if (!typedInclude(equivalentTasks, providerMapping.task)) {
1237
1259
  throw new Error(
1238
- `Model ${params.model} is not supported for task ${task} and provider ${params.provider}. Supported task: ${providerMapping.task}.`
1260
+ `Model ${params.modelId} is not supported for task ${params.task} and provider ${params.provider}. Supported task: ${providerMapping.task}.`
1239
1261
  );
1240
1262
  }
1241
1263
  if (providerMapping.status === "staging") {
1242
1264
  console.warn(
1243
- `Model ${params.model} is in staging mode for provider ${params.provider}. Meant for test purposes only.`
1265
+ `Model ${params.modelId} is in staging mode for provider ${params.provider}. Meant for test purposes only.`
1244
1266
  );
1245
1267
  }
1246
- return providerMapping.providerId;
1268
+ if (providerMapping.adapter === "lora") {
1269
+ const treeResp = await (options?.fetch ?? fetch)(`${HF_HUB_URL}/api/models/${params.modelId}/tree/main`);
1270
+ if (!treeResp.ok) {
1271
+ throw new Error(`Unable to fetch the model tree for ${params.modelId}.`);
1272
+ }
1273
+ const tree = await treeResp.json();
1274
+ const adapterWeightsPath = tree.find(({ type, path }) => type === "file" && path.endsWith(".safetensors"))?.path;
1275
+ if (!adapterWeightsPath) {
1276
+ throw new Error(`No .safetensors file found in the model tree for ${params.modelId}.`);
1277
+ }
1278
+ return {
1279
+ ...providerMapping,
1280
+ hfModelId: params.modelId,
1281
+ adapterWeightsPath
1282
+ };
1283
+ }
1284
+ return { ...providerMapping, hfModelId: params.modelId };
1247
1285
  }
1248
- throw new Error(`Model ${params.model} is not supported provider ${params.provider}.`);
1286
+ return null;
1249
1287
  }
1250
1288
 
1251
1289
  // src/lib/makeRequestOptions.ts
1252
1290
  var tasks = null;
1253
- async function makeRequestOptions(args, options) {
1291
+ async function makeRequestOptions(args, providerHelper, options) {
1254
1292
  const { provider: maybeProvider, model: maybeModel } = args;
1255
1293
  const provider = maybeProvider ?? "hf-inference";
1256
1294
  const { task } = options ?? {};
@@ -1260,28 +1298,55 @@ async function makeRequestOptions(args, options) {
1260
1298
  if (maybeModel && isUrl(maybeModel)) {
1261
1299
  throw new Error(`Model URLs are no longer supported. Use endpointUrl instead.`);
1262
1300
  }
1301
+ if (args.endpointUrl) {
1302
+ return makeRequestOptionsFromResolvedModel(
1303
+ maybeModel ?? args.endpointUrl,
1304
+ providerHelper,
1305
+ args,
1306
+ void 0,
1307
+ options
1308
+ );
1309
+ }
1263
1310
  if (!maybeModel && !task) {
1264
1311
  throw new Error("No model provided, and no task has been specified.");
1265
1312
  }
1266
1313
  const hfModel = maybeModel ?? await loadDefaultModel(task);
1267
- const providerHelper = getProviderHelper(provider, task);
1268
1314
  if (providerHelper.clientSideRoutingOnly && !maybeModel) {
1269
1315
  throw new Error(`Provider ${provider} requires a model ID to be passed directly.`);
1270
1316
  }
1271
- const resolvedModel = providerHelper.clientSideRoutingOnly ? (
1317
+ const inferenceProviderMapping = providerHelper.clientSideRoutingOnly ? {
1272
1318
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1273
- removeProviderPrefix(maybeModel, provider)
1274
- ) : await getProviderModelId({ model: hfModel, provider }, args, {
1275
- task,
1276
- fetch: options?.fetch
1277
- });
1278
- return makeRequestOptionsFromResolvedModel(resolvedModel, args, options);
1319
+ providerId: removeProviderPrefix(maybeModel, provider),
1320
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1321
+ hfModelId: maybeModel,
1322
+ status: "live",
1323
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1324
+ task
1325
+ } : await getInferenceProviderMapping(
1326
+ {
1327
+ modelId: hfModel,
1328
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1329
+ task,
1330
+ provider,
1331
+ accessToken: args.accessToken
1332
+ },
1333
+ { fetch: options?.fetch }
1334
+ );
1335
+ if (!inferenceProviderMapping) {
1336
+ throw new Error(`We have not been able to find inference provider information for model ${hfModel}.`);
1337
+ }
1338
+ return makeRequestOptionsFromResolvedModel(
1339
+ inferenceProviderMapping.providerId,
1340
+ providerHelper,
1341
+ args,
1342
+ inferenceProviderMapping,
1343
+ options
1344
+ );
1279
1345
  }
1280
- function makeRequestOptionsFromResolvedModel(resolvedModel, args, options) {
1346
+ function makeRequestOptionsFromResolvedModel(resolvedModel, providerHelper, args, mapping, options) {
1281
1347
  const { accessToken, endpointUrl, provider: maybeProvider, model, ...remainingArgs } = args;
1282
1348
  const provider = maybeProvider ?? "hf-inference";
1283
1349
  const { includeCredentials, task, signal, billTo } = options ?? {};
1284
- const providerHelper = getProviderHelper(provider, task);
1285
1350
  const authMethod = (() => {
1286
1351
  if (providerHelper.clientSideRoutingOnly) {
1287
1352
  if (accessToken && accessToken.startsWith("hf_")) {
@@ -1319,7 +1384,8 @@ function makeRequestOptionsFromResolvedModel(resolvedModel, args, options) {
1319
1384
  const body = providerHelper.makeBody({
1320
1385
  args: remainingArgs,
1321
1386
  model: resolvedModel,
1322
- task
1387
+ task,
1388
+ mapping
1323
1389
  });
1324
1390
  let credentials;
1325
1391
  if (typeof includeCredentials === "string") {
@@ -1460,12 +1526,12 @@ function newMessage() {
1460
1526
  }
1461
1527
 
1462
1528
  // src/utils/request.ts
1463
- async function innerRequest(args, options) {
1464
- const { url, info } = await makeRequestOptions(args, options);
1529
+ async function innerRequest(args, providerHelper, options) {
1530
+ const { url, info } = await makeRequestOptions(args, providerHelper, options);
1465
1531
  const response = await (options?.fetch ?? fetch)(url, info);
1466
1532
  const requestContext = { url, info };
1467
1533
  if (options?.retry_on_error !== false && response.status === 503) {
1468
- return innerRequest(args, options);
1534
+ return innerRequest(args, providerHelper, options);
1469
1535
  }
1470
1536
  if (!response.ok) {
1471
1537
  const contentType = response.headers.get("Content-Type");
@@ -1492,11 +1558,11 @@ async function innerRequest(args, options) {
1492
1558
  const blob = await response.blob();
1493
1559
  return { data: blob, requestContext };
1494
1560
  }
1495
- async function* innerStreamingRequest(args, options) {
1496
- const { url, info } = await makeRequestOptions({ ...args, stream: true }, options);
1561
+ async function* innerStreamingRequest(args, providerHelper, options) {
1562
+ const { url, info } = await makeRequestOptions({ ...args, stream: true }, providerHelper, options);
1497
1563
  const response = await (options?.fetch ?? fetch)(url, info);
1498
1564
  if (options?.retry_on_error !== false && response.status === 503) {
1499
- return yield* innerStreamingRequest(args, options);
1565
+ return yield* innerStreamingRequest(args, providerHelper, options);
1500
1566
  }
1501
1567
  if (!response.ok) {
1502
1568
  if (response.headers.get("Content-Type")?.startsWith("application/json")) {
@@ -1570,7 +1636,8 @@ async function request(args, options) {
1570
1636
  console.warn(
1571
1637
  "The request method is deprecated and will be removed in a future version of huggingface.js. Use specific task functions instead."
1572
1638
  );
1573
- const result = await innerRequest(args, options);
1639
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", options?.task);
1640
+ const result = await innerRequest(args, providerHelper, options);
1574
1641
  return result.data;
1575
1642
  }
1576
1643
 
@@ -1579,7 +1646,8 @@ async function* streamingRequest(args, options) {
1579
1646
  console.warn(
1580
1647
  "The streamingRequest method is deprecated and will be removed in a future version of huggingface.js. Use specific task functions instead."
1581
1648
  );
1582
- yield* innerStreamingRequest(args, options);
1649
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", options?.task);
1650
+ yield* innerStreamingRequest(args, providerHelper, options);
1583
1651
  }
1584
1652
 
1585
1653
  // src/tasks/audio/utils.ts
@@ -1594,7 +1662,7 @@ function preparePayload(args) {
1594
1662
  async function audioClassification(args, options) {
1595
1663
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "audio-classification");
1596
1664
  const payload = preparePayload(args);
1597
- const { data: res } = await innerRequest(payload, {
1665
+ const { data: res } = await innerRequest(payload, providerHelper, {
1598
1666
  ...options,
1599
1667
  task: "audio-classification"
1600
1668
  });
@@ -1605,7 +1673,7 @@ async function audioClassification(args, options) {
1605
1673
  async function audioToAudio(args, options) {
1606
1674
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "audio-to-audio");
1607
1675
  const payload = preparePayload(args);
1608
- const { data: res } = await innerRequest(payload, {
1676
+ const { data: res } = await innerRequest(payload, providerHelper, {
1609
1677
  ...options,
1610
1678
  task: "audio-to-audio"
1611
1679
  });
@@ -1629,7 +1697,7 @@ function base64FromBytes(arr) {
1629
1697
  async function automaticSpeechRecognition(args, options) {
1630
1698
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "automatic-speech-recognition");
1631
1699
  const payload = await buildPayload(args);
1632
- const { data: res } = await innerRequest(payload, {
1700
+ const { data: res } = await innerRequest(payload, providerHelper, {
1633
1701
  ...options,
1634
1702
  task: "automatic-speech-recognition"
1635
1703
  });
@@ -1669,7 +1737,7 @@ async function buildPayload(args) {
1669
1737
  async function textToSpeech(args, options) {
1670
1738
  const provider = args.provider ?? "hf-inference";
1671
1739
  const providerHelper = getProviderHelper(provider, "text-to-speech");
1672
- const { data: res } = await innerRequest(args, {
1740
+ const { data: res } = await innerRequest(args, providerHelper, {
1673
1741
  ...options,
1674
1742
  task: "text-to-speech"
1675
1743
  });
@@ -1685,7 +1753,7 @@ function preparePayload2(args) {
1685
1753
  async function imageClassification(args, options) {
1686
1754
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-classification");
1687
1755
  const payload = preparePayload2(args);
1688
- const { data: res } = await innerRequest(payload, {
1756
+ const { data: res } = await innerRequest(payload, providerHelper, {
1689
1757
  ...options,
1690
1758
  task: "image-classification"
1691
1759
  });
@@ -1696,7 +1764,7 @@ async function imageClassification(args, options) {
1696
1764
  async function imageSegmentation(args, options) {
1697
1765
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-segmentation");
1698
1766
  const payload = preparePayload2(args);
1699
- const { data: res } = await innerRequest(payload, {
1767
+ const { data: res } = await innerRequest(payload, providerHelper, {
1700
1768
  ...options,
1701
1769
  task: "image-segmentation"
1702
1770
  });
@@ -1721,7 +1789,7 @@ async function imageToImage(args, options) {
1721
1789
  )
1722
1790
  };
1723
1791
  }
1724
- const { data: res } = await innerRequest(reqArgs, {
1792
+ const { data: res } = await innerRequest(reqArgs, providerHelper, {
1725
1793
  ...options,
1726
1794
  task: "image-to-image"
1727
1795
  });
@@ -1732,7 +1800,7 @@ async function imageToImage(args, options) {
1732
1800
  async function imageToText(args, options) {
1733
1801
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-to-text");
1734
1802
  const payload = preparePayload2(args);
1735
- const { data: res } = await innerRequest(payload, {
1803
+ const { data: res } = await innerRequest(payload, providerHelper, {
1736
1804
  ...options,
1737
1805
  task: "image-to-text"
1738
1806
  });
@@ -1743,7 +1811,7 @@ async function imageToText(args, options) {
1743
1811
  async function objectDetection(args, options) {
1744
1812
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "object-detection");
1745
1813
  const payload = preparePayload2(args);
1746
- const { data: res } = await innerRequest(payload, {
1814
+ const { data: res } = await innerRequest(payload, providerHelper, {
1747
1815
  ...options,
1748
1816
  task: "object-detection"
1749
1817
  });
@@ -1754,11 +1822,11 @@ async function objectDetection(args, options) {
1754
1822
  async function textToImage(args, options) {
1755
1823
  const provider = args.provider ?? "hf-inference";
1756
1824
  const providerHelper = getProviderHelper(provider, "text-to-image");
1757
- const { data: res } = await innerRequest(args, {
1825
+ const { data: res } = await innerRequest(args, providerHelper, {
1758
1826
  ...options,
1759
1827
  task: "text-to-image"
1760
1828
  });
1761
- const { url, info } = await makeRequestOptions(args, { ...options, task: "text-to-image" });
1829
+ const { url, info } = await makeRequestOptions(args, providerHelper, { ...options, task: "text-to-image" });
1762
1830
  return providerHelper.getResponse(res, url, info.headers, options?.outputType);
1763
1831
  }
1764
1832
 
@@ -1766,11 +1834,15 @@ async function textToImage(args, options) {
1766
1834
  async function textToVideo(args, options) {
1767
1835
  const provider = args.provider ?? "hf-inference";
1768
1836
  const providerHelper = getProviderHelper(provider, "text-to-video");
1769
- const { data: response } = await innerRequest(args, {
1770
- ...options,
1771
- task: "text-to-video"
1772
- });
1773
- const { url, info } = await makeRequestOptions(args, { ...options, task: "text-to-video" });
1837
+ const { data: response } = await innerRequest(
1838
+ args,
1839
+ providerHelper,
1840
+ {
1841
+ ...options,
1842
+ task: "text-to-video"
1843
+ }
1844
+ );
1845
+ const { url, info } = await makeRequestOptions(args, providerHelper, { ...options, task: "text-to-video" });
1774
1846
  return providerHelper.getResponse(response, url, info.headers);
1775
1847
  }
1776
1848
 
@@ -1799,7 +1871,7 @@ async function preparePayload3(args) {
1799
1871
  async function zeroShotImageClassification(args, options) {
1800
1872
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "zero-shot-image-classification");
1801
1873
  const payload = await preparePayload3(args);
1802
- const { data: res } = await innerRequest(payload, {
1874
+ const { data: res } = await innerRequest(payload, providerHelper, {
1803
1875
  ...options,
1804
1876
  task: "zero-shot-image-classification"
1805
1877
  });
@@ -1809,7 +1881,7 @@ async function zeroShotImageClassification(args, options) {
1809
1881
  // src/tasks/nlp/chatCompletion.ts
1810
1882
  async function chatCompletion(args, options) {
1811
1883
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "conversational");
1812
- const { data: response } = await innerRequest(args, {
1884
+ const { data: response } = await innerRequest(args, providerHelper, {
1813
1885
  ...options,
1814
1886
  task: "conversational"
1815
1887
  });
@@ -1818,7 +1890,8 @@ async function chatCompletion(args, options) {
1818
1890
 
1819
1891
  // src/tasks/nlp/chatCompletionStream.ts
1820
1892
  async function* chatCompletionStream(args, options) {
1821
- yield* innerStreamingRequest(args, {
1893
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "conversational");
1894
+ yield* innerStreamingRequest(args, providerHelper, {
1822
1895
  ...options,
1823
1896
  task: "conversational"
1824
1897
  });
@@ -1827,7 +1900,7 @@ async function* chatCompletionStream(args, options) {
1827
1900
  // src/tasks/nlp/featureExtraction.ts
1828
1901
  async function featureExtraction(args, options) {
1829
1902
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "feature-extraction");
1830
- const { data: res } = await innerRequest(args, {
1903
+ const { data: res } = await innerRequest(args, providerHelper, {
1831
1904
  ...options,
1832
1905
  task: "feature-extraction"
1833
1906
  });
@@ -1837,7 +1910,7 @@ async function featureExtraction(args, options) {
1837
1910
  // src/tasks/nlp/fillMask.ts
1838
1911
  async function fillMask(args, options) {
1839
1912
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "fill-mask");
1840
- const { data: res } = await innerRequest(args, {
1913
+ const { data: res } = await innerRequest(args, providerHelper, {
1841
1914
  ...options,
1842
1915
  task: "fill-mask"
1843
1916
  });
@@ -1847,17 +1920,21 @@ async function fillMask(args, options) {
1847
1920
  // src/tasks/nlp/questionAnswering.ts
1848
1921
  async function questionAnswering(args, options) {
1849
1922
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "question-answering");
1850
- const { data: res } = await innerRequest(args, {
1851
- ...options,
1852
- task: "question-answering"
1853
- });
1923
+ const { data: res } = await innerRequest(
1924
+ args,
1925
+ providerHelper,
1926
+ {
1927
+ ...options,
1928
+ task: "question-answering"
1929
+ }
1930
+ );
1854
1931
  return providerHelper.getResponse(res);
1855
1932
  }
1856
1933
 
1857
1934
  // src/tasks/nlp/sentenceSimilarity.ts
1858
1935
  async function sentenceSimilarity(args, options) {
1859
1936
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "sentence-similarity");
1860
- const { data: res } = await innerRequest(args, {
1937
+ const { data: res } = await innerRequest(args, providerHelper, {
1861
1938
  ...options,
1862
1939
  task: "sentence-similarity"
1863
1940
  });
@@ -1867,7 +1944,7 @@ async function sentenceSimilarity(args, options) {
1867
1944
  // src/tasks/nlp/summarization.ts
1868
1945
  async function summarization(args, options) {
1869
1946
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "summarization");
1870
- const { data: res } = await innerRequest(args, {
1947
+ const { data: res } = await innerRequest(args, providerHelper, {
1871
1948
  ...options,
1872
1949
  task: "summarization"
1873
1950
  });
@@ -1877,17 +1954,21 @@ async function summarization(args, options) {
1877
1954
  // src/tasks/nlp/tableQuestionAnswering.ts
1878
1955
  async function tableQuestionAnswering(args, options) {
1879
1956
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "table-question-answering");
1880
- const { data: res } = await innerRequest(args, {
1881
- ...options,
1882
- task: "table-question-answering"
1883
- });
1957
+ const { data: res } = await innerRequest(
1958
+ args,
1959
+ providerHelper,
1960
+ {
1961
+ ...options,
1962
+ task: "table-question-answering"
1963
+ }
1964
+ );
1884
1965
  return providerHelper.getResponse(res);
1885
1966
  }
1886
1967
 
1887
1968
  // src/tasks/nlp/textClassification.ts
1888
1969
  async function textClassification(args, options) {
1889
1970
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "text-classification");
1890
- const { data: res } = await innerRequest(args, {
1971
+ const { data: res } = await innerRequest(args, providerHelper, {
1891
1972
  ...options,
1892
1973
  task: "text-classification"
1893
1974
  });
@@ -1896,9 +1977,8 @@ async function textClassification(args, options) {
1896
1977
 
1897
1978
  // src/tasks/nlp/textGeneration.ts
1898
1979
  async function textGeneration(args, options) {
1899
- const provider = args.provider ?? "hf-inference";
1900
- const providerHelper = getProviderHelper(provider, "text-generation");
1901
- const { data: response } = await innerRequest(args, {
1980
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "text-generation");
1981
+ const { data: response } = await innerRequest(args, providerHelper, {
1902
1982
  ...options,
1903
1983
  task: "text-generation"
1904
1984
  });
@@ -1907,7 +1987,8 @@ async function textGeneration(args, options) {
1907
1987
 
1908
1988
  // src/tasks/nlp/textGenerationStream.ts
1909
1989
  async function* textGenerationStream(args, options) {
1910
- yield* innerStreamingRequest(args, {
1990
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "text-generation");
1991
+ yield* innerStreamingRequest(args, providerHelper, {
1911
1992
  ...options,
1912
1993
  task: "text-generation"
1913
1994
  });
@@ -1916,17 +1997,21 @@ async function* textGenerationStream(args, options) {
1916
1997
  // src/tasks/nlp/tokenClassification.ts
1917
1998
  async function tokenClassification(args, options) {
1918
1999
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "token-classification");
1919
- const { data: res } = await innerRequest(args, {
1920
- ...options,
1921
- task: "token-classification"
1922
- });
2000
+ const { data: res } = await innerRequest(
2001
+ args,
2002
+ providerHelper,
2003
+ {
2004
+ ...options,
2005
+ task: "token-classification"
2006
+ }
2007
+ );
1923
2008
  return providerHelper.getResponse(res);
1924
2009
  }
1925
2010
 
1926
2011
  // src/tasks/nlp/translation.ts
1927
2012
  async function translation(args, options) {
1928
2013
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "translation");
1929
- const { data: res } = await innerRequest(args, {
2014
+ const { data: res } = await innerRequest(args, providerHelper, {
1930
2015
  ...options,
1931
2016
  task: "translation"
1932
2017
  });
@@ -1936,10 +2021,14 @@ async function translation(args, options) {
1936
2021
  // src/tasks/nlp/zeroShotClassification.ts
1937
2022
  async function zeroShotClassification(args, options) {
1938
2023
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "zero-shot-classification");
1939
- const { data: res } = await innerRequest(args, {
1940
- ...options,
1941
- task: "zero-shot-classification"
1942
- });
2024
+ const { data: res } = await innerRequest(
2025
+ args,
2026
+ providerHelper,
2027
+ {
2028
+ ...options,
2029
+ task: "zero-shot-classification"
2030
+ }
2031
+ );
1943
2032
  return providerHelper.getResponse(res);
1944
2033
  }
1945
2034
 
@@ -1956,6 +2045,7 @@ async function documentQuestionAnswering(args, options) {
1956
2045
  };
1957
2046
  const { data: res } = await innerRequest(
1958
2047
  reqArgs,
2048
+ providerHelper,
1959
2049
  {
1960
2050
  ...options,
1961
2051
  task: "document-question-answering"
@@ -1975,7 +2065,7 @@ async function visualQuestionAnswering(args, options) {
1975
2065
  image: base64FromBytes(new Uint8Array(await args.inputs.image.arrayBuffer()))
1976
2066
  }
1977
2067
  };
1978
- const { data: res } = await innerRequest(reqArgs, {
2068
+ const { data: res } = await innerRequest(reqArgs, providerHelper, {
1979
2069
  ...options,
1980
2070
  task: "visual-question-answering"
1981
2071
  });
@@ -1985,7 +2075,7 @@ async function visualQuestionAnswering(args, options) {
1985
2075
  // src/tasks/tabular/tabularClassification.ts
1986
2076
  async function tabularClassification(args, options) {
1987
2077
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "tabular-classification");
1988
- const { data: res } = await innerRequest(args, {
2078
+ const { data: res } = await innerRequest(args, providerHelper, {
1989
2079
  ...options,
1990
2080
  task: "tabular-classification"
1991
2081
  });
@@ -1995,7 +2085,7 @@ async function tabularClassification(args, options) {
1995
2085
  // src/tasks/tabular/tabularRegression.ts
1996
2086
  async function tabularRegression(args, options) {
1997
2087
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "tabular-regression");
1998
- const { data: res } = await innerRequest(args, {
2088
+ const { data: res } = await innerRequest(args, providerHelper, {
1999
2089
  ...options,
2000
2090
  task: "tabular-regression"
2001
2091
  });
@@ -2075,19 +2165,19 @@ var import_tasks = require("@huggingface/tasks");
2075
2165
  var templates = {
2076
2166
  "js": {
2077
2167
  "fetch": {
2078
- "basic": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\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});',
2079
- "basicAudio": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "audio/flac"\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\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});',
2080
- "basicImage": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "image/jpeg"\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\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});',
2081
- "textToAudio": '{% if model.library_name == "transformers" %}\nasync function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const 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 const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\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 %} ',
2082
- "textToImage": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.blob();\n return result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});',
2083
- "zeroShotClassification": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.json();\n return result;\n}\n\nquery({\n inputs: {{ providerInputs.asObj.inputs }},\n parameters: { candidate_labels: ["refund", "legal", "faq"] }\n}).then((response) => {\n console.log(JSON.stringify(response));\n});'
2168
+ "basic": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n{% if billTo %}\n "X-HF-Bill-To": "{{ billTo }}",\n{% endif %} },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\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});',
2169
+ "basicAudio": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "audio/flac",\n{% if billTo %}\n "X-HF-Bill-To": "{{ billTo }}",\n{% endif %} },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\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});',
2170
+ "basicImage": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "image/jpeg",\n{% if billTo %}\n "X-HF-Bill-To": "{{ billTo }}",\n{% endif %} },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\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});',
2171
+ "textToAudio": '{% if model.library_name == "transformers" %}\nasync function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n{% if billTo %}\n "X-HF-Bill-To": "{{ billTo }}",\n{% endif %} },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const 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 const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\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 %} ',
2172
+ "textToImage": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n{% if billTo %}\n "X-HF-Bill-To": "{{ billTo }}",\n{% endif %} },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.blob();\n return result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});',
2173
+ "zeroShotClassification": 'async function query(data) {\n const response = await fetch(\n "{{ fullUrl }}",\n {\n headers: {\n Authorization: "{{ authorizationHeader }}",\n "Content-Type": "application/json",\n{% if billTo %}\n "X-HF-Bill-To": "{{ billTo }}",\n{% endif %} },\n method: "POST",\n body: JSON.stringify(data),\n }\n );\n const result = await response.json();\n return result;\n}\n\nquery({\n inputs: {{ providerInputs.asObj.inputs }},\n parameters: { candidate_labels: ["refund", "legal", "faq"] }\n}).then((response) => {\n console.log(JSON.stringify(response));\n});'
2084
2174
  },
2085
2175
  "huggingface.js": {
2086
- "basic": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nconst output = await client.{{ methodName }}({\n model: "{{ model.id }}",\n inputs: {{ inputs.asObj.inputs }},\n provider: "{{ provider }}",\n});\n\nconsole.log(output);',
2087
- "basicAudio": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nconst data = fs.readFileSync({{inputs.asObj.inputs}});\n\nconst output = await client.{{ methodName }}({\n data,\n model: "{{ model.id }}",\n provider: "{{ provider }}",\n});\n\nconsole.log(output);',
2088
- "basicImage": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nconst data = fs.readFileSync({{inputs.asObj.inputs}});\n\nconst output = await client.{{ methodName }}({\n data,\n model: "{{ model.id }}",\n provider: "{{ provider }}",\n});\n\nconsole.log(output);',
2089
- "conversational": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nconst chatCompletion = await client.chatCompletion({\n provider: "{{ provider }}",\n model: "{{ model.id }}",\n{{ inputs.asTsString }}\n});\n\nconsole.log(chatCompletion.choices[0].message);',
2090
- "conversationalStream": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nlet out = "";\n\nconst stream = await client.chatCompletionStream({\n provider: "{{ provider }}",\n model: "{{ model.id }}",\n{{ inputs.asTsString }}\n});\n\nfor await (const chunk of stream) {\n if (chunk.choices && chunk.choices.length > 0) {\n const newContent = chunk.choices[0].delta.content;\n out += newContent;\n console.log(newContent);\n } \n}',
2176
+ "basic": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nconst output = await client.{{ methodName }}({\n model: "{{ model.id }}",\n inputs: {{ inputs.asObj.inputs }},\n provider: "{{ provider }}",\n}{% if billTo %}, {\n billTo: "{{ billTo }}",\n}{% endif %});\n\nconsole.log(output);',
2177
+ "basicAudio": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nconst data = fs.readFileSync({{inputs.asObj.inputs}});\n\nconst output = await client.{{ methodName }}({\n data,\n model: "{{ model.id }}",\n provider: "{{ provider }}",\n}{% if billTo %}, {\n billTo: "{{ billTo }}",\n}{% endif %});\n\nconsole.log(output);',
2178
+ "basicImage": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nconst data = fs.readFileSync({{inputs.asObj.inputs}});\n\nconst output = await client.{{ methodName }}({\n data,\n model: "{{ model.id }}",\n provider: "{{ provider }}",\n}{% if billTo %}, {\n billTo: "{{ billTo }}",\n}{% endif %});\n\nconsole.log(output);',
2179
+ "conversational": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nconst chatCompletion = await client.chatCompletion({\n provider: "{{ provider }}",\n model: "{{ model.id }}",\n{{ inputs.asTsString }}\n}{% if billTo %}, {\n billTo: "{{ billTo }}",\n}{% endif %});\n\nconsole.log(chatCompletion.choices[0].message);',
2180
+ "conversationalStream": 'import { InferenceClient } from "@huggingface/inference";\n\nconst client = new InferenceClient("{{ accessToken }}");\n\nlet out = "";\n\nconst stream = await client.chatCompletionStream({\n provider: "{{ provider }}",\n model: "{{ model.id }}",\n{{ inputs.asTsString }}\n}{% if billTo %}, {\n billTo: "{{ billTo }}",\n}{% endif %});\n\nfor await (const chunk of stream) {\n if (chunk.choices && chunk.choices.length > 0) {\n const newContent = chunk.choices[0].delta.content;\n out += newContent;\n console.log(newContent);\n } \n}',
2091
2181
  "textToImage": `import { InferenceClient } from "@huggingface/inference";
2092
2182
 
2093
2183
  const client = new InferenceClient("{{ accessToken }}");
@@ -2097,7 +2187,9 @@ const image = await client.textToImage({
2097
2187
  model: "{{ model.id }}",
2098
2188
  inputs: {{ inputs.asObj.inputs }},
2099
2189
  parameters: { num_inference_steps: 5 },
2100
- });
2190
+ }{% if billTo %}, {
2191
+ billTo: "{{ billTo }}",
2192
+ }{% endif %});
2101
2193
  /// Use the generated image (it's a Blob)`,
2102
2194
  "textToVideo": `import { InferenceClient } from "@huggingface/inference";
2103
2195
 
@@ -2107,12 +2199,14 @@ const image = await client.textToVideo({
2107
2199
  provider: "{{ provider }}",
2108
2200
  model: "{{ model.id }}",
2109
2201
  inputs: {{ inputs.asObj.inputs }},
2110
- });
2202
+ }{% if billTo %}, {
2203
+ billTo: "{{ billTo }}",
2204
+ }{% endif %});
2111
2205
  // Use the generated video (it's a Blob)`
2112
2206
  },
2113
2207
  "openai": {
2114
- "conversational": 'import { OpenAI } from "openai";\n\nconst client = new OpenAI({\n baseURL: "{{ baseUrl }}",\n apiKey: "{{ accessToken }}",\n});\n\nconst chatCompletion = await client.chat.completions.create({\n model: "{{ providerModelId }}",\n{{ inputs.asTsString }}\n});\n\nconsole.log(chatCompletion.choices[0].message);',
2115
- "conversationalStream": 'import { OpenAI } from "openai";\n\nconst client = new OpenAI({\n baseURL: "{{ baseUrl }}",\n apiKey: "{{ accessToken }}",\n});\n\nconst stream = await client.chat.completions.create({\n model: "{{ providerModelId }}",\n{{ inputs.asTsString }}\n stream: true,\n});\n\nfor await (const chunk of stream) {\n process.stdout.write(chunk.choices[0]?.delta?.content || "");\n}'
2208
+ "conversational": 'import { OpenAI } from "openai";\n\nconst client = new OpenAI({\n baseURL: "{{ baseUrl }}",\n apiKey: "{{ accessToken }}",\n{% if billTo %}\n defaultHeaders: {\n "X-HF-Bill-To": "{{ billTo }}" \n }\n{% endif %}\n});\n\nconst chatCompletion = await client.chat.completions.create({\n model: "{{ providerModelId }}",\n{{ inputs.asTsString }}\n});\n\nconsole.log(chatCompletion.choices[0].message);',
2209
+ "conversationalStream": 'import { OpenAI } from "openai";\n\nconst client = new OpenAI({\n baseURL: "{{ baseUrl }}",\n apiKey: "{{ accessToken }}",\n{% if billTo %}\n defaultHeaders: {\n "X-HF-Bill-To": "{{ billTo }}" \n }\n{% endif %}\n});\n\nconst stream = await client.chat.completions.create({\n model: "{{ providerModelId }}",\n{{ inputs.asTsString }}\n stream: true,\n});\n\nfor await (const chunk of stream) {\n process.stdout.write(chunk.choices[0]?.delta?.content || "");\n}'
2116
2210
  }
2117
2211
  },
2118
2212
  "python": {
@@ -2127,13 +2221,13 @@ const image = await client.textToVideo({
2127
2221
  "conversationalStream": 'stream = client.chat.completions.create(\n model="{{ model.id }}",\n{{ inputs.asPythonString }}\n stream=True,\n)\n\nfor chunk in stream:\n print(chunk.choices[0].delta.content, end="") ',
2128
2222
  "documentQuestionAnswering": 'output = client.document_question_answering(\n "{{ inputs.asObj.image }}",\n question="{{ inputs.asObj.question }}",\n model="{{ model.id }}",\n) ',
2129
2223
  "imageToImage": '# output is a PIL.Image object\nimage = client.image_to_image(\n "{{ inputs.asObj.inputs }}",\n prompt="{{ inputs.asObj.parameters.prompt }}",\n model="{{ model.id }}",\n) ',
2130
- "importInferenceClient": 'from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider="{{ provider }}",\n api_key="{{ accessToken }}",\n)',
2224
+ "importInferenceClient": 'from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider="{{ provider }}",\n api_key="{{ accessToken }}",\n{% if billTo %}\n bill_to="{{ billTo }}",\n{% endif %}\n)',
2131
2225
  "textToImage": '# output is a PIL.Image object\nimage = client.text_to_image(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n) ',
2132
2226
  "textToVideo": 'video = client.text_to_video(\n {{ inputs.asObj.inputs }},\n model="{{ model.id }}",\n) '
2133
2227
  },
2134
2228
  "openai": {
2135
- "conversational": 'from openai import OpenAI\n\nclient = OpenAI(\n base_url="{{ baseUrl }}",\n api_key="{{ accessToken }}"\n)\n\ncompletion = client.chat.completions.create(\n model="{{ providerModelId }}",\n{{ inputs.asPythonString }}\n)\n\nprint(completion.choices[0].message) ',
2136
- "conversationalStream": 'from openai import OpenAI\n\nclient = OpenAI(\n base_url="{{ baseUrl }}",\n api_key="{{ accessToken }}"\n)\n\nstream = client.chat.completions.create(\n model="{{ providerModelId }}",\n{{ inputs.asPythonString }}\n stream=True,\n)\n\nfor chunk in stream:\n print(chunk.choices[0].delta.content, end="")'
2229
+ "conversational": 'from openai import OpenAI\n\nclient = OpenAI(\n base_url="{{ baseUrl }}",\n api_key="{{ accessToken }}",\n{% if billTo %}\n default_headers={\n "X-HF-Bill-To": "{{ billTo }}"\n }\n{% endif %}\n)\n\ncompletion = client.chat.completions.create(\n model="{{ providerModelId }}",\n{{ inputs.asPythonString }}\n)\n\nprint(completion.choices[0].message) ',
2230
+ "conversationalStream": 'from openai import OpenAI\n\nclient = OpenAI(\n base_url="{{ baseUrl }}",\n api_key="{{ accessToken }}",\n{% if billTo %}\n default_headers={\n "X-HF-Bill-To": "{{ billTo }}"\n }\n{% endif %}\n)\n\nstream = client.chat.completions.create(\n model="{{ providerModelId }}",\n{{ inputs.asPythonString }}\n stream=True,\n)\n\nfor chunk in stream:\n print(chunk.choices[0].delta.content, end="")'
2137
2231
  },
2138
2232
  "requests": {
2139
2233
  "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}) ',
@@ -2143,7 +2237,7 @@ const image = await client.textToVideo({
2143
2237
  "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{{ providerInputs.asJsonString }},\n "stream": True,\n})\n\nfor chunk in chunks:\n print(chunk["choices"][0]["delta"]["content"], end="")',
2144
2238
  "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}) ',
2145
2239
  "imageToImage": 'def 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)) ',
2146
- "importRequests": '{% if importBase64 %}\nimport base64\n{% endif %}\n{% if importJson %}\nimport json\n{% endif %}\nimport requests\n\nAPI_URL = "{{ fullUrl }}"\nheaders = {"Authorization": "{{ authorizationHeader }}"}',
2240
+ "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}',
2147
2241
  "tabular": 'def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nresponse = query({\n "inputs": {\n "data": {{ providerInputs.asObj.inputs }}\n },\n}) ',
2148
2242
  "textToAudio": '{% if model.library_name == "transformers" %}\ndef query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\naudio_bytes = query({\n "inputs": {{ providerInputs.asObj.inputs }},\n})\n# You can access the audio with IPython.display for example\nfrom IPython.display import Audio\nAudio(audio_bytes)\n{% else %}\ndef query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\naudio, sampling_rate = query({\n "inputs": {{ providerInputs.asObj.inputs }},\n})\n# You can access the audio with IPython.display for example\nfrom IPython.display import Audio\nAudio(audio, rate=sampling_rate)\n{% endif %} ',
2149
2243
  "textToImage": '{% if provider == "hf-inference" %}\ndef query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nimage_bytes = query({\n "inputs": {{ providerInputs.asObj.inputs }},\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))\n{% endif %}',
@@ -2153,12 +2247,15 @@ const image = await client.textToVideo({
2153
2247
  },
2154
2248
  "sh": {
2155
2249
  "curl": {
2156
- "basic": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n -d '{\n{{ providerInputs.asCurlString }}\n }'",
2157
- "basicAudio": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: audio/flac' \\\n --data-binary @{{ providerInputs.asObj.inputs }}",
2158
- "basicImage": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: image/jpeg' \\\n --data-binary @{{ providerInputs.asObj.inputs }}",
2250
+ "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 }'",
2251
+ "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 }}",
2252
+ "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 }}",
2159
2253
  "conversational": `curl {{ fullUrl }} \\
2160
2254
  -H 'Authorization: {{ authorizationHeader }}' \\
2161
2255
  -H 'Content-Type: application/json' \\
2256
+ {% if billTo %}
2257
+ -H 'X-HF-Bill-To: {{ billTo }}' \\
2258
+ {% endif %}
2162
2259
  -d '{
2163
2260
  {{ providerInputs.asCurlString }},
2164
2261
  "stream": false
@@ -2166,6 +2263,9 @@ const image = await client.textToVideo({
2166
2263
  "conversationalStream": `curl {{ fullUrl }} \\
2167
2264
  -H 'Authorization: {{ authorizationHeader }}' \\
2168
2265
  -H 'Content-Type: application/json' \\
2266
+ {% if billTo %}
2267
+ -H 'X-HF-Bill-To: {{ billTo }}' \\
2268
+ {% endif %}
2169
2269
  -d '{
2170
2270
  {{ providerInputs.asCurlString }},
2171
2271
  "stream": true
@@ -2174,7 +2274,10 @@ const image = await client.textToVideo({
2174
2274
  -X POST \\
2175
2275
  -d '{"inputs": {{ providerInputs.asObj.inputs }}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}' \\
2176
2276
  -H 'Content-Type: application/json' \\
2177
- -H 'Authorization: {{ authorizationHeader }}'`
2277
+ -H 'Authorization: {{ authorizationHeader }}'
2278
+ {% if billTo %} \\
2279
+ -H 'X-HF-Bill-To: {{ billTo }}'
2280
+ {% endif %}`
2178
2281
  }
2179
2282
  }
2180
2283
  };
@@ -2243,23 +2346,34 @@ var HF_JS_METHODS = {
2243
2346
  translation: "translation"
2244
2347
  };
2245
2348
  var snippetGenerator = (templateName, inputPreparationFn) => {
2246
- return (model, accessToken, provider, providerModelId, opts) => {
2349
+ return (model, accessToken, provider, inferenceProviderMapping, opts) => {
2350
+ const providerModelId = inferenceProviderMapping?.providerId ?? model.id;
2247
2351
  let task = model.pipeline_tag;
2248
2352
  if (model.pipeline_tag && ["text-generation", "image-text-to-text"].includes(model.pipeline_tag) && model.tags.includes("conversational")) {
2249
2353
  templateName = opts?.streaming ? "conversationalStream" : "conversational";
2250
2354
  inputPreparationFn = prepareConversationalInput;
2251
2355
  task = "conversational";
2252
2356
  }
2357
+ let providerHelper;
2358
+ try {
2359
+ providerHelper = getProviderHelper(provider, task);
2360
+ } catch (e) {
2361
+ console.error(`Failed to get provider helper for ${provider} (${task})`, e);
2362
+ return [];
2363
+ }
2253
2364
  const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: (0, import_tasks.getModelInputSnippet)(model) };
2254
2365
  const request2 = makeRequestOptionsFromResolvedModel(
2255
- providerModelId ?? model.id,
2366
+ providerModelId,
2367
+ providerHelper,
2256
2368
  {
2257
2369
  accessToken,
2258
2370
  provider,
2259
2371
  ...inputs
2260
2372
  },
2373
+ inferenceProviderMapping,
2261
2374
  {
2262
- task
2375
+ task,
2376
+ billTo: opts?.billTo
2263
2377
  }
2264
2378
  );
2265
2379
  let providerInputs = inputs;
@@ -2292,7 +2406,8 @@ var snippetGenerator = (templateName, inputPreparationFn) => {
2292
2406
  },
2293
2407
  model,
2294
2408
  provider,
2295
- providerModelId: providerModelId ?? model.id
2409
+ providerModelId: providerModelId ?? model.id,
2410
+ billTo: opts?.billTo
2296
2411
  };
2297
2412
  return import_tasks.inferenceSnippetLanguages.map((language) => {
2298
2413
  return CLIENTS[language].map((client) => {
@@ -2382,8 +2497,8 @@ var snippets = {
2382
2497
  "zero-shot-classification": snippetGenerator("zeroShotClassification"),
2383
2498
  "zero-shot-image-classification": snippetGenerator("zeroShotImageClassification")
2384
2499
  };
2385
- function getInferenceSnippets(model, accessToken, provider, providerModelId, opts) {
2386
- return model.pipeline_tag && model.pipeline_tag in snippets ? snippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
2500
+ function getInferenceSnippets(model, accessToken, provider, inferenceProviderMapping, opts) {
2501
+ return model.pipeline_tag && model.pipeline_tag in snippets ? snippets[model.pipeline_tag]?.(model, accessToken, provider, inferenceProviderMapping, opts) ?? [] : [];
2387
2502
  }
2388
2503
  function formatBody(obj, format) {
2389
2504
  switch (format) {