@huggingface/inference 3.15.0 → 4.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 (134) hide show
  1. package/README.md +102 -0
  2. package/dist/commonjs/errors.d.ts +46 -0
  3. package/dist/commonjs/errors.d.ts.map +1 -0
  4. package/dist/commonjs/errors.js +70 -0
  5. package/dist/commonjs/index.d.ts +1 -1
  6. package/dist/commonjs/index.d.ts.map +1 -1
  7. package/dist/commonjs/index.js +2 -3
  8. package/dist/commonjs/lib/getInferenceProviderMapping.d.ts.map +1 -1
  9. package/dist/commonjs/lib/getInferenceProviderMapping.js +27 -16
  10. package/dist/commonjs/lib/getProviderHelper.d.ts.map +1 -1
  11. package/dist/commonjs/lib/getProviderHelper.js +4 -3
  12. package/dist/commonjs/lib/makeRequestOptions.d.ts.map +1 -1
  13. package/dist/commonjs/lib/makeRequestOptions.js +12 -11
  14. package/dist/commonjs/package.d.ts +1 -1
  15. package/dist/commonjs/package.d.ts.map +1 -1
  16. package/dist/commonjs/package.js +1 -1
  17. package/dist/commonjs/providers/black-forest-labs.d.ts.map +1 -1
  18. package/dist/commonjs/providers/black-forest-labs.js +4 -4
  19. package/dist/commonjs/providers/fal-ai.d.ts.map +1 -1
  20. package/dist/commonjs/providers/fal-ai.js +29 -17
  21. package/dist/commonjs/providers/featherless-ai.d.ts.map +1 -1
  22. package/dist/commonjs/providers/featherless-ai.js +2 -2
  23. package/dist/commonjs/providers/hf-inference.d.ts.map +1 -1
  24. package/dist/commonjs/providers/hf-inference.js +27 -30
  25. package/dist/commonjs/providers/hyperbolic.d.ts.map +1 -1
  26. package/dist/commonjs/providers/hyperbolic.js +3 -3
  27. package/dist/commonjs/providers/nebius.d.ts.map +1 -1
  28. package/dist/commonjs/providers/nebius.js +2 -2
  29. package/dist/commonjs/providers/novita.d.ts.map +1 -1
  30. package/dist/commonjs/providers/novita.js +12 -8
  31. package/dist/commonjs/providers/nscale.d.ts.map +1 -1
  32. package/dist/commonjs/providers/nscale.js +2 -2
  33. package/dist/commonjs/providers/ovhcloud.d.ts.map +1 -1
  34. package/dist/commonjs/providers/ovhcloud.js +2 -2
  35. package/dist/commonjs/providers/providerHelper.js +3 -3
  36. package/dist/commonjs/providers/replicate.js +4 -4
  37. package/dist/commonjs/providers/sambanova.d.ts +16 -0
  38. package/dist/commonjs/providers/sambanova.d.ts.map +1 -1
  39. package/dist/commonjs/providers/sambanova.js +2 -18
  40. package/dist/commonjs/providers/together.d.ts.map +1 -1
  41. package/dist/commonjs/providers/together.js +3 -3
  42. package/dist/commonjs/snippets/getInferenceSnippets.d.ts +2 -1
  43. package/dist/commonjs/snippets/getInferenceSnippets.d.ts.map +1 -1
  44. package/dist/commonjs/snippets/getInferenceSnippets.js +44 -5
  45. package/dist/commonjs/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  46. package/dist/commonjs/tasks/audio/automaticSpeechRecognition.js +2 -2
  47. package/dist/commonjs/utils/request.d.ts.map +1 -1
  48. package/dist/commonjs/utils/request.js +77 -12
  49. package/dist/commonjs/vendor/type-fest/basic.d.ts +33 -0
  50. package/dist/commonjs/vendor/type-fest/basic.d.ts.map +1 -0
  51. package/dist/commonjs/vendor/type-fest/basic.js +2 -0
  52. package/dist/esm/errors.d.ts +46 -0
  53. package/dist/esm/errors.d.ts.map +1 -0
  54. package/dist/esm/errors.js +62 -0
  55. package/dist/esm/index.d.ts +1 -1
  56. package/dist/esm/index.d.ts.map +1 -1
  57. package/dist/esm/index.js +1 -1
  58. package/dist/esm/lib/getInferenceProviderMapping.d.ts.map +1 -1
  59. package/dist/esm/lib/getInferenceProviderMapping.js +27 -16
  60. package/dist/esm/lib/getProviderHelper.d.ts.map +1 -1
  61. package/dist/esm/lib/getProviderHelper.js +4 -3
  62. package/dist/esm/lib/makeRequestOptions.d.ts.map +1 -1
  63. package/dist/esm/lib/makeRequestOptions.js +12 -11
  64. package/dist/esm/package.d.ts +1 -1
  65. package/dist/esm/package.d.ts.map +1 -1
  66. package/dist/esm/package.js +1 -1
  67. package/dist/esm/providers/black-forest-labs.d.ts.map +1 -1
  68. package/dist/esm/providers/black-forest-labs.js +4 -4
  69. package/dist/esm/providers/fal-ai.d.ts.map +1 -1
  70. package/dist/esm/providers/fal-ai.js +29 -17
  71. package/dist/esm/providers/featherless-ai.d.ts.map +1 -1
  72. package/dist/esm/providers/featherless-ai.js +2 -2
  73. package/dist/esm/providers/hf-inference.d.ts.map +1 -1
  74. package/dist/esm/providers/hf-inference.js +27 -30
  75. package/dist/esm/providers/hyperbolic.d.ts.map +1 -1
  76. package/dist/esm/providers/hyperbolic.js +3 -3
  77. package/dist/esm/providers/nebius.d.ts.map +1 -1
  78. package/dist/esm/providers/nebius.js +2 -2
  79. package/dist/esm/providers/novita.d.ts.map +1 -1
  80. package/dist/esm/providers/novita.js +12 -8
  81. package/dist/esm/providers/nscale.d.ts.map +1 -1
  82. package/dist/esm/providers/nscale.js +2 -2
  83. package/dist/esm/providers/ovhcloud.d.ts.map +1 -1
  84. package/dist/esm/providers/ovhcloud.js +2 -2
  85. package/dist/esm/providers/providerHelper.js +3 -3
  86. package/dist/esm/providers/replicate.js +4 -4
  87. package/dist/esm/providers/sambanova.d.ts +16 -0
  88. package/dist/esm/providers/sambanova.d.ts.map +1 -1
  89. package/dist/esm/providers/sambanova.js +2 -18
  90. package/dist/esm/providers/together.d.ts.map +1 -1
  91. package/dist/esm/providers/together.js +3 -3
  92. package/dist/esm/snippets/getInferenceSnippets.d.ts +2 -1
  93. package/dist/esm/snippets/getInferenceSnippets.d.ts.map +1 -1
  94. package/dist/esm/snippets/getInferenceSnippets.js +44 -5
  95. package/dist/esm/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  96. package/dist/esm/tasks/audio/automaticSpeechRecognition.js +2 -2
  97. package/dist/esm/utils/request.d.ts.map +1 -1
  98. package/dist/esm/utils/request.js +77 -12
  99. package/dist/esm/vendor/type-fest/basic.d.ts +33 -0
  100. package/dist/esm/vendor/type-fest/basic.d.ts.map +1 -0
  101. package/dist/esm/vendor/type-fest/basic.js +1 -0
  102. package/package.json +2 -2
  103. package/src/errors.ts +82 -0
  104. package/src/index.ts +1 -1
  105. package/src/lib/getInferenceProviderMapping.ts +42 -22
  106. package/src/lib/getProviderHelper.ts +8 -3
  107. package/src/lib/makeRequestOptions.ts +20 -11
  108. package/src/package.ts +1 -1
  109. package/src/providers/black-forest-labs.ts +14 -4
  110. package/src/providers/fal-ai.ts +59 -23
  111. package/src/providers/featherless-ai.ts +2 -2
  112. package/src/providers/hf-inference.ts +75 -34
  113. package/src/providers/hyperbolic.ts +3 -4
  114. package/src/providers/nebius.ts +2 -2
  115. package/src/providers/novita.ts +30 -8
  116. package/src/providers/nscale.ts +2 -2
  117. package/src/providers/ovhcloud.ts +2 -2
  118. package/src/providers/providerHelper.ts +3 -3
  119. package/src/providers/replicate.ts +4 -4
  120. package/src/providers/sambanova.ts +3 -4
  121. package/src/providers/together.ts +3 -3
  122. package/src/snippets/getInferenceSnippets.ts +69 -7
  123. package/src/tasks/audio/automaticSpeechRecognition.ts +2 -2
  124. package/src/utils/request.ts +127 -14
  125. package/src/vendor/type-fest/basic.ts +31 -0
  126. package/src/vendor/type-fest/license-cc0 +121 -0
  127. package/src/vendor/type-fest/license-mit +9 -0
  128. package/dist/commonjs/lib/InferenceOutputError.d.ts +0 -4
  129. package/dist/commonjs/lib/InferenceOutputError.d.ts.map +0 -1
  130. package/dist/commonjs/lib/InferenceOutputError.js +0 -10
  131. package/dist/esm/lib/InferenceOutputError.d.ts +0 -4
  132. package/dist/esm/lib/InferenceOutputError.d.ts.map +0 -1
  133. package/dist/esm/lib/InferenceOutputError.js +0 -6
  134. package/src/lib/InferenceOutputError.ts +0 -8
