@huggingface/inference 3.7.0 → 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 (126) hide show
  1. package/dist/index.cjs +1152 -839
  2. package/dist/index.js +1154 -841
  3. package/dist/src/lib/getProviderHelper.d.ts +37 -0
  4. package/dist/src/lib/getProviderHelper.d.ts.map +1 -0
  5. package/dist/src/lib/makeRequestOptions.d.ts +0 -2
  6. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  7. package/dist/src/providers/black-forest-labs.d.ts +14 -18
  8. package/dist/src/providers/black-forest-labs.d.ts.map +1 -1
  9. package/dist/src/providers/cerebras.d.ts +4 -2
  10. package/dist/src/providers/cerebras.d.ts.map +1 -1
  11. package/dist/src/providers/cohere.d.ts +5 -2
  12. package/dist/src/providers/cohere.d.ts.map +1 -1
  13. package/dist/src/providers/fal-ai.d.ts +50 -3
  14. package/dist/src/providers/fal-ai.d.ts.map +1 -1
  15. package/dist/src/providers/fireworks-ai.d.ts +5 -2
  16. package/dist/src/providers/fireworks-ai.d.ts.map +1 -1
  17. package/dist/src/providers/hf-inference.d.ts +125 -2
  18. package/dist/src/providers/hf-inference.d.ts.map +1 -1
  19. package/dist/src/providers/hyperbolic.d.ts +31 -2
  20. package/dist/src/providers/hyperbolic.d.ts.map +1 -1
  21. package/dist/src/providers/nebius.d.ts +20 -18
  22. package/dist/src/providers/nebius.d.ts.map +1 -1
  23. package/dist/src/providers/novita.d.ts +21 -18
  24. package/dist/src/providers/novita.d.ts.map +1 -1
  25. package/dist/src/providers/openai.d.ts +4 -2
  26. package/dist/src/providers/openai.d.ts.map +1 -1
  27. package/dist/src/providers/providerHelper.d.ts +182 -0
  28. package/dist/src/providers/providerHelper.d.ts.map +1 -0
  29. package/dist/src/providers/replicate.d.ts +23 -19
  30. package/dist/src/providers/replicate.d.ts.map +1 -1
  31. package/dist/src/providers/sambanova.d.ts +4 -2
  32. package/dist/src/providers/sambanova.d.ts.map +1 -1
  33. package/dist/src/providers/together.d.ts +32 -2
  34. package/dist/src/providers/together.d.ts.map +1 -1
  35. package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -1
  36. package/dist/src/tasks/audio/audioClassification.d.ts.map +1 -1
  37. package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  38. package/dist/src/tasks/audio/textToSpeech.d.ts.map +1 -1
  39. package/dist/src/tasks/audio/utils.d.ts +2 -1
  40. package/dist/src/tasks/audio/utils.d.ts.map +1 -1
  41. package/dist/src/tasks/custom/request.d.ts +0 -2
  42. package/dist/src/tasks/custom/request.d.ts.map +1 -1
  43. package/dist/src/tasks/custom/streamingRequest.d.ts +0 -2
  44. package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
  45. package/dist/src/tasks/cv/imageClassification.d.ts.map +1 -1
  46. package/dist/src/tasks/cv/imageSegmentation.d.ts.map +1 -1
  47. package/dist/src/tasks/cv/imageToImage.d.ts.map +1 -1
  48. package/dist/src/tasks/cv/imageToText.d.ts.map +1 -1
  49. package/dist/src/tasks/cv/objectDetection.d.ts.map +1 -1
  50. package/dist/src/tasks/cv/textToImage.d.ts.map +1 -1
  51. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  52. package/dist/src/tasks/cv/zeroShotImageClassification.d.ts.map +1 -1
  53. package/dist/src/tasks/index.d.ts +6 -6
  54. package/dist/src/tasks/index.d.ts.map +1 -1
  55. package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts.map +1 -1
  56. package/dist/src/tasks/multimodal/visualQuestionAnswering.d.ts.map +1 -1
  57. package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
  58. package/dist/src/tasks/nlp/chatCompletionStream.d.ts.map +1 -1
  59. package/dist/src/tasks/nlp/featureExtraction.d.ts.map +1 -1
  60. package/dist/src/tasks/nlp/fillMask.d.ts.map +1 -1
  61. package/dist/src/tasks/nlp/questionAnswering.d.ts.map +1 -1
  62. package/dist/src/tasks/nlp/sentenceSimilarity.d.ts.map +1 -1
  63. package/dist/src/tasks/nlp/summarization.d.ts.map +1 -1
  64. package/dist/src/tasks/nlp/tableQuestionAnswering.d.ts.map +1 -1
  65. package/dist/src/tasks/nlp/textClassification.d.ts.map +1 -1
  66. package/dist/src/tasks/nlp/textGeneration.d.ts.map +1 -1
  67. package/dist/src/tasks/nlp/tokenClassification.d.ts.map +1 -1
  68. package/dist/src/tasks/nlp/translation.d.ts.map +1 -1
  69. package/dist/src/tasks/nlp/zeroShotClassification.d.ts.map +1 -1
  70. package/dist/src/tasks/tabular/tabularClassification.d.ts.map +1 -1
  71. package/dist/src/tasks/tabular/tabularRegression.d.ts.map +1 -1
  72. package/dist/src/types.d.ts +3 -13
  73. package/dist/src/types.d.ts.map +1 -1
  74. package/package.json +3 -3
  75. package/src/lib/getProviderHelper.ts +270 -0
  76. package/src/lib/makeRequestOptions.ts +34 -91
  77. package/src/providers/black-forest-labs.ts +73 -22
  78. package/src/providers/cerebras.ts +6 -27
  79. package/src/providers/cohere.ts +9 -28
  80. package/src/providers/fal-ai.ts +195 -77
  81. package/src/providers/fireworks-ai.ts +8 -29
  82. package/src/providers/hf-inference.ts +555 -34
  83. package/src/providers/hyperbolic.ts +107 -29
  84. package/src/providers/nebius.ts +65 -29
  85. package/src/providers/novita.ts +68 -32
  86. package/src/providers/openai.ts +6 -32
  87. package/src/providers/providerHelper.ts +354 -0
  88. package/src/providers/replicate.ts +124 -34
  89. package/src/providers/sambanova.ts +5 -30
  90. package/src/providers/together.ts +92 -28
  91. package/src/snippets/getInferenceSnippets.ts +16 -9
  92. package/src/snippets/templates.exported.ts +1 -1
  93. package/src/tasks/audio/audioClassification.ts +4 -7
  94. package/src/tasks/audio/audioToAudio.ts +3 -26
  95. package/src/tasks/audio/automaticSpeechRecognition.ts +4 -3
  96. package/src/tasks/audio/textToSpeech.ts +5 -29
  97. package/src/tasks/audio/utils.ts +2 -1
  98. package/src/tasks/custom/request.ts +0 -2
  99. package/src/tasks/custom/streamingRequest.ts +0 -2
  100. package/src/tasks/cv/imageClassification.ts +3 -7
  101. package/src/tasks/cv/imageSegmentation.ts +3 -8
  102. package/src/tasks/cv/imageToImage.ts +3 -6
  103. package/src/tasks/cv/imageToText.ts +3 -6
  104. package/src/tasks/cv/objectDetection.ts +3 -18
  105. package/src/tasks/cv/textToImage.ts +9 -137
  106. package/src/tasks/cv/textToVideo.ts +11 -62
  107. package/src/tasks/cv/zeroShotImageClassification.ts +3 -7
  108. package/src/tasks/index.ts +6 -6
  109. package/src/tasks/multimodal/documentQuestionAnswering.ts +3 -19
  110. package/src/tasks/multimodal/visualQuestionAnswering.ts +3 -11
  111. package/src/tasks/nlp/chatCompletion.ts +5 -20
  112. package/src/tasks/nlp/chatCompletionStream.ts +1 -2
  113. package/src/tasks/nlp/featureExtraction.ts +3 -18
  114. package/src/tasks/nlp/fillMask.ts +3 -16
  115. package/src/tasks/nlp/questionAnswering.ts +3 -22
  116. package/src/tasks/nlp/sentenceSimilarity.ts +3 -7
  117. package/src/tasks/nlp/summarization.ts +3 -6
  118. package/src/tasks/nlp/tableQuestionAnswering.ts +3 -27
  119. package/src/tasks/nlp/textClassification.ts +3 -8
  120. package/src/tasks/nlp/textGeneration.ts +12 -79
  121. package/src/tasks/nlp/tokenClassification.ts +3 -18
  122. package/src/tasks/nlp/translation.ts +3 -6
  123. package/src/tasks/nlp/zeroShotClassification.ts +3 -16
  124. package/src/tasks/tabular/tabularClassification.ts +3 -6
  125. package/src/tasks/tabular/tabularRegression.ts +3 -6
  126. package/src/types.ts +3 -14
