@huggingface/inference 3.10.0 → 3.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/dist/index.cjs +713 -643
  2. package/dist/index.js +712 -643
  3. package/dist/src/InferenceClient.d.ts +16 -17
  4. package/dist/src/InferenceClient.d.ts.map +1 -1
  5. package/dist/src/lib/getInferenceProviderMapping.d.ts +5 -1
  6. package/dist/src/lib/getInferenceProviderMapping.d.ts.map +1 -1
  7. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  8. package/dist/src/providers/providerHelper.d.ts +1 -1
  9. package/dist/src/providers/providerHelper.d.ts.map +1 -1
  10. package/dist/src/tasks/audio/audioClassification.d.ts.map +1 -1
  11. package/dist/src/tasks/audio/audioToAudio.d.ts.map +1 -1
  12. package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  13. package/dist/src/tasks/audio/textToSpeech.d.ts.map +1 -1
  14. package/dist/src/tasks/custom/request.d.ts.map +1 -1
  15. package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
  16. package/dist/src/tasks/cv/imageClassification.d.ts.map +1 -1
  17. package/dist/src/tasks/cv/imageSegmentation.d.ts.map +1 -1
  18. package/dist/src/tasks/cv/imageToImage.d.ts.map +1 -1
  19. package/dist/src/tasks/cv/imageToText.d.ts.map +1 -1
  20. package/dist/src/tasks/cv/objectDetection.d.ts.map +1 -1
  21. package/dist/src/tasks/cv/textToImage.d.ts.map +1 -1
  22. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  23. package/dist/src/tasks/cv/zeroShotImageClassification.d.ts.map +1 -1
  24. package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts.map +1 -1
  25. package/dist/src/tasks/multimodal/visualQuestionAnswering.d.ts.map +1 -1
  26. package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
  27. package/dist/src/tasks/nlp/chatCompletionStream.d.ts.map +1 -1
  28. package/dist/src/tasks/nlp/featureExtraction.d.ts.map +1 -1
  29. package/dist/src/tasks/nlp/fillMask.d.ts.map +1 -1
  30. package/dist/src/tasks/nlp/questionAnswering.d.ts.map +1 -1
  31. package/dist/src/tasks/nlp/sentenceSimilarity.d.ts.map +1 -1
  32. package/dist/src/tasks/nlp/summarization.d.ts.map +1 -1
  33. package/dist/src/tasks/nlp/tableQuestionAnswering.d.ts.map +1 -1
  34. package/dist/src/tasks/nlp/textClassification.d.ts.map +1 -1
  35. package/dist/src/tasks/nlp/textGeneration.d.ts.map +1 -1
  36. package/dist/src/tasks/nlp/textGenerationStream.d.ts.map +1 -1
  37. package/dist/src/tasks/nlp/tokenClassification.d.ts.map +1 -1
  38. package/dist/src/tasks/nlp/translation.d.ts.map +1 -1
  39. package/dist/src/tasks/nlp/zeroShotClassification.d.ts.map +1 -1
  40. package/dist/src/tasks/tabular/tabularClassification.d.ts.map +1 -1
  41. package/dist/src/tasks/tabular/tabularRegression.d.ts.map +1 -1
  42. package/dist/src/types.d.ts +6 -4
  43. package/dist/src/types.d.ts.map +1 -1
  44. package/dist/src/utils/typedEntries.d.ts +4 -0
  45. package/dist/src/utils/typedEntries.d.ts.map +1 -0
  46. package/package.json +3 -3
  47. package/src/InferenceClient.ts +32 -43
  48. package/src/lib/getInferenceProviderMapping.ts +68 -19
  49. package/src/lib/makeRequestOptions.ts +4 -3
  50. package/src/providers/hf-inference.ts +1 -1
  51. package/src/providers/providerHelper.ts +1 -1
  52. package/src/snippets/getInferenceSnippets.ts +1 -1
  53. package/src/tasks/audio/audioClassification.ts +3 -1
  54. package/src/tasks/audio/audioToAudio.ts +4 -1
  55. package/src/tasks/audio/automaticSpeechRecognition.ts +3 -1
  56. package/src/tasks/audio/textToSpeech.ts +2 -1
  57. package/src/tasks/custom/request.ts +3 -1
  58. package/src/tasks/custom/streamingRequest.ts +3 -1
  59. package/src/tasks/cv/imageClassification.ts +3 -1
  60. package/src/tasks/cv/imageSegmentation.ts +3 -1
  61. package/src/tasks/cv/imageToImage.ts +3 -1
  62. package/src/tasks/cv/imageToText.ts +3 -1
  63. package/src/tasks/cv/objectDetection.ts +3 -1
  64. package/src/tasks/cv/textToImage.ts +2 -1
  65. package/src/tasks/cv/textToVideo.ts +2 -1
  66. package/src/tasks/cv/zeroShotImageClassification.ts +3 -1
  67. package/src/tasks/multimodal/documentQuestionAnswering.ts +3 -1
  68. package/src/tasks/multimodal/visualQuestionAnswering.ts +3 -1
  69. package/src/tasks/nlp/chatCompletion.ts +3 -1
  70. package/src/tasks/nlp/chatCompletionStream.ts +3 -1
  71. package/src/tasks/nlp/featureExtraction.ts +3 -1
  72. package/src/tasks/nlp/fillMask.ts +3 -1
  73. package/src/tasks/nlp/questionAnswering.ts +4 -1
  74. package/src/tasks/nlp/sentenceSimilarity.ts +3 -1
  75. package/src/tasks/nlp/summarization.ts +3 -1
  76. package/src/tasks/nlp/tableQuestionAnswering.ts +3 -1
  77. package/src/tasks/nlp/textClassification.ts +3 -1
  78. package/src/tasks/nlp/textGeneration.ts +3 -1
  79. package/src/tasks/nlp/textGenerationStream.ts +3 -1
  80. package/src/tasks/nlp/tokenClassification.ts +3 -1
  81. package/src/tasks/nlp/translation.ts +3 -1
  82. package/src/tasks/nlp/zeroShotClassification.ts +3 -1
  83. package/src/tasks/tabular/tabularClassification.ts +3 -1
  84. package/src/tasks/tabular/tabularRegression.ts +3 -1
  85. package/src/types.ts +8 -4
  86. package/src/utils/typedEntries.ts +5 -0
