@huggingface/inference 3.15.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) 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/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  43. package/dist/commonjs/tasks/audio/automaticSpeechRecognition.js +2 -2
  44. package/dist/commonjs/utils/request.d.ts.map +1 -1
  45. package/dist/commonjs/utils/request.js +77 -12
  46. package/dist/commonjs/vendor/type-fest/basic.d.ts +33 -0
  47. package/dist/commonjs/vendor/type-fest/basic.d.ts.map +1 -0
  48. package/dist/commonjs/vendor/type-fest/basic.js +2 -0
  49. package/dist/esm/errors.d.ts +46 -0
  50. package/dist/esm/errors.d.ts.map +1 -0
  51. package/dist/esm/errors.js +62 -0
  52. package/dist/esm/index.d.ts +1 -1
  53. package/dist/esm/index.d.ts.map +1 -1
  54. package/dist/esm/index.js +1 -1
  55. package/dist/esm/lib/getInferenceProviderMapping.d.ts.map +1 -1
  56. package/dist/esm/lib/getInferenceProviderMapping.js +27 -16
  57. package/dist/esm/lib/getProviderHelper.d.ts.map +1 -1
  58. package/dist/esm/lib/getProviderHelper.js +4 -3
  59. package/dist/esm/lib/makeRequestOptions.d.ts.map +1 -1
  60. package/dist/esm/lib/makeRequestOptions.js +12 -11
  61. package/dist/esm/package.d.ts +1 -1
  62. package/dist/esm/package.d.ts.map +1 -1
  63. package/dist/esm/package.js +1 -1
  64. package/dist/esm/providers/black-forest-labs.d.ts.map +1 -1
  65. package/dist/esm/providers/black-forest-labs.js +4 -4
  66. package/dist/esm/providers/fal-ai.d.ts.map +1 -1
  67. package/dist/esm/providers/fal-ai.js +29 -17
  68. package/dist/esm/providers/featherless-ai.d.ts.map +1 -1
  69. package/dist/esm/providers/featherless-ai.js +2 -2
  70. package/dist/esm/providers/hf-inference.d.ts.map +1 -1
  71. package/dist/esm/providers/hf-inference.js +27 -30
  72. package/dist/esm/providers/hyperbolic.d.ts.map +1 -1
  73. package/dist/esm/providers/hyperbolic.js +3 -3
  74. package/dist/esm/providers/nebius.d.ts.map +1 -1
  75. package/dist/esm/providers/nebius.js +2 -2
  76. package/dist/esm/providers/novita.d.ts.map +1 -1
  77. package/dist/esm/providers/novita.js +12 -8
  78. package/dist/esm/providers/nscale.d.ts.map +1 -1
  79. package/dist/esm/providers/nscale.js +2 -2
  80. package/dist/esm/providers/ovhcloud.d.ts.map +1 -1
  81. package/dist/esm/providers/ovhcloud.js +2 -2
  82. package/dist/esm/providers/providerHelper.js +3 -3
  83. package/dist/esm/providers/replicate.js +4 -4
  84. package/dist/esm/providers/sambanova.d.ts +16 -0
  85. package/dist/esm/providers/sambanova.d.ts.map +1 -1
  86. package/dist/esm/providers/sambanova.js +2 -18
  87. package/dist/esm/providers/together.d.ts.map +1 -1
  88. package/dist/esm/providers/together.js +3 -3
  89. package/dist/esm/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  90. package/dist/esm/tasks/audio/automaticSpeechRecognition.js +2 -2
  91. package/dist/esm/utils/request.d.ts.map +1 -1
  92. package/dist/esm/utils/request.js +77 -12
  93. package/dist/esm/vendor/type-fest/basic.d.ts +33 -0
  94. package/dist/esm/vendor/type-fest/basic.d.ts.map +1 -0
  95. package/dist/esm/vendor/type-fest/basic.js +1 -0
  96. package/package.json +2 -2
  97. package/src/errors.ts +82 -0
  98. package/src/index.ts +1 -1
  99. package/src/lib/getInferenceProviderMapping.ts +42 -22
  100. package/src/lib/getProviderHelper.ts +8 -3
  101. package/src/lib/makeRequestOptions.ts +20 -11
  102. package/src/package.ts +1 -1
  103. package/src/providers/black-forest-labs.ts +14 -4
  104. package/src/providers/fal-ai.ts +59 -23
  105. package/src/providers/featherless-ai.ts +2 -2
  106. package/src/providers/hf-inference.ts +75 -34
  107. package/src/providers/hyperbolic.ts +3 -4
  108. package/src/providers/nebius.ts +2 -2
  109. package/src/providers/novita.ts +30 -8
  110. package/src/providers/nscale.ts +2 -2
  111. package/src/providers/ovhcloud.ts +2 -2
  112. package/src/providers/providerHelper.ts +3 -3
  113. package/src/providers/replicate.ts +4 -4
  114. package/src/providers/sambanova.ts +3 -4
  115. package/src/providers/together.ts +3 -3
  116. package/src/tasks/audio/automaticSpeechRecognition.ts +2 -2
  117. package/src/utils/request.ts +127 -14
  118. package/src/vendor/type-fest/basic.ts +31 -0
  119. package/src/vendor/type-fest/license-cc0 +121 -0
  120. package/src/vendor/type-fest/license-mit +9 -0
  121. package/dist/commonjs/lib/InferenceOutputError.d.ts +0 -4
  122. package/dist/commonjs/lib/InferenceOutputError.d.ts.map +0 -1
  123. package/dist/commonjs/lib/InferenceOutputError.js +0 -10
  124. package/dist/esm/lib/InferenceOutputError.d.ts +0 -4
  125. package/dist/esm/lib/InferenceOutputError.d.ts.map +0 -1
  126. package/dist/esm/lib/InferenceOutputError.js +0 -6
  127. 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":"AA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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;