@@ -1,5 +1,5 @@
1
1
  import type { ZeroShotImageClassificationInput, ZeroShotImageClassificationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options, RequestArgs } from "../../types";
4
4
  import { base64FromBytes } from "../../utils/base64FromBytes";
5
5
  import { innerRequest } from "../../utils/request";
@@ -44,15 +44,11 @@ export async function zeroShotImageClassification(
44
44
  args: ZeroShotImageClassificationArgs,
45
45
  options?: Options
46
46
  ): Promise<ZeroShotImageClassificationOutput> {
47
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "zero-shot-image-classification");
47
48
  const payload = await preparePayload(args);
48
49
  const { data: res } = await innerRequest<ZeroShotImageClassificationOutput>(payload, {
49
50
  ...options,
50
51
  task: "zero-shot-image-classification",
51
52
  });
52
- const isValidOutput =
53
- Array.isArray(res) && res.every((x) => typeof x.label === "string" && typeof x.score === "number");
54
- if (!isValidOutput) {
55
- throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
56
- }
57
- return res;
53
+ return providerHelper.getResponse(res);
58
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";
@@ -3,11 +3,10 @@ import type {
3
3
  DocumentQuestionAnsweringInputData,
4
4
  DocumentQuestionAnsweringOutput,
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
9
  import { innerRequest } from "../../utils/request";
10
- import { toArray } from "../../utils/toArray";
11
10
 
12
11
  /// Override the type to properly set inputs.image as Blob
13
12
  export type DocumentQuestionAnsweringArgs = BaseArgs &
@@ -20,6 +19,7 @@ export async function documentQuestionAnswering(
20
19
  args: DocumentQuestionAnsweringArgs,
21
20
  options?: Options
22
21
  ): Promise<DocumentQuestionAnsweringOutput[number]> {
22
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "document-question-answering");
23
23
  const reqArgs: RequestArgs = {
24
24
  ...args,
25
25
  inputs: {
@@ -35,21 +35,5 @@ export async function documentQuestionAnswering(
35
35
  task: "document-question-answering",
36
36
  }
37
37
  );
38
- const output = toArray(res);
39
- const isValidOutput =
40
- Array.isArray(output) &&
41
- output.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 output[0];
38
+ return providerHelper.getResponse(res);
55
39
  }
@@ -3,7 +3,7 @@ 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
9
  import { innerRequest } from "../../utils/request";
@@ -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: {
@@ -32,14 +33,5 @@ export async function visualQuestionAnswering(
32
33
  ...options,
33
34
  task: "visual-question-answering",
34
35
  });
35
-
36
- const isValidOutput =
37
- Array.isArray(res) &&
38
- res.every(
39
- (elem) => typeof elem === "object" && !!elem && typeof elem?.answer === "string" && typeof elem.score === "number"
40
- );
41
- if (!isValidOutput) {
42
- throw new InferenceOutputError("Expected Array<{answer: string, score: number}>");
43
- }
44
- return res[0];
36
+ return providerHelper.getResponse(res);
45
37
  }