@@ -1,4 +1,5 @@
1
1
  import type { TextGenerationInput } from "@huggingface/tasks";
2
+ import { resolveProvider } from "../../lib/getInferenceProviderMapping";
2
3
  import { getProviderHelper } from "../../lib/getProviderHelper";
3
4
  import type { BaseArgs, Options } from "../../types";
4
5
  import { innerStreamingRequest } from "../../utils/request";
@@ -90,7 +91,8 @@ export async function* textGenerationStream(
90
91
  args: BaseArgs & TextGenerationInput,
91
92
  options?: Options
92
93
  ): AsyncGenerator<TextGenerationStreamOutput> {
93
- const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "text-generation");
94
+ const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
95
+ const providerHelper = getProviderHelper(provider, "text-generation");
94
96
  yield* innerStreamingRequest<TextGenerationStreamOutput>(args, providerHelper, {
95
97
  ...options,
96
98
  task: "text-generation",
@@ -1,4 +1,5 @@
1
1
  import type { TokenClassificationInput, TokenClassificationOutput } from "@huggingface/tasks";
2
+ import { resolveProvider } from "../../lib/getInferenceProviderMapping";
2
3
  import { getProviderHelper } from "../../lib/getProviderHelper";
3
4
  import type { BaseArgs, Options } from "../../types";
4
5
  import { innerRequest } from "../../utils/request";
@@ -12,7 +13,8 @@ export async function tokenClassification(
12
13
  args: TokenClassificationArgs,
13
14
  options?: Options
14
15
  ): Promise<TokenClassificationOutput> {
15
- const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "token-classification");
16
+ const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
17
+ const providerHelper = getProviderHelper(provider, "token-classification");
16
18
  const { data: res } = await innerRequest<TokenClassificationOutput[number] | TokenClassificationOutput>(
17
19
  args,
18
20
  providerHelper,
@@ -1,4 +1,5 @@
1
1
  import type { TranslationInput, TranslationOutput } from "@huggingface/tasks";
2
+ import { resolveProvider } from "../../lib/getInferenceProviderMapping";
2
3
  import { getProviderHelper } from "../../lib/getProviderHelper";
3
4
  import type { BaseArgs, Options } from "../../types";
4
5
  import { innerRequest } from "../../utils/request";
@@ -8,7 +9,8 @@ export type TranslationArgs = BaseArgs & TranslationInput;
8
9
  * This task is well known to translate text from one language to another. Recommended model: Helsinki-NLP/opus-mt-ru-en.
9
10
  */
10
11
  export async function translation(args: TranslationArgs, options?: Options): Promise<TranslationOutput> {
11
- const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "translation");
12
+ const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
13
+ const providerHelper = getProviderHelper(provider, "translation");
12
14
  const { data: res } = await innerRequest<TranslationOutput>(args, providerHelper, {
13
15
  ...options,
14
16
  task: "translation",
@@ -1,4 +1,5 @@
1
1
  import type { ZeroShotClassificationInput, ZeroShotClassificationOutput } from "@huggingface/tasks";
2
+ import { resolveProvider } from "../../lib/getInferenceProviderMapping";
2
3
  import { getProviderHelper } from "../../lib/getProviderHelper";
3
4
  import type { BaseArgs, Options } from "../../types";
4
5
  import { innerRequest } from "../../utils/request";
@@ -12,7 +13,8 @@ export async function zeroShotClassification(
12
13
  args: ZeroShotClassificationArgs,
13
14
  options?: Options
14
15
  ): Promise<ZeroShotClassificationOutput> {
15
- const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "zero-shot-classification");
16
+ const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
17
+ const providerHelper = getProviderHelper(provider, "zero-shot-classification");
16
18
  const { data: res } = await innerRequest<ZeroShotClassificationOutput[number] | ZeroShotClassificationOutput>(
17
19
  args,
18
20
  providerHelper,
@@ -1,3 +1,4 @@
1
+ import { resolveProvider } from "../../lib/getInferenceProviderMapping";
1
2
  import { getProviderHelper } from "../../lib/getProviderHelper";
2
3
  import type { BaseArgs, Options } from "../../types";
3
4
  import { innerRequest } from "../../utils/request";
@@ -25,7 +26,8 @@ export async function tabularClassification(
25
26
  args: TabularClassificationArgs,
26
27
  options?: Options
27
28
  ): Promise<TabularClassificationOutput> {
28
- const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "tabular-classification");
29
+ const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
30
+ const providerHelper = getProviderHelper(provider, "tabular-classification");
29
31
  const { data: res } = await innerRequest<TabularClassificationOutput>(args, providerHelper, {
30
32
  ...options,
31
33
  task: "tabular-classification",
@@ -1,3 +1,4 @@
1
+ import { resolveProvider } from "../../lib/getInferenceProviderMapping";
1
2
  import { getProviderHelper } from "../../lib/getProviderHelper";
2
3
  import type { BaseArgs, Options } from "../../types";
3
4
  import { innerRequest } from "../../utils/request";
@@ -25,7 +26,8 @@ export async function tabularRegression(
25
26
  args: TabularRegressionArgs,
26
27
  options?: Options
27
28
  ): Promise<TabularRegressionOutput> {
28
- const providerHelper = getProviderHelper(args.provider ?? "hf-inference", "tabular-regression");
29
+ const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
30
+ const providerHelper = getProviderHelper(provider, "tabular-regression");
29
31
  const { data: res } = await innerRequest<TabularRegressionOutput>(args, providerHelper, {
30
32
  ...options,
31
33
  task: "tabular-regression",
package/src/types.ts CHANGED
@@ -57,8 +57,12 @@ export const INFERENCE_PROVIDERS = [
57
57
  "together",
58
58
  ] as const;
59
59
 
60
+ export const PROVIDERS_OR_POLICIES = [...INFERENCE_PROVIDERS, "auto"] as const;
61
+
60
62
  export type InferenceProvider = (typeof INFERENCE_PROVIDERS)[number];
61
63
 
64
+ export type InferenceProviderOrPolicy = (typeof PROVIDERS_OR_POLICIES)[number];
65
+
62
66
  export interface BaseArgs {
63
67
  /**
64
68
  * The access token to use. Without it, you'll get rate-limited quickly.
@@ -80,18 +84,18 @@ export interface BaseArgs {
80
84
  model?: ModelId;
81
85
 
82
86
  /**
83
- * The URL of the endpoint to use. If not specified, will call huggingface.co/api/tasks to get the default endpoint for the task.
87
+ * The URL of the endpoint to use.
84
88
  *
85
- * If specified, will use this URL instead of the default one.
89
+ * If not specified, will call the default router.huggingface.co Inference Providers endpoint.
86
90
  */
87
91
  endpointUrl?: string;
88
92
 
89
93
  /**
90
94
  * Set an Inference provider to run this model on.
91
95
  *
92
- * Defaults to the first provider in your user settings that is compatible with this model.
96
+ * Defaults to "auto" i.e. the first of the providers available for the model, sorted by the user's order in https://hf.co/settings/inference-providers.
93
97
  */
94
- provider?: InferenceProvider;
98
+ provider?: InferenceProviderOrPolicy;
95
99
  }
96
100
 
97
101
  export type RequestArgs = BaseArgs &
@@ -0,0 +1,5 @@
1
+ export function typedEntries<T extends { [s: string]: T[keyof T] } | ArrayLike<T[keyof T]>>(
2
+ obj: T
3
+ ): [keyof T, T[keyof T]][] {
4
+ return Object.entries(obj) as [keyof T, T[keyof T]][];
5
+ }