@@ -18,8 +18,8 @@
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.OvhCloudTextGenerationTask = exports.OvhCloudConversationalTask = void 0;
20
20
  const providerHelper_js_1 = require("./providerHelper.js");
21
- const InferenceOutputError_js_1 = require("../lib/InferenceOutputError.js");
22
21
  const omit_js_1 = require("../utils/omit.js");
22
+ const errors_js_1 = require("../errors.js");
23
23
  const OVHCLOUD_API_BASE_URL = "https://oai.endpoints.kepler.ai.cloud.ovh.net";
24
24
  class OvhCloudConversationalTask extends providerHelper_js_1.BaseConversationalTask {
25
25
  constructor() {
@@ -54,7 +54,7 @@ class OvhCloudTextGenerationTask extends providerHelper_js_1.BaseTextGenerationT
54
54
  generated_text: completion.text,
55
55
  };
56
56
  }
57
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected OVHcloud text generation response format");
57
+ throw new errors_js_1.InferenceClientProviderOutputError("Received malformed response from OVHcloud text generation API");
58
58
  }
59
59
  }
60
60
  exports.OvhCloudTextGenerationTask = OvhCloudTextGenerationTask;
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BaseTextGenerationTask = exports.BaseConversationalTask = exports.TaskProviderHelper = void 0;
4
4
  const config_js_1 = require("../config.js");
5
- const InferenceOutputError_js_1 = require("../lib/InferenceOutputError.js");
5
+ const errors_js_1 = require("../errors.js");
6
6
  const toArray_js_1 = require("../utils/toArray.js");
7
7
  /**
8
8
  * Base class for task-specific provider helpers
@@ -78,7 +78,7 @@ class BaseConversationalTask extends TaskProviderHelper {
78
78
  typeof response?.usage === "object") {
79
79
  return response;
80
80
  }
81
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected ChatCompletionOutput");
81
+ throw new errors_js_1.InferenceClientProviderOutputError("Expected ChatCompletionOutput");
82
82
  }
83
83
  }
84
84
  exports.BaseConversationalTask = BaseConversationalTask;
@@ -102,7 +102,7 @@ class BaseTextGenerationTask extends TaskProviderHelper {
102
102
  res.every((x) => typeof x === "object" && !!x && "generated_text" in x && typeof x.generated_text === "string")) {
103
103
  return res[0];
104
104
  }
105
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected Array<{generated_text: string}>");
105
+ throw new errors_js_1.InferenceClientProviderOutputError("Expected Array<{generated_text: string}>");
106
106
  }
107
107
  }
108
108
  exports.BaseTextGenerationTask = BaseTextGenerationTask;
@@ -17,7 +17,7 @@ exports.ReplicateTextToVideoTask = exports.ReplicateTextToSpeechTask = exports.R
17
17
  *
18
18
  * Thanks!
19
19
  */
20
- const InferenceOutputError_js_1 = require("../lib/InferenceOutputError.js");
20
+ const errors_js_1 = require("../errors.js");
21
21
  const isUrl_js_1 = require("../lib/isUrl.js");
