@huggingface/inference 3.5.1 → 3.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.
Files changed (95) hide show
  1. package/README.md +11 -11
  2. package/dist/browser/index.cjs +1652 -0
  3. package/dist/browser/index.js +1652 -0
  4. package/dist/index.cjs +295 -930
  5. package/dist/index.js +284 -940
  6. package/dist/src/{HfInference.d.ts → InferenceClient.d.ts} +12 -7
  7. package/dist/src/InferenceClient.d.ts.map +1 -0
  8. package/dist/src/index.d.ts +1 -1
  9. package/dist/src/index.d.ts.map +1 -1
  10. package/dist/src/lib/makeRequestOptions.d.ts +16 -1
  11. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  12. package/dist/src/providers/cohere.d.ts.map +1 -1
  13. package/dist/src/providers/novita.d.ts.map +1 -1
  14. package/dist/src/snippets/getInferenceSnippets.d.ts +4 -0
  15. package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -0
  16. package/dist/src/snippets/index.d.ts +1 -4
  17. package/dist/src/snippets/index.d.ts.map +1 -1
  18. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  19. package/dist/test/InferenceClient.spec.d.ts +2 -0
  20. package/dist/test/InferenceClient.spec.d.ts.map +1 -0
  21. package/package.json +17 -8
  22. package/src/{HfInference.ts → InferenceClient.ts} +12 -7
  23. package/src/index.ts +2 -2
  24. package/src/lib/makeRequestOptions.ts +37 -10
  25. package/src/providers/black-forest-labs.ts +2 -2
  26. package/src/providers/cohere.ts +0 -1
  27. package/src/providers/fireworks-ai.ts +4 -4
  28. package/src/providers/hf-inference.ts +1 -1
  29. package/src/providers/nebius.ts +3 -3
  30. package/src/providers/novita.ts +7 -6
  31. package/src/providers/sambanova.ts +1 -1
  32. package/src/providers/together.ts +3 -3
  33. package/src/snippets/getInferenceSnippets.ts +398 -0
  34. package/src/snippets/index.ts +1 -5
  35. package/src/snippets/templates/js/fetch/basic.jinja +19 -0
  36. package/src/snippets/templates/js/fetch/basicAudio.jinja +19 -0
  37. package/src/snippets/templates/js/fetch/basicImage.jinja +19 -0
  38. package/src/snippets/templates/js/fetch/textToAudio.jinja +41 -0
  39. package/src/snippets/templates/js/fetch/textToImage.jinja +19 -0
  40. package/src/snippets/templates/js/fetch/zeroShotClassification.jinja +22 -0
  41. package/src/snippets/templates/js/huggingface.js/basic.jinja +11 -0
  42. package/src/snippets/templates/js/huggingface.js/basicAudio.jinja +13 -0
  43. package/src/snippets/templates/js/huggingface.js/basicImage.jinja +13 -0
  44. package/src/snippets/templates/js/huggingface.js/conversational.jinja +11 -0
  45. package/src/snippets/templates/js/huggingface.js/conversationalStream.jinja +19 -0
  46. package/src/snippets/templates/js/huggingface.js/textToImage.jinja +11 -0
  47. package/src/snippets/templates/js/huggingface.js/textToVideo.jinja +10 -0
  48. package/src/snippets/templates/js/openai/conversational.jinja +13 -0
  49. package/src/snippets/templates/js/openai/conversationalStream.jinja +22 -0
  50. package/src/snippets/templates/python/fal_client/textToImage.jinja +11 -0
  51. package/src/snippets/templates/python/huggingface_hub/basic.jinja +4 -0
  52. package/src/snippets/templates/python/huggingface_hub/basicAudio.jinja +1 -0
  53. package/src/snippets/templates/python/huggingface_hub/basicImage.jinja +1 -0
  54. package/src/snippets/templates/python/huggingface_hub/conversational.jinja +6 -0
  55. package/src/snippets/templates/python/huggingface_hub/conversationalStream.jinja +8 -0
  56. package/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.jinja +5 -0
  57. package/src/snippets/templates/python/huggingface_hub/imageToImage.jinja +6 -0
  58. package/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja +6 -0
  59. package/src/snippets/templates/python/huggingface_hub/textToImage.jinja +5 -0
  60. package/src/snippets/templates/python/huggingface_hub/textToVideo.jinja +4 -0
  61. package/src/snippets/templates/python/openai/conversational.jinja +13 -0
  62. package/src/snippets/templates/python/openai/conversationalStream.jinja +15 -0
  63. package/src/snippets/templates/python/requests/basic.jinja +7 -0
  64. package/src/snippets/templates/python/requests/basicAudio.jinja +7 -0
  65. package/src/snippets/templates/python/requests/basicImage.jinja +7 -0
  66. package/src/snippets/templates/python/requests/conversational.jinja +9 -0
  67. package/src/snippets/templates/python/requests/conversationalStream.jinja +16 -0
  68. package/src/snippets/templates/python/requests/documentQuestionAnswering.jinja +13 -0
  69. package/src/snippets/templates/python/requests/imageToImage.jinja +15 -0
  70. package/src/snippets/templates/python/requests/importRequests.jinja +10 -0
  71. package/src/snippets/templates/python/requests/tabular.jinja +9 -0
  72. package/src/snippets/templates/python/requests/textToAudio.jinja +23 -0
  73. package/src/snippets/templates/python/requests/textToImage.jinja +14 -0
  74. package/src/snippets/templates/python/requests/zeroShotClassification.jinja +8 -0
  75. package/src/snippets/templates/python/requests/zeroShotImageClassification.jinja +14 -0
  76. package/src/snippets/templates/sh/curl/basic.jinja +7 -0
  77. package/src/snippets/templates/sh/curl/basicAudio.jinja +5 -0
  78. package/src/snippets/templates/sh/curl/basicImage.jinja +5 -0
  79. package/src/snippets/templates/sh/curl/conversational.jinja +7 -0
  80. package/src/snippets/templates/sh/curl/conversationalStream.jinja +7 -0
  81. package/src/snippets/templates/sh/curl/zeroShotClassification.jinja +5 -0
  82. package/src/tasks/cv/textToVideo.ts +25 -5
  83. package/src/vendor/fetch-event-source/LICENSE +21 -0
  84. package/dist/src/HfInference.d.ts.map +0 -1
  85. package/dist/src/snippets/curl.d.ts +0 -17
  86. package/dist/src/snippets/curl.d.ts.map +0 -1
  87. package/dist/src/snippets/js.d.ts +0 -21
  88. package/dist/src/snippets/js.d.ts.map +0 -1
  89. package/dist/src/snippets/python.d.ts +0 -23
  90. package/dist/src/snippets/python.d.ts.map +0 -1
  91. package/dist/test/HfInference.spec.d.ts +0 -2
  92. package/dist/test/HfInference.spec.d.ts.map +0 -1
  93. package/src/snippets/curl.ts +0 -177
  94. package/src/snippets/js.ts +0 -475
  95. package/src/snippets/python.ts +0 -487
package/dist/index.js CHANGED
@@ -46,7 +46,7 @@ var HF_HUB_URL = "https://huggingface.co";
46
46
  var HF_ROUTER_URL = "https://router.huggingface.co";
47
47
 
48
48
  // src/providers/black-forest-labs.ts
49
- var BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai/v1";
49
+ var BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai";
50
50
  var makeBody = (params) => {
51
51
  return params.args;
52
52
  };
@@ -58,7 +58,7 @@ var makeHeaders = (params) => {
58
58
  }
59
59
  };
60
60
  var makeUrl = (params) => {
61
- return `${params.baseUrl}/${params.model}`;
61
+ return `${params.baseUrl}/v1/${params.model}`;
62
62
  };
