@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
@@ -87,6 +87,8 @@ interface AudioToAudioOutput {
87
87
  label: string;
88
88
  }
89
89
 
90
+ export const EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS = ["feature-extraction", "sentence-similarity"] as const;
91
+
90
92
  export class HFInferenceTask extends TaskProviderHelper {
91
93
  constructor() {
92
94
  super("hf-inference", `${HF_ROUTER_URL}/hf-inference`);
@@ -385,13 +387,13 @@ export class HFInferenceQuestionAnsweringTask extends HFInferenceTask implements
385
387
  typeof elem.end === "number" &&
386
388
  typeof elem.score === "number" &&
387
389
  typeof elem.start === "number"
388
- )
390
+ )
389
391
  : typeof response === "object" &&
390
- !!response &&
391
- typeof response.answer === "string" &&
392
- typeof response.end === "number" &&
393
- typeof response.score === "number" &&
394
- typeof response.start === "number"
392
+ !!response &&
393
+ typeof response.answer === "string" &&
394
+ typeof response.end === "number" &&
395
+ typeof response.score === "number" &&
396
+ typeof response.start === "number"
395
397
  ) {
396
398
  return Array.isArray(response) ? response[0] : response;
397
399
  }
@@ -11,6 +11,10 @@ import type { ChatCompletionInputMessage, GenerationParameters } from "@huggingf
11
11
  import { makeRequestOptionsFromResolvedModel } from "../lib/makeRequestOptions";
12
12
  import type { InferenceProvider, InferenceTask, RequestArgs } from "../types";
13
13
  import { templates } from "./templates.exported";
14
+ import type { InferenceProviderModelMapping } from "../lib/getInferenceProviderMapping";
15
+ import { getProviderHelper } from "../lib/getProviderHelper";
16
+
17
+ export type InferenceSnippetOptions = { streaming?: boolean; billTo?: string } & Record<string, unknown>;
14
18
 
15
19
  const PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"] as const;
16
20
  const JS_CLIENTS = ["fetch", "huggingface.js", "openai"] as const;
@@ -35,6 +39,7 @@ interface TemplateParams {
35
39
  model?: ModelDataMinimal;
36
40
  provider?: InferenceProvider;
37
41
  providerModelId?: string;
42
+ billTo?: string;
38
43
  methodName?: string; // specific to snippetBasic
39
44
  importBase64?: boolean; // specific to snippetImportRequests
40
45
  importJson?: boolean; // specific to snippetImportRequests
@@ -116,9 +121,10 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
116
121
  model: ModelDataMinimal,
117
122
  accessToken: string,
118
123
  provider: InferenceProvider,
119
- providerModelId?: string,
120
- opts?: Record<string, unknown>
124
+ inferenceProviderMapping?: InferenceProviderModelMapping,
125
+ opts?: InferenceSnippetOptions
121
126
  ): InferenceSnippet[] => {
127
+ const providerModelId = inferenceProviderMapping?.providerId ?? model.id;
122
128
  /// Hacky: hard-code conversational templates here
123
129
  let task = model.pipeline_tag as InferenceTask;
124
130
  if (
@@ -130,17 +136,27 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
130
136
  inputPreparationFn = prepareConversationalInput;
131
137
  task = "conversational";
132
138
  }
139
+ let providerHelper: ReturnType<typeof getProviderHelper>;
140
+ try {
141
+ providerHelper = getProviderHelper(provider, task);
142
+ } catch (e) {
143
+ console.error(`Failed to get provider helper for ${provider} (${task})`, e);
144
+ return [];
145
+ }
133
146
  /// Prepare inputs + make request
134
147
  const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: getModelInputSnippet(model) };
135
148
  const request = makeRequestOptionsFromResolvedModel(
136
- providerModelId ?? model.id,
149
+ providerModelId,
150
+ providerHelper,
137
151
  {
138
- accessToken: accessToken,
139
- provider: provider,
152
+ accessToken,
153
+ provider,
140
154
  ...inputs,
141
155
  } as RequestArgs,
156
+ inferenceProviderMapping,
142
157
  {
143
- task: task,
158
+ task,
159
+ billTo: opts?.billTo,
144
160
  }
145
161
  );