@@ -1,5 +1,5 @@
1
1
  import type { ChatCompletionInput, ChatCompletionOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -10,25 +10,10 @@ export async function chatCompletion(
10
10
  args: BaseArgs & ChatCompletionInput,
11
11
  options?: Options
12
12
  ): Promise<ChatCompletionOutput> {
13
- const { data: res } = await innerRequest<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
- const isValidOutput =
19
- typeof res === "object" &&
20
- Array.isArray(res?.choices) &&
21
- typeof res?.created === "number" &&
22
- typeof res?.id === "string" &&
23
- typeof res?.model === "string" &&
24
- /// Together.ai and Nebius do not output a system_fingerprint
25
- (res.system_fingerprint === undefined ||
26
- res.system_fingerprint === null ||
27
- typeof res.system_fingerprint === "string") &&
28
- typeof res?.usage === "object";
29
-
30
- if (!isValidOutput) {
31
- throw new InferenceOutputError("Expected ChatCompletionOutput");
32
- }
33
- return res;
18
+ return providerHelper.getResponse(response);
34
19
  }
@@ -11,7 +11,6 @@ export async function* chatCompletionStream(
11
11
  ): AsyncGenerator<ChatCompletionStreamOutput> {
12
12
  yield* innerStreamingRequest<ChatCompletionStreamOutput>(args, {
13
13
  ...options,
14
- task: "text-generation",
15
- chatCompletion: true,
14
+ task: "conversational",
16
15
  });
17
16
  }
@@ -1,5 +1,5 @@
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
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -17,25 +17,10 @@ export async function featureExtraction(
17
17
  args: FeatureExtractionArgs,
18
18
  options?: Options
19
19
  ): Promise<FeatureExtractionOutput> {
20
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "feature-extraction");
20
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,5 +1,5 @@
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
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -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 providerHelper = getProviderHelper(args.provider ?? "hf-inference", "fill-mask");
12
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,5 +1,5 @@
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
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -12,29 +12,10 @@ export async function questionAnswering(
12
12
  args: QuestionAnsweringArgs,
13
13
  options?: Options
14
14
  ): Promise<QuestionAnsweringOutput[number]> {
15
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "question-answering");
15
16
  const { data: res } = await innerRequest<QuestionAnsweringOutput | QuestionAnsweringOutput[number]>(args, {
16
17
  ...options,
17
18
  task: "question-answering",
18
19
  });
19
-
20
- const isValidOutput = Array.isArray(res)
21
- ? res.every(
22
- (elem) =>
23
- typeof elem === "object" &&
24
- !!elem &&
25
- typeof elem.answer === "string" &&
26
- typeof elem.end === "number" &&
27
- typeof elem.score === "number" &&
28
- typeof elem.start === "number"
29
- )
30
- : typeof res === "object" &&
31
- !!res &&
32
- typeof res.answer === "string" &&
33
- typeof res.end === "number" &&
34
- typeof res.score === "number" &&
35
- typeof res.start === "number";
36
- if (!isValidOutput) {
37
- throw new InferenceOutputError("Expected Array<{answer: string, end: number, score: number, start: number}>");
38
- }
39
- return Array.isArray(res) ? res[0] : res;
20
+ return providerHelper.getResponse(res);
40
21
  }
