@huggingface/inference 3.6.0 → 3.6.2

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 (92) hide show
  1. package/dist/index.cjs +240 -71
  2. package/dist/index.js +240 -60
  3. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  4. package/dist/src/providers/black-forest-labs.d.ts.map +1 -1
  5. package/dist/src/providers/cerebras.d.ts.map +1 -1
  6. package/dist/src/providers/cohere.d.ts.map +1 -1
  7. package/dist/src/providers/fal-ai.d.ts +6 -16
  8. package/dist/src/providers/fal-ai.d.ts.map +1 -1
  9. package/dist/src/providers/fireworks-ai.d.ts.map +1 -1
  10. package/dist/src/providers/hf-inference.d.ts.map +1 -1
  11. package/dist/src/providers/hyperbolic.d.ts.map +1 -1
  12. package/dist/src/providers/nebius.d.ts.map +1 -1
  13. package/dist/src/providers/novita.d.ts.map +1 -1
  14. package/dist/src/providers/openai.d.ts.map +1 -1
  15. package/dist/src/providers/replicate.d.ts.map +1 -1
  16. package/dist/src/providers/sambanova.d.ts.map +1 -1
  17. package/dist/src/providers/together.d.ts.map +1 -1
  18. package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -1
  19. package/dist/src/snippets/templates.exported.d.ts +2 -0
  20. package/dist/src/snippets/templates.exported.d.ts.map +1 -0
  21. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  22. package/dist/src/types.d.ts +4 -2
  23. package/dist/src/types.d.ts.map +1 -1
  24. package/dist/test/InferenceClient.spec.d.ts.map +1 -1
  25. package/package.json +10 -15
  26. package/src/lib/makeRequestOptions.ts +3 -1
  27. package/src/providers/black-forest-labs.ts +6 -2
  28. package/src/providers/cerebras.ts +6 -2
  29. package/src/providers/cohere.ts +6 -2
  30. package/src/providers/fal-ai.ts +85 -3
  31. package/src/providers/fireworks-ai.ts +6 -2
  32. package/src/providers/hf-inference.ts +6 -2
  33. package/src/providers/hyperbolic.ts +6 -2
  34. package/src/providers/nebius.ts +6 -2
  35. package/src/providers/novita.ts +5 -2
  36. package/src/providers/openai.ts +6 -2
  37. package/src/providers/replicate.ts +6 -2
  38. package/src/providers/sambanova.ts +6 -2
  39. package/src/providers/together.ts +6 -2
  40. package/src/snippets/getInferenceSnippets.ts +6 -24
  41. package/src/snippets/templates.exported.ts +72 -0
  42. package/src/tasks/cv/textToVideo.ts +5 -21
  43. package/src/types.ts +5 -2
  44. package/dist/browser/index.cjs +0 -1652
  45. package/dist/browser/index.js +0 -1652
  46. package/src/snippets/templates/js/fetch/basic.jinja +0 -19
  47. package/src/snippets/templates/js/fetch/basicAudio.jinja +0 -19
  48. package/src/snippets/templates/js/fetch/basicImage.jinja +0 -19
  49. package/src/snippets/templates/js/fetch/textToAudio.jinja +0 -41
  50. package/src/snippets/templates/js/fetch/textToImage.jinja +0 -19
  51. package/src/snippets/templates/js/fetch/zeroShotClassification.jinja +0 -22
  52. package/src/snippets/templates/js/huggingface.js/basic.jinja +0 -11
  53. package/src/snippets/templates/js/huggingface.js/basicAudio.jinja +0 -13
  54. package/src/snippets/templates/js/huggingface.js/basicImage.jinja +0 -13
  55. package/src/snippets/templates/js/huggingface.js/conversational.jinja +0 -11
  56. package/src/snippets/templates/js/huggingface.js/conversationalStream.jinja +0 -19
  57. package/src/snippets/templates/js/huggingface.js/textToImage.jinja +0 -11
  58. package/src/snippets/templates/js/huggingface.js/textToVideo.jinja +0 -10
  59. package/src/snippets/templates/js/openai/conversational.jinja +0 -13
  60. package/src/snippets/templates/js/openai/conversationalStream.jinja +0 -22
  61. package/src/snippets/templates/python/fal_client/textToImage.jinja +0 -11
  62. package/src/snippets/templates/python/huggingface_hub/basic.jinja +0 -4
  63. package/src/snippets/templates/python/huggingface_hub/basicAudio.jinja +0 -1
  64. package/src/snippets/templates/python/huggingface_hub/basicImage.jinja +0 -1
  65. package/src/snippets/templates/python/huggingface_hub/conversational.jinja +0 -6
  66. package/src/snippets/templates/python/huggingface_hub/conversationalStream.jinja +0 -8
  67. package/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.jinja +0 -5
  68. package/src/snippets/templates/python/huggingface_hub/imageToImage.jinja +0 -6
  69. package/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja +0 -6
  70. package/src/snippets/templates/python/huggingface_hub/textToImage.jinja +0 -5
  71. package/src/snippets/templates/python/huggingface_hub/textToVideo.jinja +0 -4
  72. package/src/snippets/templates/python/openai/conversational.jinja +0 -13
  73. package/src/snippets/templates/python/openai/conversationalStream.jinja +0 -15
  74. package/src/snippets/templates/python/requests/basic.jinja +0 -7
  75. package/src/snippets/templates/python/requests/basicAudio.jinja +0 -7
  76. package/src/snippets/templates/python/requests/basicImage.jinja +0 -7
  77. package/src/snippets/templates/python/requests/conversational.jinja +0 -9
  78. package/src/snippets/templates/python/requests/conversationalStream.jinja +0 -16
  79. package/src/snippets/templates/python/requests/documentQuestionAnswering.jinja +0 -13
  80. package/src/snippets/templates/python/requests/imageToImage.jinja +0 -15
  81. package/src/snippets/templates/python/requests/importRequests.jinja +0 -10
  82. package/src/snippets/templates/python/requests/tabular.jinja +0 -9
  83. package/src/snippets/templates/python/requests/textToAudio.jinja +0 -23
  84. package/src/snippets/templates/python/requests/textToImage.jinja +0 -14
  85. package/src/snippets/templates/python/requests/zeroShotClassification.jinja +0 -8
  86. package/src/snippets/templates/python/requests/zeroShotImageClassification.jinja +0 -14
  87. package/src/snippets/templates/sh/curl/basic.jinja +0 -7
  88. package/src/snippets/templates/sh/curl/basicAudio.jinja +0 -5
  89. package/src/snippets/templates/sh/curl/basicImage.jinja +0 -5
  90. package/src/snippets/templates/sh/curl/conversational.jinja +0 -7
  91. package/src/snippets/templates/sh/curl/conversationalStream.jinja +0 -7
  92. package/src/snippets/templates/sh/curl/zeroShotClassification.jinja +0 -5