22
22
  const omit_js_1 = require("../utils/omit.js");
23
23
  const providerHelper_js_1 = require("./providerHelper.js");
@@ -84,7 +84,7 @@ class ReplicateTextToImageTask extends ReplicateTask {
84
84
  const urlResponse = await fetch(res.output[0]);
85
85
  return await urlResponse.blob();
86
86
  }
87
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected Replicate text-to-image response format");
87
+ throw new errors_js_1.InferenceClientProviderOutputError("Received malformed response from Replicate text-to-image API");
88
88
  }
89
89
  }
90
90
  exports.ReplicateTextToImageTask = ReplicateTextToImageTask;
@@ -115,7 +115,7 @@ class ReplicateTextToSpeechTask extends ReplicateTask {
115
115
  }
116
116
  }
117
117
  }
118
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected Blob or object with output");
118
+ throw new errors_js_1.InferenceClientProviderOutputError("Received malformed response from Replicate text-to-speech API");
119
119
  }
120
120
  }
121
121
  exports.ReplicateTextToSpeechTask = ReplicateTextToSpeechTask;
@@ -129,7 +129,7 @@ class ReplicateTextToVideoTask extends ReplicateTask {
129
129
  const urlResponse = await fetch(response.output);
130
130
  return await urlResponse.blob();
131
131
  }
132
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected { output: string }");
132
+ throw new errors_js_1.InferenceClientProviderOutputError("Received malformed response from Replicate text-to-video API");
133
133
  }
134
134
  }
135
135
  exports.ReplicateTextToVideoTask = ReplicateTextToVideoTask;
@@ -1,3 +1,19 @@
1
+ /**
2
+ * See the registered mapping of HF model ID => Sambanova model ID here:
3
+ *
4
+ * https://huggingface.co/api/partners/sambanova/models
5
+ *
6
+ * This is a publicly available mapping.
7
+ *
8
+ * If you want to try to run inference for a new model locally before it's registered on huggingface.co,
9
+ * you can add it to the dictionary "HARDCODED_MODEL_ID_MAPPING" in consts.ts, for dev purposes.
10
+ *
11
+ * - If you work at Sambanova and want to update this mapping, please use the model mapping API we provide on huggingface.co
12
+ * - If you're a community member and want to add a new supported HF model to Sambanova, please open an issue on the present repo
13
+ * and we will tag Sambanova team members.
14
+ *
15
+ * Thanks!
16
+ */
1
17
  import type { FeatureExtractionOutput } from "@huggingface/tasks";
2
18
  import type { BodyParams } from "../types.js";
3
19
  import type { FeatureExtractionTaskHelper } from "./providerHelper.js";