146
162
 
@@ -179,6 +195,7 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
179
195
  model,
180
196
  provider,
181
197
  providerModelId: providerModelId ?? model.id,
198
+ billTo: opts?.billTo,
182
199
  };
183
200
 
184
201
  /// Iterate over clients => check if a snippet exists => generate
@@ -266,8 +283,8 @@ const snippets: Partial<
266
283
  model: ModelDataMinimal,
267
284
  accessToken: string,
268
285
  provider: InferenceProvider,
269
- providerModelId?: string,
270
- opts?: Record<string, unknown>
286
+ inferenceProviderMapping?: InferenceProviderModelMapping,
287
+ opts?: InferenceSnippetOptions
271
288
  ) => InferenceSnippet[]
272
289
  >
273
290
  > = {
@@ -306,11 +323,11 @@ export function getInferenceSnippets(
306
323
  model: ModelDataMinimal,
307
324
  accessToken: string,
308
325
  provider: InferenceProvider,
309
- providerModelId?: string,
326
+ inferenceProviderMapping?: InferenceProviderModelMapping,
310
327
  opts?: Record<string, unknown>
311
328
  ): InferenceSnippet[] {
312
329
  return model.pipeline_tag && model.pipeline_tag in snippets
313
- ? snippets[model.pipeline_tag]?.(model, accessToken, provider, providerModelId, opts) ?? []
330
+ ? snippets[model.pipeline_tag]?.(model, accessToken, provider, inferenceProviderMapping, opts) ?? []
314
331
  : [];
315
332
  }
316
333
 
@@ -1 +1 @@
1
- export { getInferenceSnippets } from "./getInferenceSnippets.js";
1
+ export { getInferenceSnippets, type InferenceSnippetOptions } from "./getInferenceSnippets.js";
@@ -2,25 +2,25 @@
2
2
  export const templates: Record<string, Record<string, Record<string, string>>> = {
3
3
  "js": {
4
4
  "fetch": {
5
- "basic": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
6
- "basicAudio": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"audio/flac\"\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
7
- "basicImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\"\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
8
- "textToAudio": "{% if model.library_name == \"transformers\" %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n // Returns a byte object of the Audio wavform. Use it directly!\n});\n{% else %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});\n{% endif %} ",
9
- "textToImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n\treturn result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});",
10
- "zeroShotClassification": "async function query(data) {\n const response = await fetch(\n\t\t\"{{ fullUrl }}\",\n {\n headers: {\n\t\t\t\tAuthorization: \"{{ 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});"
5
+ "basic": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
6
+ "basicAudio": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"audio/flac\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
7
+ "basicImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
8
+ "textToAudio": "{% if model.library_name == \"transformers\" %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n // Returns a byte object of the Audio wavform. Use it directly!\n});\n{% else %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});\n{% endif %} ",
9
+ "textToImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n\treturn result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});",
10
+ "zeroShotClassification": "async function query(data) {\n const response = await fetch(\n\t\t\"{{ fullUrl }}\",\n {\n headers: {\n\t\t\t\tAuthorization: \"{{ 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});"
11
11
  },
12
12
  "huggingface.js": {
13
- "basic": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst output = await client.{{ methodName }}({\n\tmodel: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n\tprovider: \"{{ provider }}\",\n});\n\nconsole.log(output);",
14
- "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\tdata,\n\tmodel: \"{{ model.id }}\",\n\tprovider: \"{{ provider }}\",\n});\n\nconsole.log(output);",
15
- "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\tdata,\n\tmodel: \"{{ model.id }}\",\n\tprovider: \"{{ provider }}\",\n});\n\nconsole.log(output);",
16
- "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);",
17
- "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\tif (chunk.choices && chunk.choices.length > 0) {\n\t\tconst newContent = chunk.choices[0].delta.content;\n\t\tout += newContent;\n\t\tconsole.log(newContent);\n\t} \n}",
18
- "textToImage": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst image = await client.textToImage({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n\tparameters: { num_inference_steps: 5 },\n});\n/// Use the generated image (it's a Blob)",
19
- "textToVideo": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst image = await client.textToVideo({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n});\n// Use the generated video (it's a Blob)"
13
+ "basic": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst output = await client.{{ methodName }}({\n\tmodel: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n\tprovider: \"{{ provider }}\",\n}{% if billTo %}, {\n\tbillTo: \"{{ billTo }}\",\n}{% endif %});\n\nconsole.log(output);",
14
+ "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\tdata,\n\tmodel: \"{{ model.id }}\",\n\tprovider: \"{{ provider }}\",\n}{% if billTo %}, {\n\tbillTo: \"{{ billTo }}\",\n}{% endif %});\n\nconsole.log(output);",
15
+ "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\tdata,\n\tmodel: \"{{ model.id }}\",\n\tprovider: \"{{ provider }}\",\n}{% if billTo %}, {\n\tbillTo: \"{{ billTo }}\",\n}{% endif %});\n\nconsole.log(output);",
16
+ "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);",
17
+ "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\tif (chunk.choices && chunk.choices.length > 0) {\n\t\tconst newContent = chunk.choices[0].delta.content;\n\t\tout += newContent;\n\t\tconsole.log(newContent);\n\t} \n}",
18
+ "textToImage": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst image = await client.textToImage({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n\tparameters: { num_inference_steps: 5 },\n}{% if billTo %}, {\n billTo: \"{{ billTo }}\",\n}{% endif %});\n/// Use the generated image (it's a Blob)",
19
+ "textToVideo": "import { InferenceClient } from \"@huggingface/inference\";\n\nconst client = new InferenceClient(\"{{ accessToken }}\");\n\nconst image = await client.textToVideo({\n provider: \"{{ provider }}\",\n model: \"{{ model.id }}\",\n\tinputs: {{ inputs.asObj.inputs }},\n}{% if billTo %}, {\n billTo: \"{{ billTo }}\",\n}{% endif %});\n// Use the generated video (it's a Blob)"
20
20
  },
21
21
  "openai": {
22
- "conversational": "import { OpenAI } from \"openai\";\n\nconst client = new OpenAI({\n\tbaseURL: \"{{ baseUrl }}\",\n\tapiKey: \"{{ accessToken }}\",\n});\n\nconst chatCompletion = await client.chat.completions.create({\n\tmodel: \"{{ providerModelId }}\",\n{{ inputs.asTsString }}\n});\n\nconsole.log(chatCompletion.choices[0].message);",
23
- "conversationalStream": "import { OpenAI } from \"openai\";\n\nconst client = new OpenAI({\n\tbaseURL: \"{{ baseUrl }}\",\n\tapiKey: \"{{ 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}"
22
+ "conversational": "import { OpenAI } from \"openai\";\n\nconst client = new OpenAI({\n\tbaseURL: \"{{ baseUrl }}\",\n\tapiKey: \"{{ accessToken }}\",\n{% if billTo %}\n\tdefaultHeaders: {\n\t\t\"X-HF-Bill-To\": \"{{ billTo }}\" \n\t}\n{% endif %}\n});\n\nconst chatCompletion = await client.chat.completions.create({\n\tmodel: \"{{ providerModelId }}\",\n{{ inputs.asTsString }}\n});\n\nconsole.log(chatCompletion.choices[0].message);",
23
+ "conversationalStream": "import { OpenAI } from \"openai\";\n\nconst client = new OpenAI({\n\tbaseURL: \"{{ baseUrl }}\",\n\tapiKey: \"{{ accessToken }}\",\n{% if billTo %}\n defaultHeaders: {\n\t\t\"X-HF-Bill-To\": \"{{ billTo }}\" \n\t}\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}"
24
24
  }
25
25
  },
26
26
  "python": {
@@ -35,13 +35,13 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
35
35
  "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=\"\") ",
36
36
  "documentQuestionAnswering": "output = client.document_question_answering(\n \"{{ inputs.asObj.image }}\",\n question=\"{{ inputs.asObj.question }}\",\n model=\"{{ model.id }}\",\n) ",
37
37
  "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) ",
38
- "importInferenceClient": "from huggingface_hub import InferenceClient\n\nclient = InferenceClient(\n provider=\"{{ provider }}\",\n api_key=\"{{ accessToken }}\",\n)",
38
+ "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)",
39
39
  "textToImage": "# output is a PIL.Image object\nimage = client.text_to_image(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) ",
40
40
  "textToVideo": "video = client.text_to_video(\n {{ inputs.asObj.inputs }},\n model=\"{{ model.id }}\",\n) "
41
41
  },
42
42
  "openai": {
43
- "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) ",
44
- "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=\"\")"
43
+ "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) ",
44
+ "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=\"\")"
45
45
  },
46
46
  "requests": {
47
47
  "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}) ",
@@ -51,7 +51,7 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
51
51
  "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=\"\")",
52
52
  "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}) ",
53
53
  "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)) ",
54
- "importRequests": "{% if importBase64 %}\nimport base64\n{% endif %}\n{% if importJson %}\nimport json\n{% endif %}\nimport requests\n\nAPI_URL = \"{{ fullUrl }}\"\nheaders = {\"Authorization\": \"{{ authorizationHeader }}\"}",
54
+ "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}",
55
55
  "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}) ",
56
56
  "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 %} ",
57
57
  "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 %}",
@@ -61,12 +61,12 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
61
61
  },
62
62
  "sh": {
63
63
  "curl": {
64
- "basic": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n -d '{\n{{ providerInputs.asCurlString }}\n }'",
65
- "basicAudio": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: audio/flac' \\\n --data-binary @{{ providerInputs.asObj.inputs }}",
66
- "basicImage": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: image/jpeg' \\\n --data-binary @{{ providerInputs.asObj.inputs }}",
67
- "conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": false\n }'",
68
- "conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": true\n }'",
69
- "zeroShotClassification": "curl {{ fullUrl }} \\\n -X POST \\\n -d '{\"inputs\": {{ providerInputs.asObj.inputs }}, \"parameters\": {\"candidate_labels\": [\"refund\", \"legal\", \"faq\"]}}' \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: {{ authorizationHeader }}'"
64
+ "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 }'",
65
+ "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 }}",
66
+ "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 }}",
67
+ "conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": false\n }'",
68
+ "conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": true\n }'",
69
+ "zeroShotClassification": "curl {{ fullUrl }} \\\n -X POST \\\n -d '{\"inputs\": {{ providerInputs.asObj.inputs }}, \"parameters\": {\"candidate_labels\": [\"refund\", \"legal\", \"faq\"]}}' \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: {{ authorizationHeader }}'\n{% if billTo %} \\\n -H 'X-HF-Bill-To: {{ billTo }}'\n{% endif %}"
70
70
  }
71
71
  }
72
72
  } as const;
@@ -17,7 +17,7 @@ export async function audioClassification(
17
17
  ): Promise<AudioClassificationOutput> {
18
18
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "audio-classification");
19
19
  const payload = preparePayload(args);
20
- const { data: res } = await innerRequest<AudioClassificationOutput>(payload, {
20
+ const { data: res } = await innerRequest<AudioClassificationOutput>(payload, providerHelper, {
21
21
  ...options,
22
22
  task: "audio-classification",
23
23
  });
@@ -38,7 +38,7 @@ export interface AudioToAudioOutput {
38
38
  export async function audioToAudio(args: AudioToAudioArgs, options?: Options): Promise<AudioToAudioOutput[]> {
39
39
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "audio-to-audio");
40
40
  const payload = preparePayload(args);
41
- const { data: res } = await innerRequest<AudioToAudioOutput>(payload, {
41
+ const { data: res } = await innerRequest<AudioToAudioOutput>(payload, providerHelper, {
42
42
  ...options,
43
43
  task: "audio-to-audio",
44
44
  });
@@ -20,7 +20,7 @@ export async function automaticSpeechRecognition(
20
20
  ): Promise<AutomaticSpeechRecognitionOutput> {
21
21
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "automatic-speech-recognition");
22
22
  const payload = await buildPayload(args);
23
- const { data: res } = await innerRequest<AutomaticSpeechRecognitionOutput>(payload, {
23
+ const { data: res } = await innerRequest<AutomaticSpeechRecognitionOutput>(payload, providerHelper, {
24
24
  ...options,
25
25
  task: "automatic-speech-recognition",
26
26
  });
@@ -14,7 +14,7 @@ interface OutputUrlTextToSpeechGeneration {
14
14
  export async function textToSpeech(args: TextToSpeechArgs, options?: Options): Promise<Blob> {
15
15
  const provider = args.provider ?? "hf-inference";
16
16
  const providerHelper = getProviderHelper(provider, "text-to-speech");
17
- const { data: res } = await innerRequest<Blob | OutputUrlTextToSpeechGeneration>(args, {
17
+ const { data: res } = await innerRequest<Blob | OutputUrlTextToSpeechGeneration>(args, providerHelper, {
18
18
  ...options,
19
19
  task: "text-to-speech",
20
20
  });
@@ -1,3 +1,4 @@
1
+ import { getProviderHelper } from "../../lib/getProviderHelper";
1
2
  import type { InferenceTask, Options, RequestArgs } from "../../types";
2
3
  import { innerRequest } from "../../utils/request";
3
4
 
@@ -15,6 +16,7 @@ export async function request<T>(
15
16
  console.warn(
16
17
  "The request method is deprecated and will be removed in a future version of huggingface.js. Use specific task functions instead."
17
18
  );
18
- const result = await innerRequest<T>(args, options);
19
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", options?.task);
20
+ const result = await innerRequest<T>(args, providerHelper, options);
19
21
  return result.data;
20
22
  }
@@ -1,5 +1,7 @@
1
+ import { getProviderHelper } from "../../lib/getProviderHelper";
1
2
  import type { InferenceTask, Options, RequestArgs } from "../../types";
2
3
  import { innerStreamingRequest } from "../../utils/request";
4
+
3
5
  /**
4
6
  * Primitive to make custom inference calls that expect server-sent events, and returns the response through a generator
5
7
  * @deprecated Use specific task functions instead. This function will be removed in a future version.
@@ -14,5 +16,6 @@ export async function* streamingRequest<T>(
14
16
  console.warn(
15
17
  "The streamingRequest method is deprecated and will be removed in a future version of huggingface.js. Use specific task functions instead."
16
18
  );
17
- yield* innerStreamingRequest(args, options);
19
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", options?.task);
20
+ yield* innerStreamingRequest(args, providerHelper, options);
18
21
  }
@@ -16,7 +16,7 @@ export async function imageClassification(
16
16
  ): Promise<ImageClassificationOutput> {
17
17
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-classification");
18
18
  const payload = preparePayload(args);
19
- const { data: res } = await innerRequest<ImageClassificationOutput>(payload, {
19
+ const { data: res } = await innerRequest<ImageClassificationOutput>(payload, providerHelper, {
20
20
  ...options,
21
21
  task: "image-classification",
22
22
  });
@@ -16,7 +16,7 @@ export async function imageSegmentation(
16
16
  ): Promise<ImageSegmentationOutput> {
17
17
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-segmentation");
18
18
  const payload = preparePayload(args);
19
- const { data: res } = await innerRequest<ImageSegmentationOutput>(payload, {
19
+ const { data: res } = await innerRequest<ImageSegmentationOutput>(payload, providerHelper, {
20
20
  ...options,
21
21
  task: "image-segmentation",
22
22
  });
@@ -27,7 +27,7 @@ export async function imageToImage(args: ImageToImageArgs, options?: Options): P
27
27
  ),
28
28
  };
29
29
  }
30
- const { data: res } = await innerRequest<Blob>(reqArgs, {
30
+ const { data: res } = await innerRequest<Blob>(reqArgs, providerHelper, {
31
31
  ...options,
32
32
  task: "image-to-image",
33
33
  });
@@ -12,7 +12,7 @@ export type ImageToTextArgs = BaseArgs & (ImageToTextInput | LegacyImageInput);
12
12
  export async function imageToText(args: ImageToTextArgs, options?: Options): Promise<ImageToTextOutput> {
13
13
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "image-to-text");
14
14
  const payload = preparePayload(args);
15
- const { data: res } = await innerRequest<[ImageToTextOutput]>(payload, {
15
+ const { data: res } = await innerRequest<[ImageToTextOutput]>(payload, providerHelper, {
16
16
  ...options,
17
17
  task: "image-to-text",
18
18
  });
@@ -13,7 +13,7 @@ export type ObjectDetectionArgs = BaseArgs & (ObjectDetectionInput | LegacyImage
13
13
  export async function objectDetection(args: ObjectDetectionArgs, options?: Options): Promise<ObjectDetectionOutput> {
14
14
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "object-detection");
15
15
  const payload = preparePayload(args);
16
- const { data: res } = await innerRequest<ObjectDetectionOutput>(payload, {
16
+ const { data: res } = await innerRequest<ObjectDetectionOutput>(payload, providerHelper, {
17
17
  ...options,
18
18
  task: "object-detection",
19
19
  });
@@ -25,11 +25,11 @@ export async function textToImage(
25
25
  export async function textToImage(args: TextToImageArgs, options?: TextToImageOptions): Promise<Blob | string> {
26
26
  const provider = args.provider ?? "hf-inference";
27
27
  const providerHelper = getProviderHelper(provider, "text-to-image");
28
- const { data: res } = await innerRequest<Record<string, unknown>>(args, {
28
+ const { data: res } = await innerRequest<Record<string, unknown>>(args, providerHelper, {
29
29
  ...options,
30
30
  task: "text-to-image",
31
31
  });
32
32
 
33
- const { url, info } = await makeRequestOptions(args, { ...options, task: "text-to-image" });
33
+ const { url, info } = await makeRequestOptions(args, providerHelper, { ...options, task: "text-to-image" });
34
34
  return providerHelper.getResponse(res, url, info.headers as Record<string, string>, options?.outputType);
35
35
  }
@@ -14,10 +14,14 @@ export type TextToVideoOutput = Blob;
14
14
  export async function textToVideo(args: TextToVideoArgs, options?: Options): Promise<TextToVideoOutput> {
15
15
  const provider = args.provider ?? "hf-inference";
16
16
  const providerHelper = getProviderHelper(provider, "text-to-video");
17
- const { data: response } = await innerRequest<FalAiQueueOutput | ReplicateOutput | NovitaOutput>(args, {
18
- ...options,
19
- task: "text-to-video",
20
- });
21
- const { url, info } = await makeRequestOptions(args, { ...options, task: "text-to-video" });
17
+ const { data: response } = await innerRequest<FalAiQueueOutput | ReplicateOutput | NovitaOutput>(
18
+ args,
19
+ providerHelper,
20
+ {
21
+ ...options,
22
+ task: "text-to-video",
23
+ }
24
+ );
25
+ const { url, info } = await makeRequestOptions(args, providerHelper, { ...options, task: "text-to-video" });
22
26
  return providerHelper.getResponse(response, url, info.headers as Record<string, string>);
23
27
  }
@@ -46,7 +46,7 @@ export async function zeroShotImageClassification(
46
46
  ): Promise<ZeroShotImageClassificationOutput> {
47
47
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "zero-shot-image-classification");
48
48
  const payload = await preparePayload(args);
49
- const { data: res } = await innerRequest<ZeroShotImageClassificationOutput>(payload, {
49
+ const { data: res } = await innerRequest<ZeroShotImageClassificationOutput>(payload, providerHelper, {
50
50
  ...options,
51
51
  task: "zero-shot-image-classification",
52
52
  });
@@ -30,6 +30,7 @@ export async function documentQuestionAnswering(
30
30
  } as RequestArgs;
31
31
  const { data: res } = await innerRequest<DocumentQuestionAnsweringOutput | DocumentQuestionAnsweringOutput[number]>(
32
32
  reqArgs,
33
+ providerHelper,
33
34
  {
34
35
  ...options,
35
36
  task: "document-question-answering",
@@ -29,7 +29,7 @@ export async function visualQuestionAnswering(
29
29
  },
30
30
  } as RequestArgs;
31
31
 
32
- const { data: res } = await innerRequest<VisualQuestionAnsweringOutput>(reqArgs, {
32
+ const { data: res } = await innerRequest<VisualQuestionAnsweringOutput>(reqArgs, providerHelper, {
33
33
  ...options,
34
34
  task: "visual-question-answering",
35
35
  });
@@ -11,7 +11,7 @@ export async function chatCompletion(
11
11
  options?: Options
12
12
  ): Promise<ChatCompletionOutput> {
13
13
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "conversational");
14
- const { data: response } = await innerRequest<ChatCompletionOutput>(args, {
14
+ const { data: response } = await innerRequest<ChatCompletionOutput>(args, providerHelper, {
15
15
  ...options,
16
16
  task: "conversational",
17
17
  });
@@ -1,4 +1,5 @@
1
1
  import type { ChatCompletionInput, ChatCompletionStreamOutput } from "@huggingface/tasks";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
2
3
  import type { BaseArgs, Options } from "../../types";
3
4
  import { innerStreamingRequest } from "../../utils/request";
4
5
 
@@ -9,7 +10,8 @@ export async function* chatCompletionStream(
9
10
  args: BaseArgs & ChatCompletionInput,
10
11
  options?: Options
11
12
  ): AsyncGenerator<ChatCompletionStreamOutput> {
12
- yield* innerStreamingRequest<ChatCompletionStreamOutput>(args, {
13
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "conversational");
14
+ yield* innerStreamingRequest<ChatCompletionStreamOutput>(args, providerHelper, {
13
15
  ...options,
14
16
  task: "conversational",
15
17
  });
@@ -18,7 +18,7 @@ export async function featureExtraction(
18
18
  options?: Options
19
19
  ): Promise<FeatureExtractionOutput> {
20
20
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "feature-extraction");
21
- const { data: res } = await innerRequest<FeatureExtractionOutput>(args, {
21
+ const { data: res } = await innerRequest<FeatureExtractionOutput>(args, providerHelper, {
22
22
  ...options,
23
23
  task: "feature-extraction",
24
24
  });
@@ -10,7 +10,7 @@ export type FillMaskArgs = BaseArgs & FillMaskInput;
10
10
  */
11
11
  export async function fillMask(args: FillMaskArgs, options?: Options): Promise<FillMaskOutput> {
12
12
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "fill-mask");
13
- const { data: res } = await innerRequest<FillMaskOutput>(args, {
13
+ const { data: res } = await innerRequest<FillMaskOutput>(args, providerHelper, {
14
14
  ...options,
15
15
  task: "fill-mask",
16
16
  });
@@ -13,9 +13,13 @@ export async function questionAnswering(
13
13
  options?: Options
14
14
  ): Promise<QuestionAnsweringOutput[number]> {
15
15
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "question-answering");
16
- const { data: res } = await innerRequest<QuestionAnsweringOutput | QuestionAnsweringOutput[number]>(args, {
17
- ...options,
18
- task: "question-answering",
19
- });
16
+ const { data: res } = await innerRequest<QuestionAnsweringOutput | QuestionAnsweringOutput[number]>(
17
+ args,
18
+ providerHelper,
19
+ {
20
+ ...options,
21
+ task: "question-answering",
22
+ }
23
+ );
20
24
  return providerHelper.getResponse(res);
21
25
  }
@@ -13,7 +13,7 @@ export async function sentenceSimilarity(
13
13
  options?: Options
14
14
  ): Promise<SentenceSimilarityOutput> {
15
15
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "sentence-similarity");
16
- const { data: res } = await innerRequest<SentenceSimilarityOutput>(args, {
16
+ const { data: res } = await innerRequest<SentenceSimilarityOutput>(args, providerHelper, {
17
17
  ...options,
18
18
  task: "sentence-similarity",
19
19
  });
@@ -10,7 +10,7 @@ export type SummarizationArgs = BaseArgs & SummarizationInput;
10
10
  */
11
11
  export async function summarization(args: SummarizationArgs, options?: Options): Promise<SummarizationOutput> {
12
12
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "summarization");
13
- const { data: res } = await innerRequest<SummarizationOutput[]>(args, {
13
+ const { data: res } = await innerRequest<SummarizationOutput[]>(args, providerHelper, {
14
14
  ...options,
15
15
  task: "summarization",
16
16
  });
@@ -13,9 +13,13 @@ export async function tableQuestionAnswering(
13
13
  options?: Options
14
14
  ): Promise<TableQuestionAnsweringOutput[number]> {
15
15
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "table-question-answering");
16
- const { data: res } = await innerRequest<TableQuestionAnsweringOutput | TableQuestionAnsweringOutput[number]>(args, {
17
- ...options,
18
- task: "table-question-answering",
19
- });
16
+ const { data: res } = await innerRequest<TableQuestionAnsweringOutput | TableQuestionAnsweringOutput[number]>(
17
+ args,
18
+ providerHelper,
19
+ {
20
+ ...options,
21
+ task: "table-question-answering",
22
+ }
23
+ );
20
24
  return providerHelper.getResponse(res);
21
25
  }
@@ -13,7 +13,7 @@ export async function textClassification(
13
13
  options?: Options
14
14
  ): Promise<TextClassificationOutput> {
15
15
  const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "text-classification");
16
- const { data: res } = await innerRequest<TextClassificationOutput>(args, {
16
+ const { data: res } = await innerRequest<TextClassificationOutput>(args, providerHelper, {
17
17
  ...options,
18
18
  task: "text-classification",
19
19
  });
@@ -13,11 +13,10 @@ export async function textGeneration(
13
13
  args: BaseArgs & TextGenerationInput,
14
14
  options?: Options
15
15
  ): Promise<TextGenerationOutput> {
16
- const provider = args.provider ?? "hf-inference";
17
- const providerHelper = getProviderHelper(provider, "text-generation");
16
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "text-generation");
18
17
  const { data: response } = await innerRequest<
19
18
  HyperbolicTextCompletionOutput | TextGenerationOutput | TextGenerationOutput[]
20
- >(args, {
19
+ >(args, providerHelper, {
21
20
  ...options,
22
21
  task: "text-generation",
23
22
  });
@@ -1,4 +1,5 @@
1
1
  import type { TextGenerationInput } from "@huggingface/tasks";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
2
3
  import type { BaseArgs, Options } from "../../types";
3
4
  import { innerStreamingRequest } from "../../utils/request";
4
5
 
@@ -89,7 +90,8 @@ export async function* textGenerationStream(
89
90
  args: BaseArgs & TextGenerationInput,
90
91
  options?: Options
91
92
  ): AsyncGenerator<TextGenerationStreamOutput> {
92
- yield* innerStreamingRequest<TextGenerationStreamOutput>(args, {
93
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "text-generation");
94
+ yield* innerStreamingRequest<TextGenerationStreamOutput>(args, providerHelper, {
93
95
  ...options,
94
96
  task: "text-generation",
95
97
  });