63
63
  var BLACK_FOREST_LABS_CONFIG = {
64
64
  baseUrl: BLACK_FOREST_LABS_AI_API_BASE_URL,
@@ -130,7 +130,7 @@ var FAL_AI_CONFIG = {
130
130
  };
131
131
 
132
132
  // src/providers/fireworks-ai.ts
133
- var FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai/inference";
133
+ var FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai";
134
134
  var makeBody5 = (params) => {
135
135
  return {
136
136
  ...params.args,
@@ -141,10 +141,10 @@ var makeHeaders5 = (params) => {
141
141
  return { Authorization: `Bearer ${params.accessToken}` };
142
142
  };
143
143
  var makeUrl5 = (params) => {
144
- if (params.task === "text-generation" && params.chatCompletion) {
145
- return `${params.baseUrl}/v1/chat/completions`;
144
+ if (params.chatCompletion) {
145
+ return `${params.baseUrl}/inference/v1/chat/completions`;
146
146
  }
147
- return params.baseUrl;
147
+ return `${params.baseUrl}/inference`;
148
148
  };
149
149
  var FIREWORKS_AI_CONFIG = {
150
150
  baseUrl: FIREWORKS_AI_API_BASE_URL,
@@ -167,7 +167,7 @@ var makeUrl6 = (params) => {
167
167
  if (params.task && ["feature-extraction", "sentence-similarity"].includes(params.task)) {
168
168
  return `${params.baseUrl}/pipeline/${params.task}/${params.model}`;
169
169
  }
170
- if (params.task === "text-generation" && params.chatCompletion) {
170
+ if (params.chatCompletion) {
171
171
  return `${params.baseUrl}/models/${params.model}/v1/chat/completions`;
172
172
  }
173
173
  return `${params.baseUrl}/models/${params.model}`;
@@ -218,10 +218,10 @@ var makeUrl8 = (params) => {
218
218
  if (params.task === "text-to-image") {
219
219
  return `${params.baseUrl}/v1/images/generations`;
220
220
  }
221
+ if (params.chatCompletion) {
222
+ return `${params.baseUrl}/v1/chat/completions`;
223
+ }
221
224
  if (params.task === "text-generation") {
222
- if (params.chatCompletion) {
223
- return `${params.baseUrl}/v1/chat/completions`;
224
- }
225
225
  return `${params.baseUrl}/v1/completions`;
226
226
  }
227
227
  return params.baseUrl;
@@ -234,7 +234,7 @@ var NEBIUS_CONFIG = {
234
234
  };
235
235
 
236
236
  // src/providers/novita.ts
237
- var NOVITA_API_BASE_URL = "https://api.novita.ai/v3/openai";
237
+ var NOVITA_API_BASE_URL = "https://api.novita.ai";
238
238
  var makeBody9 = (params) => {
239
239
  return {
240
240
  ...params.args,
@@ -245,11 +245,12 @@ var makeHeaders9 = (params) => {
245
245
  return { Authorization: `Bearer ${params.accessToken}` };
246
246
  };
247
247
  var makeUrl9 = (params) => {
248
- if (params.task === "text-generation") {
249
- if (params.chatCompletion) {
250
- return `${params.baseUrl}/chat/completions`;
251
- }
252
- return `${params.baseUrl}/completions`;
248
+ if (params.chatCompletion) {
249
+ return `${params.baseUrl}/v3/openai/chat/completions`;
250
+ } else if (params.task === "text-generation") {
251
+ return `${params.baseUrl}/v3/openai/completions`;
252
+ } else if (params.task === "text-to-video") {
253
+ return `${params.baseUrl}/v3/hf/${params.model}`;
253
254
  }
254
255
  return params.baseUrl;
255
256
  };
@@ -296,7 +297,7 @@ var makeHeaders11 = (params) => {
296
297
  return { Authorization: `Bearer ${params.accessToken}` };
297
298
  };
298
299
  var makeUrl11 = (params) => {
299
- if (params.task === "text-generation" && params.chatCompletion) {
300
+ if (params.chatCompletion) {
300
301
  return `${params.baseUrl}/v1/chat/completions`;
301
302
  }
302
303
  return params.baseUrl;
@@ -323,10 +324,10 @@ var makeUrl12 = (params) => {
323
324
  if (params.task === "text-to-image") {
324
325
  return `${params.baseUrl}/v1/images/generations`;
325
326
  }
327
+ if (params.chatCompletion) {
328
+ return `${params.baseUrl}/v1/chat/completions`;
329
+ }
326
330
  if (params.task === "text-generation") {
327
- if (params.chatCompletion) {
328
- return `${params.baseUrl}/v1/chat/completions`;
329
- }
330
331
  return `${params.baseUrl}/v1/completions`;
331
332
  }
332
333
  return params.baseUrl;
@@ -373,7 +374,7 @@ function isUrl(modelOrUrl) {
373
374
 
374
375
  // package.json
375
376
  var name = "@huggingface/inference";
376
- var version = "3.5.1";
377
+ var version = "3.6.0";
377
378
 
378
379
  // src/providers/consts.ts
379
380
  var HARDCODED_MODEL_ID_MAPPING = {
@@ -461,11 +462,11 @@ var providerConfigs = {
461
462
  together: TOGETHER_CONFIG
462
463
  };
463
464
  async function makeRequestOptions(args, options) {
464
- const { accessToken, endpointUrl, provider: maybeProvider, model: maybeModel, ...remainingArgs } = args;
465
+ const { provider: maybeProvider, model: maybeModel } = args;
465
466
  const provider = maybeProvider ?? "hf-inference";
466
467
  const providerConfig = providerConfigs[provider];
467
- const { includeCredentials, task, chatCompletion: chatCompletion2, signal } = options ?? {};
468
- if (endpointUrl && provider !== "hf-inference") {
468
+ const { task, chatCompletion: chatCompletion2 } = options ?? {};
469
+ if (args.endpointUrl && provider !== "hf-inference") {
469
470
  throw new Error(`Cannot use endpointUrl with a third-party provider.`);
470
471
  }
471
472
  if (maybeModel && isUrl(maybeModel)) {
@@ -481,17 +482,21 @@ async function makeRequestOptions(args, options) {
481
482
  throw new Error(`Provider ${provider} requires a model ID to be passed directly.`);
482
483
  }
483
484
  const hfModel = maybeModel ?? await loadDefaultModel(task);
484
- const model = providerConfig.clientSideRoutingOnly ? (
485
+ const resolvedModel = providerConfig.clientSideRoutingOnly ? (
485
486
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
486
487
  removeProviderPrefix(maybeModel, provider)
487
- ) : (
488
- // For closed-models API providers, one needs to pass the model ID directly (e.g. "gpt-3.5-turbo")
489
- await getProviderModelId({ model: hfModel, provider }, args, {
490
- task,
491
- chatCompletion: chatCompletion2,
492
- fetch: options?.fetch
493
- })
494
- );
488
+ ) : await getProviderModelId({ model: hfModel, provider }, args, {
489
+ task,
490
+ chatCompletion: chatCompletion2,
491
+ fetch: options?.fetch
492
+ });
493
+ return makeRequestOptionsFromResolvedModel(resolvedModel, args, options);
494
+ }
495
+ function makeRequestOptionsFromResolvedModel(resolvedModel, args, options) {
496
+ const { accessToken, endpointUrl, provider: maybeProvider, model, ...remainingArgs } = args;
497
+ const provider = maybeProvider ?? "hf-inference";
498
+ const providerConfig = providerConfigs[provider];
499
+ const { includeCredentials, task, chatCompletion: chatCompletion2, signal } = options ?? {};
495
500
  const authMethod = (() => {
496
501
  if (providerConfig.clientSideRoutingOnly) {
497
502
  if (accessToken && accessToken.startsWith("hf_")) {
@@ -509,7 +514,7 @@ async function makeRequestOptions(args, options) {
509
514
  })();
510
515
  const url = endpointUrl ? chatCompletion2 ? endpointUrl + `/v1/chat/completions` : endpointUrl : providerConfig.makeUrl({
511
516
  baseUrl: authMethod !== "provider-key" ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider) : providerConfig.baseUrl,
512
- model,
517
+ model: resolvedModel,
513
518
  chatCompletion: chatCompletion2,
514
519
  task
515
520
  });
@@ -527,7 +532,7 @@ async function makeRequestOptions(args, options) {
527
532
  const body = binary ? args.data : JSON.stringify(
528
533
  providerConfig.makeBody({
529
534
  args: remainingArgs,
530
- model,
535
+ model: resolvedModel,
531
536
  task,
532
537
  chatCompletion: chatCompletion2
533
538
  })
@@ -1161,14 +1166,14 @@ async function zeroShotImageClassification(args, options) {
1161
1166
  }
1162
1167
 
1163
1168
  // src/tasks/cv/textToVideo.ts
1164
- var SUPPORTED_PROVIDERS = ["fal-ai", "replicate"];
1169
+ var SUPPORTED_PROVIDERS = ["fal-ai", "novita", "replicate"];
1165
1170
  async function textToVideo(args, options) {
1166
1171
  if (!args.provider || !typedInclude(SUPPORTED_PROVIDERS, args.provider)) {
1167
1172
  throw new Error(
1168
1173
  `textToVideo inference is only supported for the following providers: ${SUPPORTED_PROVIDERS.join(", ")}`
1169
1174
  );
1170
1175
  }
1171
- const payload = args.provider === "fal-ai" || args.provider === "replicate" ? { ...omit(args, ["inputs", "parameters"]), ...args.parameters, prompt: args.inputs } : args;
1176
+ const payload = args.provider === "fal-ai" || args.provider === "replicate" || args.provider === "novita" ? { ...omit(args, ["inputs", "parameters"]), ...args.parameters, prompt: args.inputs } : args;
1172
1177
  const res = await request(payload, {
1173
1178
  ...options,
1174
1179
  task: "text-to-video"
@@ -1180,6 +1185,13 @@ async function textToVideo(args, options) {
1180
1185
  }
1181
1186
  const urlResponse = await fetch(res.video.url);
1182
1187
  return await urlResponse.blob();
1188
+ } else if (args.provider === "novita") {
1189
+ const isValidOutput = typeof res === "object" && !!res && "video" in res && typeof res.video === "object" && !!res.video && "video_url" in res.video && typeof res.video.video_url === "string" && isUrl(res.video.video_url);
1190
+ if (!isValidOutput) {
1191
+ throw new InferenceOutputError("Expected { video: { video_url: string } }");
1192
+ }
1193
+ const urlResponse = await fetch(res.video.video_url);
1194
+ return await urlResponse.blob();
1183
1195
  } else {
1184
1196
  const isValidOutput = typeof res === "object" && !!res && "output" in res && typeof res.output === "string" && isUrl(res.output);
1185
1197
  if (!isValidOutput) {
@@ -1526,8 +1538,8 @@ async function tabularClassification(args, options) {
1526
1538
  return res;
1527
1539
  }
1528
1540
 
1529
- // src/HfInference.ts
1530
- var HfInference = class {
1541
+ // src/InferenceClient.ts
1542
+ var InferenceClient = class {
1531
1543
  accessToken;
1532
1544
  defaultOptions;
1533
1545
  constructor(accessToken = "", defaultOptions = {}) {
@@ -1544,13 +1556,13 @@ var HfInference = class {
1544
1556
  }
1545
1557
  }
1546
1558
  /**
1547
- * Returns copy of HfInference tied to a specified endpoint.
1559
+ * Returns copy of InferenceClient tied to a specified endpoint.
1548
1560
  */
1549
1561
  endpoint(endpointUrl) {
1550
- return new HfInferenceEndpoint(endpointUrl, this.accessToken, this.defaultOptions);
1562
+ return new InferenceClientEndpoint(endpointUrl, this.accessToken, this.defaultOptions);
1551
1563
  }
1552
1564
  };
1553
- var HfInferenceEndpoint = class {
1565
+ var InferenceClientEndpoint = class {
1554
1566
  constructor(endpointUrl, accessToken = "", defaultOptions = {}) {
1555
1567
  accessToken;
1556
1568
  defaultOptions;
@@ -1565,6 +1577,8 @@ var HfInferenceEndpoint = class {
1565
1577
  }
1566
1578
  }
1567
1579
  };
1580
+ var HfInference = class extends InferenceClient {
1581
+ };
1568
1582
 
1569
1583
  // src/types.ts
1570
1584
  var INFERENCE_PROVIDERS = [
@@ -1586,947 +1600,277 @@ var INFERENCE_PROVIDERS = [
1586
1600
  // src/snippets/index.ts
1587
1601
  var snippets_exports = {};
1588
1602
  __export(snippets_exports, {
1589
- curl: () => curl_exports,
1590
- js: () => js_exports,
1591
- python: () => python_exports
1603
+ getInferenceSnippets: () => getInferenceSnippets
1592
1604
  });
1593
1605
 
1594
- // src/snippets/curl.ts
1595
- var curl_exports = {};
1596
- __export(curl_exports, {
1597
- curlSnippets: () => curlSnippets,
1598
- getCurlInferenceSnippet: () => getCurlInferenceSnippet,
1599
- snippetBasic: () => snippetBasic,
1600
- snippetFile: () => snippetFile,
1601
- snippetTextGeneration: () => snippetTextGeneration,
1602
- snippetZeroShotClassification: () => snippetZeroShotClassification
1603
- });
1604
- import { HF_HUB_INFERENCE_PROXY_TEMPLATE as HF_HUB_INFERENCE_PROXY_TEMPLATE2 } from "@huggingface/tasks";
1606
+ // src/snippets/getInferenceSnippets.ts
1605
1607
  import {
1606
- getModelInputSnippet,
1607
- stringifyGenerationConfig,
1608
- stringifyMessages
1608
+ inferenceSnippetLanguages,
1609
+ getModelInputSnippet
1609
1610
  } from "@huggingface/tasks";
1610
- var snippetBasic = (model, accessToken, provider) => {
1611
- if (provider !== "hf-inference") {
1612
- return [];
1613
- }
1614
- return [
1615
- {
1616
- client: "curl",
1617
- content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
1618
- -X POST \\
1619
- -d '{"inputs": ${getModelInputSnippet(model, true)}}' \\
1620
- -H 'Content-Type: application/json' \\
1621
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
1611
+ import { Template } from "@huggingface/jinja";
1612
+ import fs from "fs";
1613
+ import path from "path";
1614
+ import { existsSync as pathExists } from "node:fs";
1615
+ var PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"];
1616
+ var JS_CLIENTS = ["fetch", "huggingface.js", "openai"];
1617
+ var SH_CLIENTS = ["curl"];
1618
+ var CLIENTS = {
1619
+ js: [...JS_CLIENTS],
1620
+ python: [...PYTHON_CLIENTS],
1621
+ sh: [...SH_CLIENTS]
1622
+ };
1623
+ var rootDirFinder = () => {
1624
+ let currentPath = typeof import.meta !== "undefined" && import.meta.url ? path.normalize(new URL(import.meta.url).pathname) : __dirname;
1625
+ while (currentPath !== "/") {
1626
+ if (pathExists(path.join(currentPath, "package.json"))) {
1627
+ return currentPath;
1622
1628
  }
1623
- ];
1624
- };
1625
- var snippetTextGeneration = (model, accessToken, provider, providerModelId, opts) => {
1626
- if (model.tags.includes("conversational")) {
1627
- const baseUrl = provider === "hf-inference" ? `https://router.huggingface.co/hf-inference/models/${model.id}/v1/chat/completions` : HF_HUB_INFERENCE_PROXY_TEMPLATE2.replace("{{PROVIDER}}", provider) + "/v1/chat/completions";
1628
- const modelId = providerModelId ?? model.id;
1629
- const streaming = opts?.streaming ?? true;
1630
- const exampleMessages = getModelInputSnippet(model);
1631
- const messages = opts?.messages ?? exampleMessages;
1632
- const config = {
1633
- ...opts?.temperature ? { temperature: opts.temperature } : void 0,
1634
- max_tokens: opts?.max_tokens ?? 500,
1635
- ...opts?.top_p ? { top_p: opts.top_p } : void 0
1636
- };
1637
- return [
1638
- {
1639
- client: "curl",
1640
- content: `curl '${baseUrl}' \\
1641
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}' \\
1642
- -H 'Content-Type: application/json' \\
1643
- --data '{
1644
- "model": "${modelId}",
1645
- "messages": ${stringifyMessages(messages, {
1646
- indent: " ",
1647
- attributeKeyQuotes: true,
1648
- customContentEscaper: (str) => str.replace(/'/g, "'\\''")
1649
- })},
1650
- ${stringifyGenerationConfig(config, {
1651
- indent: "\n ",
1652
- attributeKeyQuotes: true,
1653
- attributeValueConnector: ": "
1654
- })}
1655
- "stream": ${!!streaming}
1656
- }'`
1657
- }
1658
- ];
1659
- } else {
1660
- return snippetBasic(model, accessToken, provider);
1629
+ currentPath = path.normalize(path.join(currentPath, ".."));
1661
1630
  }
1631
+ return "/";
1662
1632
  };
1663
- var snippetZeroShotClassification = (model, accessToken, provider) => {
1664
- if (provider !== "hf-inference") {
1665
- return [];
1666
- }
1667
- return [
1668
- {
1669
- client: "curl",
1670
- content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
1671
- -X POST \\
1672
- -d '{"inputs": ${getModelInputSnippet(model, true)}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}' \\
1673
- -H 'Content-Type: application/json' \\
1674
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
1675
- }
1676
- ];
1677
- };
1678
- var snippetFile = (model, accessToken, provider) => {
1679
- if (provider !== "hf-inference") {
1680
- return [];
1681
- }
1682
- return [
1683
- {
1684
- client: "curl",
1685
- content: `curl https://router.huggingface.co/hf-inference/models/${model.id} \\
1686
- -X POST \\
1687
- --data-binary '@${getModelInputSnippet(model, true, true)}' \\
1688
- -H 'Authorization: Bearer ${accessToken || `{API_TOKEN}`}'`
1689
- }
1690
- ];
1691
- };
1692
- var curlSnippets = {
1693
- // Same order as in tasks/src/pipelines.ts
1694
- "text-classification": snippetBasic,
1695
- "token-classification": snippetBasic,
1696
- "table-question-answering": snippetBasic,
1697
- "question-answering": snippetBasic,
1698
- "zero-shot-classification": snippetZeroShotClassification,
1699
- translation: snippetBasic,
1700
- summarization: snippetBasic,
1701
- "feature-extraction": snippetBasic,
1702
- "text-generation": snippetTextGeneration,
1703
- "image-text-to-text": snippetTextGeneration,
1704
- "text2text-generation": snippetBasic,
1705
- "fill-mask": snippetBasic,
1706
- "sentence-similarity": snippetBasic,
1707
- "automatic-speech-recognition": snippetFile,
1708
- "text-to-image": snippetBasic,
1709
- "text-to-speech": snippetBasic,
1710
- "text-to-audio": snippetBasic,
1711
- "audio-to-audio": snippetFile,
1712
- "audio-classification": snippetFile,
1713
- "image-classification": snippetFile,
1714
- "image-to-text": snippetFile,
1715
- "object-detection": snippetFile,
1716
- "image-segmentation": snippetFile
1717
- };
1718
- function getCurlInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
1719
- return model.pipeline_tag && model.pipeline_tag in curlSnippets ? curlSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
1720
- }
1721
-
1722
- // src/snippets/python.ts
1723
- var python_exports = {};
1724
- __export(python_exports, {
1725
- getPythonInferenceSnippet: () => getPythonInferenceSnippet,
1726
- pythonSnippets: () => pythonSnippets,
1727
- snippetBasic: () => snippetBasic2,
1728
- snippetConversational: () => snippetConversational,
1729
- snippetDocumentQuestionAnswering: () => snippetDocumentQuestionAnswering,
1730
- snippetFile: () => snippetFile2,
1731
- snippetTabular: () => snippetTabular,
1732
- snippetTextToAudio: () => snippetTextToAudio,
1733
- snippetTextToImage: () => snippetTextToImage,
1734
- snippetTextToVideo: () => snippetTextToVideo,
1735
- snippetZeroShotClassification: () => snippetZeroShotClassification2,
1736
- snippetZeroShotImageClassification: () => snippetZeroShotImageClassification
1737
- });
1738
- import { openAIbaseUrl } from "@huggingface/tasks";
1739
- import {
1740
- getModelInputSnippet as getModelInputSnippet2,
1741
- stringifyGenerationConfig as stringifyGenerationConfig2,
1742
- stringifyMessages as stringifyMessages2
1743
- } from "@huggingface/tasks";
1744
- var HFH_INFERENCE_CLIENT_METHODS = {
1633
+ var templatePath = (language, client, templateName) => path.join(rootDirFinder(), "src", "snippets", "templates", language, client, `${templateName}.jinja`);
1634
+ var hasTemplate = (language, client, templateName) => pathExists(templatePath(language, client, templateName));
1635
+ var loadTemplate = (language, client, templateName) => {
1636
+ const template = fs.readFileSync(templatePath(language, client, templateName), "utf8");
1637
+ return (data) => new Template(template).render({ ...data });
1638
+ };
1639
+ var snippetImportPythonInferenceClient = loadTemplate("python", "huggingface_hub", "importInferenceClient");
1640
+ var snippetImportRequests = loadTemplate("python", "requests", "importRequests");
1641
+ var HF_PYTHON_METHODS = {
1745
1642
  "audio-classification": "audio_classification",
1746
1643
  "audio-to-audio": "audio_to_audio",
1747
1644
  "automatic-speech-recognition": "automatic_speech_recognition",
1748
- "text-to-speech": "text_to_speech",
1645
+ "document-question-answering": "document_question_answering",
1646
+ "feature-extraction": "feature_extraction",
1647
+ "fill-mask": "fill_mask",
1749
1648
  "image-classification": "image_classification",
1750
1649
  "image-segmentation": "image_segmentation",
1751
1650
  "image-to-image": "image_to_image",
1752
1651
  "image-to-text": "image_to_text",
1753
1652
  "object-detection": "object_detection",
1754
- "text-to-image": "text_to_image",
1755
- "text-to-video": "text_to_video",
1756
- "zero-shot-image-classification": "zero_shot_image_classification",
1757
- "document-question-answering": "document_question_answering",
1758
- "visual-question-answering": "visual_question_answering",
1759
- "feature-extraction": "feature_extraction",
1760
- "fill-mask": "fill_mask",
1761
1653
  "question-answering": "question_answering",
1762
1654
  "sentence-similarity": "sentence_similarity",
1763
1655
  summarization: "summarization",
1764
1656
  "table-question-answering": "table_question_answering",
1657
+ "tabular-classification": "tabular_classification",
1658
+ "tabular-regression": "tabular_regression",
1765
1659
  "text-classification": "text_classification",
1766
1660
  "text-generation": "text_generation",
1661
+ "text-to-image": "text_to_image",
1662
+ "text-to-speech": "text_to_speech",
1663
+ "text-to-video": "text_to_video",
1767
1664
  "token-classification": "token_classification",
1768
1665
  translation: "translation",
1666
+ "visual-question-answering": "visual_question_answering",
1769
1667
  "zero-shot-classification": "zero_shot_classification",
1770
- "tabular-classification": "tabular_classification",
1771
- "tabular-regression": "tabular_regression"
1772
- };
1773
- var snippetImportInferenceClient = (accessToken, provider) => `from huggingface_hub import InferenceClient
1774
-
1775
- client = InferenceClient(
1776
- provider="${provider}",
1777
- api_key="${accessToken || "{API_TOKEN}"}"
1778
- )`;
1779
- var snippetConversational = (model, accessToken, provider, providerModelId, opts) => {
1780
- const streaming = opts?.streaming ?? true;
1781
- const exampleMessages = getModelInputSnippet2(model);
1782
- const messages = opts?.messages ?? exampleMessages;
1783
- const messagesStr = stringifyMessages2(messages, { attributeKeyQuotes: true });
1784
- const config = {
1785
- ...opts?.temperature ? { temperature: opts.temperature } : void 0,
1786
- max_tokens: opts?.max_tokens ?? 500,
1787
- ...opts?.top_p ? { top_p: opts.top_p } : void 0
1788
- };
1789
- const configStr = stringifyGenerationConfig2(config, {
1790
- indent: "\n ",
1791
- attributeValueConnector: "="
1792
- });
1793
- if (streaming) {
1794
- return [
1795
- {
1796
- client: "huggingface_hub",
1797
- content: `${snippetImportInferenceClient(accessToken, provider)}
1798
-
1799
- messages = ${messagesStr}
1800
-
1801
- stream = client.chat.completions.create(
1802
- model="${model.id}",
1803
- messages=messages,
1804
- ${configStr}
1805
- stream=True
1806
- )
1807
-
1808
- for chunk in stream:
1809
- print(chunk.choices[0].delta.content, end="")`
1810
- },
1811
- {
1812
- client: "openai",
1813
- content: `from openai import OpenAI
1814
-
1815
- client = OpenAI(
1816
- base_url="${openAIbaseUrl(provider)}",
1817
- api_key="${accessToken || "{API_TOKEN}"}"
1818
- )
1819
-
1820
- messages = ${messagesStr}
1821
-
1822
- stream = client.chat.completions.create(
1823
- model="${providerModelId ?? model.id}",
1824
- messages=messages,
1825
- ${configStr}
1826
- stream=True
1827
- )
1828
-
1829
- for chunk in stream:
1830
- print(chunk.choices[0].delta.content, end="")`
1831
- }
1832
- ];
1833
- } else {
1834
- return [
1835
- {
1836
- client: "huggingface_hub",
1837
- content: `${snippetImportInferenceClient(accessToken, provider)}
1838
-
1839
- messages = ${messagesStr}
1840
-
1841
- completion = client.chat.completions.create(
1842
- model="${model.id}",
1843
- messages=messages,
1844
- ${configStr}
1845
- )
1846
-
1847
- print(completion.choices[0].message)`
1848
- },
1849
- {
1850
- client: "openai",
1851
- content: `from openai import OpenAI
1852
-
1853
- client = OpenAI(
1854
- base_url="${openAIbaseUrl(provider)}",
1855
- api_key="${accessToken || "{API_TOKEN}"}"
1856
- )
1857
-
1858
- messages = ${messagesStr}
1859
-
1860
- completion = client.chat.completions.create(
1861
- model="${providerModelId ?? model.id}",
1862
- messages=messages,
1863
- ${configStr}
1864
- )
1865
-
1866
- print(completion.choices[0].message)`
1867
- }
1868
- ];
1869
- }
1668
+ "zero-shot-image-classification": "zero_shot_image_classification"
1870
1669
  };
1871
- var snippetZeroShotClassification2 = (model) => {
1872
- return [
1873
- {
1874
- client: "requests",
1875
- content: `def query(payload):
1876
- response = requests.post(API_URL, headers=headers, json=payload)
1877
- return response.json()
1878
-
1879
- output = query({
1880
- "inputs": ${getModelInputSnippet2(model)},
1881
- "parameters": {"candidate_labels": ["refund", "legal", "faq"]},
1882
- })`
1883
- }
1884
- ];
1885
- };
1886
- var snippetZeroShotImageClassification = (model) => {
1887
- return [
1888
- {
1889
- client: "requests",
1890
- content: `def query(data):
1891
- with open(data["image_path"], "rb") as f:
1892
- img = f.read()
1893
- payload={
1894
- "parameters": data["parameters"],
1895
- "inputs": base64.b64encode(img).decode("utf-8")
1896
- }
1897
- response = requests.post(API_URL, headers=headers, json=payload)
1898
- return response.json()
1899
-
1900
- output = query({
1901
- "image_path": ${getModelInputSnippet2(model)},
1902
- "parameters": {"candidate_labels": ["cat", "dog", "llama"]},
1903
- })`
1904
- }
1905
- ];
1906
- };
1907
- var snippetBasic2 = (model, accessToken, provider) => {
1908
- return [
1909
- ...model.pipeline_tag && model.pipeline_tag in HFH_INFERENCE_CLIENT_METHODS ? [
1910
- {
1911
- client: "huggingface_hub",
1912
- content: `${snippetImportInferenceClient(accessToken, provider)}
1913
-
1914
- result = client.${HFH_INFERENCE_CLIENT_METHODS[model.pipeline_tag]}(
1915
- model="${model.id}",
1916
- inputs=${getModelInputSnippet2(model)},
1917
- provider="${provider}",
1918
- )
1919
-
1920
- print(result)
1921
- `
1922
- }
1923
- ] : [],
1924
- {
1925
- client: "requests",
1926
- content: `def query(payload):
1927
- response = requests.post(API_URL, headers=headers, json=payload)
1928
- return response.json()
1929
-
1930
- output = query({
1931
- "inputs": ${getModelInputSnippet2(model)},
1932
- })`
1933
- }
1934
- ];
1935
- };
1936
- var snippetFile2 = (model) => {
1937
- return [
1938
- {
1939
- client: "requests",
1940
- content: `def query(filename):
1941
- with open(filename, "rb") as f:
1942
- data = f.read()
1943
- response = requests.post(API_URL, headers=headers, data=data)
1944
- return response.json()
1945
-
1946
- output = query(${getModelInputSnippet2(model)})`
1947
- }
1948
- ];
1949
- };
1950
- var snippetTextToImage = (model, accessToken, provider, providerModelId) => {
1951
- return [
1952
- {
1953
- client: "huggingface_hub",
1954
- content: `${snippetImportInferenceClient(accessToken, provider)}
1955
-
1956
- # output is a PIL.Image object
1957
- image = client.text_to_image(
1958
- ${getModelInputSnippet2(model)},
1959
- model="${model.id}"
1960
- )`
1961
- },
1962
- ...provider === "fal-ai" ? [
1963
- {
1964
- client: "fal-client",
1965
- content: `import fal_client
1966
-
1967
- result = fal_client.subscribe(
1968
- "${providerModelId ?? model.id}",
1969
- arguments={
1970
- "prompt": ${getModelInputSnippet2(model)},
1971
- },
1972
- )
1973
- print(result)
1974
- `
1975
- }
1976
- ] : [],
1977
- ...provider === "hf-inference" ? [
1978
- {
1979
- client: "requests",
1980
- content: `def query(payload):
1981
- response = requests.post(API_URL, headers=headers, json=payload)
1982
- return response.content
1983
-
1984
- image_bytes = query({
1985
- "inputs": ${getModelInputSnippet2(model)},
1986
- })
1987
-
1988
- # You can access the image with PIL.Image for example
1989
- import io
1990
- from PIL import Image
1991
- image = Image.open(io.BytesIO(image_bytes))`
1992
- }
1993
- ] : []
1994
- ];
1995
- };
1996
- var snippetTextToVideo = (model, accessToken, provider) => {
1997
- return ["fal-ai", "replicate"].includes(provider) ? [
1998
- {
1999
- client: "huggingface_hub",
2000
- content: `${snippetImportInferenceClient(accessToken, provider)}
2001
-
2002
- video = client.text_to_video(
2003
- ${getModelInputSnippet2(model)},
2004
- model="${model.id}"
2005
- )`
2006
- }
2007
- ] : [];
2008
- };
2009
- var snippetTabular = (model) => {
2010
- return [
2011
- {
2012
- client: "requests",
2013
- content: `def query(payload):
2014
- response = requests.post(API_URL, headers=headers, json=payload)
2015
- return response.content
2016
-
2017
- response = query({
2018
- "inputs": {"data": ${getModelInputSnippet2(model)}},
2019
- })`
2020
- }
2021
- ];
2022
- };
2023
- var snippetTextToAudio = (model) => {
2024
- if (model.library_name === "transformers") {
2025
- return [
2026
- {
2027
- client: "requests",
2028
- content: `def query(payload):
2029
- response = requests.post(API_URL, headers=headers, json=payload)
2030
- return response.content
2031
-
2032
- audio_bytes = query({
2033
- "inputs": ${getModelInputSnippet2(model)},
2034
- })
2035
- # You can access the audio with IPython.display for example
2036
- from IPython.display import Audio
2037
- Audio(audio_bytes)`
2038
- }
2039
- ];
2040
- } else {
2041
- return [
2042
- {
2043
- client: "requests",
2044
- content: `def query(payload):
2045
- response = requests.post(API_URL, headers=headers, json=payload)
2046
- return response.json()
2047
-
2048
- audio, sampling_rate = query({
2049
- "inputs": ${getModelInputSnippet2(model)},
2050
- })
2051
- # You can access the audio with IPython.display for example
2052
- from IPython.display import Audio
2053
- Audio(audio, rate=sampling_rate)`
2054
- }
2055
- ];
2056
- }
2057
- };
2058
- var snippetDocumentQuestionAnswering = (model) => {
2059
- return [
2060
- {
2061
- client: "requests",
2062
- content: `def query(payload):
2063
- with open(payload["image"], "rb") as f:
2064
- img = f.read()
2065
- payload["image"] = base64.b64encode(img).decode("utf-8")
2066
- response = requests.post(API_URL, headers=headers, json=payload)
2067
- return response.json()
2068
-
2069
- output = query({
2070
- "inputs": ${getModelInputSnippet2(model)},
2071
- })`
2072
- }
2073
- ];
2074
- };
2075
- var pythonSnippets = {
2076
- // Same order as in tasks/src/pipelines.ts
2077
- "text-classification": snippetBasic2,
2078
- "token-classification": snippetBasic2,
2079
- "table-question-answering": snippetBasic2,
2080
- "question-answering": snippetBasic2,
2081
- "zero-shot-classification": snippetZeroShotClassification2,
2082
- translation: snippetBasic2,
2083
- summarization: snippetBasic2,
2084
- "feature-extraction": snippetBasic2,
2085
- "text-generation": snippetBasic2,
2086
- "text2text-generation": snippetBasic2,
2087
- "image-text-to-text": snippetConversational,
2088
- "fill-mask": snippetBasic2,
2089
- "sentence-similarity": snippetBasic2,
2090
- "automatic-speech-recognition": snippetFile2,
2091
- "text-to-image": snippetTextToImage,
2092
- "text-to-video": snippetTextToVideo,
2093
- "text-to-speech": snippetTextToAudio,
2094
- "text-to-audio": snippetTextToAudio,
2095
- "audio-to-audio": snippetFile2,
2096
- "audio-classification": snippetFile2,
2097
- "image-classification": snippetFile2,
2098
- "tabular-regression": snippetTabular,
2099
- "tabular-classification": snippetTabular,
2100
- "object-detection": snippetFile2,
2101
- "image-segmentation": snippetFile2,
2102
- "document-question-answering": snippetDocumentQuestionAnswering,
2103
- "image-to-text": snippetFile2,
2104
- "zero-shot-image-classification": snippetZeroShotImageClassification
2105
- };
2106
- function getPythonInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
2107
- if (model.tags.includes("conversational")) {
2108
- return snippetConversational(model, accessToken, provider, providerModelId, opts);
2109
- } else {
2110
- const snippets = model.pipeline_tag && model.pipeline_tag in pythonSnippets ? pythonSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId) ?? [] : [];
2111
- return snippets.map((snippet) => {
2112
- return {
2113
- ...snippet,
2114
- content: snippet.client === "requests" ? `import requests
2115
-
2116
- API_URL = "${openAIbaseUrl(provider)}"
2117
- headers = {"Authorization": ${accessToken ? `"Bearer ${accessToken}"` : `f"Bearer {API_TOKEN}"`}}
2118
-
2119
- ${snippet.content}` : snippet.content
2120
- };
2121
- });
2122
- }
2123
- }
2124
-
2125
- // src/snippets/js.ts
2126
- var js_exports = {};
2127
- __export(js_exports, {
2128
- getJsInferenceSnippet: () => getJsInferenceSnippet,
2129
- jsSnippets: () => jsSnippets,
2130
- snippetAutomaticSpeechRecognition: () => snippetAutomaticSpeechRecognition,
2131
- snippetBasic: () => snippetBasic3,
2132
- snippetFile: () => snippetFile3,
2133
- snippetTextGeneration: () => snippetTextGeneration2,
2134
- snippetTextToAudio: () => snippetTextToAudio2,
2135
- snippetTextToImage: () => snippetTextToImage2,
2136
- snippetTextToVideo: () => snippetTextToVideo2,
2137
- snippetZeroShotClassification: () => snippetZeroShotClassification3
2138
- });
2139
- import { openAIbaseUrl as openAIbaseUrl2 } from "@huggingface/tasks";
2140
- import {
2141
- getModelInputSnippet as getModelInputSnippet3,
2142
- stringifyGenerationConfig as stringifyGenerationConfig3,
2143
- stringifyMessages as stringifyMessages3
2144
- } from "@huggingface/tasks";
2145
- var HFJS_METHODS = {
2146
- "text-classification": "textClassification",
2147
- "token-classification": "tokenClassification",
2148
- "table-question-answering": "tableQuestionAnswering",
1670
+ var HF_JS_METHODS = {
1671
+ "automatic-speech-recognition": "automaticSpeechRecognition",
1672
+ "feature-extraction": "featureExtraction",
1673
+ "fill-mask": "fillMask",
1674
+ "image-classification": "imageClassification",
2149
1675
  "question-answering": "questionAnswering",
2150
- translation: "translation",
1676
+ "sentence-similarity": "sentenceSimilarity",
2151
1677
  summarization: "summarization",
2152
- "feature-extraction": "featureExtraction",
1678
+ "table-question-answering": "tableQuestionAnswering",
1679
+ "text-classification": "textClassification",
2153
1680
  "text-generation": "textGeneration",
2154
1681
  "text2text-generation": "textGeneration",
2155
- "fill-mask": "fillMask",
2156
- "sentence-similarity": "sentenceSimilarity"
1682
+ "token-classification": "tokenClassification",
1683
+ translation: "translation"
2157
1684
  };
2158
- var snippetBasic3 = (model, accessToken, provider) => {
2159
- return [
2160
- ...model.pipeline_tag && model.pipeline_tag in HFJS_METHODS ? [
2161
- {
2162
- client: "huggingface.js",
2163
- content: `import { HfInference } from "@huggingface/inference";
2164
-
2165
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2166
-
2167
- const output = await client.${HFJS_METHODS[model.pipeline_tag]}({
2168
- model: "${model.id}",
2169
- inputs: ${getModelInputSnippet3(model)},
2170
- provider: "${provider}",
2171
- });
2172
-
2173
- console.log(output);
2174
- `
1685
+ var snippetGenerator = (templateName, inputPreparationFn) => {
1686
+ return (model, accessToken, provider, providerModelId, opts) => {
1687
+ if (model.pipeline_tag && ["text-generation", "image-text-to-text"].includes(model.pipeline_tag) && model.tags.includes("conversational")) {
1688
+ templateName = opts?.streaming ? "conversationalStream" : "conversational";
1689
+ inputPreparationFn = prepareConversationalInput;
1690
+ }
1691
+ const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: getModelInputSnippet(model) };
1692
+ const request2 = makeRequestOptionsFromResolvedModel(
1693
+ providerModelId ?? model.id,
1694
+ { accessToken, provider, ...inputs },
1695
+ { chatCompletion: templateName.includes("conversational"), task: model.pipeline_tag }
1696
+ );
1697
+ let providerInputs = inputs;
1698
+ const bodyAsObj = request2.info.body;
1699
+ if (typeof bodyAsObj === "string") {
1700
+ try {
1701
+ providerInputs = JSON.parse(bodyAsObj);
1702
+ } catch (e) {
1703
+ console.error("Failed to parse body as JSON", e);
2175
1704
  }
2176
- ] : [],
2177
- {
2178
- client: "fetch",
2179
- content: `async function query(data) {
2180
- const response = await fetch(
2181
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2182
- {
2183
- headers: {
2184
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2185
- "Content-Type": "application/json",
2186
- },
2187
- method: "POST",
2188
- body: JSON.stringify(data),
2189
- }
2190
- );
2191
- const result = await response.json();
2192
- return result;
2193
- }
2194
-
2195
- query({"inputs": ${getModelInputSnippet3(model)}}).then((response) => {
2196
- console.log(JSON.stringify(response));
2197
- });`
2198
1705
  }
2199
- ];
2200
- };
2201
- var snippetTextGeneration2 = (model, accessToken, provider, providerModelId, opts) => {
2202
- if (model.tags.includes("conversational")) {
2203
- const streaming = opts?.streaming ?? true;
2204
- const exampleMessages = getModelInputSnippet3(model);
2205
- const messages = opts?.messages ?? exampleMessages;
2206
- const messagesStr = stringifyMessages3(messages, { indent: " " });
2207
- const config = {
2208
- ...opts?.temperature ? { temperature: opts.temperature } : void 0,
2209
- max_tokens: opts?.max_tokens ?? 500,
2210
- ...opts?.top_p ? { top_p: opts.top_p } : void 0
1706
+ const params = {
1707
+ accessToken,
1708
+ authorizationHeader: request2.info.headers?.Authorization,
1709
+ baseUrl: removeSuffix(request2.url, "/chat/completions"),
1710
+ fullUrl: request2.url,
1711
+ inputs: {
1712
+ asObj: inputs,
1713
+ asCurlString: formatBody(inputs, "curl"),
1714
+ asJsonString: formatBody(inputs, "json"),
1715
+ asPythonString: formatBody(inputs, "python"),
1716
+ asTsString: formatBody(inputs, "ts")
1717
+ },
1718
+ providerInputs: {
1719
+ asObj: providerInputs,
1720
+ asCurlString: formatBody(providerInputs, "curl"),
1721
+ asJsonString: formatBody(providerInputs, "json"),
1722
+ asPythonString: formatBody(providerInputs, "python"),
1723
+ asTsString: formatBody(providerInputs, "ts")
1724
+ },
1725
+ model,
1726
+ provider,
1727
+ providerModelId: providerModelId ?? model.id
2211
1728
  };
2212
- const configStr = stringifyGenerationConfig3(config, {
2213
- indent: "\n ",
2214
- attributeValueConnector: ": "
2215
- });
2216
- if (streaming) {
2217
- return [
2218
- {
2219
- client: "huggingface.js",
2220
- content: `import { HfInference } from "@huggingface/inference";
2221
-
2222
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2223
-
2224
- let out = "";
2225
-
2226
- const stream = client.chatCompletionStream({
2227
- model: "${model.id}",
2228
- messages: ${messagesStr},
2229
- provider: "${provider}",
2230
- ${configStr}
2231
- });
2232
-
2233
- for await (const chunk of stream) {
2234
- if (chunk.choices && chunk.choices.length > 0) {
2235
- const newContent = chunk.choices[0].delta.content;
2236
- out += newContent;
2237
- console.log(newContent);
2238
- }
2239
- }`
2240
- },
2241
- {
2242
- client: "openai",
2243
- content: `import { OpenAI } from "openai";
2244
-
2245
- const client = new OpenAI({
2246
- baseURL: "${openAIbaseUrl2(provider)}",
2247
- apiKey: "${accessToken || `{API_TOKEN}`}"
2248
- });
2249
-
2250
- let out = "";
2251
-
2252
- const stream = await client.chat.completions.create({
2253
- model: "${providerModelId ?? model.id}",
2254
- messages: ${messagesStr},
2255
- ${configStr}
2256
- stream: true,
2257
- });
2258
-
2259
- for await (const chunk of stream) {
2260
- if (chunk.choices && chunk.choices.length > 0) {
2261
- const newContent = chunk.choices[0].delta.content;
2262
- out += newContent;
2263
- console.log(newContent);
2264
- }
2265
- }`
1729
+ return inferenceSnippetLanguages.map((language) => {
1730
+ return CLIENTS[language].map((client) => {
1731
+ if (!hasTemplate(language, client, templateName)) {
1732
+ return;
2266
1733
  }
2267
- ];
2268
- } else {
2269
- return [
2270
- {
2271
- client: "huggingface.js",
2272
- content: `import { HfInference } from "@huggingface/inference";
2273
-
2274
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2275
-
2276
- const chatCompletion = await client.chatCompletion({
2277
- model: "${model.id}",
2278
- messages: ${messagesStr},
2279
- provider: "${provider}",
2280
- ${configStr}
2281
- });
2282
-
2283
- console.log(chatCompletion.choices[0].message);
2284
- `
2285
- },
2286
- {
2287
- client: "openai",
2288
- content: `import { OpenAI } from "openai";
2289
-
2290
- const client = new OpenAI({
2291
- baseURL: "${openAIbaseUrl2(provider)}",
2292
- apiKey: "${accessToken || `{API_TOKEN}`}"
2293
- });
2294
-
2295
- const chatCompletion = await client.chat.completions.create({
2296
- model: "${providerModelId ?? model.id}",
2297
- messages: ${messagesStr},
2298
- ${configStr}
2299
- });
2300
-
2301
- console.log(chatCompletion.choices[0].message);
2302
- `
1734
+ const template = loadTemplate(language, client, templateName);
1735
+ if (client === "huggingface_hub" && templateName.includes("basic")) {
1736
+ if (!(model.pipeline_tag && model.pipeline_tag in HF_PYTHON_METHODS)) {
1737
+ return;
1738
+ }
1739
+ params["methodName"] = HF_PYTHON_METHODS[model.pipeline_tag];
2303
1740
  }
2304
- ];
2305
- }
2306
- } else {
2307
- return snippetBasic3(model, accessToken, provider);
2308
- }
2309
- };
2310
- var snippetZeroShotClassification3 = (model, accessToken) => {
2311
- return [
2312
- {
2313
- client: "fetch",
2314
- content: `async function query(data) {
2315
- const response = await fetch(
2316
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2317
- {
2318
- headers: {
2319
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2320
- "Content-Type": "application/json",
2321
- },
2322
- method: "POST",
2323
- body: JSON.stringify(data),
2324
- }
2325
- );
2326
- const result = await response.json();
2327
- return result;
2328
- }
2329
-
2330
- query({"inputs": ${getModelInputSnippet3(
2331
- model
2332
- )}, "parameters": {"candidate_labels": ["refund", "legal", "faq"]}}).then((response) => {
2333
- console.log(JSON.stringify(response));
2334
- });`
2335
- }
2336
- ];
1741
+ if (client === "huggingface.js" && templateName.includes("basic")) {
1742
+ if (!(model.pipeline_tag && model.pipeline_tag in HF_JS_METHODS)) {
1743
+ return;
1744
+ }
1745
+ params["methodName"] = HF_JS_METHODS[model.pipeline_tag];
1746
+ }
1747
+ let snippet = template(params).trim();
1748
+ if (!snippet) {
1749
+ return;
1750
+ }
1751
+ if (client === "huggingface_hub") {
1752
+ const importSection = snippetImportPythonInferenceClient({ ...params });
1753
+ snippet = `${importSection}
1754
+
1755
+ ${snippet}`;
1756
+ } else if (client === "requests") {
1757
+ const importSection = snippetImportRequests({
1758
+ ...params,
1759
+ importBase64: snippet.includes("base64"),
1760
+ importJson: snippet.includes("json.")
1761
+ });
1762
+ snippet = `${importSection}
1763
+
1764
+ ${snippet}`;
1765
+ }
1766
+ return { language, client, content: snippet };
1767
+ }).filter((snippet) => snippet !== void 0);
1768
+ }).flat();
1769
+ };
2337
1770
  };
2338
- var snippetTextToImage2 = (model, accessToken, provider) => {
2339
- return [
2340
- {
2341
- client: "huggingface.js",
2342
- content: `import { HfInference } from "@huggingface/inference";
2343
-
2344
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2345
-
2346
- const image = await client.textToImage({
2347
- model: "${model.id}",
2348
- inputs: ${getModelInputSnippet3(model)},
2349
- parameters: { num_inference_steps: 5 },
2350
- provider: "${provider}",
2351
- });
2352
- /// Use the generated image (it's a Blob)
2353
- `
2354
- },
2355
- ...provider === "hf-inference" ? [
2356
- {
2357
- client: "fetch",
2358
- content: `async function query(data) {
2359
- const response = await fetch(
2360
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2361
- {
2362
- headers: {
2363
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2364
- "Content-Type": "application/json",
2365
- },
2366
- method: "POST",
2367
- body: JSON.stringify(data),
2368
- }
2369
- );
2370
- const result = await response.blob();
2371
- return result;
1771
+ var prepareDocumentQuestionAnsweringInput = (model) => {
1772
+ return JSON.parse(getModelInputSnippet(model));
1773
+ };
1774
+ var prepareImageToImageInput = (model) => {
1775
+ const data = JSON.parse(getModelInputSnippet(model));
1776
+ return { inputs: data.image, parameters: { prompt: data.prompt } };
1777
+ };
1778
+ var prepareConversationalInput = (model, opts) => {
1779
+ return {
1780
+ messages: opts?.messages ?? getModelInputSnippet(model),
1781
+ ...opts?.temperature ? { temperature: opts?.temperature } : void 0,
1782
+ max_tokens: opts?.max_tokens ?? 500,
1783
+ ...opts?.top_p ? { top_p: opts?.top_p } : void 0
1784
+ };
1785
+ };
1786
+ var snippets = {
1787
+ "audio-classification": snippetGenerator("basicAudio"),
1788
+ "audio-to-audio": snippetGenerator("basicAudio"),
1789
+ "automatic-speech-recognition": snippetGenerator("basicAudio"),
1790
+ "document-question-answering": snippetGenerator("documentQuestionAnswering", prepareDocumentQuestionAnsweringInput),
1791
+ "feature-extraction": snippetGenerator("basic"),
1792
+ "fill-mask": snippetGenerator("basic"),
1793
+ "image-classification": snippetGenerator("basicImage"),
1794
+ "image-segmentation": snippetGenerator("basicImage"),
1795
+ "image-text-to-text": snippetGenerator("conversational"),
1796
+ "image-to-image": snippetGenerator("imageToImage", prepareImageToImageInput),
1797
+ "image-to-text": snippetGenerator("basicImage"),
1798
+ "object-detection": snippetGenerator("basicImage"),
1799
+ "question-answering": snippetGenerator("basic"),
1800
+ "sentence-similarity": snippetGenerator("basic"),
1801
+ summarization: snippetGenerator("basic"),
1802
+ "tabular-classification": snippetGenerator("tabular"),
1803
+ "tabular-regression": snippetGenerator("tabular"),
1804
+ "table-question-answering": snippetGenerator("basic"),
1805
+ "text-classification": snippetGenerator("basic"),
1806
+ "text-generation": snippetGenerator("basic"),
1807
+ "text-to-audio": snippetGenerator("textToAudio"),
1808
+ "text-to-image": snippetGenerator("textToImage"),
1809
+ "text-to-speech": snippetGenerator("textToAudio"),
1810
+ "text-to-video": snippetGenerator("textToVideo"),
1811
+ "text2text-generation": snippetGenerator("basic"),
1812
+ "token-classification": snippetGenerator("basic"),
1813
+ translation: snippetGenerator("basic"),
1814
+ "zero-shot-classification": snippetGenerator("zeroShotClassification"),
1815
+ "zero-shot-image-classification": snippetGenerator("zeroShotImageClassification")
1816
+ };
1817
+ function getInferenceSnippets(model, accessToken, provider, providerModelId, opts) {
1818
+ return model.pipeline_tag && model.pipeline_tag in snippets ? snippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
2372
1819
  }
2373
- query({"inputs": ${getModelInputSnippet3(model)}}).then((response) => {
2374
- // Use image
2375
- });`
2376
- }
2377
- ] : []
2378
- ];
2379
- };
2380
- var snippetTextToVideo2 = (model, accessToken, provider) => {
2381
- return ["fal-ai", "replicate"].includes(provider) ? [
2382
- {
2383
- client: "huggingface.js",
2384
- content: `import { HfInference } from "@huggingface/inference";
2385
-
2386
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2387
-
2388
- const video = await client.textToVideo({
2389
- model: "${model.id}",
2390
- provider: "${provider}",
2391
- inputs: ${getModelInputSnippet3(model)},
2392
- parameters: { num_inference_steps: 5 },
2393
- });
2394
- // Use the generated video (it's a Blob)
2395
- `
2396
- }
2397
- ] : [];
2398
- };
2399
- var snippetTextToAudio2 = (model, accessToken, provider) => {
2400
- if (provider !== "hf-inference") {
2401
- return [];
2402
- }
2403
- const commonSnippet = `async function query(data) {
2404
- const response = await fetch(
2405
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2406
- {
2407
- headers: {
2408
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2409
- "Content-Type": "application/json",
2410
- },
2411
- method: "POST",
2412
- body: JSON.stringify(data),
2413
- }
2414
- );`;
2415
- if (model.library_name === "transformers") {
2416
- return [
2417
- {
2418
- client: "fetch",
2419
- content: commonSnippet + `
2420
- const result = await response.blob();
2421
- return result;
2422
- }
2423
- query({"inputs": ${getModelInputSnippet3(model)}}).then((response) => {
2424
- // Returns a byte object of the Audio wavform. Use it directly!
2425
- });`
2426
- }
2427
- ];
2428
- } else {
2429
- return [
2430
- {
2431
- client: "fetch",
2432
- content: commonSnippet + `
2433
- const result = await response.json();
2434
- return result;
2435
- }
2436
-
2437
- query({"inputs": ${getModelInputSnippet3(model)}}).then((response) => {
2438
- console.log(JSON.stringify(response));
2439
- });`
2440
- }
2441
- ];
1820
+ function formatBody(obj, format) {
1821
+ switch (format) {
1822
+ case "curl":
1823
+ return indentString(formatBody(obj, "json"));
1824
+ case "json":
1825
+ return JSON.stringify(obj, null, 4).split("\n").slice(1, -1).join("\n");
1826
+ case "python":
1827
+ return indentString(
1828
+ Object.entries(obj).map(([key, value]) => {
1829
+ const formattedValue = JSON.stringify(value, null, 4).replace(/"/g, '"');
1830
+ return `${key}=${formattedValue},`;
1831
+ }).join("\n")
1832
+ );
1833
+ case "ts":
1834
+ return formatTsObject(obj).split("\n").slice(1, -1).join("\n");
1835
+ default:
1836
+ throw new Error(`Unsupported format: ${format}`);
2442
1837
  }
2443
- };
2444
- var snippetAutomaticSpeechRecognition = (model, accessToken, provider) => {
2445
- return [
2446
- {
2447
- client: "huggingface.js",
2448
- content: `import { HfInference } from "@huggingface/inference";
2449
-
2450
- const client = new HfInference("${accessToken || `{API_TOKEN}`}");
2451
-
2452
- const data = fs.readFileSync(${getModelInputSnippet3(model)});
2453
-
2454
- const output = await client.automaticSpeechRecognition({
2455
- data,
2456
- model: "${model.id}",
2457
- provider: "${provider}",
2458
- });
2459
-
2460
- console.log(output);
2461
- `
2462
- },
2463
- ...provider === "hf-inference" ? snippetFile3(model, accessToken, provider) : []
2464
- ];
2465
- };
2466
- var snippetFile3 = (model, accessToken, provider) => {
2467
- if (provider !== "hf-inference") {
2468
- return [];
2469
- }
2470
- return [
2471
- {
2472
- client: "fetch",
2473
- content: `async function query(filename) {
2474
- const data = fs.readFileSync(filename);
2475
- const response = await fetch(
2476
- "https://router.huggingface.co/hf-inference/models/${model.id}",
2477
- {
2478
- headers: {
2479
- Authorization: "Bearer ${accessToken || `{API_TOKEN}`}",
2480
- "Content-Type": "application/json",
2481
- },
2482
- method: "POST",
2483
- body: data,
2484
- }
2485
- );
2486
- const result = await response.json();
2487
- return result;
2488
1838
  }
2489
-
2490
- query(${getModelInputSnippet3(model)}).then((response) => {
2491
- console.log(JSON.stringify(response));
2492
- });`
2493
- }
2494
- ];
2495
- };
2496
- var jsSnippets = {
2497
- // Same order as in tasks/src/pipelines.ts
2498
- "text-classification": snippetBasic3,
2499
- "token-classification": snippetBasic3,
2500
- "table-question-answering": snippetBasic3,
2501
- "question-answering": snippetBasic3,
2502
- "zero-shot-classification": snippetZeroShotClassification3,
2503
- translation: snippetBasic3,
2504
- summarization: snippetBasic3,
2505
- "feature-extraction": snippetBasic3,
2506
- "text-generation": snippetTextGeneration2,
2507
- "image-text-to-text": snippetTextGeneration2,
2508
- "text2text-generation": snippetBasic3,
2509
- "fill-mask": snippetBasic3,
2510
- "sentence-similarity": snippetBasic3,
2511
- "automatic-speech-recognition": snippetAutomaticSpeechRecognition,
2512
- "text-to-image": snippetTextToImage2,
2513
- "text-to-video": snippetTextToVideo2,
2514
- "text-to-speech": snippetTextToAudio2,
2515
- "text-to-audio": snippetTextToAudio2,
2516
- "audio-to-audio": snippetFile3,
2517
- "audio-classification": snippetFile3,
2518
- "image-classification": snippetFile3,
2519
- "image-to-text": snippetFile3,
2520
- "object-detection": snippetFile3,
2521
- "image-segmentation": snippetFile3
2522
- };
2523
- function getJsInferenceSnippet(model, accessToken, provider, providerModelId, opts) {
2524
- return model.pipeline_tag && model.pipeline_tag in jsSnippets ? jsSnippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? [] : [];
1839
+ function formatTsObject(obj, depth) {
1840
+ depth = depth ?? 0;
1841
+ if (typeof obj !== "object" || obj === null) {
1842
+ return JSON.stringify(obj);
1843
+ }
1844
+ if (Array.isArray(obj)) {
1845
+ const items = obj.map((item) => {
1846
+ const formatted = formatTsObject(item, depth + 1);
1847
+ return `${" ".repeat(4 * (depth + 1))}${formatted},`;
1848
+ }).join("\n");
1849
+ return `[
1850
+ ${items}
1851
+ ${" ".repeat(4 * depth)}]`;
1852
+ }
1853
+ const entries = Object.entries(obj);
1854
+ const lines = entries.map(([key, value]) => {
1855
+ const formattedValue = formatTsObject(value, depth + 1);
1856
+ const keyStr = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : `"${key}"`;
1857
+ return `${" ".repeat(4 * (depth + 1))}${keyStr}: ${formattedValue},`;
1858
+ }).join("\n");
1859
+ return `{
1860
+ ${lines}
1861
+ ${" ".repeat(4 * depth)}}`;
1862
+ }
1863
+ function indentString(str) {
1864
+ return str.split("\n").map((line) => " ".repeat(4) + line).join("\n");
1865
+ }
1866
+ function removeSuffix(str, suffix) {
1867
+ return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
2525
1868
  }
2526
1869
  export {
2527
1870
  HfInference,
2528
- HfInferenceEndpoint,
2529
1871
  INFERENCE_PROVIDERS,
1872
+ InferenceClient,
1873
+ InferenceClientEndpoint,
2530
1874
  InferenceOutputError,
2531
1875
  audioClassification,
2532
1876
  audioToAudio,