@@ -1,5 +1,5 @@
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
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -12,14 +12,10 @@ export async function sentenceSimilarity(
12
12
  args: SentenceSimilarityArgs,
13
13
  options?: Options
14
14
  ): Promise<SentenceSimilarityOutput> {
15
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "sentence-similarity");
15
16
  const { data: res } = await innerRequest<SentenceSimilarityOutput>(args, {
16
17
  ...options,
17
18
  task: "sentence-similarity",
18
19
  });
19
-
20
- const isValidOutput = Array.isArray(res) && res.every((x) => typeof x === "number");
21
- if (!isValidOutput) {
22
- throw new InferenceOutputError("Expected number[]");
23
- }
24
- return res;
20
+ return providerHelper.getResponse(res);
25
21
  }
@@ -1,5 +1,5 @@
1
1
  import type { SummarizationInput, SummarizationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -9,13 +9,10 @@ export type SummarizationArgs = BaseArgs & SummarizationInput;
9
9
  * This task is well known to summarize longer text into shorter text. Be careful, some models have a maximum length of input. That means that the summary cannot handle full books for instance. Be careful when choosing your model.
10
10
  */
11
11
  export async function summarization(args: SummarizationArgs, options?: Options): Promise<SummarizationOutput> {
12
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "summarization");
12
13
  const { data: res } = await innerRequest<SummarizationOutput[]>(args, {
13
14
  ...options,
14
15
  task: "summarization",
15
16
  });
16
- const isValidOutput = Array.isArray(res) && res.every((x) => typeof x?.summary_text === "string");
17
- if (!isValidOutput) {
18
- throw new InferenceOutputError("Expected Array<{summary_text: string}>");
19
- }
20
- return res?.[0];
17
+ return providerHelper.getResponse(res);
21
18
  }
