@huggingface/inference 4.5.1 → 4.5.3

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 (91) hide show
  1. package/dist/commonjs/config.d.ts +1 -0
  2. package/dist/commonjs/config.d.ts.map +1 -1
  3. package/dist/commonjs/config.js +2 -1
  4. package/dist/commonjs/package.d.ts +1 -1
  5. package/dist/commonjs/package.js +1 -1
  6. package/dist/commonjs/providers/black-forest-labs.d.ts +1 -1
  7. package/dist/commonjs/providers/black-forest-labs.d.ts.map +1 -1
  8. package/dist/commonjs/providers/black-forest-labs.js +3 -0
  9. package/dist/commonjs/providers/fal-ai.d.ts +1 -1
  10. package/dist/commonjs/providers/fal-ai.d.ts.map +1 -1
  11. package/dist/commonjs/providers/fal-ai.js +4 -1
  12. package/dist/commonjs/providers/hf-inference.d.ts +1 -1
  13. package/dist/commonjs/providers/hf-inference.d.ts.map +1 -1
  14. package/dist/commonjs/providers/hf-inference.js +5 -2
  15. package/dist/commonjs/providers/hyperbolic.d.ts +1 -1
  16. package/dist/commonjs/providers/hyperbolic.d.ts.map +1 -1
  17. package/dist/commonjs/providers/hyperbolic.js +3 -0
  18. package/dist/commonjs/providers/nebius.d.ts +1 -1
  19. package/dist/commonjs/providers/nebius.d.ts.map +1 -1
  20. package/dist/commonjs/providers/nebius.js +3 -0
  21. package/dist/commonjs/providers/nscale.d.ts +1 -1
  22. package/dist/commonjs/providers/nscale.d.ts.map +1 -1
  23. package/dist/commonjs/providers/nscale.js +3 -0
  24. package/dist/commonjs/providers/providerHelper.d.ts +1 -1
  25. package/dist/commonjs/providers/providerHelper.d.ts.map +1 -1
  26. package/dist/commonjs/providers/replicate.d.ts +1 -1
  27. package/dist/commonjs/providers/replicate.d.ts.map +1 -1
  28. package/dist/commonjs/providers/replicate.js +3 -0
  29. package/dist/commonjs/providers/together.d.ts +1 -1
  30. package/dist/commonjs/providers/together.d.ts.map +1 -1
  31. package/dist/commonjs/providers/together.js +4 -1
  32. package/dist/commonjs/snippets/getInferenceSnippets.d.ts +1 -0
  33. package/dist/commonjs/snippets/getInferenceSnippets.d.ts.map +1 -1
  34. package/dist/commonjs/snippets/getInferenceSnippets.js +42 -7
  35. package/dist/commonjs/snippets/templates.exported.d.ts.map +1 -1
  36. package/dist/commonjs/snippets/templates.exported.js +5 -4
  37. package/dist/commonjs/tasks/cv/textToImage.d.ts +4 -1
  38. package/dist/commonjs/tasks/cv/textToImage.d.ts.map +1 -1
  39. package/dist/esm/config.d.ts +1 -0
  40. package/dist/esm/config.d.ts.map +1 -1
  41. package/dist/esm/config.js +1 -0
  42. package/dist/esm/package.d.ts +1 -1
  43. package/dist/esm/package.js +1 -1
  44. package/dist/esm/providers/black-forest-labs.d.ts +1 -1
  45. package/dist/esm/providers/black-forest-labs.d.ts.map +1 -1
  46. package/dist/esm/providers/black-forest-labs.js +3 -0
  47. package/dist/esm/providers/fal-ai.d.ts +1 -1
  48. package/dist/esm/providers/fal-ai.d.ts.map +1 -1
  49. package/dist/esm/providers/fal-ai.js +4 -1
  50. package/dist/esm/providers/hf-inference.d.ts +1 -1
  51. package/dist/esm/providers/hf-inference.d.ts.map +1 -1
  52. package/dist/esm/providers/hf-inference.js +5 -2
  53. package/dist/esm/providers/hyperbolic.d.ts +1 -1
  54. package/dist/esm/providers/hyperbolic.d.ts.map +1 -1
  55. package/dist/esm/providers/hyperbolic.js +3 -0
  56. package/dist/esm/providers/nebius.d.ts +1 -1
  57. package/dist/esm/providers/nebius.d.ts.map +1 -1
  58. package/dist/esm/providers/nebius.js +3 -0
  59. package/dist/esm/providers/nscale.d.ts +1 -1
  60. package/dist/esm/providers/nscale.d.ts.map +1 -1
  61. package/dist/esm/providers/nscale.js +3 -0
  62. package/dist/esm/providers/providerHelper.d.ts +1 -1
  63. package/dist/esm/providers/providerHelper.d.ts.map +1 -1
  64. package/dist/esm/providers/replicate.d.ts +1 -1
  65. package/dist/esm/providers/replicate.d.ts.map +1 -1
  66. package/dist/esm/providers/replicate.js +3 -0
  67. package/dist/esm/providers/together.d.ts +1 -1
  68. package/dist/esm/providers/together.d.ts.map +1 -1
  69. package/dist/esm/providers/together.js +4 -1
  70. package/dist/esm/snippets/getInferenceSnippets.d.ts +1 -0
  71. package/dist/esm/snippets/getInferenceSnippets.d.ts.map +1 -1
  72. package/dist/esm/snippets/getInferenceSnippets.js +42 -7
  73. package/dist/esm/snippets/templates.exported.d.ts.map +1 -1
  74. package/dist/esm/snippets/templates.exported.js +5 -4
  75. package/dist/esm/tasks/cv/textToImage.d.ts +4 -1
  76. package/dist/esm/tasks/cv/textToImage.d.ts.map +1 -1
  77. package/package.json +2 -2
  78. package/src/config.ts +1 -0
  79. package/src/package.ts +1 -1
  80. package/src/providers/black-forest-labs.ts +5 -2
  81. package/src/providers/fal-ai.ts +9 -1
  82. package/src/providers/hf-inference.ts +7 -4
  83. package/src/providers/hyperbolic.ts +5 -2
  84. package/src/providers/nebius.ts +5 -2
  85. package/src/providers/nscale.ts +5 -2
  86. package/src/providers/providerHelper.ts +2 -2
  87. package/src/providers/replicate.ts +5 -2
  88. package/src/providers/together.ts +9 -1
  89. package/src/snippets/getInferenceSnippets.ts +49 -7
  90. package/src/snippets/templates.exported.ts +5 -4
  91. package/src/tasks/cv/textToImage.ts +9 -2