@@ -1 +1 @@
1
- {"version":3,"file":"sambanova.d.ts","sourceRoot":"","sources":["../../../src/providers/sambanova.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAEjF,qBAAa,2BAA4B,SAAQ,sBAAsB;;CAItE;AAED,qBAAa,8BAA+B,SAAQ,kBAAmB,YAAW,2BAA2B;;IAKnG,SAAS,IAAI,MAAM;IAIb,WAAW,CAAC,QAAQ,EAAE,uBAAuB,GAAG,OAAO,CAAC,uBAAuB,CAAC;IAStF,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAOpE"}
1
+ {"version":3,"file":"sambanova.d.ts","sourceRoot":"","sources":["../../../src/providers/sambanova.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAGjF,qBAAa,2BAA4B,SAAQ,sBAAsB;;CAItE;AAED,qBAAa,8BAA+B,SAAQ,kBAAmB,YAAW,2BAA2B;;IAKnG,SAAS,IAAI,MAAM;IAIb,WAAW,CAAC,QAAQ,EAAE,uBAAuB,GAAG,OAAO,CAAC,uBAAuB,CAAC;IAStF,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAOpE"}
@@ -1,24 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SambanovaFeatureExtractionTask = exports.SambanovaConversationalTask = void 0;
4
- /**
5
- * See the registered mapping of HF model ID => Sambanova model ID here:
6
- *
7
- * https://huggingface.co/api/partners/sambanova/models
8
- *
9
- * This is a publicly available mapping.
10
- *
11
- * If you want to try to run inference for a new model locally before it's registered on huggingface.co,
12
- * you can add it to the dictionary "HARDCODED_MODEL_ID_MAPPING" in consts.ts, for dev purposes.
13
- *
14
- * - If you work at Sambanova and want to update this mapping, please use the model mapping API we provide on huggingface.co
15
- * - If you're a community member and want to add a new supported HF model to Sambanova, please open an issue on the present repo
16
- * and we will tag Sambanova team members.
17
- *
18
- * Thanks!
19
- */
20
- const InferenceOutputError_js_1 = require("../lib/InferenceOutputError.js");
21
4
  const providerHelper_js_1 = require("./providerHelper.js");
5
+ const errors_js_1 = require("../errors.js");
22
6
  class SambanovaConversationalTask extends providerHelper_js_1.BaseConversationalTask {
23
7
  constructor() {
24
8
  super("sambanova", "https://api.sambanova.ai");
@@ -36,7 +20,7 @@ class SambanovaFeatureExtractionTask extends providerHelper_js_1.TaskProviderHel
36
20
  if (typeof response === "object" && "data" in response && Array.isArray(response.data)) {
37
21
  return response.data.map((item) => item.embedding);
38
22
  }
39
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected Sambanova feature-extraction (embeddings) response format to be {'data' : list of {'embedding' : number[]}}");
23
+ throw new errors_js_1.InferenceClientProviderOutputError("Received malformed response from Sambanova feature-extraction (embeddings) API");
40
24
  }
41
25
  preparePayload(params) {
42
26
  return {
@@ -1 +1 @@
1
- {"version":3,"file":"together.d.ts","sourceRoot":"","sources":["../../../src/providers/together.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,gCAAgC,EAAE,MAAM,oBAAoB,CAAC;AAEvH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,EACN,sBAAsB,EACtB,sBAAsB,EACtB,kBAAkB,EAClB,KAAK,qBAAqB,EAC1B,MAAM,qBAAqB,CAAC;AAI7B,UAAU,4BAA6B,SAAQ,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC;IACnF,OAAO,EAAE,KAAK,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;QACb,aAAa,EAAE,gCAAgC,CAAC;QAChD,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,OAAO,CAAC;QAClB,KAAK,EAAE,MAAM,CAAC;KACd,CAAC,CAAC;CACH;AAED,UAAU,6BAA6B;IACtC,IAAI,EAAE,KAAK,CAAC;QACX,QAAQ,EAAE,MAAM,CAAC;KACjB,CAAC,CAAC;CACH;AAED,qBAAa,0BAA2B,SAAQ,sBAAsB;;CAIrE;AAED,qBAAa,0BAA2B,SAAQ,sBAAsB;;IAK5D,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAQrD,WAAW,CAAC,QAAQ,EAAE,4BAA4B,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAcjG;AAED,qBAAa,uBAAwB,SAAQ,kBAAmB,YAAW,qBAAqB;;IAK/F,SAAS,IAAI,MAAM;IAInB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAUrD,WAAW,CAAC,QAAQ,EAAE,6BAA6B,EAAE,UAAU,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAkB/G"}
1
+ {"version":3,"file":"together.d.ts","sourceRoot":"","sources":["../../../src/providers/together.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,gCAAgC,EAAE,MAAM,oBAAoB,CAAC;AACvH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,EACN,sBAAsB,EACtB,sBAAsB,EACtB,kBAAkB,EAClB,KAAK,qBAAqB,EAC1B,MAAM,qBAAqB,CAAC;AAK7B,UAAU,4BAA6B,SAAQ,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC;IACnF,OAAO,EAAE,KAAK,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;QACb,aAAa,EAAE,gCAAgC,CAAC;QAChD,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,OAAO,CAAC;QAClB,KAAK,EAAE,MAAM,CAAC;KACd,CAAC,CAAC;CACH;AAED,UAAU,6BAA6B;IACtC,IAAI,EAAE,KAAK,CAAC;QACX,QAAQ,EAAE,MAAM,CAAC;KACjB,CAAC,CAAC;CACH;AAED,qBAAa,0BAA2B,SAAQ,sBAAsB;;CAIrE;AAED,qBAAa,0BAA2B,SAAQ,sBAAsB;;IAK5D,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAQrD,WAAW,CAAC,QAAQ,EAAE,4BAA4B,GAAG,OAAO,CAAC,oBAAoB,CAAC;CAcjG;AAED,qBAAa,uBAAwB,SAAQ,kBAAmB,YAAW,qBAAqB;;IAK/F,SAAS,IAAI,MAAM;IAInB,cAAc,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAUrD,WAAW,CAAC,QAAQ,EAAE,6BAA6B,EAAE,UAAU,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;CAkB/G"}
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TogetherTextToImageTask = exports.TogetherTextGenerationTask = exports.TogetherConversationalTask = void 0;
4
- const InferenceOutputError_js_1 = require("../lib/InferenceOutputError.js");
5
4
  const omit_js_1 = require("../utils/omit.js");
6
5
  const providerHelper_js_1 = require("./providerHelper.js");
6
+ const errors_js_1 = require("../errors.js");
7
7
  const TOGETHER_API_BASE_URL = "https://api.together.xyz";
8
8
  class TogetherConversationalTask extends providerHelper_js_1.BaseConversationalTask {
9
9
  constructor() {
@@ -32,7 +32,7 @@ class TogetherTextGenerationTask extends providerHelper_js_1.BaseTextGenerationT
32
32
  generated_text: completion.text,
33
33
  };
34
34
  }
35
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected Together text generation response format");
35
+ throw new errors_js_1.InferenceClientProviderOutputError("Received malformed response from Together text generation API");
36
36
  }
37
37
  }
38
38
  exports.TogetherTextGenerationTask = TogetherTextGenerationTask;
@@ -65,7 +65,7 @@ class TogetherTextToImageTask extends providerHelper_js_1.TaskProviderHelper {
65
65
  }
66
66
  return fetch(`data:image/jpeg;base64,${base64Data}`).then((res) => res.blob());
67
67
  }
68
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected Together text-to-image response format");
68
+ throw new errors_js_1.InferenceClientProviderOutputError("Received malformed response from Together text-to-image API");
69
69
  }
70
70
  }
71
71
  exports.TogetherTextToImageTask = TogetherTextToImageTask;
@@ -4,6 +4,7 @@ import type { InferenceProviderOrPolicy } from "../types.js";
4
4
  export type InferenceSnippetOptions = {
5
5
  streaming?: boolean;
6
6
  billTo?: string;
7
+ accessToken?: string;
7
8
  } & Record<string, unknown>;
8
- export declare function getInferenceSnippets(model: ModelDataMinimal, accessToken: string, provider: InferenceProviderOrPolicy, inferenceProviderMapping?: InferenceProviderModelMapping, opts?: Record<string, unknown>): InferenceSnippet[];
9
+ export declare function getInferenceSnippets(model: ModelDataMinimal, provider: InferenceProviderOrPolicy, inferenceProviderMapping?: InferenceProviderModelMapping, opts?: Record<string, unknown>): InferenceSnippet[];
9
10
  //# sourceMappingURL=getInferenceSnippets.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,uCAAuC,CAAC;AAG3F,OAAO,KAAK,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAGzF,MAAM,MAAM,uBAAuB,GAAG;IAAE,SAAS,CAAC,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAmUzG,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,gBAAgB,EACvB,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,yBAAyB,EACnC,wBAAwB,CAAC,EAAE,6BAA6B,EACxD,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CAIpB"}
1
+ {"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AACA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,uCAAuC,CAAC;AAG3F,OAAO,KAAK,EAAE,yBAAyB,EAA8B,MAAM,aAAa,CAAC;AAGzF,MAAM,MAAM,uBAAuB,GAAG;IAAE,SAAS,CAAC,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAC5G,MAAM,EACN,OAAO,CACP,CAAC;AA0UF,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,yBAAyB,EACnC,wBAAwB,CAAC,EAAE,6BAA6B,EACxD,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CAIpB"}
@@ -76,9 +76,10 @@ const HF_JS_METHODS = {
76
76
  "text-to-speech": "textToSpeech",
77
77
  translation: "translation",
78
78
  };
79
+ const ACCESS_TOKEN_PLACEHOLDER = "<ACCESS_TOKEN>"; // Placeholder to replace with env variable in snippets
79
80
  // Snippet generators
80
81
  const snippetGenerator = (templateName, inputPreparationFn) => {
81
- return (model, accessToken, provider, inferenceProviderMapping, opts) => {
82
+ return (model, provider, inferenceProviderMapping, opts) => {
82
83
  const providerModelId = inferenceProviderMapping?.providerId ?? model.id;
83
84
  /// Hacky: hard-code conversational templates here
84
85
  let task = model.pipeline_tag;
@@ -97,10 +98,11 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
97
98
  console.error(`Failed to get provider helper for ${provider} (${task})`, e);
98
99
  return [];
99
100
  }
101
+ const accessTokenOrPlaceholder = opts?.accessToken ?? ACCESS_TOKEN_PLACEHOLDER;
100
102
  /// Prepare inputs + make request
101
103
  const inputs = inputPreparationFn ? inputPreparationFn(model, opts) : { inputs: (0, tasks_1.getModelInputSnippet)(model) };
102
104
  const request = (0, makeRequestOptions_js_1.makeRequestOptionsFromResolvedModel)(providerModelId, providerHelper, {
103
- accessToken,
105
+ accessToken: accessTokenOrPlaceholder,
104
106
  provider,
105
107
  ...inputs,
106
108
  }, inferenceProviderMapping, {
@@ -121,7 +123,7 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
121
123
  }
122
124
  /// Prepare template injection data
123
125
  const params = {
124
- accessToken,
126
+ accessToken: accessTokenOrPlaceholder,
125
127
  authorizationHeader: request.info.headers?.Authorization,
126
128
  baseUrl: removeSuffix(request.url, "/chat/completions"),
127
129
  fullUrl: request.url,
@@ -185,6 +187,10 @@ const snippetGenerator = (templateName, inputPreparationFn) => {
185
187
  });
186
188
  snippet = `${importSection}\n\n${snippet}`;
187
189
  }
190
+ /// Replace access token placeholder
191
+ if (snippet.includes(ACCESS_TOKEN_PLACEHOLDER)) {
192
+ snippet = replaceAccessTokenPlaceholder(snippet, language, provider);
193
+ }
188
194
  /// Snippet is ready!
189
195
  return { language, client: client, content: snippet };
190
196
  })
@@ -247,9 +253,9 @@ const snippets = {
247
253
  "zero-shot-classification": snippetGenerator("zeroShotClassification"),
248
254
  "zero-shot-image-classification": snippetGenerator("zeroShotImageClassification"),
249
255
  };
250
- function getInferenceSnippets(model, accessToken, provider, inferenceProviderMapping, opts) {
256
+ function getInferenceSnippets(model, provider, inferenceProviderMapping, opts) {
251
257
  return model.pipeline_tag && model.pipeline_tag in snippets
252
- ? snippets[model.pipeline_tag]?.(model, accessToken, provider, inferenceProviderMapping, opts) ?? []
258
+ ? snippets[model.pipeline_tag]?.(model, provider, inferenceProviderMapping, opts) ?? []
253
259
  : [];
254
260
  }
255
261
  // String manipulation helpers
@@ -310,3 +316,36 @@ function indentString(str) {
310
316
  function removeSuffix(str, suffix) {
311
317
  return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str;
312
318
  }
319
+ function replaceAccessTokenPlaceholder(snippet, language, provider) {
320
+ // If "opts.accessToken" is not set, the snippets are generated with a placeholder.
321
+ // Once snippets are rendered, we replace the placeholder with code to fetch the access token from an environment variable.
322
+ // Determine if HF_TOKEN or specific provider token should be used
323
+ const accessTokenEnvVar = !snippet.includes("https://") || // no URL provided => using a client => use $HF_TOKEN
324
+ snippet.includes("https://router.huggingface.co") || // explicit routed request => use $HF_TOKEN
325
+ provider == "hf-inference" // hf-inference provider => use $HF_TOKEN
326
+ ? "HF_TOKEN"
327
+ : provider.toUpperCase().replace("-", "_") + "_API_KEY"; // e.g. "REPLICATE_API_KEY"
328
+ // Replace the placeholder with the env variable
329
+ if (language === "sh") {
330
+ snippet = snippet.replace(`'Authorization: Bearer ${ACCESS_TOKEN_PLACEHOLDER}'`, `"Authorization: Bearer $${accessTokenEnvVar}"` // e.g. "Authorization: Bearer $HF_TOKEN"
331
+ );
332
+ }
333
+ else if (language === "python") {
334
+ snippet = "import os\n" + snippet;
335
+ snippet = snippet.replace(`"${ACCESS_TOKEN_PLACEHOLDER}"`, `os.environ["${accessTokenEnvVar}"]` // e.g. os.environ["HF_TOKEN")
336
+ );
337
+ snippet = snippet.replace(`"Bearer ${ACCESS_TOKEN_PLACEHOLDER}"`, `f"Bearer {os.environ['${accessTokenEnvVar}']}"` // e.g. f"Bearer {os.environ['HF_TOKEN']}"
338
+ );
339
+ snippet = snippet.replace(`"Key ${ACCESS_TOKEN_PLACEHOLDER}"`, `f"Key {os.environ['${accessTokenEnvVar}']}"` // e.g. f"Key {os.environ['FAL_AI_API_KEY']}"
340
+ );
341
+ }
342
+ else if (language === "js") {
343
+ snippet = snippet.replace(`"${ACCESS_TOKEN_PLACEHOLDER}"`, `process.env.${accessTokenEnvVar}` // e.g. process.env.HF_TOKEN
344
+ );
345
+ snippet = snippet.replace(`Authorization: "Bearer ${ACCESS_TOKEN_PLACEHOLDER}",`, `Authorization: \`Bearer $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `Bearer ${process.env.HF_TOKEN}`,
346
+ );
347
+ snippet = snippet.replace(`Authorization: "Key ${ACCESS_TOKEN_PLACEHOLDER}",`, `Authorization: \`Key $\{process.env.${accessTokenEnvVar}}\`,` // e.g. Authorization: `Key ${process.env.FAL_AI_API_KEY}`,
348
+ );
349
+ }
350
+ return snippet;
351
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"automaticSpeechRecognition.d.ts","sourceRoot":"","sources":["../../../../src/tasks/audio/automaticSpeechRecognition.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,+BAA+B,EAAE,gCAAgC,EAAE,MAAM,oBAAoB,CAAC;AAI5G,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAExD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,MAAM,MAAM,8BAA8B,GAAG,QAAQ,GAAG,CAAC,+BAA+B,GAAG,gBAAgB,CAAC,CAAC;AAC7G;;;GAGG;AACH,wBAAsB,0BAA0B,CAC/C,IAAI,EAAE,8BAA8B,EACpC,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,gCAAgC,CAAC,CAa3C"}
1
+ {"version":3,"file":"automaticSpeechRecognition.d.ts","sourceRoot":"","sources":["../../../../src/tasks/audio/automaticSpeechRecognition.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,+BAA+B,EAAE,gCAAgC,EAAE,MAAM,oBAAoB,CAAC;AAG5G,OAAO,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAExD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAGnD,MAAM,MAAM,8BAA8B,GAAG,QAAQ,GAAG,CAAC,+BAA+B,GAAG,gBAAgB,CAAC,CAAC;AAC7G;;;GAGG;AACH,wBAAsB,0BAA0B,CAC/C,IAAI,EAAE,8BAA8B,EACpC,OAAO,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,gCAAgC,CAAC,CAa3C"}
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.automaticSpeechRecognition = automaticSpeechRecognition;
4
4
  const getInferenceProviderMapping_js_1 = require("../../lib/getInferenceProviderMapping.js");
5
5
  const getProviderHelper_js_1 = require("../../lib/getProviderHelper.js");
6
- const InferenceOutputError_js_1 = require("../../lib/InferenceOutputError.js");
7
6
  const request_js_1 = require("../../utils/request.js");
7
+ const errors_js_1 = require("../../errors.js");
8
8
  /**
9
9
  * This task reads some audio input and outputs the said words within the audio files.
10
10
  * Recommended model (english language): facebook/wav2vec2-large-960h-lv60-self
@@ -19,7 +19,7 @@ async function automaticSpeechRecognition(args, options) {
19
19
  });
20
20
  const isValidOutput = typeof res?.text === "string";
21
21
  if (!isValidOutput) {
22
- throw new InferenceOutputError_js_1.InferenceOutputError("Expected {text: string}");
22
+ throw new errors_js_1.InferenceClientProviderOutputError("Received malformed response from automatic-speech-recognition API");
23
23
  }
24
24
  return providerHelper.getResponse(res);
25
25
  }
@@ -1 +1 @@
1
- {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/utils/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAErE,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAIvE,MAAM,WAAW,eAAe,CAAC,CAAC;IACjC,IAAI,EAAE,CAAC,CAAC;IACR,cAAc,EAAE;QACf,GAAG,EAAE,MAAM,CAAC;QACZ,IAAI,EAAE,WAAW,CAAC;KAClB,CAAC;CACF;AAED;;GAEG;AACH,wBAAsB,YAAY,CAAC,CAAC,EACnC,IAAI,EAAE,WAAW,EACjB,cAAc,EAAE,UAAU,CAAC,OAAO,iBAAiB,CAAC,EACpD,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,oEAAoE;IACpE,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,oCAAoC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAoC7B;AAED;;GAEG;AACH,wBAAuB,qBAAqB,CAAC,CAAC,EAC7C,IAAI,EAAE,WAAW,EACjB,cAAc,EAAE,UAAU,CAAC,OAAO,iBAAiB,CAAC,EACpD,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,oEAAoE;IACpE,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,oCAAoC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,cAAc,CAAC,CAAC,CAAC,CAuFnB"}
1
+ {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../../src/utils/request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAErE,OAAO,KAAK,EAAE,aAAa,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAMvE,MAAM,WAAW,eAAe,CAAC,CAAC;IACjC,IAAI,EAAE,CAAC,CAAC;IACR,cAAc,EAAE;QACf,GAAG,EAAE,MAAM,CAAC;QACZ,IAAI,EAAE,WAAW,CAAC;KAClB,CAAC;CACF;AAaD;;GAEG;AACH,wBAAsB,YAAY,CAAC,CAAC,EACnC,IAAI,EAAE,WAAW,EACjB,cAAc,EAAE,UAAU,CAAC,OAAO,iBAAiB,CAAC,EACpD,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,oEAAoE;IACpE,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,oCAAoC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAwE7B;AAED;;GAEG;AACH,wBAAuB,qBAAqB,CAAC,CAAC,EAC7C,IAAI,EAAE,WAAW,EACjB,cAAc,EAAE,UAAU,CAAC,OAAO,iBAAiB,CAAC,EACpD,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,oEAAoE;IACpE,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,oCAAoC;IACpC,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,cAAc,CAAC,CAAC,CAAC,CAuJnB"}
@@ -4,6 +4,15 @@ exports.innerRequest = innerRequest;
4
4
  exports.innerStreamingRequest = innerStreamingRequest;
5
5
  const makeRequestOptions_js_1 = require("../lib/makeRequestOptions.js");
6
6
  const parse_js_1 = require("../vendor/fetch-event-source/parse.js");
7
+ const errors_js_1 = require("../errors.js");
8
+ function requestArgsToJson(args) {
9
+ // Convert the entire args object to a JSON-serializable format
10
+ const argsWithData = args;
11
+ return JSON.parse(JSON.stringify({
12
+ ...argsWithData,
13
+ data: argsWithData.data ? "[Blob or ArrayBuffer]" : null,
14
+ }));
15
+ }
7
16
  /**
8
17
  * Primitive to make custom calls to the inference provider
9
18
  */
@@ -19,17 +28,37 @@ async function innerRequest(args, providerHelper, options) {
19
28
  if (["application/json", "application/problem+json"].some((ct) => contentType?.startsWith(ct))) {
20
29
  const output = await response.json();
21
30
  if ([400, 422, 404, 500].includes(response.status) && options?.chatCompletion) {
22
- throw new Error(`Server ${args.model} does not seem to support chat completion. Error: ${JSON.stringify(output.error)}`);
31
+ throw new errors_js_1.InferenceClientProviderApiError(`Provider ${args.provider} does not seem to support chat completion for model ${args.model} . Error: ${JSON.stringify(output.error)}`, {
32
+ url,
33
+ method: info.method ?? "GET",
34
+ headers: info.headers,
35
+ body: requestArgsToJson(args),
36
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: output });
23
37
  }
24
- if (output.error || output.detail) {
25
- throw new Error(JSON.stringify(output.error ?? output.detail));
38
+ if (typeof output.error === "string" || typeof output.detail === "string" || typeof output.message === "string") {
39
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: ${output.error ?? output.detail ?? output.message}`, {
40
+ url,
41
+ method: info.method ?? "GET",
42
+ headers: info.headers,
43
+ body: requestArgsToJson(args),
44
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: output });
26
45
  }
27
46
  else {
28
- throw new Error(output);
47
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: an HTTP error occurred when requesting the provider.`, {
48
+ url,
49
+ method: info.method ?? "GET",
50
+ headers: info.headers,
51
+ body: requestArgsToJson(args),
52
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: output });
29
53
  }
30
54
  }
31
55
  const message = contentType?.startsWith("text/plain;") ? await response.text() : undefined;
32
- throw new Error(message ?? "An error occurred while fetching the blob");
56
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: ${message ?? "an HTTP error occurred when requesting the provider"}`, {
57
+ url,
58
+ method: info.method ?? "GET",
59
+ headers: info.headers,
60
+ body: requestArgsToJson(args),
61
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: message ?? "" });
33
62
  }
34
63
  if (response.headers.get("Content-Type")?.startsWith("application/json")) {
35
64
  const data = (await response.json());
@@ -51,24 +80,55 @@ async function* innerStreamingRequest(args, providerHelper, options) {
51
80
  if (response.headers.get("Content-Type")?.startsWith("application/json")) {
52
81
  const output = await response.json();
53
82
  if ([400, 422, 404, 500].includes(response.status) && options?.chatCompletion) {
54
- throw new Error(`Server ${args.model} does not seem to support chat completion. Error: ${output.error}`);
83
+ throw new errors_js_1.InferenceClientProviderApiError(`Provider ${args.provider} does not seem to support chat completion for model ${args.model} . Error: ${JSON.stringify(output.error)}`, {
84
+ url,
85
+ method: info.method ?? "GET",
86
+ headers: info.headers,
87
+ body: requestArgsToJson(args),
88
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: output });
55
89
  }
56
90
  if (typeof output.error === "string") {
57
- throw new Error(output.error);
91
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: ${output.error}`, {
92
+ url,
93
+ method: info.method ?? "GET",
94
+ headers: info.headers,
95
+ body: requestArgsToJson(args),
96
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: output });
58
97
  }
59
98
  if (output.error && "message" in output.error && typeof output.error.message === "string") {
60
99
  /// OpenAI errors
61
- throw new Error(output.error.message);
100
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: ${output.error.message}`, {
101
+ url,
102
+ method: info.method ?? "GET",
103
+ headers: info.headers,
104
+ body: requestArgsToJson(args),
105
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: output });
62
106
  }
63
107
  // Sambanova errors
64
108
  if (typeof output.message === "string") {
65
- throw new Error(output.message);
109
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: ${output.message}`, {
110
+ url,
111
+ method: info.method ?? "GET",
112
+ headers: info.headers,
113
+ body: requestArgsToJson(args),
114
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: output });
66
115
  }
67
116
  }
68
- throw new Error(`Server response contains error: ${response.status}`);
117
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: an HTTP error occurred when requesting the provider.`, {
118
+ url,
119
+ method: info.method ?? "GET",
120
+ headers: info.headers,
121
+ body: requestArgsToJson(args),
122
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: "" });
69
123
  }
70
124
  if (!response.headers.get("content-type")?.startsWith("text/event-stream")) {
71
- throw new Error(`Server does not support event stream content type, it returned ` + response.headers.get("content-type"));
125
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: server does not support event stream content type, it returned ` +
126
+ response.headers.get("content-type"), {
127
+ url,
128
+ method: info.method ?? "GET",
129
+ headers: info.headers,
130
+ body: requestArgsToJson(args),
131
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: "" });
72
132
  }
73
133
  if (!response.body) {
74
134
  return;
@@ -102,7 +162,12 @@ async function* innerStreamingRequest(args, providerHelper, options) {
102
162
  typeof data.error.message === "string"
103
163
  ? data.error.message
104
164
  : JSON.stringify(data.error);
105
- throw new Error(`Error forwarded from backend: ` + errorStr);
165
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: an occurred while streaming the response: ${errorStr}`, {
166
+ url,
167
+ method: info.method ?? "GET",
168
+ headers: info.headers,
169
+ body: requestArgsToJson(args),
170
+ }, { requestId: response.headers.get("x-request-id") ?? "", status: response.status, body: data });
106
171
  }
107
172
  yield data;
108
173
  }
@@ -0,0 +1,33 @@
1
+ /**
2
+ Matches a JSON object.
3
+
4
+ This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`.
5
+
6
+ @category JSON
7
+ */
8
+ export type JsonObject = {
9
+ [Key in string]: JsonValue;
10
+ } & {
11
+ [Key in string]?: JsonValue | undefined;
12
+ };
13
+ /**
14
+ Matches a JSON array.
15
+
16
+ @category JSON
17
+ */
18
+ export type JsonArray = JsonValue[] | readonly JsonValue[];
19
+ /**
20
+ Matches any valid JSON primitive value.
21
+
22
+ @category JSON
23
+ */
24
+ export type JsonPrimitive = string | number | boolean | null;
25
+ /**
26
+ Matches any valid JSON value.
27
+
28
+ @see `Jsonify` if you need to transform a type to one that is assignable to `JsonValue`.
29
+
30
+ @category JSON
31
+ */
32
+ export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
33
+ //# sourceMappingURL=basic.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"basic.d.ts","sourceRoot":"","sources":["../../../../src/vendor/type-fest/basic.ts"],"names":[],"mappings":"AAAA;;;;;;EAME;AACF,MAAM,MAAM,UAAU,GAAG;KAAG,GAAG,IAAI,MAAM,GAAG,SAAS;CAAE,GAAG;KAAG,GAAG,IAAI,MAAM,CAAC,CAAC,EAAE,SAAS,GAAG,SAAS;CAAE,CAAC;AAEtG;;;;EAIE;AACF,MAAM,MAAM,SAAS,GAAG,SAAS,EAAE,GAAG,SAAS,SAAS,EAAE,CAAC;AAE3D;;;;EAIE;AACF,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAE7D;;;;;;EAME;AACF,MAAM,MAAM,SAAS,GAAG,aAAa,GAAG,UAAU,GAAG,SAAS,CAAC"}
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,46 @@
1
+ import type { JsonObject } from "./vendor/type-fest/basic.js";
2
+ /**
3
+ * Base class for all inference-related errors.
4
+ */
5
+ export declare abstract class InferenceClientError extends Error {
6
+ constructor(message: string);
7
+ }
8
+ export declare class InferenceClientInputError extends InferenceClientError {
9
+ constructor(message: string);
10
+ }
11
+ interface HttpRequest {
12
+ url: string;
13
+ method: string;
14
+ headers?: Record<string, string>;
15
+ body?: JsonObject;
16
+ }
17
+ interface HttpResponse {
18
+ requestId: string;
19
+ status: number;
20
+ body: JsonObject | string;
21
+ }
22
+ declare abstract class InferenceClientHttpRequestError extends InferenceClientError {
23
+ httpRequest: HttpRequest;
24
+ httpResponse: HttpResponse;
25
+ constructor(message: string, httpRequest: HttpRequest, httpResponse: HttpResponse);
26
+ }
27
+ /**
28
+ * Thrown when the HTTP request to the provider fails, e.g. due to API issues or server errors.
29
+ */
30
+ export declare class InferenceClientProviderApiError extends InferenceClientHttpRequestError {
31
+ constructor(message: string, httpRequest: HttpRequest, httpResponse: HttpResponse);
32
+ }
33
+ /**
34
+ * Thrown when the HTTP request to the hub fails, e.g. due to API issues or server errors.
35
+ */
36
+ export declare class InferenceClientHubApiError extends InferenceClientHttpRequestError {
37
+ constructor(message: string, httpRequest: HttpRequest, httpResponse: HttpResponse);
38
+ }
39
+ /**
40
+ * Thrown when the inference output returned by the provider is invalid / does not match the expectations
41
+ */
42
+ export declare class InferenceClientProviderOutputError extends InferenceClientError {
43
+ constructor(message: string);
44
+ }
45
+ export {};
46
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAE9D;;GAEG;AACH,8BAAsB,oBAAqB,SAAQ,KAAK;gBAC3C,OAAO,EAAE,MAAM;CAI3B;AAED,qBAAa,yBAA0B,SAAQ,oBAAoB;gBACtD,OAAO,EAAE,MAAM;CAI3B;AAED,UAAU,WAAW;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,UAAU,CAAC;CAClB;AAED,UAAU,YAAY;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,UAAU,GAAG,MAAM,CAAC;CAC1B;AAED,uBAAe,+BAAgC,SAAQ,oBAAoB;IAC1E,WAAW,EAAE,WAAW,CAAC;IACzB,YAAY,EAAE,YAAY,CAAC;gBACf,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY;CAgBjF;AAED;;GAEG;AACH,qBAAa,+BAAgC,SAAQ,+BAA+B;gBACvE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY;CAIjF;AAED;;GAEG;AACH,qBAAa,0BAA2B,SAAQ,+BAA+B;gBAClE,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,YAAY,EAAE,YAAY;CAIjF;AAED;;GAEG;AACH,qBAAa,kCAAmC,SAAQ,oBAAoB;gBAC/D,OAAO,EAAE,MAAM;CAI3B"}
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Base class for all inference-related errors.
3
+ */
4
+ export class InferenceClientError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "InferenceClientError";
8
+ }
9
+ }
10
+ export class InferenceClientInputError extends InferenceClientError {
11
+ constructor(message) {
12
+ super(message);
13
+ this.name = "InputError";
14
+ }
15
+ }
16
+ class InferenceClientHttpRequestError extends InferenceClientError {
17
+ httpRequest;
18
+ httpResponse;
19
+ constructor(message, httpRequest, httpResponse) {
20
+ super(message);
21
+ this.httpRequest = {
22
+ ...httpRequest,
23
+ ...(httpRequest.headers
24
+ ? {
25
+ headers: {
26
+ ...httpRequest.headers,
27
+ ...("Authorization" in httpRequest.headers ? { Authorization: `Bearer [redacted]` } : undefined),
28
+ /// redact authentication in the request headers
29
+ },
30
+ }
31
+ : undefined),
32
+ };
33
+ this.httpResponse = httpResponse;
34
+ }
35
+ }
36
+ /**
37
+ * Thrown when the HTTP request to the provider fails, e.g. due to API issues or server errors.
38
+ */
39
+ export class InferenceClientProviderApiError extends InferenceClientHttpRequestError {
40
+ constructor(message, httpRequest, httpResponse) {
41
+ super(message, httpRequest, httpResponse);
42
+ this.name = "ProviderApiError";
43
+ }
44
+ }
45
+ /**
46
+ * Thrown when the HTTP request to the hub fails, e.g. due to API issues or server errors.
47
+ */
48
+ export class InferenceClientHubApiError extends InferenceClientHttpRequestError {
49
+ constructor(message, httpRequest, httpResponse) {
50
+ super(message, httpRequest, httpResponse);
51
+ this.name = "HubApiError";
52
+ }
53
+ }
54
+ /**
55
+ * Thrown when the inference output returned by the provider is invalid / does not match the expectations
56
+ */
57
+ export class InferenceClientProviderOutputError extends InferenceClientError {
58
+ constructor(message) {
59
+ super(message);
60
+ this.name = "ProviderOutputError";
61
+ }
62
+ }
@@ -1,5 +1,5 @@
1
1
  export { InferenceClient, InferenceClientEndpoint, HfInference } from "./InferenceClient.js";
2
- export { InferenceOutputError } from "./lib/InferenceOutputError.js";
2
+ export * from "./errors.js";
3
3
  export * from "./types.js";
4
4
  export * from "./tasks/index.js";
5
5
  import * as snippets from "./snippets/index.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAC7F,OAAO,EAAE,oBAAoB,EAAE,MAAM,+BAA+B,CAAC;AACrE,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAC7F,cAAc,aAAa,CAAC;AAC5B,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,CAAC"}