@@ -1,5 +1,5 @@
1
1
  import type { TableQuestionAnsweringInput, TableQuestionAnsweringOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -12,34 +12,10 @@ export async function tableQuestionAnswering(
12
12
  args: TableQuestionAnsweringArgs,
13
13
  options?: Options
14
14
  ): Promise<TableQuestionAnsweringOutput[number]> {
15
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "table-question-answering");
15
16
  const { data: res } = await innerRequest<TableQuestionAnsweringOutput | TableQuestionAnsweringOutput[number]>(args, {
16
17
  ...options,
17
18
  task: "table-question-answering",
18
19
  });
19
- const isValidOutput = Array.isArray(res) ? res.every((elem) => validate(elem)) : validate(res);
20
- if (!isValidOutput) {
21
- throw new InferenceOutputError(
22
- "Expected {aggregator: string, answer: string, cells: string[], coordinates: number[][]}"
23
- );
24
- }
25
- return Array.isArray(res) ? res[0] : res;
26
- }
27
-
28
- function validate(elem: unknown): elem is TableQuestionAnsweringOutput[number] {
29
- return (
30
- typeof elem === "object" &&
31
- !!elem &&
32
- "aggregator" in elem &&
33
- typeof elem.aggregator === "string" &&
34
- "answer" in elem &&
35
- typeof elem.answer === "string" &&
36
- "cells" in elem &&
37
- Array.isArray(elem.cells) &&
38
- elem.cells.every((x: unknown): x is string => typeof x === "string") &&
39
- "coordinates" in elem &&
40
- Array.isArray(elem.coordinates) &&
41
- elem.coordinates.every(
42
- (coord: unknown): coord is number[] => Array.isArray(coord) && coord.every((x) => typeof x === "number")
43
- )
44
- );
20
+ return providerHelper.getResponse(res);
45
21
  }
@@ -1,5 +1,5 @@
1
1
  import type { TextClassificationInput, TextClassificationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -12,15 +12,10 @@ export async function textClassification(
12
12
  args: TextClassificationArgs,
13
13
  options?: Options
14
14
  ): Promise<TextClassificationOutput> {
15
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "text-classification");
15
16
  const { data: res } = await innerRequest<TextClassificationOutput>(args, {
16
17
  ...options,
17
18
  task: "text-classification",
18
19
  });
19
- const output = res?.[0];
20
- const isValidOutput =
21
- Array.isArray(output) && output.every((x) => typeof x?.label === "string" && typeof x.score === "number");
22
- if (!isValidOutput) {
23
- throw new InferenceOutputError("Expected Array<{label: string, score: number}>");
24
- }
25
- return output;
20
+ return providerHelper.getResponse(res);
26
21
  }
@@ -1,33 +1,11 @@
1
- import type {
2
- ChatCompletionOutput,
3
- TextGenerationInput,
4
- TextGenerationOutput,
5
- TextGenerationOutputFinishReason,
6
- } from "@huggingface/tasks";
7
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
1
+ import type { TextGenerationInput, TextGenerationOutput } from "@huggingface/tasks";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
+ import type { HyperbolicTextCompletionOutput } from "../../providers/hyperbolic";
8
4
  import type { BaseArgs, Options } from "../../types";
9
- import { omit } from "../../utils/omit";
10
5
  import { innerRequest } from "../../utils/request";
11
- import { toArray } from "../../utils/toArray";
12
6
 
13
7
  export type { TextGenerationInput, TextGenerationOutput };
14
8
 
15
- interface TogeteherTextCompletionOutput extends Omit<ChatCompletionOutput, "choices"> {
16
- choices: Array<{
17
- text: string;
18
- finish_reason: TextGenerationOutputFinishReason;
19
- seed: number;
20
- logprobs: unknown;
21
- index: number;
22
- }>;
23
- }
24
-
25
- interface HyperbolicTextCompletionOutput extends Omit<ChatCompletionOutput, "choices"> {
26
- choices: Array<{
27
- message: { content: string };
28
- }>;
29
- }
30
-
31
9
  /**
32
10
  * Use to continue text from a prompt. This is a very generic task. Recommended model: gpt2 (it’s a simple model, but fun to play with).
33
11
  */
