@huggingface/inference 3.6.2 → 3.7.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 (140) hide show
  1. package/README.md +0 -25
  2. package/dist/index.cjs +1232 -898
  3. package/dist/index.js +1234 -900
  4. package/dist/src/config.d.ts +1 -0
  5. package/dist/src/config.d.ts.map +1 -1
  6. package/dist/src/lib/getProviderHelper.d.ts +37 -0
  7. package/dist/src/lib/getProviderHelper.d.ts.map +1 -0
  8. package/dist/src/lib/makeRequestOptions.d.ts +0 -2
  9. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  10. package/dist/src/providers/black-forest-labs.d.ts +14 -18
  11. package/dist/src/providers/black-forest-labs.d.ts.map +1 -1
  12. package/dist/src/providers/cerebras.d.ts +4 -2
  13. package/dist/src/providers/cerebras.d.ts.map +1 -1
  14. package/dist/src/providers/cohere.d.ts +5 -2
  15. package/dist/src/providers/cohere.d.ts.map +1 -1
  16. package/dist/src/providers/fal-ai.d.ts +50 -3
  17. package/dist/src/providers/fal-ai.d.ts.map +1 -1
  18. package/dist/src/providers/fireworks-ai.d.ts +5 -2
  19. package/dist/src/providers/fireworks-ai.d.ts.map +1 -1
  20. package/dist/src/providers/hf-inference.d.ts +125 -2
  21. package/dist/src/providers/hf-inference.d.ts.map +1 -1
  22. package/dist/src/providers/hyperbolic.d.ts +31 -2
  23. package/dist/src/providers/hyperbolic.d.ts.map +1 -1
  24. package/dist/src/providers/nebius.d.ts +20 -18
  25. package/dist/src/providers/nebius.d.ts.map +1 -1
  26. package/dist/src/providers/novita.d.ts +21 -18
  27. package/dist/src/providers/novita.d.ts.map +1 -1
  28. package/dist/src/providers/openai.d.ts +4 -2
  29. package/dist/src/providers/openai.d.ts.map +1 -1
  30. package/dist/src/providers/providerHelper.d.ts +182 -0
  31. package/dist/src/providers/providerHelper.d.ts.map +1 -0
  32. package/dist/src/providers/replicate.d.ts +23 -19
  33. package/dist/src/providers/replicate.d.ts.map +1 -1
  34. package/dist/src/providers/sambanova.d.ts +4 -2
  35. package/dist/src/providers/sambanova.d.ts.map +1 -1
  36. package/dist/src/providers/together.d.ts +32 -2
  37. package/dist/src/providers/together.d.ts.map +1 -1
  38. package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -1
  39. package/dist/src/tasks/audio/audioClassification.d.ts.map +1 -1
  40. package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  41. package/dist/src/tasks/audio/textToSpeech.d.ts.map +1 -1
  42. package/dist/src/tasks/audio/utils.d.ts +2 -1
  43. package/dist/src/tasks/audio/utils.d.ts.map +1 -1
  44. package/dist/src/tasks/custom/request.d.ts +1 -2
  45. package/dist/src/tasks/custom/request.d.ts.map +1 -1
  46. package/dist/src/tasks/custom/streamingRequest.d.ts +1 -2
  47. package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
  48. package/dist/src/tasks/cv/imageClassification.d.ts.map +1 -1
  49. package/dist/src/tasks/cv/imageSegmentation.d.ts.map +1 -1
  50. package/dist/src/tasks/cv/imageToImage.d.ts.map +1 -1
  51. package/dist/src/tasks/cv/imageToText.d.ts.map +1 -1
  52. package/dist/src/tasks/cv/objectDetection.d.ts +1 -1
  53. package/dist/src/tasks/cv/objectDetection.d.ts.map +1 -1
  54. package/dist/src/tasks/cv/textToImage.d.ts.map +1 -1
  55. package/dist/src/tasks/cv/textToVideo.d.ts +1 -1
  56. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  57. package/dist/src/tasks/cv/zeroShotImageClassification.d.ts +1 -1
  58. package/dist/src/tasks/cv/zeroShotImageClassification.d.ts.map +1 -1
  59. package/dist/src/tasks/index.d.ts +6 -6
  60. package/dist/src/tasks/index.d.ts.map +1 -1
  61. package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts +1 -1
  62. package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts.map +1 -1
  63. package/dist/src/tasks/multimodal/visualQuestionAnswering.d.ts.map +1 -1
  64. package/dist/src/tasks/nlp/chatCompletion.d.ts +1 -1
  65. package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
  66. package/dist/src/tasks/nlp/chatCompletionStream.d.ts +1 -1
  67. package/dist/src/tasks/nlp/chatCompletionStream.d.ts.map +1 -1
  68. package/dist/src/tasks/nlp/featureExtraction.d.ts.map +1 -1
  69. package/dist/src/tasks/nlp/fillMask.d.ts.map +1 -1
  70. package/dist/src/tasks/nlp/questionAnswering.d.ts.map +1 -1
  71. package/dist/src/tasks/nlp/sentenceSimilarity.d.ts.map +1 -1
  72. package/dist/src/tasks/nlp/summarization.d.ts.map +1 -1
  73. package/dist/src/tasks/nlp/tableQuestionAnswering.d.ts.map +1 -1
  74. package/dist/src/tasks/nlp/textClassification.d.ts.map +1 -1
  75. package/dist/src/tasks/nlp/textGeneration.d.ts.map +1 -1
  76. package/dist/src/tasks/nlp/tokenClassification.d.ts.map +1 -1
  77. package/dist/src/tasks/nlp/translation.d.ts.map +1 -1
  78. package/dist/src/tasks/nlp/zeroShotClassification.d.ts.map +1 -1
  79. package/dist/src/tasks/tabular/tabularClassification.d.ts.map +1 -1
  80. package/dist/src/tasks/tabular/tabularRegression.d.ts.map +1 -1
  81. package/dist/src/types.d.ts +10 -13
  82. package/dist/src/types.d.ts.map +1 -1
  83. package/dist/src/utils/request.d.ts +27 -0
  84. package/dist/src/utils/request.d.ts.map +1 -0
  85. package/package.json +3 -3
  86. package/src/config.ts +1 -0
  87. package/src/lib/getProviderHelper.ts +270 -0
  88. package/src/lib/makeRequestOptions.ts +36 -90
  89. package/src/providers/black-forest-labs.ts +73 -22
  90. package/src/providers/cerebras.ts +6 -27
  91. package/src/providers/cohere.ts +9 -28
  92. package/src/providers/fal-ai.ts +195 -77
  93. package/src/providers/fireworks-ai.ts +8 -29
  94. package/src/providers/hf-inference.ts +555 -34
  95. package/src/providers/hyperbolic.ts +107 -29
  96. package/src/providers/nebius.ts +65 -29
  97. package/src/providers/novita.ts +68 -32
  98. package/src/providers/openai.ts +6 -32
  99. package/src/providers/providerHelper.ts +354 -0
  100. package/src/providers/replicate.ts +124 -34
  101. package/src/providers/sambanova.ts +5 -30
  102. package/src/providers/together.ts +92 -28
  103. package/src/snippets/getInferenceSnippets.ts +16 -9
  104. package/src/snippets/templates.exported.ts +2 -2
  105. package/src/tasks/audio/audioClassification.ts +6 -9
  106. package/src/tasks/audio/audioToAudio.ts +5 -28
  107. package/src/tasks/audio/automaticSpeechRecognition.ts +7 -6
  108. package/src/tasks/audio/textToSpeech.ts +6 -30
  109. package/src/tasks/audio/utils.ts +2 -1
  110. package/src/tasks/custom/request.ts +7 -34
  111. package/src/tasks/custom/streamingRequest.ts +5 -87
  112. package/src/tasks/cv/imageClassification.ts +5 -9
  113. package/src/tasks/cv/imageSegmentation.ts +5 -10
  114. package/src/tasks/cv/imageToImage.ts +5 -8
  115. package/src/tasks/cv/imageToText.ts +8 -13
  116. package/src/tasks/cv/objectDetection.ts +6 -21
  117. package/src/tasks/cv/textToImage.ts +10 -138
  118. package/src/tasks/cv/textToVideo.ts +11 -59
  119. package/src/tasks/cv/zeroShotImageClassification.ts +7 -12
  120. package/src/tasks/index.ts +6 -6
  121. package/src/tasks/multimodal/documentQuestionAnswering.ts +10 -26
  122. package/src/tasks/multimodal/visualQuestionAnswering.ts +6 -12
  123. package/src/tasks/nlp/chatCompletion.ts +7 -23
  124. package/src/tasks/nlp/chatCompletionStream.ts +4 -5
  125. package/src/tasks/nlp/featureExtraction.ts +5 -20
  126. package/src/tasks/nlp/fillMask.ts +5 -18
  127. package/src/tasks/nlp/questionAnswering.ts +5 -23
  128. package/src/tasks/nlp/sentenceSimilarity.ts +5 -18
  129. package/src/tasks/nlp/summarization.ts +5 -8
  130. package/src/tasks/nlp/tableQuestionAnswering.ts +5 -29
  131. package/src/tasks/nlp/textClassification.ts +8 -14
  132. package/src/tasks/nlp/textGeneration.ts +13 -80
  133. package/src/tasks/nlp/textGenerationStream.ts +2 -2
  134. package/src/tasks/nlp/tokenClassification.ts +8 -24
  135. package/src/tasks/nlp/translation.ts +5 -8
  136. package/src/tasks/nlp/zeroShotClassification.ts +8 -22
  137. package/src/tasks/tabular/tabularClassification.ts +5 -8
  138. package/src/tasks/tabular/tabularRegression.ts +5 -8
  139. package/src/types.ts +11 -14
  140. package/src/utils/request.ts +161 -0