@@ -1 +1 @@
1
- {"version":3,"file":"textToImage.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToImage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAI3D,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAGxD,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,gBAAgB,CAAC;AAE1D,UAAU,kBAAmB,SAAQ,OAAO;IAC3C,UAAU,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;CAC5B;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAChC,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,kBAAkB,GAAG;IAAE,UAAU,EAAE,KAAK,CAAA;CAAE,GAClD,OAAO,CAAC,MAAM,CAAC,CAAC;AACnB,wBAAsB,WAAW,CAChC,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,kBAAkB,GAAG;IAAE,UAAU,CAAC,EAAE,SAAS,GAAG,MAAM,CAAA;CAAE,GAChE,OAAO,CAAC,IAAI,CAAC,CAAC"}
1
+ {"version":3,"file":"textToImage.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToImage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAI3D,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAGxD,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,gBAAgB,CAAC;AAE1D,UAAU,kBAAmB,SAAQ,OAAO;IAC3C,UAAU,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,CAAC;CACrC;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAChC,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,kBAAkB,GAAG;IAAE,UAAU,EAAE,KAAK,CAAA;CAAE,GAClD,OAAO,CAAC,MAAM,CAAC,CAAC;AACnB,wBAAsB,WAAW,CAChC,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,kBAAkB,GAAG;IAAE,UAAU,CAAC,EAAE,SAAS,GAAG,MAAM,CAAA;CAAE,GAChE,OAAO,CAAC,IAAI,CAAC,CAAC;AACjB,wBAAsB,WAAW,CAChC,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,kBAAkB,GAAG;IAAE,UAAU,CAAC,EAAE,SAAS,GAAG,MAAM,CAAA;CAAE,GAChE,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huggingface/inference",
3
- "version": "4.5.1",
3
+ "version": "4.5.3",
4
4
  "license": "MIT",
5
5
  "author": "Hugging Face and Tim Mikeladze <tim.mikeladze@gmail.com>",
6
6
  "description": "Typescript client for the Hugging Face Inference Providers and Inference Endpoints",
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "type": "module",
42
42
  "dependencies": {
43
- "@huggingface/tasks": "^0.19.23",
43
+ "@huggingface/tasks": "^0.19.26",
44
44
  "@huggingface/jinja": "^0.5.1"
45
45
  },
46
46
  "devDependencies": {
package/src/config.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export const HF_HUB_URL = "https://huggingface.co";
2
2
  export const HF_ROUTER_URL = "https://router.huggingface.co";
3
+ export const HF_ROUTER_AUTO_ENDPOINT = `${HF_ROUTER_URL}/v1`;
3
4
  export const HF_HEADER_X_BILL_TO = "X-HF-Bill-To";
package/src/package.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  // Generated file from package.json. Issues importing JSON directly when publishing on commonjs/ESM - see https://github.com/microsoft/TypeScript/issues/51783
2
- export const PACKAGE_VERSION = "4.5.1";
2
+ export const PACKAGE_VERSION = "4.5.3";
3
3
  export const PACKAGE_NAME = "@huggingface/inference";
@@ -66,8 +66,8 @@ export class BlackForestLabsTextToImageTask extends TaskProviderHelper implement
66
66
  response: BlackForestLabsResponse,
67
67
  url?: string,
68
68
  headers?: HeadersInit,
69
- outputType?: "url" | "blob"
70
- ): Promise<string | Blob> {
69
+ outputType?: "url" | "blob" | "json"
70
+ ): Promise<string | Blob | Record<string, unknown>> {
71
71
  const logger = getLogger();
72
72
  const urlObj = new URL(response.polling_url);
73
73
  for (let step = 0; step < 5; step++) {
@@ -95,6 +95,9 @@ export class BlackForestLabsTextToImageTask extends TaskProviderHelper implement
95
95
  "sample" in payload.result &&
96
96
  typeof payload.result.sample === "string"
97
97
  ) {
98
+ if (outputType === "json") {
99
+ return payload.result;
100
+ }
98
101
  if (outputType === "url") {
99
102
  return payload.result.sample;
100
103
  }
@@ -182,7 +182,12 @@ export class FalAITextToImageTask extends FalAITask implements TextToImageTaskHe
182
182
  return payload;
183
183
  }
184
184
 
185
- override async getResponse(response: FalAITextToImageOutput, outputType?: "url" | "blob"): Promise<string | Blob> {
185
+ override async getResponse(
186
+ response: FalAITextToImageOutput,
187
+ url?: string,
188
+ headers?: HeadersInit,
189
+ outputType?: "url" | "blob" | "json"
190
+ ): Promise<string | Blob | Record<string, unknown>> {
186
191
  if (
187
192
  typeof response === "object" &&
188
193
  "images" in response &&
@@ -191,6 +196,9 @@ export class FalAITextToImageTask extends FalAITask implements TextToImageTaskHe
191
196
  "url" in response.images[0] &&
192
197
  typeof response.images[0].url === "string"
193
198
  ) {
199
+ if (outputType === "json") {
200
+ return { ...response };
201
+ }
194
202
  if (outputType === "url") {
195
203
  return response.images[0].url;
196
204
  }
@@ -127,14 +127,17 @@ export class HFInferenceTextToImageTask extends HFInferenceTask implements TextT
127
127
  response: Base64ImageGeneration | OutputUrlImageGeneration,
128
128
  url?: string,
129
129
  headers?: HeadersInit,
130
- outputType?: "url" | "blob"
131
- ): Promise<string | Blob> {
130
+ outputType?: "url" | "blob" | "json"
131
+ ): Promise<string | Blob | Record<string, unknown>> {
132
132
  if (!response) {
133
133
  throw new InferenceClientProviderOutputError(
134
134
  "Received malformed response from HF-Inference text-to-image API: response is undefined"
135
135
  );
136
136
  }
137
137
  if (typeof response == "object") {
138
+ if (outputType === "json") {
139
+ return { ...response };
140
+ }
138
141
  if ("data" in response && Array.isArray(response.data) && response.data[0].b64_json) {
139
142
  const base64Data = response.data[0].b64_json;
140
143
  if (outputType === "url") {
@@ -153,9 +156,9 @@ export class HFInferenceTextToImageTask extends HFInferenceTask implements TextT
153
156
  }
154
157
  }
155
158
  if (response instanceof Blob) {
156
- if (outputType === "url") {
159
+ if (outputType === "url" || outputType === "json") {
157
160
  const b64 = await response.arrayBuffer().then((buf) => Buffer.from(buf).toString("base64"));
158
- return `data:image/jpeg;base64,${b64}`;
161
+ return outputType === "url" ? `data:image/jpeg;base64,${b64}` : { output: `data:image/jpeg;base64,${b64}` };
159
162
  }
160
163
  return response;
161
164
  }
@@ -105,8 +105,8 @@ export class HyperbolicTextToImageTask extends TaskProviderHelper implements Tex
105
105
  response: HyperbolicTextToImageOutput,
106
106
  url?: string,
107
107
  headers?: HeadersInit,
108
- outputType?: "url" | "blob"
109
- ): Promise<string | Blob> {
108
+ outputType?: "url" | "blob" | "json"
109
+ ): Promise<string | Blob | Record<string, unknown>> {
110
110
  if (
111
111
  typeof response === "object" &&
112
112
  "images" in response &&
@@ -114,6 +114,9 @@ export class HyperbolicTextToImageTask extends TaskProviderHelper implements Tex
114
114
  response.images[0] &&
115
115
  typeof response.images[0].image === "string"
116
116
  ) {
117
+ if (outputType === "json") {
118
+ return { ...response };
119
+ }
117
120
  if (outputType === "url") {
118
121
  return `data:image/jpeg;base64,${response.images[0].image}`;
119
122
  }
@@ -116,8 +116,8 @@ export class NebiusTextToImageTask extends TaskProviderHelper implements TextToI
116
116
  response: NebiusBase64ImageGeneration,
117
117
  url?: string,
118
118
  headers?: HeadersInit,
119
- outputType?: "url" | "blob"
120
- ): Promise<string | Blob> {
119
+ outputType?: "url" | "blob" | "json"
120
+ ): Promise<string | Blob | Record<string, unknown>> {
121
121
  if (
122
122
  typeof response === "object" &&
123
123
  "data" in response &&
@@ -126,6 +126,9 @@ export class NebiusTextToImageTask extends TaskProviderHelper implements TextToI
126
126
  "b64_json" in response.data[0] &&
127
127
  typeof response.data[0].b64_json === "string"
128
128
  ) {
129
+ if (outputType === "json") {
130
+ return { ...response };
131
+ }
129
132
  const base64Data = response.data[0].b64_json;
130
133
  if (outputType === "url") {
131
134
  return `data:image/jpeg;base64,${base64Data}`;
@@ -57,8 +57,8 @@ export class NscaleTextToImageTask extends TaskProviderHelper implements TextToI
57
57
  response: NscaleCloudBase64ImageGeneration,
58
58
  url?: string,
59
59
  headers?: HeadersInit,
60
- outputType?: "url" | "blob"
61
- ): Promise<string | Blob> {
60
+ outputType?: "url" | "blob" | "json"
61
+ ): Promise<string | Blob | Record<string, unknown>> {
62
62
  if (
63
63
  typeof response === "object" &&
64
64
  "data" in response &&
@@ -67,6 +67,9 @@ export class NscaleTextToImageTask extends TaskProviderHelper implements TextToI
67
67
  "b64_json" in response.data[0] &&
68
68
  typeof response.data[0].b64_json === "string"
69
69
  ) {
70
+ if (outputType === "json") {
71
+ return { ...response };
72
+ }
70
73
  const base64Data = response.data[0].b64_json;
71
74
  if (outputType === "url") {
72
75
  return `data:image/jpeg;base64,${base64Data}`;
@@ -137,8 +137,8 @@ export interface TextToImageTaskHelper {
137
137
  response: unknown,
138
138
  url?: string,
139
139
  headers?: HeadersInit,
140
- outputType?: "url" | "blob"
141
- ): Promise<string | Blob>;
140
+ outputType?: "url" | "blob" | "json"
141
+ ): Promise<string | Blob | Record<string, unknown>>;
142
142
  preparePayload(params: BodyParams<TextToImageInput & BaseArgs>): Record<string, unknown>;
143
143
  }
144
144
 
@@ -88,8 +88,8 @@ export class ReplicateTextToImageTask extends ReplicateTask implements TextToIma
88
88
  res: ReplicateOutput | Blob,
89
89
  url?: string,
90
90
  headers?: Record<string, string>,
91
- outputType?: "url" | "blob"
92
- ): Promise<string | Blob> {
91
+ outputType?: "url" | "blob" | "json"
92
+ ): Promise<string | Blob | Record<string, unknown>> {
93
93
  void url;
94
94
  void headers;
95
95
  if (
@@ -99,6 +99,9 @@ export class ReplicateTextToImageTask extends ReplicateTask implements TextToIma
99
99
  res.output.length > 0 &&
100
100
  typeof res.output[0] === "string"
101
101
  ) {
102
+ if (outputType === "json") {
103
+ return { ...res };
104
+ }
102
105
  if (outputType === "url") {
103
106
  return res.output[0];
104
107
  }
@@ -114,7 +114,12 @@ export class TogetherTextToImageTask extends TaskProviderHelper implements TextT
114
114
  };
115
115
  }
116
116
 
117
- async getResponse(response: TogetherBase64ImageGeneration, outputType?: "url" | "blob"): Promise<string | Blob> {
117
+ async getResponse(
118
+ response: TogetherBase64ImageGeneration,
119
+ url?: string,
120
+ headers?: HeadersInit,
121
+ outputType?: "url" | "blob" | "json"
122
+ ): Promise<string | Blob | Record<string, unknown>> {
118
123
  if (
119
124
  typeof response === "object" &&
120
125
  "data" in response &&
@@ -123,6 +128,9 @@ export class TogetherTextToImageTask extends TaskProviderHelper implements TextT
123
128
  "b64_json" in response.data[0] &&
124
129
  typeof response.data[0].b64_json === "string"
125
130
  ) {
131
+ if (outputType === "json") {
132
+ return { ...response };
133
+ }
126
134
  const base64Data = response.data[0].b64_json;
127
135
  if (outputType === "url") {
128
136
  return `data:image/jpeg;base64,${base64Data}`;
@@ -13,6 +13,7 @@ import { makeRequestOptionsFromResolvedModel } from "../lib/makeRequestOptions.j
13
13
  import type { InferenceProviderMappingEntry, InferenceProviderOrPolicy, InferenceTask, RequestArgs } from "../types.js";
14
14
  import { templates } from "./templates.exported.js";
15
15
  import { getLogger } from "../lib/logger.js";
16
+ import { HF_ROUTER_AUTO_ENDPOINT } from "../config.js";
16
17
 
17
18
  export type InferenceSnippetOptions = {
18
19
  streaming?: boolean;
@@ -20,6 +21,7 @@ export type InferenceSnippetOptions = {
20
21
  accessToken?: string;
21
22
  directRequest?: boolean; // to bypass HF routing and call the provider directly
22
23
  endpointUrl?: string; // to call a local endpoint directly
24
+ inputs?: Record<string, unknown>; // overrides the default snippet's inputs
23
25
  } & Record<string, unknown>;
24
26
 
25
27
  const PYTHON_CLIENTS = ["huggingface_hub", "fal_client", "requests", "openai"] as const;
@@ -34,7 +36,9 @@ const CLIENTS: Record<InferenceSnippetLanguage, Client[]> = {
34
36
  sh: [...SH_CLIENTS],
35
37
  };
36
38
 
37
- const CLIENTS_AUTO_POLICY: Partial<Record<InferenceSnippetLanguage, Client[]>> = {
39
+ // The "auto"-provider policy is only available through the HF SDKs (huggingface.js / huggingface_hub)
40
+ // except for conversational tasks for which we have https://router.huggingface.co/v1/chat/completions
41
+ const CLIENTS_NON_CONVERSATIONAL_AUTO_POLICY: Partial<Record<InferenceSnippetLanguage, Client[]>> = {
38
42
  js: ["huggingface.js"],
39
43
  python: ["huggingface_hub"],
40
44
  };
@@ -47,6 +51,7 @@ interface TemplateParams {
47
51
  fullUrl?: string;
48
52
  inputs?: object;
49
53
  providerInputs?: object;
54
+ autoInputs?: object;
50
55
  model?: ModelDataMinimal;
51
56
  provider?: InferenceProviderOrPolicy;
52
57
  providerModelId?: string;
@@ -167,14 +172,18 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
167
172
  const accessTokenOrPlaceholder = opts?.accessToken ?? placeholder;
168
173
 
169
174
  /// Prepare inputs + make request
170
- const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: getModelInputSnippet(model) };
175
+ const inputs = opts?.inputs
176
+ ? { inputs: opts.inputs }
177
+ : inputPreparationFn
178
+ ? inputPreparationFn(model, opts)
179
+ : { inputs: getModelInputSnippet(model) };
171
180
  const request = makeRequestOptionsFromResolvedModel(
172
181
  providerModelId,
173
182
  providerHelper,
174
183
  {
175
184
  accessToken: accessTokenOrPlaceholder,
176
185
  provider,
177
- endpointUrl: opts?.endpointUrl,
186
+ endpointUrl: opts?.endpointUrl ?? (provider === "auto" ? HF_ROUTER_AUTO_ENDPOINT : undefined),
178
187
  ...inputs,
179
188
  } as RequestArgs,
180
189
  inferenceProviderMapping,
@@ -196,12 +205,33 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
196
205
  }
197
206
  }
198
207
 
208
+ // Inputs for the "auto" route is strictly the same as "inputs", except the model includes the provider
209
+ // If not "auto" route, use the providerInputs
210
+ const autoInputs =
211
+ !opts?.endpointUrl && !opts?.directRequest
212
+ ? provider !== "auto"
213
+ ? {
214
+ ...inputs,
215
+ model: `${model.id}:${provider}`,
216
+ }
217
+ : {
218
+ ...inputs,
219
+ model: `${model.id}`, // if no :provider => auto
220
+ }
221
+ : providerInputs;
222
+
199
223
  /// Prepare template injection data
200
224
  const params: TemplateParams = {
201
225
  accessToken: accessTokenOrPlaceholder,
202
226
  authorizationHeader: (request.info.headers as Record<string, string>)?.Authorization,
203
- baseUrl: removeSuffix(request.url, "/chat/completions"),
204
- fullUrl: request.url,
227
+ baseUrl:
228
+ task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
229
+ ? HF_ROUTER_AUTO_ENDPOINT
230
+ : removeSuffix(request.url, "/chat/completions"),
231
+ fullUrl:
232
+ task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
233
+ ? HF_ROUTER_AUTO_ENDPOINT + "/chat/completions"
234
+ : request.url,
205
235
  inputs: {
206
236
  asObj: inputs,
207
237
  asCurlString: formatBody(inputs, "curl"),
@@ -216,15 +246,27 @@ const snippetGenerator = (templateName: string, inputPreparationFn?: InputPrepar
216
246
  asPythonString: formatBody(providerInputs, "python"),
217
247
  asTsString: formatBody(providerInputs, "ts"),
218
248
  },
249
+ autoInputs: {
250
+ asObj: autoInputs,
251
+ asCurlString: formatBody(autoInputs, "curl"),
252
+ asJsonString: formatBody(autoInputs, "json"),
253
+ asPythonString: formatBody(autoInputs, "python"),
254
+ asTsString: formatBody(autoInputs, "ts"),
255
+ },
219
256
  model,
220
257
  provider,
221
- providerModelId: providerModelId ?? model.id,
258
+ providerModelId:
259
+ task === "conversational" && !opts?.endpointUrl && !opts?.directRequest
260
+ ? provider !== "auto"
261
+ ? `${model.id}:${provider}` // e.g. "moonshotai/Kimi-K2-Instruct:groq"
262
+ : model.id
263
+ : providerModelId ?? model.id,
222
264
  billTo: opts?.billTo,
223
265
  endpointUrl: opts?.endpointUrl,
224
266
  };
225
267
 
226
268
  /// Iterate over clients => check if a snippet exists => generate
227
- const clients = provider === "auto" ? CLIENTS_AUTO_POLICY : CLIENTS;
269
+ const clients = provider === "auto" && task !== "conversational" ? CLIENTS_NON_CONVERSATIONAL_AUTO_POLICY : CLIENTS;
228
270
  return inferenceSnippetLanguages
229
271
  .map((language) => {
230
272
  const langClients = clients[language] ?? [];
@@ -5,6 +5,7 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
5
5
  "basic": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
6
6
  "basicAudio": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"audio/flac\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
7
7
  "basicImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});",
8
+ "conversational": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ \n{{ autoInputs.asTsString }}\n}).then((response) => {\n console.log(JSON.stringify(response));\n});",
8
9
  "imageToImage": "const image = fs.readFileSync(\"{{inputs.asObj.inputs}}\");\n\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"image/jpeg\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: {\n\t\t\t\t\"inputs\": `data:image/png;base64,${data.inputs.encode(\"base64\")}`,\n\t\t\t\t\"parameters\": data.parameters,\n\t\t\t}\n\t\t}\n\t);\n\tconst result = await response.json();\n\treturn result;\n}\n\nquery({ \n\tinputs: image,\n\tparameters: {\n\t\tprompt: \"{{ inputs.asObj.parameters.prompt }}\",\n\t}\n}).then((response) => {\n console.log(JSON.stringify(response));\n});",
9
10
  "textToAudio": "{% if model.library_name == \"transformers\" %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n // Returns a byte object of the Audio wavform. Use it directly!\n});\n{% else %}\nasync function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n const result = await response.json();\n return result;\n}\n\nquery({ inputs: {{ providerInputs.asObj.inputs }} }).then((response) => {\n console.log(JSON.stringify(response));\n});\n{% endif %} ",
10
11
  "textToImage": "async function query(data) {\n\tconst response = await fetch(\n\t\t\"{{ fullUrl }}\",\n\t\t{\n\t\t\theaders: {\n\t\t\t\tAuthorization: \"{{ authorizationHeader }}\",\n\t\t\t\t\"Content-Type\": \"application/json\",\n{% if billTo %}\n\t\t\t\t\"X-HF-Bill-To\": \"{{ billTo }}\",\n{% endif %}\t\t\t},\n\t\t\tmethod: \"POST\",\n\t\t\tbody: JSON.stringify(data),\n\t\t}\n\t);\n\tconst result = await response.blob();\n\treturn result;\n}\n\n\nquery({ {{ providerInputs.asTsString }} }).then((response) => {\n // Use image\n});",
@@ -55,8 +56,8 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
55
56
  "basic": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {{ providerInputs.asObj.inputs }},\n}) ",
56
57
  "basicAudio": "def query(filename):\n with open(filename, \"rb\") as f:\n data = f.read()\n response = requests.post(API_URL, headers={\"Content-Type\": \"audio/flac\", **headers}, data=data)\n return response.json()\n\noutput = query({{ providerInputs.asObj.inputs }})",
57
58
  "basicImage": "def query(filename):\n with open(filename, \"rb\") as f:\n data = f.read()\n response = requests.post(API_URL, headers={\"Content-Type\": \"image/jpeg\", **headers}, data=data)\n return response.json()\n\noutput = query({{ providerInputs.asObj.inputs }})",
58
- "conversational": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\nresponse = query({\n{{ providerInputs.asJsonString }}\n})\n\nprint(response[\"choices\"][0][\"message\"])",
59
- "conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{ providerInputs.asJsonString }},\n \"stream\": True,\n})\n\nfor chunk in chunks:\n print(chunk[\"choices\"][0][\"delta\"][\"content\"], end=\"\")",
59
+ "conversational": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\nresponse = query({\n{{ autoInputs.asJsonString }}\n})\n\nprint(response[\"choices\"][0][\"message\"])",
60
+ "conversationalStream": "def query(payload):\n response = requests.post(API_URL, headers=headers, json=payload, stream=True)\n for line in response.iter_lines():\n if not line.startswith(b\"data:\"):\n continue\n if line.strip() == b\"data: [DONE]\":\n return\n yield json.loads(line.decode(\"utf-8\").lstrip(\"data:\").rstrip(\"/n\"))\n\nchunks = query({\n{{ autoInputs.asJsonString }},\n \"stream\": True,\n})\n\nfor chunk in chunks:\n print(chunk[\"choices\"][0][\"delta\"][\"content\"], end=\"\")",
60
61
  "documentQuestionAnswering": "def query(payload):\n with open(payload[\"image\"], \"rb\") as f:\n img = f.read()\n payload[\"image\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.json()\n\noutput = query({\n \"inputs\": {\n \"image\": \"{{ inputs.asObj.image }}\",\n \"question\": \"{{ inputs.asObj.question }}\",\n },\n}) ",
61
62
  "imageToImage": "with open(\"{{inputs.asObj.inputs}}\", \"rb\") as image_file:\n image_base_64 = base64.b64encode(image_file.read()).decode('utf-8')\n\ndef query(payload):\n with open(payload[\"inputs\"], \"rb\") as f:\n img = f.read()\n payload[\"inputs\"] = base64.b64encode(img).decode(\"utf-8\")\n response = requests.post(API_URL, headers=headers, json=payload)\n return response.content\n\nimage_bytes = query({\n{{ providerInputs.asJsonString }}\n})\n\n# You can access the image with PIL.Image for example\nimport io\nfrom PIL import Image\nimage = Image.open(io.BytesIO(image_bytes)) ",
62
63
  "importRequests": "{% if importBase64 %}\nimport base64\n{% endif %}\n{% if importJson %}\nimport json\n{% endif %}\nimport requests\n\nAPI_URL = \"{{ fullUrl }}\"\nheaders = {\n \"Authorization\": \"{{ authorizationHeader }}\",\n{% if billTo %}\n \"X-HF-Bill-To\": \"{{ billTo }}\"\n{% endif %}\n}",
@@ -73,8 +74,8 @@ export const templates: Record<string, Record<string, Record<string, string>>> =
73
74
  "basic": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }}\n }'",
74
75
  "basicAudio": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: audio/flac' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
75
76
  "basicImage": "curl {{ fullUrl }} \\\n -X POST \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: image/jpeg' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n --data-binary @{{ providerInputs.asObj.inputs }}",
76
- "conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": false\n }'",
77
- "conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ providerInputs.asCurlString }},\n \"stream\": true\n }'",
77
+ "conversational": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ autoInputs.asCurlString }},\n \"stream\": false\n }'",
78
+ "conversationalStream": "curl {{ fullUrl }} \\\n -H 'Authorization: {{ authorizationHeader }}' \\\n -H 'Content-Type: application/json' \\\n{% if billTo %}\n -H 'X-HF-Bill-To: {{ billTo }}' \\\n{% endif %}\n -d '{\n{{ autoInputs.asCurlString }},\n \"stream\": true\n }'",
78
79
  "zeroShotClassification": "curl {{ fullUrl }} \\\n -X POST \\\n -d '{\"inputs\": {{ providerInputs.asObj.inputs }}, \"parameters\": {\"candidate_labels\": [\"refund\", \"legal\", \"faq\"]}}' \\\n -H 'Content-Type: application/json' \\\n -H 'Authorization: {{ authorizationHeader }}'\n{% if billTo %} \\\n -H 'X-HF-Bill-To: {{ billTo }}'\n{% endif %}"
79
80
  }
80
81
  }
@@ -8,7 +8,7 @@ import { innerRequest } from "../../utils/request.js";
8
8
  export type TextToImageArgs = BaseArgs & TextToImageInput;
9
9
 
10
10
  interface TextToImageOptions extends Options {
11
- outputType?: "url" | "blob";
11
+ outputType?: "url" | "blob" | "json";
12
12
  }
13
13
 
14
14
  /**
@@ -23,7 +23,14 @@ export async function textToImage(
23
23
  args: TextToImageArgs,
24
24
  options?: TextToImageOptions & { outputType?: undefined | "blob" }
25
25
  ): Promise<Blob>;
26
- export async function textToImage(args: TextToImageArgs, options?: TextToImageOptions): Promise<Blob | string> {
26
+ export async function textToImage(
27
+ args: TextToImageArgs,
28
+ options?: TextToImageOptions & { outputType?: undefined | "json" }
29
+ ): Promise<Record<string, unknown>>;
30
+ export async function textToImage(
31
+ args: TextToImageArgs,
32
+ options?: TextToImageOptions
33
+ ): Promise<Blob | string | Record<string, unknown>> {
27
34
  const provider = await resolveProvider(args.provider, args.model, args.endpointUrl);
28
35
  const providerHelper = getProviderHelper(provider, "text-to-image");
29
36
  const { data: res } = await innerRequest<Record<string, unknown>>(args, providerHelper, {