@@ -0,0 +1,2 @@
1
+ export declare const templates: Record<string, Record<string, Record<string, string>>>;
2
+ //# sourceMappingURL=templates.exported.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templates.exported.d.ts","sourceRoot":"","sources":["../../../src/snippets/templates.exported.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAsEnE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"textToVideo.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToVideo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAqB,OAAO,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAO3D,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,gBAAgB,CAAC;AAE1D,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAoBrC,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAwDtG"}
1
+ {"version":3,"file":"textToVideo.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToVideo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAqB,OAAO,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAS3D,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,gBAAgB,CAAC;AAE1D,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAcrC,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CA4CtG"}
@@ -69,17 +69,19 @@ export type RequestArgs = BaseArgs & ({
69
69
  parameters?: Record<string, unknown>;
70
70
  };
71
71
  export interface ProviderConfig {
72
- baseUrl: string;
72
+ makeBaseUrl: ((task?: InferenceTask) => string) | (() => string);
73
73
  makeBody: (params: BodyParams) => Record<string, unknown>;
74
74
  makeHeaders: (params: HeaderParams) => Record<string, string>;
75
75
  makeUrl: (params: UrlParams) => string;
76
76
  clientSideRoutingOnly?: boolean;
77
77
  }
78
+ export type AuthMethod = "none" | "hf-token" | "credentials-include" | "provider-key";
78
79
  export interface HeaderParams {
79
80
  accessToken?: string;
80
- authMethod: "none" | "hf-token" | "credentials-include" | "provider-key";
81
+ authMethod: AuthMethod;
81
82
  }
82
83
  export interface UrlParams {
84
+ authMethod: AuthMethod;
83
85
  baseUrl: string;
84
86
  model: string;
85
87
  task?: InferenceTask;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAE5E;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC;AAE7B,MAAM,WAAW,OAAO;IACvB;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IAEzB;;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,kLActB,CAAC;AAEX,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;;;;O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AE,OAAO,CAAC,CAAC;CACrC,CAAC;AAEH,MAAM,WAAW,cAAc;IAC9B,WAAW,EAAE,CAAC,CAAC,IAAI,CAAC,EAAE,aAAa,KAAK,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,CAAC;IACjE,QAAQ,EAAE,CAAC,MAAM,EAAE,UAAU,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC1D,WAAW,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9D,OAAO,EAAE,CAAC,MAAM,EAAE,SAAS,KAAK,MAAM,CAAC;IACvC,qBAAqB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,UAAU,GAAG,qBAAqB,GAAG,cAAc,CAAC;AAEtF,MAAM,WAAW,YAAY;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,UAAU,CAAC;CACvB;AAED,MAAM,WAAW,SAAS;IACzB,UAAU,EAAE,UAAU,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,UAAU;IAC1B,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,aAAa,CAAC;CACrB"}
@@ -1 +1 @@
1
- {"version":3,"file":"InferenceClient.spec.d.ts","sourceRoot":"","sources":["../../test/InferenceClient.spec.ts"],"names":[],"mappings":"AAeA,OAAO,OAAO,CAAC"}
1
+ {"version":3,"file":"InferenceClient.spec.d.ts","sourceRoot":"","sources":["../../test/InferenceClient.spec.ts"],"names":[],"mappings":"AAcA,OAAO,OAAO,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/inference",
3
- "version": "3.6.0",
3
+ "version": "3.6.2",
4
4
  "packageManager": "pnpm@8.10.5",
5
5
  "license": "MIT",
6
6
  "author": "Hugging Face and Tim Mikeladze <tim.mikeladze@gmail.com>",
@@ -26,27 +26,21 @@
26
26
  },
27
27
  "files": [
28
28
  "dist",
29
- "src"
29
+ "src",
30
+ "!src/snippets/templates/**/*.jinja"
30
31
  ],
31
32
  "source": "src/index.ts",
32
33
  "types": "./dist/src/index.d.ts",
33
34
  "main": "./dist/index.cjs",
34
35
  "module": "./dist/index.js",
35
36
  "exports": {
36
- ".": {
37
- "types": "./dist/src/index.d.ts",
38
- "require": "./dist/index.cjs",
39
- "import": "./dist/index.js"
40
- }
41
- },
42
- "browser": {
43
- "./src/snippets/index.js": false,
44
- "./dist/index.js": "./dist/browser/index.js",
45
- "./dist/index.mjs": "./dist/browser/index.mjs"
37
+ "types": "./dist/src/index.d.ts",
38
+ "require": "./dist/index.cjs",
39
+ "import": "./dist/index.js"
46
40
  },
47
41
  "type": "module",
48
42
  "dependencies": {
49
- "@huggingface/tasks": "^0.17.8",
43
+ "@huggingface/tasks": "^0.18.4",
50
44
  "@huggingface/jinja": "^0.3.3"
51
45
  },
52
46
  "devDependencies": {
@@ -54,7 +48,7 @@
54
48
  },
55
49
  "resolutions": {},
56
50
  "scripts": {
57
- "build": "tsup src/index.ts --format cjs,esm --clean && tsc --emitDeclarationOnly --declaration",
51
+ "build": "pnpm run export-templates && tsup src/index.ts --format cjs,esm --clean && tsc --emitDeclarationOnly --declaration",
58
52
  "dts": "tsx scripts/generate-dts.ts && tsc --noEmit dist/index.d.ts",
59
53
  "lint": "eslint --quiet --fix --ext .cjs,.ts .",
60
54
  "lint:check": "eslint --ext .cjs,.ts .",
@@ -63,6 +57,7 @@
63
57
  "test": "vitest run --config vitest.config.mts",
64
58
  "test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest.config.mts",
65
59
  "check": "tsc",
66
- "dev": "tsup src/index.ts --format cjs,esm --watch"
60
+ "dev": "pnpm run export-templates && tsup src/index.ts --format cjs,esm --watch",
61
+ "export-templates": "tsx scripts/export-templates.ts"
67
62
  }
68
63
  }
@@ -112,6 +112,7 @@ export function makeRequestOptionsFromResolvedModel(
112
112
  }
113
113
  ): { url: string; info: RequestInit } {
114
114
  const { accessToken, endpointUrl, provider: maybeProvider, model, ...remainingArgs } = args;
115
+ void model;
115
116
 
116
117
  const provider = maybeProvider ?? "hf-inference";
117
118
  const providerConfig = providerConfigs[provider];
@@ -142,10 +143,11 @@ export function makeRequestOptionsFromResolvedModel(
142
143
  ? endpointUrl + `/v1/chat/completions`
143
144
  : endpointUrl
144
145
  : providerConfig.makeUrl({
146
+ authMethod,
145
147
  baseUrl:
146
148
  authMethod !== "provider-key"
147
149
  ? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider)
148
- : providerConfig.baseUrl,
150
+ : providerConfig.makeBaseUrl(task),
149
151
  model: resolvedModel,
150
152
  chatCompletion,
151
153
  task,
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return BLACK_FOREST_LABS_AI_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return params.args;
23
27
  };
@@ -35,7 +39,7 @@ const makeUrl = (params: UrlParams): string => {
35
39
  };
36
40
 
37
41
  export const BLACK_FOREST_LABS_CONFIG: ProviderConfig = {
38
- baseUrl: BLACK_FOREST_LABS_AI_API_BASE_URL,
42
+ makeBaseUrl,
39
43
  makeBody,
40
44
  makeHeaders,
41
45
  makeUrl,
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const CEREBRAS_API_BASE_URL = "https://api.cerebras.ai";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return CEREBRAS_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return {
23
27
  ...params.args,
@@ -34,7 +38,7 @@ const makeUrl = (params: UrlParams): string => {
34
38
  };
35
39
 
36
40
  export const CEREBRAS_CONFIG: ProviderConfig = {
37
- baseUrl: CEREBRAS_API_BASE_URL,
41
+ makeBaseUrl,
38
42
  makeBody,
39
43
  makeHeaders,
40
44
  makeUrl,
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const COHERE_API_BASE_URL = "https://api.cohere.com";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return COHERE_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return {
23
27
  ...params.args,
@@ -34,7 +38,7 @@ const makeUrl = (params: UrlParams): string => {
34
38
  };
35
39
 
36
40
  export const COHERE_CONFIG: ProviderConfig = {
37
- baseUrl: COHERE_API_BASE_URL,
41
+ makeBaseUrl,
38
42
  makeBody,
39
43
  makeHeaders,
40
44
  makeUrl,
@@ -14,9 +14,17 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import { InferenceOutputError } from "../lib/InferenceOutputError";
18
+ import { isUrl } from "../lib/isUrl";
19
+ import type { BodyParams, HeaderParams, InferenceTask, ProviderConfig, UrlParams } from "../types";
20
+ import { delay } from "../utils/delay";
18
21
 
19
22
  const FAL_AI_API_BASE_URL = "https://fal.run";
23
+ const FAL_AI_API_BASE_URL_QUEUE = "https://queue.fal.run";
24
+
25
+ const makeBaseUrl = (task?: InferenceTask): string => {
26
+ return task === "text-to-video" ? FAL_AI_API_BASE_URL_QUEUE : FAL_AI_API_BASE_URL;
27
+ };
20
28
 
21
29
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
30
  return params.args;
@@ -29,12 +37,86 @@ const makeHeaders = (params: HeaderParams): Record<string, string> => {
29
37
  };
30
38
 
31
39
  const makeUrl = (params: UrlParams): string => {
32
- return `${params.baseUrl}/${params.model}`;
40
+ const baseUrl = `${params.baseUrl}/${params.model}`;
41
+ if (params.authMethod !== "provider-key" && params.task === "text-to-video") {
42
+ return `${baseUrl}?_subdomain=queue`;
43
+ }
44
+ return baseUrl;
33
45
  };
34
46
 
35
47
  export const FAL_AI_CONFIG: ProviderConfig = {
36
- baseUrl: FAL_AI_API_BASE_URL,
48
+ makeBaseUrl,
37
49
  makeBody,
38
50
  makeHeaders,
39
51
  makeUrl,
40
52
  };
53
+
54
+ export interface FalAiQueueOutput {
55
+ request_id: string;
56
+ status: string;
57
+ response_url: string;
58
+ }
59
+
60
+ export async function pollFalResponse(
61
+ res: FalAiQueueOutput,
62
+ url: string,
63
+ headers: Record<string, string>
64
+ ): Promise<Blob> {
65
+ const requestId = res.request_id;
66
+ if (!requestId) {
67
+ throw new InferenceOutputError("No request ID found in the response");
68
+ }
69
+ let status = res.status;
70
+
71
+ const parsedUrl = new URL(url);
72
+ const baseUrl = `${parsedUrl.protocol}//${parsedUrl.host}${
73
+ parsedUrl.host === "router.huggingface.co" ? "/fal-ai" : ""
74
+ }`;
75
+
76
+ // extracting the provider model id for status and result urls
77
+ // from the response as it might be different from the mapped model in `url`
78
+ const modelId = new URL(res.response_url).pathname;
79
+ const queryParams = parsedUrl.search;
80
+
81
+ const statusUrl = `${baseUrl}${modelId}/status${queryParams}`;
82
+ const resultUrl = `${baseUrl}${modelId}${queryParams}`;
83
+
84
+ while (status !== "COMPLETED") {
85
+ await delay(500);
86
+ const statusResponse = await fetch(statusUrl, { headers });
87
+
88
+ if (!statusResponse.ok) {
89
+ throw new InferenceOutputError("Failed to fetch response status from fal-ai API");
90
+ }
91
+ try {
92
+ status = (await statusResponse.json()).status;
93
+ } catch (error) {
94
+ throw new InferenceOutputError("Failed to parse status response from fal-ai API");
95
+ }
96
+ }
97
+
98
+ const resultResponse = await fetch(resultUrl, { headers });
99
+ let result: unknown;
100
+ try {
101
+ result = await resultResponse.json();
102
+ } catch (error) {
103
+ throw new InferenceOutputError("Failed to parse result response from fal-ai API");
104
+ }
105
+ if (
106
+ typeof result === "object" &&
107
+ !!result &&
108
+ "video" in result &&
109
+ typeof result.video === "object" &&
110
+ !!result.video &&
111
+ "url" in result.video &&
112
+ typeof result.video.url === "string" &&
113
+ isUrl(result.video.url)
114
+ ) {
115
+ const urlResponse = await fetch(result.video.url);
116
+ return await urlResponse.blob();
117
+ } else {
118
+ throw new InferenceOutputError(
119
+ "Expected { video: { url: string } } result format, got instead: " + JSON.stringify(result)
120
+ );
121
+ }
122
+ }
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return FIREWORKS_AI_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return {
23
27
  ...params.args,
@@ -37,7 +41,7 @@ const makeUrl = (params: UrlParams): string => {
37
41
  };
38
42
 
39
43
  export const FIREWORKS_AI_CONFIG: ProviderConfig = {
40
- baseUrl: FIREWORKS_AI_API_BASE_URL,
44
+ makeBaseUrl,
41
45
  makeBody,
42
46
  makeHeaders,
43
47
  makeUrl,
@@ -11,7 +11,11 @@
11
11
  * Thanks!
12
12
  */
13
13
  import { HF_ROUTER_URL } from "../config";
14
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
14
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
15
+
16
+ const makeBaseUrl = (): string => {
17
+ return `${HF_ROUTER_URL}/hf-inference`;
18
+ };
15
19
 
16
20
  const makeBody = (params: BodyParams): Record<string, unknown> => {
17
21
  return {
@@ -36,7 +40,7 @@ const makeUrl = (params: UrlParams): string => {
36
40
  };
37
41
 
38
42
  export const HF_INFERENCE_CONFIG: ProviderConfig = {
39
- baseUrl: `${HF_ROUTER_URL}/hf-inference`,
43
+ makeBaseUrl,
40
44
  makeBody,
41
45
  makeHeaders,
42
46
  makeUrl,
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const HYPERBOLIC_API_BASE_URL = "https://api.hyperbolic.xyz";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return HYPERBOLIC_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return {
23
27
  ...params.args,
@@ -37,7 +41,7 @@ const makeUrl = (params: UrlParams): string => {
37
41
  };
38
42
 
39
43
  export const HYPERBOLIC_CONFIG: ProviderConfig = {
40
- baseUrl: HYPERBOLIC_API_BASE_URL,
44
+ makeBaseUrl,
41
45
  makeBody,
42
46
  makeHeaders,
43
47
  makeUrl,
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const NEBIUS_API_BASE_URL = "https://api.studio.nebius.ai";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return NEBIUS_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return {
23
27
  ...params.args,
@@ -43,7 +47,7 @@ const makeUrl = (params: UrlParams): string => {
43
47
  };
44
48
 
45
49
  export const NEBIUS_CONFIG: ProviderConfig = {
46
- baseUrl: NEBIUS_API_BASE_URL,
50
+ makeBaseUrl,
47
51
  makeBody,
48
52
  makeHeaders,
49
53
  makeUrl,
@@ -14,10 +14,13 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const NOVITA_API_BASE_URL = "https://api.novita.ai";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return NOVITA_API_BASE_URL;
23
+ };
21
24
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
25
  return {
23
26
  ...params.args,
@@ -41,7 +44,7 @@ const makeUrl = (params: UrlParams): string => {
41
44
  };
42
45
 
43
46
  export const NOVITA_CONFIG: ProviderConfig = {
44
- baseUrl: NOVITA_API_BASE_URL,
47
+ makeBaseUrl,
45
48
  makeBody,
46
49
  makeHeaders,
47
50
  makeUrl,
@@ -1,10 +1,14 @@
1
1
  /**
2
2
  * Special case: provider configuration for a private models provider (OpenAI in this case).
3
3
  */
4
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
4
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
5
5
 
6
6
  const OPENAI_API_BASE_URL = "https://api.openai.com";
7
7
 
8
+ const makeBaseUrl = (): string => {
9
+ return OPENAI_API_BASE_URL;
10
+ };
11
+
8
12
  const makeBody = (params: BodyParams): Record<string, unknown> => {
9
13
  if (!params.chatCompletion) {
10
14
  throw new Error("OpenAI only supports chat completions.");
@@ -27,7 +31,7 @@ const makeUrl = (params: UrlParams): string => {
27
31
  };
28
32
 
29
33
  export const OPENAI_CONFIG: ProviderConfig = {
30
- baseUrl: OPENAI_API_BASE_URL,
34
+ makeBaseUrl,
31
35
  makeBody,
32
36
  makeHeaders,
33
37
  makeUrl,
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  export const REPLICATE_API_BASE_URL = "https://api.replicate.com";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return REPLICATE_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return {
23
27
  input: params.args,
@@ -39,7 +43,7 @@ const makeUrl = (params: UrlParams): string => {
39
43
  };
40
44
 
41
45
  export const REPLICATE_CONFIG: ProviderConfig = {
42
- baseUrl: REPLICATE_API_BASE_URL,
46
+ makeBaseUrl,
43
47
  makeBody,
44
48
  makeHeaders,
45
49
  makeUrl,
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const SAMBANOVA_API_BASE_URL = "https://api.sambanova.ai";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return SAMBANOVA_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return {
23
27
  ...params.args,
@@ -37,7 +41,7 @@ const makeUrl = (params: UrlParams): string => {
37
41
  };
38
42
 
39
43
  export const SAMBANOVA_CONFIG: ProviderConfig = {
40
- baseUrl: SAMBANOVA_API_BASE_URL,
44
+ makeBaseUrl,
41
45
  makeBody,
42
46
  makeHeaders,
43
47
  makeUrl,
@@ -14,10 +14,14 @@
14
14
  *
15
15
  * Thanks!
16
16
  */
17
- import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
17
+ import type { BodyParams, HeaderParams, ProviderConfig, UrlParams } from "../types";
18
18
 
19
19
  const TOGETHER_API_BASE_URL = "https://api.together.xyz";
20
20
 
21
+ const makeBaseUrl = (): string => {
22
+ return TOGETHER_API_BASE_URL;
23
+ };
24
+
21
25
  const makeBody = (params: BodyParams): Record<string, unknown> => {
22
26
  return {
23
27
  ...params.args,
@@ -43,7 +47,7 @@ const makeUrl = (params: UrlParams): string => {
43
47
  };
44
48
 
45
49
  export const TOGETHER_CONFIG: ProviderConfig = {
46
- baseUrl: TOGETHER_API_BASE_URL,
50
+ makeBaseUrl,
47
51
  makeBody,
48
52
  makeHeaders,
49
53
  makeUrl,
@@ -10,9 +10,7 @@ import {
10
10
  import type { InferenceProvider, InferenceTask, RequestArgs } from "../types";
11
11
  import { Template } from "@huggingface/jinja";
12
12
  import { makeRequestOptionsFromResolvedModel } from "../lib/makeRequestOptions";
13
- import fs from "fs";
14
- import path from "path";
15
- import { existsSync as pathExists } from "node:fs";
13
+ import { templates } from "./templates.exported";
16
14
 
17
15
  const PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"] as const;
18
16
  const JS_CLIENTS = ["fetch", "huggingface.js", "openai"] as const;
@@ -44,34 +42,18 @@ interface TemplateParams {
44
42
 
45
43
  // Helpers to find + load templates
46
44
 
47
- const rootDirFinder = (): string => {
48
- let currentPath =
49
- typeof import.meta !== "undefined" && import.meta.url
50
- ? path.normalize(new URL(import.meta.url).pathname) /// for ESM
51
- : __dirname; /// for CJS
52
-
53
- while (currentPath !== "/") {
54
- if (pathExists(path.join(currentPath, "package.json"))) {
55
- return currentPath;
56
- }
57
-
58
- currentPath = path.normalize(path.join(currentPath, ".."));
59
- }
60
-
61
- return "/";
62
- };
63
-
64
- const templatePath = (language: InferenceSnippetLanguage, client: Client, templateName: string): string =>
65
- path.join(rootDirFinder(), "src", "snippets", "templates", language, client, `${templateName}.jinja`);
66
45
  const hasTemplate = (language: InferenceSnippetLanguage, client: Client, templateName: string): boolean =>
67
- pathExists(templatePath(language, client, templateName));
46
+ templates[language]?.[client]?.[templateName] !== undefined;
68
47
 
69
48
  const loadTemplate = (
70
49
  language: InferenceSnippetLanguage,
71
50
  client: Client,
72
51
  templateName: string
73
52
  ): ((data: TemplateParams) => string) => {
74
- const template = fs.readFileSync(templatePath(language, client, templateName), "utf8");
53
+ const template = templates[language]?.[client]?.[templateName];
54
+ if (!template) {
55
+ throw new Error(`Template not found: ${language}/${client}/${templateName}`);
56
+ }
75
57
  return (data: TemplateParams) => new Template(template).render({ ...data });
76
58
  };
77
59