@@ -35,58 +13,13 @@ export async function textGeneration(
35
13
  args: BaseArgs & TextGenerationInput,
36
14
  options?: Options
37
15
  ): Promise<TextGenerationOutput> {
38
- if (args.provider === "together") {
39
- args.prompt = args.inputs;
40
- const { data: raw } = await innerRequest<TogeteherTextCompletionOutput>(args, {
41
- ...options,
42
- task: "text-generation",
43
- });
44
- const isValidOutput =
45
- typeof raw === "object" && "choices" in raw && Array.isArray(raw?.choices) && typeof raw?.model === "string";
46
- if (!isValidOutput) {
47
- throw new InferenceOutputError("Expected ChatCompletionOutput");
48
- }
49
- const completion = raw.choices[0];
50
- return {
51
- generated_text: completion.text,
52
- };
53
- } else if (args.provider === "hyperbolic") {
54
- const payload = {
55
- messages: [{ content: args.inputs, role: "user" }],
56
- ...(args.parameters
57
- ? {
58
- max_tokens: args.parameters.max_new_tokens,
59
- ...omit(args.parameters, "max_new_tokens"),
60
- }
61
- : undefined),
62
- ...omit(args, ["inputs", "parameters"]),
63
- };
64
- const raw = (
65
- await innerRequest<HyperbolicTextCompletionOutput>(payload, {
66
- ...options,
67
- task: "text-generation",
68
- })
69
- ).data;
70
- const isValidOutput =
71
- typeof raw === "object" && "choices" in raw && Array.isArray(raw?.choices) && typeof raw?.model === "string";
72
- if (!isValidOutput) {
73
- throw new InferenceOutputError("Expected ChatCompletionOutput");
74
- }
75
- const completion = raw.choices[0];
76
- return {
77
- generated_text: completion.message.content,
78
- };
79
- } else {
80
- const { data: res } = await innerRequest<TextGenerationOutput | TextGenerationOutput[]>(args, {
81
- ...options,
82
- task: "text-generation",
83
- });
84
- const output = toArray(res);
85
- const isValidOutput =
86
- Array.isArray(output) && output.every((x) => "generated_text" in x && typeof x?.generated_text === "string");
87
- if (!isValidOutput) {
88
- throw new InferenceOutputError("Expected Array<{generated_text: string}>");
89
- }
90
- return (output as TextGenerationOutput[])?.[0];
91
- }
16
+ const provider = args.provider ?? "hf-inference";
17
+ const providerHelper = getProviderHelper(provider, "text-generation");
18
+ const { data: response } = await innerRequest<
19
+ HyperbolicTextCompletionOutput | TextGenerationOutput | TextGenerationOutput[]
20
+ >(args, {
21
+ ...options,
22
+ task: "text-generation",
23
+ });
24
+ return providerHelper.getResponse(response);
92
25
  }
@@ -1,5 +1,5 @@
1
1
  import type { TokenClassificationInput, TokenClassificationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
  import { toArray } from "../../utils/toArray";
@@ -13,25 +13,10 @@ export async function tokenClassification(
13
13
  args: TokenClassificationArgs,
14
14
  options?: Options
15
15
  ): Promise<TokenClassificationOutput> {
16
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "token-classification");
16
17
  const { data: res } = await innerRequest<TokenClassificationOutput[number] | TokenClassificationOutput>(args, {
17
18
  ...options,
18
19
  task: "token-classification",
19
20
  });
20
- const output = toArray(res);
21
- const isValidOutput =
22
- Array.isArray(output) &&
23
- output.every(
24
- (x) =>
25
- typeof x.end === "number" &&
26
- typeof x.entity_group === "string" &&
27
- typeof x.score === "number" &&
28
- typeof x.start === "number" &&
29
- typeof x.word === "string"
30
- );
31
- if (!isValidOutput) {
32
- throw new InferenceOutputError(
33
- "Expected Array<{end: number, entity_group: string, score: number, start: number, word: string}>"
34
- );
35
- }
36
- return output;
21
+ return providerHelper.getResponse(res);
37
22
  }
