@huggingface/inference 2.8.1 → 3.0.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 (50) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +45 -17
  3. package/dist/index.cjs +388 -134
  4. package/dist/index.js +383 -134
  5. package/dist/src/config.d.ts +3 -0
  6. package/dist/src/config.d.ts.map +1 -0
  7. package/dist/src/index.d.ts +5 -0
  8. package/dist/src/index.d.ts.map +1 -1
  9. package/dist/src/lib/getDefaultTask.d.ts +0 -1
  10. package/dist/src/lib/getDefaultTask.d.ts.map +1 -1
  11. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  12. package/dist/src/providers/fal-ai.d.ts +6 -0
  13. package/dist/src/providers/fal-ai.d.ts.map +1 -0
  14. package/dist/src/providers/replicate.d.ts +6 -0
  15. package/dist/src/providers/replicate.d.ts.map +1 -0
  16. package/dist/src/providers/sambanova.d.ts +6 -0
  17. package/dist/src/providers/sambanova.d.ts.map +1 -0
  18. package/dist/src/providers/together.d.ts +12 -0
  19. package/dist/src/providers/together.d.ts.map +1 -0
  20. package/dist/src/providers/types.d.ts +4 -0
  21. package/dist/src/providers/types.d.ts.map +1 -0
  22. package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  23. package/dist/src/tasks/audio/textToSpeech.d.ts.map +1 -1
  24. package/dist/src/tasks/custom/request.d.ts +1 -1
  25. package/dist/src/tasks/custom/request.d.ts.map +1 -1
  26. package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
  27. package/dist/src/tasks/cv/textToImage.d.ts +8 -0
  28. package/dist/src/tasks/cv/textToImage.d.ts.map +1 -1
  29. package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
  30. package/dist/src/tasks/nlp/textGeneration.d.ts.map +1 -1
  31. package/dist/src/types.d.ts +16 -2
  32. package/dist/src/types.d.ts.map +1 -1
  33. package/package.json +2 -2
  34. package/src/config.ts +2 -0
  35. package/src/index.ts +5 -0
  36. package/src/lib/getDefaultTask.ts +1 -1
  37. package/src/lib/makeRequestOptions.ts +201 -59
  38. package/src/providers/fal-ai.ts +23 -0
  39. package/src/providers/replicate.ts +16 -0
  40. package/src/providers/sambanova.ts +23 -0
  41. package/src/providers/together.ts +60 -0
  42. package/src/providers/types.ts +6 -0
  43. package/src/tasks/audio/automaticSpeechRecognition.ts +10 -1
  44. package/src/tasks/audio/textToSpeech.ts +17 -2
  45. package/src/tasks/custom/request.ts +12 -6
  46. package/src/tasks/custom/streamingRequest.ts +18 -3
  47. package/src/tasks/cv/textToImage.ts +44 -1
  48. package/src/tasks/nlp/chatCompletion.ts +2 -2
  49. package/src/tasks/nlp/textGeneration.ts +43 -9
  50. package/src/types.ts +20 -2
@@ -1,5 +1,10 @@
1
+ export type { ProviderMapping } from "./providers/types";
1
2
  export { HfInference, HfInferenceEndpoint } from "./HfInference";
2
3
  export { InferenceOutputError } from "./lib/InferenceOutputError";
4
+ export { FAL_AI_SUPPORTED_MODEL_IDS } from "./providers/fal-ai";
5
+ export { REPLICATE_SUPPORTED_MODEL_IDS } from "./providers/replicate";
6
+ export { SAMBANOVA_SUPPORTED_MODEL_IDS } from "./providers/sambanova";
7
+ export { TOGETHER_SUPPORTED_MODEL_IDS } from "./providers/together";
3
8
  export * from "./types";
4
9
  export * from "./tasks";
5
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EAAE,6BAA6B,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,6BAA6B,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,EAAE,4BAA4B,EAAE,MAAM,sBAAsB,CAAC;AACpE,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
@@ -1,4 +1,3 @@
1
- export declare const HF_HUB_URL = "https://huggingface.co";
2
1
  export interface DefaultTaskOptions {
3
2
  fetch?: typeof fetch;
4
3
  }
