@huggingface/inference 3.6.2 → 3.7.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 (72) hide show
  1. package/README.md +0 -25
  2. package/dist/index.cjs +135 -114
  3. package/dist/index.js +135 -114
  4. package/dist/src/config.d.ts +1 -0
  5. package/dist/src/config.d.ts.map +1 -1
  6. package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
  7. package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
  8. package/dist/src/tasks/custom/request.d.ts +1 -0
  9. package/dist/src/tasks/custom/request.d.ts.map +1 -1
  10. package/dist/src/tasks/custom/streamingRequest.d.ts +1 -0
  11. package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
  12. package/dist/src/tasks/cv/imageToText.d.ts.map +1 -1
  13. package/dist/src/tasks/cv/objectDetection.d.ts +1 -1
  14. package/dist/src/tasks/cv/objectDetection.d.ts.map +1 -1
  15. package/dist/src/tasks/cv/textToVideo.d.ts +1 -1
  16. package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
  17. package/dist/src/tasks/cv/zeroShotImageClassification.d.ts +1 -1
  18. package/dist/src/tasks/cv/zeroShotImageClassification.d.ts.map +1 -1
  19. package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts +1 -1
  20. package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts.map +1 -1
  21. package/dist/src/tasks/multimodal/visualQuestionAnswering.d.ts.map +1 -1
  22. package/dist/src/tasks/nlp/chatCompletion.d.ts +1 -1
  23. package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
  24. package/dist/src/tasks/nlp/chatCompletionStream.d.ts +1 -1
  25. package/dist/src/tasks/nlp/chatCompletionStream.d.ts.map +1 -1
  26. package/dist/src/tasks/nlp/questionAnswering.d.ts.map +1 -1
  27. package/dist/src/tasks/nlp/sentenceSimilarity.d.ts.map +1 -1
  28. package/dist/src/tasks/nlp/textClassification.d.ts.map +1 -1
  29. package/dist/src/tasks/nlp/tokenClassification.d.ts.map +1 -1
  30. package/dist/src/tasks/nlp/zeroShotClassification.d.ts.map +1 -1
  31. package/dist/src/types.d.ts +7 -0
  32. package/dist/src/types.d.ts.map +1 -1
  33. package/dist/src/utils/request.d.ts +27 -0
  34. package/dist/src/utils/request.d.ts.map +1 -0
  35. package/package.json +2 -2
  36. package/src/config.ts +1 -0
  37. package/src/lib/makeRequestOptions.ts +5 -2
  38. package/src/snippets/templates.exported.ts +1 -1
  39. package/src/tasks/audio/audioClassification.ts +2 -2
  40. package/src/tasks/audio/audioToAudio.ts +2 -2
  41. package/src/tasks/audio/automaticSpeechRecognition.ts +3 -3
  42. package/src/tasks/audio/textToSpeech.ts +2 -2
  43. package/src/tasks/custom/request.ts +7 -32
  44. package/src/tasks/custom/streamingRequest.ts +5 -85
  45. package/src/tasks/cv/imageClassification.ts +2 -2
  46. package/src/tasks/cv/imageSegmentation.ts +2 -2
  47. package/src/tasks/cv/imageToImage.ts +2 -2
  48. package/src/tasks/cv/imageToText.ts +7 -9
  49. package/src/tasks/cv/objectDetection.ts +4 -4
  50. package/src/tasks/cv/textToImage.ts +3 -3
  51. package/src/tasks/cv/textToVideo.ts +23 -20
  52. package/src/tasks/cv/zeroShotImageClassification.ts +4 -5
  53. package/src/tasks/multimodal/documentQuestionAnswering.ts +13 -13
  54. package/src/tasks/multimodal/visualQuestionAnswering.ts +4 -2
  55. package/src/tasks/nlp/chatCompletion.ts +3 -4
  56. package/src/tasks/nlp/chatCompletionStream.ts +3 -3
  57. package/src/tasks/nlp/featureExtraction.ts +2 -2
  58. package/src/tasks/nlp/fillMask.ts +2 -2
  59. package/src/tasks/nlp/questionAnswering.ts +3 -2
  60. package/src/tasks/nlp/sentenceSimilarity.ts +2 -11
  61. package/src/tasks/nlp/summarization.ts +2 -2
  62. package/src/tasks/nlp/tableQuestionAnswering.ts +2 -2
  63. package/src/tasks/nlp/textClassification.ts +8 -9
  64. package/src/tasks/nlp/textGeneration.ts +16 -16
  65. package/src/tasks/nlp/textGenerationStream.ts +2 -2
  66. package/src/tasks/nlp/tokenClassification.ts +9 -10
  67. package/src/tasks/nlp/translation.ts +2 -2
  68. package/src/tasks/nlp/zeroShotClassification.ts +9 -10
  69. package/src/tasks/tabular/tabularClassification.ts +2 -2
  70. package/src/tasks/tabular/tabularRegression.ts +2 -2
  71. package/src/types.ts +8 -0
  72. package/src/utils/request.ts +161 -0