@@ -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") {
39
+ throw new errors_js_1.InferenceClientProviderApiError(`Failed to perform inference: ${output.error ?? output.detail}`, {
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"}
package/dist/esm/index.js CHANGED
@@ -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":"getInferenceProviderMapping.d.ts","sourceRoot":"","sources":["../../../src/lib/getInferenceProviderMapping.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAIrD,OAAO,KAAK,EAAE,iBAAiB,EAAE,yBAAyB,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAGzF,eAAO,MAAM,6BAA6B,uTAA+C,CAAC;AAE1F,MAAM,MAAM,wBAAwB,GAAG,OAAO,CAC7C,MAAM,CAAC,iBAAiB,EAAE,IAAI,CAAC,6BAA6B,EAAE,WAAW,CAAC,CAAC,CAC3E,CAAC;AAEF,MAAM,WAAW,6BAA6B;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,SAAS,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,IAAI,EAAE,UAAU,CAAC;CACjB;AAED,wBAAsB,qCAAqC,CAC1D,OAAO,EAAE,OAAO,EAChB,WAAW,CAAC,EAAE,MAAM,EACpB,OAAO,CAAC,EAAE;IACT,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CACtE,GACC,OAAO,CAAC,wBAAwB,CAAC,CA6BnC;AAED,wBAAsB,2BAA2B,CAChD,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O,CAAC,EAAE;IACT,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CACtE,GACC,OAAO,CAAC,wBAAwB,CAAC,CAgDnC;AAED,wBAAsB,2BAA2B,CAChD,MAAM,EAAE;IACP,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,iBAAiB,CAAC;IAC5B,IAAI,EAAE,UAAU,CAAC;CACjB,EACD,OAAO,EAAE;IACR,KAAK,CAAC,EAAE,CAAC,KAAK,EAAE,WAAW,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CACtE,GACC,OAAO,CAAC,6BAA6B,GAAG,IAAI,CAAC,CA4B/C;AAED,wBAAsB,eAAe,CACpC,QAAQ,CAAC,EAAE,yBAAyB,EACpC,OAAO,CAAC,EAAE,MAAM,EAChB,WAAW,CAAC,EAAE,MAAM,GAClB,OAAO,CAAC,iBAAiB,CAAC,CAyB5B"}
@@ -2,6 +2,7 @@ import { HF_HUB_URL } from "../config.js";
2
2
  import { HARDCODED_MODEL_INFERENCE_MAPPING } from "../providers/consts.js";
3
3
  import { EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS } from "../providers/hf-inference.js";
4
4
  import { typedInclude } from "../utils/typedInclude.js";
5
+ import { InferenceClientHubApiError, InferenceClientInputError } from "../errors.js";
5
6
  export const inferenceProviderMappingCache = new Map();
6
7
  export async function fetchInferenceProviderMappingForModel(modelId, accessToken, options) {
7
8
  let inferenceProviderMapping;
@@ -10,22 +11,32 @@ export async function fetchInferenceProviderMappingForModel(modelId, accessToken
10
11
  inferenceProviderMapping = inferenceProviderMappingCache.get(modelId);
11
12
  }
12
13
  else {
13
- const resp = await (options?.fetch ?? fetch)(`${HF_HUB_URL}/api/models/${modelId}?expand[]=inferenceProviderMapping`, {
14
+ const url = `${HF_HUB_URL}/api/models/${modelId}?expand[]=inferenceProviderMapping`;
15
+ const resp = await (options?.fetch ?? fetch)(url, {
14
16
  headers: accessToken?.startsWith("hf_") ? { Authorization: `Bearer ${accessToken}` } : {},
15
17
  });
16
- if (resp.status === 404) {
17
- throw new Error(`Model ${modelId} does not exist`);
18
+ if (!resp.ok) {
19
+ if (resp.headers.get("Content-Type")?.startsWith("application/json")) {
20
+ const error = await resp.json();
21
+ if ("error" in error && typeof error.error === "string") {
22
+ throw new InferenceClientHubApiError(`Failed to fetch inference provider mapping for model ${modelId}: ${error.error}`, { url, method: "GET" }, { requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: error });
23
+ }
24
+ }
25
+ else {
26
+ throw new InferenceClientHubApiError(`Failed to fetch inference provider mapping for model ${modelId}`, { url, method: "GET" }, { requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: await resp.text() });
27
+ }
18
28
  }
19
- inferenceProviderMapping = await resp
20
- .json()
21
- .then((json) => json.inferenceProviderMapping)
22
- .catch(() => null);
23
- if (inferenceProviderMapping) {
24
- inferenceProviderMappingCache.set(modelId, inferenceProviderMapping);
29
+ let payload = null;
30
+ try {
31
+ payload = await resp.json();
25
32
  }
26
- }
27
- if (!inferenceProviderMapping) {
28
- throw new Error(`We have not been able to find inference provider information for model ${modelId}.`);
33
+ catch {
34
+ throw new InferenceClientHubApiError(`Failed to fetch inference provider mapping for model ${modelId}: malformed API response, invalid JSON`, { url, method: "GET" }, { requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: await resp.text() });
35
+ }
36
+ if (!payload?.inferenceProviderMapping) {
37
+ throw new InferenceClientHubApiError(`We have not been able to find inference provider information for model ${modelId}.`, { url, method: "GET" }, { requestId: resp.headers.get("x-request-id") ?? "", status: resp.status, body: await resp.text() });
38
+ }
39
+ inferenceProviderMapping = payload.inferenceProviderMapping;
29
40
  }
30
41
  return inferenceProviderMapping;
31
42
  }
@@ -40,7 +51,7 @@ export async function getInferenceProviderMapping(params, options) {
40
51
  ? EQUIVALENT_SENTENCE_TRANSFORMERS_TASKS
41
52
  : [params.task];
42
53
  if (!typedInclude(equivalentTasks, providerMapping.task)) {
43
- throw new Error(`Model ${params.modelId} is not supported for task ${params.task} and provider ${params.provider}. Supported task: ${providerMapping.task}.`);
54
+ throw new InferenceClientInputError(`Model ${params.modelId} is not supported for task ${params.task} and provider ${params.provider}. Supported task: ${providerMapping.task}.`);
44
55
  }
45
56
  if (providerMapping.status === "staging") {
46
57
  console.warn(`Model ${params.modelId} is in staging mode for provider ${params.provider}. Meant for test purposes only.`);
@@ -52,7 +63,7 @@ export async function getInferenceProviderMapping(params, options) {
52
63
  export async function resolveProvider(provider, modelId, endpointUrl) {
53
64
  if (endpointUrl) {
54
65
  if (provider) {
55
- throw new Error("Specifying both endpointUrl and provider is not supported.");
66
+ throw new InferenceClientInputError("Specifying both endpointUrl and provider is not supported.");
56
67
  }
57
68
  /// Defaulting to hf-inference helpers / API
58
69
  return "hf-inference";
@@ -63,13 +74,13 @@ export async function resolveProvider(provider, modelId, endpointUrl) {
63
74
  }
64
75
  if (provider === "auto") {
65
76
  if (!modelId) {
66
- throw new Error("Specifying a model is required when provider is 'auto'");
77
+ throw new InferenceClientInputError("Specifying a model is required when provider is 'auto'");
67
78
  }
68
79
  const inferenceProviderMapping = await fetchInferenceProviderMappingForModel(modelId);
69
80
  provider = Object.keys(inferenceProviderMapping)[0];
70
81
  }
71
82
  if (!provider) {
72
- throw new Error(`No Inference Provider available for model ${modelId}.`);
83
+ throw new InferenceClientInputError(`No Inference Provider available for model ${modelId}.`);
73
84
  }
74
85
  return provider;
75
86
  }