@@ -1,48 +1,15 @@
1
- import type { TextToImageInput, TextToImageOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
3
- import type { BaseArgs, InferenceProvider, Options } from "../../types";
4
- import { omit } from "../../utils/omit";
5
- import { request } from "../custom/request";
6
- import { delay } from "../../utils/delay";
1
+ import type { TextToImageInput } from "@huggingface/tasks";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
+ import { makeRequestOptions } from "../../lib/makeRequestOptions";
4
+ import type { BaseArgs, Options } from "../../types";
5
+ import { innerRequest } from "../../utils/request";
7
6
 
8
7
  export type TextToImageArgs = BaseArgs & TextToImageInput;
9
8
 
10
- interface Base64ImageGeneration {
11
- data: Array<{
12
- b64_json: string;
13
- }>;
14
- }
15
- interface OutputUrlImageGeneration {
16
- output: string[];
17
- }
18
- interface HyperbolicTextToImageOutput {
19
- images: Array<{ image: string }>;
20
- }
21
-
22
- interface BlackForestLabsResponse {
23
- id: string;
24
- polling_url: string;
25
- }
26
-
27
9
  interface TextToImageOptions extends Options {
28
10
  outputType?: "url" | "blob";
29
11
  }
30
12
 
31
- function getResponseFormatArg(provider: InferenceProvider) {
32
- switch (provider) {
33
- case "fal-ai":
34
- return { sync_mode: true };
35
- case "nebius":
36
- return { response_format: "b64_json" };
37
- case "replicate":
38
- return undefined;
39
- case "together":
40
- return { response_format: "base64" };
41
- default:
42
- return undefined;
43
- }
44
- }
45
-
46
13
  /**
47
14
  * This task reads some text input and outputs an image.
48
15
  * Recommended model: stabilityai/stable-diffusion-2
@@ -56,108 +23,13 @@ export async function textToImage(
56
23
  options?: TextToImageOptions & { outputType?: undefined | "blob" }
57
24
  ): Promise<Blob>;
58
25
  export async function textToImage(args: TextToImageArgs, options?: TextToImageOptions): Promise<Blob | string> {
59
- const payload =
60
- !args.provider || args.provider === "hf-inference" || args.provider === "sambanova"
61
- ? args
62
- : {
63
- ...omit(args, ["inputs", "parameters"]),
64
- ...args.parameters,
65
- ...getResponseFormatArg(args.provider),
66
- prompt: args.inputs,
67
- };
68
- const res = await request<
69
- | TextToImageOutput
70
- | Base64ImageGeneration
71
- | OutputUrlImageGeneration
72
- | BlackForestLabsResponse
73
- | HyperbolicTextToImageOutput
74
- >(payload, {
26
+ const provider = args.provider ?? "hf-inference";
27
+ const providerHelper = getProviderHelper(provider, "text-to-image");
28
+ const { data: res } = await innerRequest<Record<string, unknown>>(args, {
75
29
  ...options,
76
30
  task: "text-to-image",
77
31
  });
78
32
 
79
- if (res && typeof res === "object") {
80
- if (args.provider === "black-forest-labs" && "polling_url" in res && typeof res.polling_url === "string") {
81
- return await pollBflResponse(res.polling_url, options?.outputType);
82
- }
83
- if (args.provider === "fal-ai" && "images" in res && Array.isArray(res.images) && res.images[0].url) {
84
- if (options?.outputType === "url") {
85
- return res.images[0].url;
86
- } else {
87
- const image = await fetch(res.images[0].url);
88
- return await image.blob();
89
- }
90
- }
91
- if (
92
- args.provider === "hyperbolic" &&
93
- "images" in res &&
94
- Array.isArray(res.images) &&
95
- res.images[0] &&
96
- typeof res.images[0].image === "string"
97
- ) {
98
- if (options?.outputType === "url") {
99
- return `data:image/jpeg;base64,${res.images[0].image}`;
100
- }
101
- const base64Response = await fetch(`data:image/jpeg;base64,${res.images[0].image}`);
102
- return await base64Response.blob();
103
- }
104
- if ("data" in res && Array.isArray(res.data) && res.data[0].b64_json) {
105
- const base64Data = res.data[0].b64_json;
106
- if (options?.outputType === "url") {
107
- return `data:image/jpeg;base64,${base64Data}`;
108
- }
109
- const base64Response = await fetch(`data:image/jpeg;base64,${base64Data}`);
110
- return await base64Response.blob();
111
- }
112
- if ("output" in res && Array.isArray(res.output)) {
113
- if (options?.outputType === "url") {
114
- return res.output[0];
115
- }
116
- const urlResponse = await fetch(res.output[0]);
117
- const blob = await urlResponse.blob();
118
- return blob;
119
- }
120
- }
121
- const isValidOutput = res && res instanceof Blob;
122
- if (!isValidOutput) {
123
- throw new InferenceOutputError("Expected Blob");
124
- }
125
- if (options?.outputType === "url") {
126
- const b64 = await res.arrayBuffer().then((buf) => Buffer.from(buf).toString("base64"));
127
- return `data:image/jpeg;base64,${b64}`;
128
- }
129
- return res;
130
- }
131
-
132
- async function pollBflResponse(url: string, outputType?: "url" | "blob"): Promise<Blob> {
133
- const urlObj = new URL(url);
134
- for (let step = 0; step < 5; step++) {
135
- await delay(1000);
136
- console.debug(`Polling Black Forest Labs API for the result... ${step + 1}/5`);
137
- urlObj.searchParams.set("attempt", step.toString(10));
138
- const resp = await fetch(urlObj, { headers: { "Content-Type": "application/json" } });
139
- if (!resp.ok) {
140
- throw new InferenceOutputError("Failed to fetch result from black forest labs API");
141
- }
142
- const payload = await resp.json();
143
- if (
144
- typeof payload === "object" &&
145
- payload &&
146
- "status" in payload &&
147
- typeof payload.status === "string" &&
148
- payload.status === "Ready" &&
149
- "result" in payload &&
150
- typeof payload.result === "object" &&
151
- payload.result &&
152
- "sample" in payload.result &&
153
- typeof payload.result.sample === "string"
154
- ) {
155
- if (outputType === "url") {
156
- return payload.result.sample;
157
- }
158
- const image = await fetch(payload.result.sample);
159
- return await image.blob();
160
- }
161
- }
162
- throw new InferenceOutputError("Failed to fetch result from black forest labs API");
33
+ const { url, info } = await makeRequestOptions(args, { ...options, task: "text-to-image" });
34
+ return providerHelper.getResponse(res, url, info.headers as Record<string, string>, options?.outputType);
163
35
  }
@@ -1,71 +1,23 @@
1
- import type { BaseArgs, InferenceProvider, Options } from "../../types";
2
1
  import type { TextToVideoInput } from "@huggingface/tasks";
3
- import { request } from "../custom/request";
4
- import { omit } from "../../utils/omit";
5
- import { isUrl } from "../../lib/isUrl";
6
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
7
- import { typedInclude } from "../../utils/typedInclude";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
8
3
  import { makeRequestOptions } from "../../lib/makeRequestOptions";
9
- import { pollFalResponse, type FalAiQueueOutput } from "../../providers/fal-ai";
4
+ import type { FalAiQueueOutput } from "../../providers/fal-ai";
5
+ import type { NovitaOutput } from "../../providers/novita";
6
+ import type { ReplicateOutput } from "../../providers/replicate";
7
+ import type { BaseArgs, Options } from "../../types";
8
+ import { innerRequest } from "../../utils/request";
10
9
 
11
10
  export type TextToVideoArgs = BaseArgs & TextToVideoInput;
12
11
 
13
12
  export type TextToVideoOutput = Blob;
14
13
 
15
- interface ReplicateOutput {
16
- output: string;
17
- }
18
-
19
- interface NovitaOutput {
20
- video: {
21
- video_url: string;
22
- };
23
- }
24
-
25
- const SUPPORTED_PROVIDERS = ["fal-ai", "novita", "replicate"] as const satisfies readonly InferenceProvider[];
26
-
27
14
  export async function textToVideo(args: TextToVideoArgs, options?: Options): Promise<TextToVideoOutput> {
28
- if (!args.provider || !typedInclude(SUPPORTED_PROVIDERS, args.provider)) {
29
- throw new Error(
30
- `textToVideo inference is only supported for the following providers: ${SUPPORTED_PROVIDERS.join(", ")}`
31
- );
32
- }
33
-
34
- const payload =
35
- args.provider === "fal-ai" || args.provider === "replicate" || args.provider === "novita"
36
- ? { ...omit(args, ["inputs", "parameters"]), ...args.parameters, prompt: args.inputs }
37
- : args;
38
- const res = await request<FalAiQueueOutput | ReplicateOutput | NovitaOutput>(payload, {
15
+ const provider = args.provider ?? "hf-inference";
16
+ const providerHelper = getProviderHelper(provider, "text-to-video");
17
+ const { data: response } = await innerRequest<FalAiQueueOutput | ReplicateOutput | NovitaOutput>(args, {
39
18
  ...options,
40
19
  task: "text-to-video",
41
20
  });
42
- if (args.provider === "fal-ai") {
43
- const { url, info } = await makeRequestOptions(args, { ...options, task: "text-to-video" });
44
- return await pollFalResponse(res as FalAiQueueOutput, url, info.headers as Record<string, string>);
45
- } else if (args.provider === "novita") {
46
- const isValidOutput =
47
- typeof res === "object" &&
48
- !!res &&
49
- "video" in res &&
50
- typeof res.video === "object" &&
51
- !!res.video &&
52
- "video_url" in res.video &&
53
- typeof res.video.video_url === "string" &&
54
- isUrl(res.video.video_url);
55
- if (!isValidOutput) {
56
- throw new InferenceOutputError("Expected { video: { video_url: string } }");
57
- }
58
- const urlResponse = await fetch((res as NovitaOutput).video.video_url);
59
- return await urlResponse.blob();
60
- } else {
61
- /// TODO: Replicate: handle the case where the generation request "times out" / is async (ie output is null)
62
- /// https://replicate.com/docs/topics/predictions/create-a-prediction
63
- const isValidOutput =
64
- typeof res === "object" && !!res && "output" in res && typeof res.output === "string" && isUrl(res.output);
65
- if (!isValidOutput) {
66
- throw new InferenceOutputError("Expected { output: string }");
67
- }
68
- const urlResponse = await fetch(res.output);
69
- return await urlResponse.blob();
70
- }
21
+ const { url, info } = await makeRequestOptions(args, { ...options, task: "text-to-video" });
22
+ return providerHelper.getResponse(response, url, info.headers as Record<string, string>);
71
23
  }
@@ -1,9 +1,8 @@
1
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
- import type { BaseArgs, Options } from "../../types";
3
- import { request } from "../custom/request";
4
- import type { RequestArgs } from "../../types";
5
- import { base64FromBytes } from "../../utils/base64FromBytes";
6
1
  import type { ZeroShotImageClassificationInput, ZeroShotImageClassificationOutput } from "@huggingface/tasks";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
+ import type { BaseArgs, Options, RequestArgs } from "../../types";
4
+ import { base64FromBytes } from "../../utils/base64FromBytes";
5
+ import { innerRequest } from "../../utils/request";
7
6
 
8
7
  /**
9
8
  * @deprecated
@@ -45,15 +44,11 @@ export async function zeroShotImageClassification(
45
44
  args: ZeroShotImageClassificationArgs,
46
45
  options?: Options
47
46
  ): Promise<ZeroShotImageClassificationOutput> {
47
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "zero-shot-image-classification");
48
48
  const payload = await preparePayload(args);
49
- const res = await request<ZeroShotImageClassificationOutput>(payload, {
49
+ const { data: res } = await innerRequest<ZeroShotImageClassificationOutput>(payload, {
50
50
  ...options,
51
51
  task: "zero-shot-image-classification",
52
52
  });
53
- const isValidOutput =
54
- Array.isArray(res) && res.every((x) => typeof x.label === "string" && typeof x.score === "number");
55
- if (!isValidOutput) {
56
- throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
57
- }
58
- return res;
53
+ return providerHelper.getResponse(res);
59
54
  }
@@ -4,21 +4,23 @@ export * from "./custom/streamingRequest";
4
4
 
5
5
  // Audio tasks
6
6
  export * from "./audio/audioClassification";
7
+ export * from "./audio/audioToAudio";
7
8
  export * from "./audio/automaticSpeechRecognition";
8
9
  export * from "./audio/textToSpeech";
9
- export * from "./audio/audioToAudio";
10
10
 
11
11
  // Computer Vision tasks
12
12
  export * from "./cv/imageClassification";
13
13
  export * from "./cv/imageSegmentation";
14
+ export * from "./cv/imageToImage";
14
15
  export * from "./cv/imageToText";
15
16
  export * from "./cv/objectDetection";
16
17
  export * from "./cv/textToImage";
17
- export * from "./cv/imageToImage";
18
- export * from "./cv/zeroShotImageClassification";
19
18
  export * from "./cv/textToVideo";
19
+ export * from "./cv/zeroShotImageClassification";
20
20
 
21
21
  // Natural Language Processing tasks
22
+ export * from "./nlp/chatCompletion";
23
+ export * from "./nlp/chatCompletionStream";
22
24
  export * from "./nlp/featureExtraction";
23
25
  export * from "./nlp/fillMask";
24
26
  export * from "./nlp/questionAnswering";
@@ -31,13 +33,11 @@ export * from "./nlp/textGenerationStream";
31
33
  export * from "./nlp/tokenClassification";
32
34
  export * from "./nlp/translation";
33
35
  export * from "./nlp/zeroShotClassification";
34
- export * from "./nlp/chatCompletion";
35
- export * from "./nlp/chatCompletionStream";
36
36
 
37
37
  // Multimodal tasks
38
38
  export * from "./multimodal/documentQuestionAnswering";
39
39
  export * from "./multimodal/visualQuestionAnswering";
40
40
 
41
41
  // Tabular tasks
42
- export * from "./tabular/tabularRegression";
43
42
  export * from "./tabular/tabularClassification";
43
+ export * from "./tabular/tabularRegression";
@@ -1,14 +1,12 @@
1
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
- import type { BaseArgs, Options } from "../../types";
3
- import { request } from "../custom/request";
4
- import type { RequestArgs } from "../../types";
5
- import { toArray } from "../../utils/toArray";
6
- import { base64FromBytes } from "../../utils/base64FromBytes";
7
1
  import type {
8
2
  DocumentQuestionAnsweringInput,
9
3
  DocumentQuestionAnsweringInputData,
10
4
  DocumentQuestionAnsweringOutput,
11
5
  } from "@huggingface/tasks";
6
+ import { getProviderHelper } from "../../lib/getProviderHelper";
7
+ import type { BaseArgs, Options, RequestArgs } from "../../types";
8
+ import { base64FromBytes } from "../../utils/base64FromBytes";
9
+ import { innerRequest } from "../../utils/request";
12
10
 
13
11
  /// Override the type to properly set inputs.image as Blob
14
12
  export type DocumentQuestionAnsweringArgs = BaseArgs &
@@ -21,6 +19,7 @@ export async function documentQuestionAnswering(
21
19
  args: DocumentQuestionAnsweringArgs,
22
20
  options?: Options
23
21
  ): Promise<DocumentQuestionAnsweringOutput[number]> {
22
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "document-question-answering");
24
23
  const reqArgs: RequestArgs = {
25
24
  ...args,
26
25
  inputs: {
@@ -29,27 +28,12 @@ export async function documentQuestionAnswering(
29
28
  image: base64FromBytes(new Uint8Array(await args.inputs.image.arrayBuffer())),
30
29
  },
31
30
  } as RequestArgs;
32
- const res = toArray(
33
- await request<DocumentQuestionAnsweringOutput | DocumentQuestionAnsweringOutput[number]>(reqArgs, {
31
+ const { data: res } = await innerRequest<DocumentQuestionAnsweringOutput | DocumentQuestionAnsweringOutput[number]>(
32
+ reqArgs,
33
+ {
34
34
  ...options,
35
35
  task: "document-question-answering",
36
- })
36
+ }
37
37
  );
38
-
39
- const isValidOutput =
40
- Array.isArray(res) &&
41
- res.every(
42
- (elem) =>
43
- typeof elem === "object" &&
44
- !!elem &&
45
- typeof elem?.answer === "string" &&
46
- (typeof elem.end === "number" || typeof elem.end === "undefined") &&
47
- (typeof elem.score === "number" || typeof elem.score === "undefined") &&
48
- (typeof elem.start === "number" || typeof elem.start === "undefined")
49
- );
50
- if (!isValidOutput) {
51
- throw new InferenceOutputError("Expected Array<{answer: string, end?: number, score?: number, start?: number}>");
52
- }
53
-
54
- return res[0];
38
+ return providerHelper.getResponse(res);
55
39
  }
@@ -3,10 +3,10 @@ import type {
3
3
  VisualQuestionAnsweringInputData,
4
4
  VisualQuestionAnsweringOutput,
5
5
  } from "@huggingface/tasks";
6
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
6
+ import { getProviderHelper } from "../../lib/getProviderHelper";
7
7
  import type { BaseArgs, Options, RequestArgs } from "../../types";
8
8
  import { base64FromBytes } from "../../utils/base64FromBytes";
9
- import { request } from "../custom/request";
9
+ import { innerRequest } from "../../utils/request";
10
10
 
11
11
  /// Override the type to properly set inputs.image as Blob
12
12
  export type VisualQuestionAnsweringArgs = BaseArgs &
@@ -19,6 +19,7 @@ export async function visualQuestionAnswering(
19
19
  args: VisualQuestionAnsweringArgs,
20
20
  options?: Options
21
21
  ): Promise<VisualQuestionAnsweringOutput[number]> {
22
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "visual-question-answering");
22
23
  const reqArgs: RequestArgs = {
23
24
  ...args,
24
25
  inputs: {
@@ -27,17 +28,10 @@ export async function visualQuestionAnswering(
27
28
  image: base64FromBytes(new Uint8Array(await args.inputs.image.arrayBuffer())),
28
29
  },
29
30
  } as RequestArgs;
30
- const res = await request<VisualQuestionAnsweringOutput>(reqArgs, {
31
+
32
+ const { data: res } = await innerRequest<VisualQuestionAnsweringOutput>(reqArgs, {
31
33
  ...options,
32
34
  task: "visual-question-answering",
33
35
  });
34
- const isValidOutput =
35
- Array.isArray(res) &&
36
- res.every(
37
- (elem) => typeof elem === "object" && !!elem && typeof elem?.answer === "string" && typeof elem.score === "number"
38
- );
39
- if (!isValidOutput) {
40
- throw new InferenceOutputError("Expected Array<{answer: string, score: number}>");
41
- }
42
- return res[0];
36
+ return providerHelper.getResponse(res);
43
37
  }
@@ -1,7 +1,7 @@
1
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
- import type { BaseArgs, Options } from "../../types";
3
- import { request } from "../custom/request";
4
1
  import type { ChatCompletionInput, ChatCompletionOutput } from "@huggingface/tasks";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
+ import type { BaseArgs, Options } from "../../types";
4
+ import { innerRequest } from "../../utils/request";
5
5
 
6
6
  /**
7
7
  * Use the chat completion endpoint to generate a response to a prompt, using OpenAI message completion API no stream
@@ -10,26 +10,10 @@ export async function chatCompletion(
10
10
  args: BaseArgs & ChatCompletionInput,
11
11
  options?: Options
12
12
  ): Promise<ChatCompletionOutput> {
13
- const res = await request<ChatCompletionOutput>(args, {
13
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "conversational");
14
+ const { data: response } = await innerRequest<ChatCompletionOutput>(args, {
14
15
  ...options,
15
- task: "text-generation",
16
- chatCompletion: true,
16
+ task: "conversational",
17
17
  });
18
-
19
- const isValidOutput =
20
- typeof res === "object" &&
21
- Array.isArray(res?.choices) &&
22
- typeof res?.created === "number" &&
23
- typeof res?.id === "string" &&
24
- typeof res?.model === "string" &&
25
- /// Together.ai and Nebius do not output a system_fingerprint
26
- (res.system_fingerprint === undefined ||
27
- res.system_fingerprint === null ||
28
- typeof res.system_fingerprint === "string") &&
29
- typeof res?.usage === "object";
30
-
31
- if (!isValidOutput) {
32
- throw new InferenceOutputError("Expected ChatCompletionOutput");
33
- }
34
- return res;
18
+ return providerHelper.getResponse(response);
35
19
  }
@@ -1,6 +1,6 @@
1
- import type { BaseArgs, Options } from "../../types";
2
- import { streamingRequest } from "../custom/streamingRequest";
3
1
  import type { ChatCompletionInput, ChatCompletionStreamOutput } from "@huggingface/tasks";
2
+ import type { BaseArgs, Options } from "../../types";
3
+ import { innerStreamingRequest } from "../../utils/request";
4
4
 
5
5
  /**
6
6
  * Use to continue text from a prompt. Same as `textGeneration` but returns generator that can be read one token at a time
@@ -9,9 +9,8 @@ export async function* chatCompletionStream(
9
9
  args: BaseArgs & ChatCompletionInput,
10
10
  options?: Options
11
11
  ): AsyncGenerator<ChatCompletionStreamOutput> {
12
- yield* streamingRequest<ChatCompletionStreamOutput>(args, {
12
+ yield* innerStreamingRequest<ChatCompletionStreamOutput>(args, {
13
13
  ...options,
14
- task: "text-generation",
15
- chatCompletion: true,
14
+ task: "conversational",
16
15
  });
17
16
  }
@@ -1,7 +1,7 @@
1
1
  import type { FeatureExtractionInput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
- import { request } from "../custom/request";
4
+ import { innerRequest } from "../../utils/request";
5
5
 
6
6
  export type FeatureExtractionArgs = BaseArgs & FeatureExtractionInput;
7
7
 
@@ -17,25 +17,10 @@ export async function featureExtraction(
17
17
  args: FeatureExtractionArgs,
18
18
  options?: Options
19
19
  ): Promise<FeatureExtractionOutput> {
20
- const res = await request<FeatureExtractionOutput>(args, {
20
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "feature-extraction");
21
+ const { data: res } = await innerRequest<FeatureExtractionOutput>(args, {
21
22
  ...options,
22
23
  task: "feature-extraction",
23
24
  });
24
- let isValidOutput = true;
25
-
26
- const isNumArrayRec = (arr: unknown[], maxDepth: number, curDepth = 0): boolean => {
27
- if (curDepth > maxDepth) return false;
28
- if (arr.every((x) => Array.isArray(x))) {
29
- return arr.every((x) => isNumArrayRec(x as unknown[], maxDepth, curDepth + 1));
30
- } else {
31
- return arr.every((x) => typeof x === "number");
32
- }
33
- };
34
-
35
- isValidOutput = Array.isArray(res) && isNumArrayRec(res, 3, 0);
36
-
37
- if (!isValidOutput) {
38
- throw new InferenceOutputError("Expected Array<number[][][] | number[][] | number[] | number>");
39
- }
40
- return res;
25
+ return providerHelper.getResponse(res);
41
26
  }
@@ -1,7 +1,7 @@
1
1
  import type { FillMaskInput, FillMaskOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
- import { request } from "../custom/request";
4
+ import { innerRequest } from "../../utils/request";
5
5
 
6
6
  export type FillMaskArgs = BaseArgs & FillMaskInput;
7
7
 
@@ -9,23 +9,10 @@ export type FillMaskArgs = BaseArgs & FillMaskInput;
9
9
  * Tries to fill in a hole with a missing word (token to be precise). That’s the base task for BERT models.
10
10
  */
11
11
  export async function fillMask(args: FillMaskArgs, options?: Options): Promise<FillMaskOutput> {
12
- const res = await request<FillMaskOutput>(args, {
12
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "fill-mask");
13
+ const { data: res } = await innerRequest<FillMaskOutput>(args, {
13
14
  ...options,
14
15
  task: "fill-mask",
15
16
  });
16
- const isValidOutput =
17
- Array.isArray(res) &&
18
- res.every(
19
- (x) =>
20
- typeof x.score === "number" &&
21
- typeof x.sequence === "string" &&
22
- typeof x.token === "number" &&
23
- typeof x.token_str === "string"
24
- );
25
- if (!isValidOutput) {
26
- throw new InferenceOutputError(
27
- "Expected Array<{score: number, sequence: string, token: number, token_str: string}>"
28
- );
29
- }
30
- return res;
17
+ return providerHelper.getResponse(res);
31
18
  }
@@ -1,7 +1,7 @@
1
1
  import type { QuestionAnsweringInput, QuestionAnsweringOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
- import { request } from "../custom/request";
4
+ import { innerRequest } from "../../utils/request";
5
5
 
6
6
  export type QuestionAnsweringArgs = BaseArgs & QuestionAnsweringInput;
7
7
 
@@ -12,28 +12,10 @@ export async function questionAnswering(
12
12
  args: QuestionAnsweringArgs,
13
13
  options?: Options
14
14
  ): Promise<QuestionAnsweringOutput[number]> {
15
- const res = await request<QuestionAnsweringOutput | QuestionAnsweringOutput[number]>(args, {
15
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "question-answering");
16
+ const { data: res } = await innerRequest<QuestionAnsweringOutput | QuestionAnsweringOutput[number]>(args, {
16
17
  ...options,
17
18
  task: "question-answering",
18
19
  });
19
- const isValidOutput = Array.isArray(res)
20
- ? res.every(
21
- (elem) =>
22
- typeof elem === "object" &&
23
- !!elem &&
24
- typeof elem.answer === "string" &&
25
- typeof elem.end === "number" &&
26
- typeof elem.score === "number" &&
27
- typeof elem.start === "number"
28
- )
29
- : typeof res === "object" &&
30
- !!res &&
31
- typeof res.answer === "string" &&
32
- typeof res.end === "number" &&
33
- typeof res.score === "number" &&
34
- typeof res.start === "number";
35
- if (!isValidOutput) {
36
- throw new InferenceOutputError("Expected Array<{answer: string, end: number, score: number, start: number}>");
37
- }
38
- return Array.isArray(res) ? res[0] : res;
20
+ return providerHelper.getResponse(res);
39
21
  }
@@ -1,8 +1,7 @@
1
1
  import type { SentenceSimilarityInput, SentenceSimilarityOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
- import { request } from "../custom/request";
5
- import { omit } from "../../utils/omit";
4
+ import { innerRequest } from "../../utils/request";
6
5
 
7
6
  export type SentenceSimilarityArgs = BaseArgs & SentenceSimilarityInput;
8
7
 
@@ -13,22 +12,10 @@ export async function sentenceSimilarity(
13
12
  args: SentenceSimilarityArgs,
14
13
  options?: Options
15
14
  ): Promise<SentenceSimilarityOutput> {
16
- const res = await request<SentenceSimilarityOutput>(prepareInput(args), {
15
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "sentence-similarity");
16
+ const { data: res } = await innerRequest<SentenceSimilarityOutput>(args, {
17
17
  ...options,
18
18
  task: "sentence-similarity",
19
19
  });
20
-
21
- const isValidOutput = Array.isArray(res) && res.every((x) => typeof x === "number");
22
- if (!isValidOutput) {
23
- throw new InferenceOutputError("Expected number[]");
24
- }
25
- return res;
26
- }
27
-
28
- function prepareInput(args: SentenceSimilarityArgs) {
29
- return {
30
- ...omit(args, ["inputs", "parameters"]),
31
- inputs: { ...omit(args.inputs, "sourceSentence") },
32
- parameters: { source_sentence: args.inputs.sourceSentence, ...args.parameters },
33
- };
20
+ return providerHelper.getResponse(res);
34
21
  }