@@ -0,0 +1,161 @@
1
+ import { makeRequestOptions } from "../lib/makeRequestOptions";
2
+ import type { InferenceTask, Options, RequestArgs } from "../types";
3
+ import type { EventSourceMessage } from "../vendor/fetch-event-source/parse";
4
+ import { getLines, getMessages } from "../vendor/fetch-event-source/parse";
5
+
6
+ export interface ResponseWrapper<T> {
7
+ data: T;
8
+ requestContext: {
9
+ url: string;
10
+ info: RequestInit;
11
+ };
12
+ }
13
+
14
+ /**
15
+ * Primitive to make custom calls to the inference provider
16
+ */
17
+ export async function innerRequest<T>(
18
+ args: RequestArgs,
19
+ options?: Options & {
20
+ /** In most cases (unless we pass a endpointUrl) we know the task */
21
+ task?: InferenceTask;
22
+ /** Is chat completion compatible */
23
+ chatCompletion?: boolean;
24
+ }
25
+ ): Promise<ResponseWrapper<T>> {
26
+ const { url, info } = await makeRequestOptions(args, options);
27
+ const response = await (options?.fetch ?? fetch)(url, info);
28
+
29
+ const requestContext: ResponseWrapper<T>["requestContext"] = { url, info };
30
+
31
+ if (options?.retry_on_error !== false && response.status === 503) {
32
+ return innerRequest(args, options);
33
+ }
34
+
35
+ if (!response.ok) {
36
+ const contentType = response.headers.get("Content-Type");
37
+ if (["application/json", "application/problem+json"].some((ct) => contentType?.startsWith(ct))) {
38
+ const output = await response.json();
39
+ if ([400, 422, 404, 500].includes(response.status) && options?.chatCompletion) {
40
+ throw new Error(
41
+ `Server ${args.model} does not seem to support chat completion. Error: ${JSON.stringify(output.error)}`
42
+ );
43
+ }
44
+ if (output.error || output.detail) {
45
+ throw new Error(JSON.stringify(output.error ?? output.detail));
46
+ } else {
47
+ throw new Error(output);
48
+ }
49
+ }
50
+ const message = contentType?.startsWith("text/plain;") ? await response.text() : undefined;
51
+ throw new Error(message ?? "An error occurred while fetching the blob");
52
+ }
53
+
54
+ if (response.headers.get("Content-Type")?.startsWith("application/json")) {
55
+ const data = (await response.json()) as T;
56
+ return { data, requestContext };
57
+ }
58
+
59
+ const blob = (await response.blob()) as T;
60
+ return { data: blob as unknown as T, requestContext };
61
+ }
62
+
63
+ /**
64
+ * Primitive to make custom inference calls that expect server-sent events, and returns the response through a generator
65
+ */
66
+ export async function* innerStreamingRequest<T>(
67
+ args: RequestArgs,
68
+ options?: Options & {
69
+ /** In most cases (unless we pass a endpointUrl) we know the task */
70
+ task?: InferenceTask;
71
+ /** Is chat completion compatible */
72
+ chatCompletion?: boolean;
73
+ }
74
+ ): AsyncGenerator<T> {
75
+ const { url, info } = await makeRequestOptions({ ...args, stream: true }, options);
76
+ const response = await (options?.fetch ?? fetch)(url, info);
77
+
78
+ if (options?.retry_on_error !== false && response.status === 503) {
79
+ return yield* innerStreamingRequest(args, options);
80
+ }
81
+ if (!response.ok) {
82
+ if (response.headers.get("Content-Type")?.startsWith("application/json")) {
83
+ const output = await response.json();
84
+ if ([400, 422, 404, 500].includes(response.status) && options?.chatCompletion) {
85
+ throw new Error(`Server ${args.model} does not seem to support chat completion. Error: ${output.error}`);
86
+ }
87
+ if (typeof output.error === "string") {
88
+ throw new Error(output.error);
89
+ }
90
+ if (output.error && "message" in output.error && typeof output.error.message === "string") {
91
+ /// OpenAI errors
92
+ throw new Error(output.error.message);
93
+ }
94
+ // Sambanova errors
95
+ if (typeof output.message === "string") {
96
+ throw new Error(output.message);
97
+ }
98
+ }
99
+
100
+ throw new Error(`Server response contains error: ${response.status}`);
101
+ }
102
+ if (!response.headers.get("content-type")?.startsWith("text/event-stream")) {
103
+ throw new Error(
104
+ `Server does not support event stream content type, it returned ` + response.headers.get("content-type")
105
+ );
106
+ }
107
+
108
+ if (!response.body) {
109
+ return;
110
+ }
111
+
112
+ const reader = response.body.getReader();
113
+ let events: EventSourceMessage[] = [];
114
+
115
+ const onEvent = (event: EventSourceMessage) => {
116
+ // accumulate events in array
117
+ events.push(event);
118
+ };
119
+
120
+ const onChunk = getLines(
121
+ getMessages(
122
+ () => {},
123
+ () => {},
124
+ onEvent
125
+ )
126
+ );
127
+
128
+ try {
129
+ while (true) {
130
+ const { done, value } = await reader.read();
131
+ if (done) {
132
+ return;
133
+ }
134
+ onChunk(value);
135
+ for (const event of events) {
136
+ if (event.data.length > 0) {
137
+ if (event.data === "[DONE]") {
138
+ return;
139
+ }
140
+ const data = JSON.parse(event.data);
141
+ if (typeof data === "object" && data !== null && "error" in data) {
142
+ const errorStr =
143
+ typeof data.error === "string"
144
+ ? data.error
145
+ : typeof data.error === "object" &&
146
+ data.error &&
147
+ "message" in data.error &&
148
+ typeof data.error.message === "string"
149
+ ? data.error.message
150
+ : JSON.stringify(data.error);
151
+ throw new Error(`Error forwarded from backend: ` + errorStr);
152
+ }
153
+ yield data as T;
154
+ }
155
+ }
156
+ events = [];
157
+ }
158
+ } finally {
159
+ reader.releaseLock();
160
+ }
161
+ }