@@ -1 +1 @@
1
- {"version":3,"file":"getDefaultTask.d.ts","sourceRoot":"","sources":["../../../src/lib/getDefaultTask.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,UAAU,2BAA2B,CAAC;AAEnD,MAAM,WAAW,kBAAkB;IAClC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CACnC,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,OAAO,CAAC,EAAE,kBAAkB,GAC1B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAkCxB"}
1
+ {"version":3,"file":"getDefaultTask.d.ts","sourceRoot":"","sources":["../../../src/lib/getDefaultTask.ts"],"names":[],"mappings":"AAYA,MAAM,WAAW,kBAAkB;IAClC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;CACrB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CACnC,KAAK,EAAE,MAAM,EACb,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,OAAO,CAAC,EAAE,kBAAkB,GAC1B,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAkCxB"}
@@ -1 +1 @@
1
- {"version":3,"file":"makeRequestOptions.d.ts","sourceRoot":"","sources":["../../../src/lib/makeRequestOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAYpE;;GAEG;AACH,wBAAsB,kBAAkB,CACvC,IAAI,EAAE,WAAW,GAAG;IACnB,IAAI,CAAC,EAAE,IAAI,GAAG,WAAW,CAAC;IAC1B,MAAM,CAAC,EAAE,OAAO,CAAC;CACjB,EACD,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,yFAAyF;IACzF,SAAS,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;IACnC,sCAAsC;IACtC,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,WAAW,CAAA;CAAE,CAAC,CAkG7C"}
1
+ {"version":3,"file":"makeRequestOptions.d.ts","sourceRoot":"","sources":["../../../src/lib/makeRequestOptions.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAWpE;;GAEG;AACH,wBAAsB,kBAAkB,CACvC,IAAI,EAAE,WAAW,GAAG;IACnB,IAAI,CAAC,EAAE,IAAI,GAAG,WAAW,CAAC;IAC1B,MAAM,CAAC,EAAE,OAAO,CAAC;CACjB,EACD,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,yFAAyF;IACzF,SAAS,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;IACnC,sCAAsC;IACtC,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,WAAW,CAAA;CAAE,CAAC,CAiH7C"}
@@ -0,0 +1,6 @@
1
+ import type { ProviderMapping } from "./types";
2
+ export declare const FAL_AI_API_BASE_URL = "https://fal.run";
3
+ type FalAiId = string;
4
+ export declare const FAL_AI_SUPPORTED_MODEL_IDS: ProviderMapping<FalAiId>;
5
+ export {};
6
+ //# sourceMappingURL=fal-ai.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fal-ai.d.ts","sourceRoot":"","sources":["../../../src/providers/fal-ai.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,eAAO,MAAM,mBAAmB,oBAAoB,CAAC;AAErD,KAAK,OAAO,GAAG,MAAM,CAAC;AAEtB,eAAO,MAAM,0BAA0B,EAAE,eAAe,CAAC,OAAO,CAgB/D,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { ProviderMapping } from "./types";
2
+ export declare const REPLICATE_API_BASE_URL = "https://api.replicate.com";
3
+ type ReplicateId = string;
4
+ export declare const REPLICATE_SUPPORTED_MODEL_IDS: ProviderMapping<ReplicateId>;
5
+ export {};
6
+ //# sourceMappingURL=replicate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replicate.d.ts","sourceRoot":"","sources":["../../../src/providers/replicate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,eAAO,MAAM,sBAAsB,8BAA8B,CAAC;AAElE,KAAK,WAAW,GAAG,MAAM,CAAC;AAE1B,eAAO,MAAM,6BAA6B,EAAE,eAAe,CAAC,WAAW,CAStE,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { ProviderMapping } from "./types";
2
+ export declare const SAMBANOVA_API_BASE_URL = "https://api.sambanova.ai";
3
+ type SambanovaId = string;
4
+ export declare const SAMBANOVA_SUPPORTED_MODEL_IDS: ProviderMapping<SambanovaId>;
5
+ export {};
6
+ //# sourceMappingURL=sambanova.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sambanova.d.ts","sourceRoot":"","sources":["../../../src/providers/sambanova.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,eAAO,MAAM,sBAAsB,6BAA6B,CAAC;AAEjE,KAAK,WAAW,GAAG,MAAM,CAAC;AAE1B,eAAO,MAAM,6BAA6B,EAAE,eAAe,CAAC,WAAW,CAgBtE,CAAC"}
@@ -0,0 +1,12 @@
1
+ import type { ProviderMapping } from "./types";
2
+ export declare const TOGETHER_API_BASE_URL = "https://api.together.xyz";
3
+ /**
4
+ * Same comment as in sambanova.ts
5
+ */
6
+ type TogetherId = string;
7
+ /**
8
+ * https://docs.together.ai/reference/models-1
9
+ */
10
+ export declare const TOGETHER_SUPPORTED_MODEL_IDS: ProviderMapping<TogetherId>;
11
+ export {};
12
+ //# sourceMappingURL=together.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"together.d.ts","sourceRoot":"","sources":["../../../src/providers/together.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAE/C,eAAO,MAAM,qBAAqB,6BAA6B,CAAC;AAEhE;;GAEG;AACH,KAAK,UAAU,GAAG,MAAM,CAAC;AAEzB;;GAEG;AACH,eAAO,MAAM,4BAA4B,EAAE,eAAe,CAAC,UAAU,CA+CpE,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { WidgetType } from "@huggingface/tasks";
2
+ import type { ModelId } from "../types";
3
+ export type ProviderMapping<ProviderId extends string> = Partial<Record<WidgetType, Partial<Record<ModelId, ProviderId>>>>;
4
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/providers/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAExC,MAAM,MAAM,eAAe,CAAC,UAAU,SAAS,MAAM,IAAI,OAAO,CAC/D,MAAM,CAAC,UAAU,EAAE,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,CACxD,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"automaticSpeechRecognition.d.ts","sourceRoot":"","sources":["../../../../src/tasks/audio/automaticSpeechRecognition.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGrD,MAAM,MAAM,8BAA8B,GAAG,QAAQ,GAAG;IACvD;;OAEG;IACH,IAAI,EAAE,IAAI,GAAG,WAAW,CAAC;CACzB,CAAC;AAEF,MAAM,WAAW,gCAAgC;IAChD;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACb;AAED;;;GAGG;AACH,wBAAsB,0BAA0B,CAC/C,IAAI,EAAE,8BAA8B,EACpC,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,gCAAgC,CAAC,CAU3C"}
1
+ {"version":3,"file":"automaticSpeechRecognition.d.ts","sourceRoot":"","sources":["../../../../src/tasks/audio/automaticSpeechRecognition.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAe,MAAM,aAAa,CAAC;AAIlE,MAAM,MAAM,8BAA8B,GAAG,QAAQ,GAAG;IACvD;;OAEG;IACH,IAAI,EAAE,IAAI,GAAG,WAAW,CAAC;CACzB,CAAC;AAEF,MAAM,WAAW,gCAAgC;IAChD;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACb;AAED;;;GAGG;AACH,wBAAsB,0BAA0B,CAC/C,IAAI,EAAE,8BAA8B,EACpC,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,gCAAgC,CAAC,CAkB3C"}
@@ -1 +1 @@
1
- {"version":3,"file":"textToSpeech.d.ts","sourceRoot":"","sources":["../../../../src/tasks/audio/textToSpeech.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGrD,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG;IACzC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAEtC;;;GAGG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAUzG"}
1
+ {"version":3,"file":"textToSpeech.d.ts","sourceRoot":"","sources":["../../../../src/tasks/audio/textToSpeech.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGrD,MAAM,MAAM,gBAAgB,GAAG,QAAQ,GAAG;IACzC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG,IAAI,CAAC;AAItC;;;GAGG;AACH,wBAAsB,YAAY,CAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,kBAAkB,CAAC,CAuBzG"}
@@ -1,6 +1,6 @@
1
1
  import type { InferenceTask, Options, RequestArgs } from "../../types";
2
2
  /**
3
- * Primitive to make custom calls to Inference Endpoints
3
+ * Primitive to make custom calls to the inference provider
4
4
  */
5
5
  export declare function request<T>(args: RequestArgs, options?: Options & {
6
6
  /** When a model can be used for multiple tasks, and we want to run a non-default task */
@@ -1 +1 @@
1
- {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../../src/tasks/custom/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAGvE;;GAEG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC9B,IAAI,EAAE,WAAW,EACjB,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,yFAAyF;IACzF,IAAI,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;IAC9B,sCAAsC;IACtC,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,oCAAoC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,OAAO,CAAC,CAAC,CAAC,CA6BZ"}
1
+ {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../../src/tasks/custom/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAGvE;;GAEG;AACH,wBAAsB,OAAO,CAAC,CAAC,EAC9B,IAAI,EAAE,WAAW,EACjB,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,yFAAyF;IACzF,IAAI,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;IAC9B,sCAAsC;IACtC,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,oCAAoC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,OAAO,CAAC,CAAC,CAAC,CAmCZ"}
@@ -1 +1 @@
1
- {"version":3,"file":"streamingRequest.d.ts","sourceRoot":"","sources":["../../../../src/tasks/custom/streamingRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAKvE;;GAEG;AACH,wBAAuB,gBAAgB,CAAC,CAAC,EACxC,IAAI,EAAE,WAAW,EACjB,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,yFAAyF;IACzF,IAAI,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;IAC9B,sCAAsC;IACtC,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,oCAAoC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,cAAc,CAAC,CAAC,CAAC,CAuEnB"}
1
+ {"version":3,"file":"streamingRequest.d.ts","sourceRoot":"","sources":["../../../../src/tasks/custom/streamingRequest.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAKvE;;GAEG;AACH,wBAAuB,gBAAgB,CAAC,CAAC,EACxC,IAAI,EAAE,WAAW,EACjB,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,yFAAyF;IACzF,IAAI,CAAC,EAAE,MAAM,GAAG,aAAa,CAAC;IAC9B,sCAAsC;IACtC,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,oCAAoC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,cAAc,CAAC,CAAC,CAAC,CAsFnB"}
@@ -4,6 +4,14 @@ export type TextToImageArgs = BaseArgs & {
4
4
  * The text to generate an image from
5
5
  */
6
6
  inputs: string;
7
+ /**
8
+ * Same param but for external providers like Together, Replicate
9
+ */
10
+ prompt?: string;
11
+ response_format?: "base64";
12
+ input?: {
13
+ prompt: string;
14
+ };
7
15
  parameters?: {
8
16
  /**
9
17
  * An optional negative prompt for the image generation
@@ -1 +1 @@
1
- {"version":3,"file":"textToImage.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToImage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGrD,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG;IACxC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf,UAAU,CAAC,EAAE;QACZ;;WAEG;QACH,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB;;WAEG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB;;WAEG;QACH,KAAK,CAAC,EAAE,MAAM,CAAC;QACf;;WAEG;QACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;QAC7B;;WAEG;QACH,cAAc,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;CACF,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAErC;;;GAGG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAUtG"}
1
+ {"version":3,"file":"textToImage.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToImage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGrD,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG;IACxC;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,eAAe,CAAC,EAAE,QAAQ,CAAC;IAC3B,KAAK,CAAC,EAAE;QACP,MAAM,EAAE,MAAM,CAAC;KACf,CAAC;IAEF,UAAU,CAAC,EAAE;QACZ;;WAEG;QACH,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB;;WAEG;QACH,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB;;WAEG;QACH,KAAK,CAAC,EAAE,MAAM,CAAC;QACf;;WAEG;QACH,mBAAmB,CAAC,EAAE,MAAM,CAAC;QAC7B;;WAEG;QACH,cAAc,CAAC,EAAE,MAAM,CAAC;KACxB,CAAC;CACF,CAAC;AAEF,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAWrC;;;GAGG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAmCtG"}
@@ -1 +1 @@
1
- {"version":3,"file":"chatCompletion.d.ts","sourceRoot":"","sources":["../../../../src/tasks/nlp/chatCompletion.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAEpF;;GAEG;AAEH,wBAAsB,cAAc,CACnC,IAAI,EAAE,QAAQ,GAAG,mBAAmB,EACpC,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,oBAAoB,CAAC,CAmB/B"}
1
+ {"version":3,"file":"chatCompletion.d.ts","sourceRoot":"","sources":["../../../../src/tasks/nlp/chatCompletion.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAErD,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAEpF;;GAEG;AACH,wBAAsB,cAAc,CACnC,IAAI,EAAE,QAAQ,GAAG,mBAAmB,EACpC,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,oBAAoB,CAAC,CAoB/B"}
@@ -1 +1 @@
1
- {"version":3,"file":"textGeneration.d.ts","sourceRoot":"","sources":["../../../../src/tasks/nlp/textGeneration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAEpF,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAIrD,YAAY,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,CAAC;AAE1D;;GAEG;AACH,wBAAsB,cAAc,CACnC,IAAI,EAAE,QAAQ,GAAG,mBAAmB,EACpC,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,oBAAoB,CAAC,CAY/B"}
1
+ {"version":3,"file":"textGeneration.d.ts","sourceRoot":"","sources":["../../../../src/tasks/nlp/textGeneration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEX,mBAAmB,EACnB,oBAAoB,EAEpB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAIrD,YAAY,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,CAAC;AAY1D;;GAEG;AACH,wBAAsB,cAAc,CACnC,IAAI,EAAE,QAAQ,GAAG,mBAAmB,EACpC,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,oBAAoB,CAAC,CA+B/B"}
@@ -1,5 +1,9 @@
1
1
  import type { PipelineType } from "@huggingface/tasks";
2
2
  import type { ChatCompletionInput } from "@huggingface/tasks";
3
+ /**
4
+ * HF model id, like "meta-llama/Llama-3.3-70B-Instruct"
5
+ */
6
+ export type ModelId = string;
3
7
  export interface Options {
4
8
  /**
5
9
  * (Default: true) Boolean. If a request 503s and wait_for_model is set to false, the request will be retried with the same parameters but with wait_for_model set to true.
@@ -35,28 +39,38 @@ export interface Options {
35
39
  includeCredentials?: string | boolean;
36
40
  }
37
41
  export type InferenceTask = Exclude<PipelineType, "other">;
42
+ export declare const INFERENCE_PROVIDERS: readonly ["fal-ai", "replicate", "sambanova", "together", "hf-inference"];
43
+ export type InferenceProvider = (typeof INFERENCE_PROVIDERS)[number];
38
44
  export interface BaseArgs {
39
45
  /**
40
46
  * The access token to use. Without it, you'll get rate-limited quickly.
41
47
  *
42
48
  * Can be created for free in hf.co/settings/token
49
+ *
50
+ * You can also pass an external Inference provider's key if you intend to call a compatible provider like Sambanova, Together, Replicate...
43
51
  */
44
52
  accessToken?: string;
45
53
  /**
46
- * The model to use.
54
+ * The HF model to use.
47
55
  *
48
56
  * If not specified, will call huggingface.co/api/tasks to get the default model for the task.
49
57
  *
50
58
  * /!\ Legacy behavior allows this to be an URL, but this is deprecated and will be removed in the future.
51
59
  * Use the `endpointUrl` parameter instead.
52
60
  */
53
- model?: string;
61
+ model?: ModelId;
54
62
  /**
55
63
  * The URL of the endpoint to use. If not specified, will call huggingface.co/api/tasks to get the default endpoint for the task.
56
64
  *
57
65
  * If specified, will use this URL instead of the default one.
58
66
  */
59
67
  endpointUrl?: string;
68
+ /**
69
+ * Set an Inference provider to run this model on.
70
+ *
71
+ * Defaults to the first provider in your user settings that is compatible with this model.
72
+ */
73
+ provider?: InferenceProvider;
60
74
  }
61
75
  export type RequestArgs = BaseArgs & ({
62
76
  data: Blob | ArrayBuffer;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAE9D,MAAM,WAAW,OAAO;IACvB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;OAEG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB;;OAEG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IAErB;;OAEG;IACH,kBAAkB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CACtC;AAED,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;AAE3D,MAAM,WAAW,QAAQ;IACxB;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,MAAM,WAAW,GAAG,QAAQ,GACjC,CAAC;IAAE,IAAI,EAAE,IAAI,GAAG,WAAW,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,GAAG,mBAAmB,CAAC,GAAG;IAC5E,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAE9D;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAE7B,MAAM,WAAW,OAAO;IACvB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;OAEG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB;;OAEG;IACH,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;OAEG;IACH,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB;;OAEG;IACH,MAAM,CAAC,EAAE,WAAW,CAAC;IAErB;;OAEG;IACH,kBAAkB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CACtC;AAED,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;AAE3D,eAAO,MAAM,mBAAmB,2EAA4E,CAAC;AAC7G,MAAM,MAAM,iBAAiB,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAErE,MAAM,WAAW,QAAQ;IACxB;;;;;;OAMG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;;;;;OAOG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;;;OAIG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,iBAAiB,CAAC;CAC7B;AAED,MAAM,MAAM,WAAW,GAAG,QAAQ,GACjC,CAAC;IAAE,IAAI,EAAE,IAAI,GAAG,WAAW,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,OAAO,CAAA;CAAE,GAAG,mBAAmB,CAAC,GAAG;IAC5E,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/inference",
3
- "version": "2.8.1",
3
+ "version": "3.0.1",
4
4
  "packageManager": "pnpm@8.10.5",
5
5
  "license": "MIT",
6
6
  "author": "Tim Mikeladze <tim.mikeladze@gmail.com>",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "type": "module",
41
41
  "dependencies": {
42
- "@huggingface/tasks": "^0.12.9"
42
+ "@huggingface/tasks": "^0.13.17"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/node": "18.13.0"
package/src/config.ts ADDED
@@ -0,0 +1,2 @@
1
+ export const HF_HUB_URL = "https://huggingface.co";
2
+ export const HF_INFERENCE_API_URL = "https://api-inference.huggingface.co";
package/src/index.ts CHANGED
@@ -1,4 +1,9 @@
1
+ export type { ProviderMapping } from "./providers/types";
1
2
  export { HfInference, HfInferenceEndpoint } from "./HfInference";
2
3
  export { InferenceOutputError } from "./lib/InferenceOutputError";
4
+ export { FAL_AI_SUPPORTED_MODEL_IDS } from "./providers/fal-ai";
5
+ export { REPLICATE_SUPPORTED_MODEL_IDS } from "./providers/replicate";
6
+ export { SAMBANOVA_SUPPORTED_MODEL_IDS } from "./providers/sambanova";
7
+ export { TOGETHER_SUPPORTED_MODEL_IDS } from "./providers/together";
3
8
  export * from "./types";
4
9
  export * from "./tasks";
@@ -1,3 +1,4 @@
1
+ import { HF_HUB_URL } from "../config";
1
2
  import { isUrl } from "./isUrl";
2
3
 
3
4
  /**
@@ -8,7 +9,6 @@ import { isUrl } from "./isUrl";
8
9
  const taskCache = new Map<string, { task: string; date: Date }>();
9
10
  const CACHE_DURATION = 10 * 60 * 1000;
10
11
  const MAX_CACHE_ITEMS = 1000;
11
- export const HF_HUB_URL = "https://huggingface.co";
12
12
 
13
13
  export interface DefaultTaskOptions {
14
14
  fetch?: typeof fetch;
@@ -1,12 +1,18 @@
1
+ import type { WidgetType } from "@huggingface/tasks";
2
+ import { HF_HUB_URL, HF_INFERENCE_API_URL } from "../config";
3
+ import { FAL_AI_API_BASE_URL, FAL_AI_SUPPORTED_MODEL_IDS } from "../providers/fal-ai";
4
+ import { REPLICATE_API_BASE_URL, REPLICATE_SUPPORTED_MODEL_IDS } from "../providers/replicate";
5
+ import { SAMBANOVA_API_BASE_URL, SAMBANOVA_SUPPORTED_MODEL_IDS } from "../providers/sambanova";
6
+ import { TOGETHER_API_BASE_URL, TOGETHER_SUPPORTED_MODEL_IDS } from "../providers/together";
7
+ import type { InferenceProvider } from "../types";
1
8
  import type { InferenceTask, Options, RequestArgs } from "../types";
2
- import { omit } from "../utils/omit";
3
- import { HF_HUB_URL } from "./getDefaultTask";
4
9
  import { isUrl } from "./isUrl";
5
10
 
6
- const HF_INFERENCE_API_BASE_URL = "https://api-inference.huggingface.co";
11
+ const HF_HUB_INFERENCE_PROXY_TEMPLATE = `${HF_HUB_URL}/api/inference-proxy/{{PROVIDER}}`;
7
12
 
8
13
  /**
9
- * Loaded from huggingface.co/api/tasks if needed
14
+ * Lazy-loaded from huggingface.co/api/tasks when needed
15
+ * Used to determine the default model to use when it's not user defined
10
16
  */
11
17
  let tasks: Record<string, { models: { id: string }[] }> | null = null;
12
18
 
@@ -26,40 +32,61 @@ export async function makeRequestOptions(
26
32
  chatCompletion?: boolean;
27
33
  }
28
34
  ): Promise<{ url: string; info: RequestInit }> {
29
- const { accessToken, endpointUrl, ...otherArgs } = args;
30
- let { model } = args;
31
- const {
32
- forceTask: task,
33
- includeCredentials,
34
- taskHint,
35
- wait_for_model,
36
- use_cache,
37
- dont_load_model,
38
- chatCompletion,
39
- } = options ?? {};
35
+ const { accessToken, endpointUrl, provider: maybeProvider, model: maybeModel, ...remainingArgs } = args;
36
+ let otherArgs = remainingArgs;
37
+ const provider = maybeProvider ?? "hf-inference";
40
38
 
41
- const headers: Record<string, string> = {};
42
- if (accessToken) {
43
- headers["Authorization"] = `Bearer ${accessToken}`;
44
- }
45
-
46
- if (!model && !tasks && taskHint) {
47
- const res = await fetch(`${HF_HUB_URL}/api/tasks`);
39
+ const { forceTask, includeCredentials, taskHint, wait_for_model, use_cache, dont_load_model, chatCompletion } =
40
+ options ?? {};
48
41
 
49
- if (res.ok) {
50
- tasks = await res.json();
51
- }
42
+ if (endpointUrl && provider !== "hf-inference") {
43
+ throw new Error(`Cannot use endpointUrl with a third-party provider.`);
44
+ }
45
+ if (forceTask && provider !== "hf-inference") {
46
+ throw new Error(`Cannot use forceTask with a third-party provider.`);
47
+ }
48
+ if (maybeModel && isUrl(maybeModel)) {
49
+ throw new Error(`Model URLs are no longer supported. Use endpointUrl instead.`);
52
50
  }
53
51
 
54
- if (!model && tasks && taskHint) {
55
- const taskInfo = tasks[taskHint];
56
- if (taskInfo) {
57
- model = taskInfo.models[0].id;
52
+ let model: string;
53
+ if (!maybeModel) {
54
+ if (taskHint) {
55
+ model = mapModel({ model: await loadDefaultModel(taskHint), provider, taskHint, chatCompletion });
56
+ } else {
57
+ throw new Error("No model provided, and no default model found for this task");
58
+ /// TODO : change error message ^
58
59
  }
60
+ } else {
61
+ model = mapModel({ model: maybeModel, provider, taskHint, chatCompletion });
59
62
  }
60
63
 
61
- if (!model) {
62
- throw new Error("No model provided, and no default model found for this task");
64
+ /// If accessToken is passed, it should take precedence over includeCredentials
65
+ const authMethod = accessToken
66
+ ? accessToken.startsWith("hf_")
67
+ ? "hf-token"
68
+ : "provider-key"
69
+ : includeCredentials === "include"
70
+ ? "credentials-include"
71
+ : "none";
72
+
73
+ const url = endpointUrl
74
+ ? chatCompletion
75
+ ? endpointUrl + `/v1/chat/completions`
76
+ : endpointUrl
77
+ : makeUrl({
78
+ authMethod,
79
+ chatCompletion: chatCompletion ?? false,
80
+ forceTask,
81
+ model,
82
+ provider: provider ?? "hf-inference",
83
+ taskHint,
84
+ });
85
+
86
+ const headers: Record<string, string> = {};
87
+ if (accessToken) {
88
+ headers["Authorization"] =
89
+ provider === "fal-ai" && authMethod === "provider-key" ? `Key ${accessToken}` : `Bearer ${accessToken}`;
63
90
  }
64
91
 
65
92
  const binary = "data" in args && !!args.data;
@@ -68,36 +95,20 @@ export async function makeRequestOptions(
68
95
  headers["Content-Type"] = "application/json";
69
96
  }
70
97
 
71
- if (wait_for_model) {
72
- headers["X-Wait-For-Model"] = "true";
73
- }
74
- if (use_cache === false) {
75
- headers["X-Use-Cache"] = "false";
76
- }
77
- if (dont_load_model) {
78
- headers["X-Load-Model"] = "0";
79
- }
80
-
81
- let url = (() => {
82
- if (endpointUrl && isUrl(model)) {
83
- throw new TypeError("Both model and endpointUrl cannot be URLs");
98
+ if (provider === "hf-inference") {
99
+ if (wait_for_model) {
100
+ headers["X-Wait-For-Model"] = "true";
84
101
  }
85
- if (isUrl(model)) {
86
- console.warn("Using a model URL is deprecated, please use the `endpointUrl` parameter instead");
87
- return model;
102
+ if (use_cache === false) {
103
+ headers["X-Use-Cache"] = "false";
88
104
  }
89
- if (endpointUrl) {
90
- return endpointUrl;
105
+ if (dont_load_model) {
106
+ headers["X-Load-Model"] = "0";
91
107
  }
92
- if (task) {
93
- return `${HF_INFERENCE_API_BASE_URL}/pipeline/${task}/${model}`;
94
- }
95
-
96
- return `${HF_INFERENCE_API_BASE_URL}/models/${model}`;
97
- })();
108
+ }
98
109
 
99
- if (chatCompletion && !url.endsWith("/chat/completions")) {
100
- url += "/v1/chat/completions";
110
+ if (provider === "replicate") {
111
+ headers["Prefer"] = "wait";
101
112
  }
102
113
 
103
114
  /**
@@ -110,17 +121,148 @@ export async function makeRequestOptions(
110
121
  credentials = "include";
111
122
  }
112
123
 
124
+ /**
125
+ * Replicate models wrap all inputs inside { input: ... }
126
+ * Versioned Replicate models in the format `owner/model:version` expect the version in the body
127
+ */
128
+ if (provider === "replicate") {
129
+ const version = model.includes(":") ? model.split(":")[1] : undefined;
130
+ (otherArgs as unknown) = { input: otherArgs, version };
131
+ }
132
+
113
133
  const info: RequestInit = {
114
134
  headers,
115
135
  method: "POST",
116
136
  body: binary
117
137
  ? args.data
118
138
  : JSON.stringify({
119
- ...(otherArgs.model && isUrl(otherArgs.model) ? omit(otherArgs, "model") : otherArgs),
139
+ ...otherArgs,
140
+ ...(chatCompletion || provider === "together" ? { model } : undefined),
120
141
  }),
121
- ...(credentials && { credentials }),
142
+ ...(credentials ? { credentials } : undefined),
122
143
  signal: options?.signal,
123
144
  };
124
145
 
125
146
  return { url, info };
126
147
  }
148
+
149
+ function mapModel(params: {
150
+ model: string;
151
+ provider: InferenceProvider;
152
+ taskHint: InferenceTask | undefined;
153
+ chatCompletion: boolean | undefined;
154
+ }): string {
155
+ if (params.provider === "hf-inference") {
156
+ return params.model;
157
+ }
158
+ if (!params.taskHint) {
159
+ throw new Error("taskHint must be specified when using a third-party provider");
160
+ }
161
+ const task: WidgetType =
162
+ params.taskHint === "text-generation" && params.chatCompletion ? "conversational" : params.taskHint;
163
+ const model = (() => {
164
+ switch (params.provider) {
165
+ case "fal-ai":
166
+ return FAL_AI_SUPPORTED_MODEL_IDS[task]?.[params.model];
167
+ case "replicate":
168
+ return REPLICATE_SUPPORTED_MODEL_IDS[task]?.[params.model];
169
+ case "sambanova":
170
+ return SAMBANOVA_SUPPORTED_MODEL_IDS[task]?.[params.model];
171
+ case "together":
172
+ return TOGETHER_SUPPORTED_MODEL_IDS[task]?.[params.model];
173
+ }
174
+ })();
175
+
176
+ if (!model) {
177
+ throw new Error(`Model ${params.model} is not supported for task ${task} and provider ${params.provider}`);
178
+ }
179
+ return model;
180
+ }
181
+
182
+ function makeUrl(params: {
183
+ authMethod: "none" | "hf-token" | "credentials-include" | "provider-key";
184
+ chatCompletion: boolean;
185
+ model: string;
186
+ provider: InferenceProvider;
187
+ taskHint: InferenceTask | undefined;
188
+ forceTask?: string | InferenceTask;
189
+ }): string {
190
+ if (params.authMethod === "none" && params.provider !== "hf-inference") {
191
+ throw new Error("Authentication is required when requesting a third-party provider. Please provide accessToken");
192
+ }
193
+
194
+ const shouldProxy = params.provider !== "hf-inference" && params.authMethod !== "provider-key";
195
+ switch (params.provider) {
196
+ case "fal-ai": {
197
+ const baseUrl = shouldProxy
198
+ ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", params.provider)
199
+ : FAL_AI_API_BASE_URL;
200
+ return `${baseUrl}/${params.model}`;
201
+ }
202
+ case "replicate": {
203
+ const baseUrl = shouldProxy
204
+ ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", params.provider)
205
+ : REPLICATE_API_BASE_URL;
206
+ if (params.model.includes(":")) {
207
+ /// Versioned model
208
+ return `${baseUrl}/v1/predictions`;
209
+ }
210
+ /// Evergreen / Canonical model
211
+ return `${baseUrl}/v1/models/${params.model}/predictions`;
212
+ }
213
+ case "sambanova": {
214
+ const baseUrl = shouldProxy
215
+ ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", params.provider)
216
+ : SAMBANOVA_API_BASE_URL;
217
+ /// Sambanova API matches OpenAI-like APIs: model is defined in the request body
218
+ if (params.taskHint === "text-generation" && params.chatCompletion) {
219
+ return `${baseUrl}/v1/chat/completions`;
220
+ }
221
+ return baseUrl;
222
+ }
223
+ case "together": {
224
+ const baseUrl = shouldProxy
225
+ ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", params.provider)
226
+ : TOGETHER_API_BASE_URL;
227
+ /// Together API matches OpenAI-like APIs: model is defined in the request body
228
+ if (params.taskHint === "text-to-image") {
229
+ return `${baseUrl}/v1/images/generations`;
230
+ }
231
+ if (params.taskHint === "text-generation") {
232
+ if (params.chatCompletion) {
233
+ return `${baseUrl}/v1/chat/completions`;
234
+ }
235
+ return `${baseUrl}/v1/completions`;
236
+ }
237
+ return baseUrl;
238
+ }
239
+ default: {
240
+ const url = params.forceTask
241
+ ? `${HF_INFERENCE_API_URL}/pipeline/${params.forceTask}/${params.model}`
242
+ : `${HF_INFERENCE_API_URL}/models/${params.model}`;
243
+ if (params.taskHint === "text-generation" && params.chatCompletion) {
244
+ return url + `/v1/chat/completions`;
245
+ }
246
+ return url;
247
+ }
248
+ }
249
+ }
250
+ async function loadDefaultModel(task: InferenceTask): Promise<string> {
251
+ if (!tasks) {
252
+ tasks = await loadTaskInfo();
253
+ }
254
+ const taskInfo = tasks[task];
255
+ if ((taskInfo?.models.length ?? 0) <= 0) {
256
+ throw new Error(`No default model defined for task ${task}, please define the model explicitly.`);
257
+ }
258
+ return taskInfo.models[0].id;
259
+ }
260
+
261
+ async function loadTaskInfo(): Promise<Record<string, { models: { id: string }[] }>> {
262
+ const res = await fetch(`${HF_HUB_URL}/api/tasks`);
263
+
264
+ if (!res.ok) {
265
+ throw new Error("Failed to load tasks definitions from Hugging Face Hub.");
266
+ }
267
+ return await res.json();
268
+ }
@@ -0,0 +1,23 @@
1
+ import type { ProviderMapping } from "./types";
2
+
3
+ export const FAL_AI_API_BASE_URL = "https://fal.run";
4
+
5
+ type FalAiId = string;
6
+
7
+ export const FAL_AI_SUPPORTED_MODEL_IDS: ProviderMapping<FalAiId> = {
8
+ "text-to-image": {
9
+ "black-forest-labs/FLUX.1-schnell": "fal-ai/flux/schnell",
10
+ "black-forest-labs/FLUX.1-dev": "fal-ai/flux/dev",
11
+ "playgroundai/playground-v2.5-1024px-aesthetic": "fal-ai/playground-v25",
12
+ "ByteDance/SDXL-Lightning": "fal-ai/lightning-models",
13
+ "PixArt-alpha/PixArt-Sigma-XL-2-1024-MS": "fal-ai/pixart-sigma",
14
+ "stabilityai/stable-diffusion-3-medium": "fal-ai/stable-diffusion-v3-medium",
15
+ "Warlord-K/Sana-1024": "fal-ai/sana",
16
+ "fal/AuraFlow-v0.2": "fal-ai/aura-flow",
17
+ "stabilityai/stable-diffusion-3.5-large": "fal-ai/stable-diffusion-v35-large",
18
+ "Kwai-Kolors/Kolors": "fal-ai/kolors",
19
+ },
20
+ "automatic-speech-recognition": {
21
+ "openai/whisper-large-v3": "fal-ai/whisper",
22
+ },
23
+ };
@@ -0,0 +1,16 @@
1
+ import type { ProviderMapping } from "./types";
2
+
3
+ export const REPLICATE_API_BASE_URL = "https://api.replicate.com";
4
+
5
+ type ReplicateId = string;
6
+
7
+ export const REPLICATE_SUPPORTED_MODEL_IDS: ProviderMapping<ReplicateId> = {
8
+ "text-to-image": {
9
+ "black-forest-labs/FLUX.1-schnell": "black-forest-labs/flux-schnell",
10
+ "ByteDance/SDXL-Lightning":
11
+ "bytedance/sdxl-lightning-4step:5599ed30703defd1d160a25a63321b4dec97101d98b4674bcc56e41f62f35637",
12
+ },
13
+ "text-to-speech": {
14
+ "OuteAI/OuteTTS-0.3-500M": "jbilcke/oute-tts:39a59319327b27327fa3095149c5a746e7f2aee18c75055c3368237a6503cd26",
15
+ },
16
+ };