@@ -1,5 +1,5 @@
1
1
  import type { TranslationInput, TranslationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
 
@@ -8,13 +8,10 @@ export type TranslationArgs = BaseArgs & TranslationInput;
8
8
  * This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
9
9
  */
10
10
  export async function translation(args: TranslationArgs, options?: Options): Promise<TranslationOutput> {
11
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "translation");
11
12
  const { data: res } = await innerRequest<TranslationOutput>(args, {
12
13
  ...options,
13
14
  task: "translation",
14
15
  });
15
- const isValidOutput = Array.isArray(res) && res.every((x) => typeof x?.translation_text === "string");
16
- if (!isValidOutput) {
17
- throw new InferenceOutputError("Expected type Array<{translation_text: string}>");
18
- }
19
- return res?.length === 1 ? res?.[0] : res;
16
+ return providerHelper.getResponse(res);
20
17
  }
@@ -1,5 +1,5 @@
1
1
  import type { ZeroShotClassificationInput, ZeroShotClassificationOutput } from "@huggingface/tasks";
2
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
2
+ import { getProviderHelper } from "../../lib/getProviderHelper";
3
3
  import type { BaseArgs, Options } from "../../types";
4
4
  import { innerRequest } from "../../utils/request";
5
5
  import { toArray } from "../../utils/toArray";
@@ -13,23 +13,10 @@ export async function zeroShotClassification(
13
13
  args: ZeroShotClassificationArgs,
14
14
  options?: Options
15
15
  ): Promise<ZeroShotClassificationOutput> {
16
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "zero-shot-classification");
16
17
  const { data: res } = await innerRequest<ZeroShotClassificationOutput[number] | ZeroShotClassificationOutput>(args, {
17
18
  ...options,
18
19
  task: "zero-shot-classification",
19
20
  });
20
- const output = toArray(res);
21
- const isValidOutput =
22
- Array.isArray(output) &&
23
- output.every(
24
- (x) =>
25
- Array.isArray(x.labels) &&
26
- x.labels.every((_label) => typeof _label === "string") &&
27
- Array.isArray(x.scores) &&
28
- x.scores.every((_score) => typeof _score === "number") &&
29
- typeof x.sequence === "string"
30
- );
31
- if (!isValidOutput) {
32
- throw new InferenceOutputError("Expected Array<{labels: string[], scores: number[], sequence: string}>");
33
- }
34
- return output;
21
+ return providerHelper.getResponse(res);
35
22
  }
@@ -1,4 +1,4 @@
1
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
1
+ import { getProviderHelper } from "../../lib/getProviderHelper";
2
2
  import type { BaseArgs, Options } from "../../types";
3
3
  import { innerRequest } from "../../utils/request";
4
4
 
@@ -25,13 +25,10 @@ export async function tabularClassification(
25
25
  args: TabularClassificationArgs,
26
26
  options?: Options
27
27
  ): Promise<TabularClassificationOutput> {
28
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "tabular-classification");
28
29
  const { data: res } = await innerRequest<TabularClassificationOutput>(args, {
29
30
  ...options,
30
31
  task: "tabular-classification",
31
32
  });
32
- const isValidOutput = Array.isArray(res) && res.every((x) => typeof x === "number");
33
- if (!isValidOutput) {
34
- throw new InferenceOutputError("Expected number[]");
35
- }
36
- return res;
33
+ return providerHelper.getResponse(res);
37
34
  }
@@ -1,4 +1,4 @@
1
- import { InferenceOutputError } from "../../lib/InferenceOutputError";
1
+ import { getProviderHelper } from "../../lib/getProviderHelper";
2
2
  import type { BaseArgs, Options } from "../../types";
3
3
  import { innerRequest } from "../../utils/request";
4
4
 
@@ -25,13 +25,10 @@ export async function tabularRegression(
25
25
  args: TabularRegressionArgs,
26
26
  options?: Options
27
27
  ): Promise<TabularRegressionOutput> {
28
+ const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "tabular-regression");
28
29
  const { data: res } = await innerRequest<TabularRegressionOutput>(args, {
29
30
  ...options,
30
31
  task: "tabular-regression",
31
32
  });
32
- const isValidOutput = Array.isArray(res) && res.every((x) => typeof x === "number");
33
- if (!isValidOutput) {
34
- throw new InferenceOutputError("Expected number[]");
35
- }
36
- return res;
33
+ return providerHelper.getResponse(res);
37
34
  }