@huggingface/inference 3.6.1 → 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.
- package/README.md +0 -25
- package/dist/index.cjs +256 -154
- package/dist/index.js +256 -154
- package/dist/src/config.d.ts +1 -0
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
- package/dist/src/providers/black-forest-labs.d.ts.map +1 -1
- package/dist/src/providers/cerebras.d.ts.map +1 -1
- package/dist/src/providers/cohere.d.ts.map +1 -1
- package/dist/src/providers/fal-ai.d.ts +6 -16
- package/dist/src/providers/fal-ai.d.ts.map +1 -1
- package/dist/src/providers/fireworks-ai.d.ts.map +1 -1
- package/dist/src/providers/hf-inference.d.ts.map +1 -1
- package/dist/src/providers/hyperbolic.d.ts.map +1 -1
- package/dist/src/providers/nebius.d.ts.map +1 -1
- package/dist/src/providers/novita.d.ts.map +1 -1
- package/dist/src/providers/openai.d.ts.map +1 -1
- package/dist/src/providers/replicate.d.ts.map +1 -1
- package/dist/src/providers/sambanova.d.ts.map +1 -1
- package/dist/src/providers/together.d.ts.map +1 -1
- package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
- package/dist/src/tasks/custom/request.d.ts +1 -0
- package/dist/src/tasks/custom/request.d.ts.map +1 -1
- package/dist/src/tasks/custom/streamingRequest.d.ts +1 -0
- package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
- package/dist/src/tasks/cv/imageToText.d.ts.map +1 -1
- package/dist/src/tasks/cv/objectDetection.d.ts +1 -1
- package/dist/src/tasks/cv/objectDetection.d.ts.map +1 -1
- package/dist/src/tasks/cv/textToVideo.d.ts +1 -1
- package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
- package/dist/src/tasks/cv/zeroShotImageClassification.d.ts +1 -1
- package/dist/src/tasks/cv/zeroShotImageClassification.d.ts.map +1 -1
- package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts +1 -1
- package/dist/src/tasks/multimodal/documentQuestionAnswering.d.ts.map +1 -1
- package/dist/src/tasks/multimodal/visualQuestionAnswering.d.ts.map +1 -1
- package/dist/src/tasks/nlp/chatCompletion.d.ts +1 -1
- package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
- package/dist/src/tasks/nlp/chatCompletionStream.d.ts +1 -1
- package/dist/src/tasks/nlp/chatCompletionStream.d.ts.map +1 -1
- package/dist/src/tasks/nlp/questionAnswering.d.ts.map +1 -1
- package/dist/src/tasks/nlp/sentenceSimilarity.d.ts.map +1 -1
- package/dist/src/tasks/nlp/textClassification.d.ts.map +1 -1
- package/dist/src/tasks/nlp/tokenClassification.d.ts.map +1 -1
- package/dist/src/tasks/nlp/zeroShotClassification.d.ts.map +1 -1
- package/dist/src/types.d.ts +11 -2
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils/request.d.ts +27 -0
- package/dist/src/utils/request.d.ts.map +1 -0
- package/dist/test/InferenceClient.spec.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/config.ts +1 -0
- package/src/lib/makeRequestOptions.ts +8 -3
- package/src/providers/black-forest-labs.ts +6 -2
- package/src/providers/cerebras.ts +6 -2
- package/src/providers/cohere.ts +6 -2
- package/src/providers/fal-ai.ts +85 -3
- package/src/providers/fireworks-ai.ts +6 -2
- package/src/providers/hf-inference.ts +6 -2
- package/src/providers/hyperbolic.ts +6 -2
- package/src/providers/nebius.ts +6 -2
- package/src/providers/novita.ts +5 -2
- package/src/providers/openai.ts +6 -2
- package/src/providers/replicate.ts +6 -2
- package/src/providers/sambanova.ts +6 -2
- package/src/providers/together.ts +6 -2
- package/src/snippets/templates.exported.ts +1 -1
- package/src/tasks/audio/audioClassification.ts +2 -2
- package/src/tasks/audio/audioToAudio.ts +2 -2
- package/src/tasks/audio/automaticSpeechRecognition.ts +3 -3
- package/src/tasks/audio/textToSpeech.ts +2 -2
- package/src/tasks/custom/request.ts +7 -32
- package/src/tasks/custom/streamingRequest.ts +5 -85
- package/src/tasks/cv/imageClassification.ts +2 -2
- package/src/tasks/cv/imageSegmentation.ts +2 -2
- package/src/tasks/cv/imageToImage.ts +2 -2
- package/src/tasks/cv/imageToText.ts +7 -9
- package/src/tasks/cv/objectDetection.ts +4 -4
- package/src/tasks/cv/textToImage.ts +3 -3
- package/src/tasks/cv/textToVideo.ts +23 -36
- package/src/tasks/cv/zeroShotImageClassification.ts +4 -5
- package/src/tasks/multimodal/documentQuestionAnswering.ts +13 -13
- package/src/tasks/multimodal/visualQuestionAnswering.ts +4 -2
- package/src/tasks/nlp/chatCompletion.ts +3 -4
- package/src/tasks/nlp/chatCompletionStream.ts +3 -3
- package/src/tasks/nlp/featureExtraction.ts +2 -2
- package/src/tasks/nlp/fillMask.ts +2 -2
- package/src/tasks/nlp/questionAnswering.ts +3 -2
- package/src/tasks/nlp/sentenceSimilarity.ts +2 -11
- package/src/tasks/nlp/summarization.ts +2 -2
- package/src/tasks/nlp/tableQuestionAnswering.ts +2 -2
- package/src/tasks/nlp/textClassification.ts +8 -9
- package/src/tasks/nlp/textGeneration.ts +16 -16
- package/src/tasks/nlp/textGenerationStream.ts +2 -2
- package/src/tasks/nlp/tokenClassification.ts +9 -10
- package/src/tasks/nlp/translation.ts +2 -2
- package/src/tasks/nlp/zeroShotClassification.ts +9 -10
- package/src/tasks/tabular/tabularClassification.ts +2 -2
- package/src/tasks/tabular/tabularRegression.ts +2 -2
- package/src/types.ts +13 -2
- package/src/utils/request.ts +161 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InferenceOutputError } from "../../lib/InferenceOutputError";
|
|
2
2
|
import type { BaseArgs, Options } from "../../types";
|
|
3
|
-
import {
|
|
3
|
+
import { innerRequest } from "../../utils/request";
|
|
4
4
|
|
|
5
5
|
export type TabularClassificationArgs = BaseArgs & {
|
|
6
6
|
inputs: {
|
|
@@ -25,7 +25,7 @@ export async function tabularClassification(
|
|
|
25
25
|
args: TabularClassificationArgs,
|
|
26
26
|
options?: Options
|
|
27
27
|
): Promise<TabularClassificationOutput> {
|
|
28
|
-
const res = await
|
|
28
|
+
const { data: res } = await innerRequest<TabularClassificationOutput>(args, {
|
|
29
29
|
...options,
|
|
30
30
|
task: "tabular-classification",
|
|
31
31
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InferenceOutputError } from "../../lib/InferenceOutputError";
|
|
2
2
|
import type { BaseArgs, Options } from "../../types";
|
|
3
|
-
import {
|
|
3
|
+
import { innerRequest } from "../../utils/request";
|
|
4
4
|
|
|
5
5
|
export type TabularRegressionArgs = BaseArgs & {
|
|
6
6
|
inputs: {
|
|
@@ -25,7 +25,7 @@ export async function tabularRegression(
|
|
|
25
25
|
args: TabularRegressionArgs,
|
|
26
26
|
options?: Options
|
|
27
27
|
): Promise<TabularRegressionOutput> {
|
|
28
|
-
const res = await
|
|
28
|
+
const { data: res } = await innerRequest<TabularRegressionOutput>(args, {
|
|
29
29
|
...options,
|
|
30
30
|
task: "tabular-regression",
|
|
31
31
|
});
|
package/src/types.ts
CHANGED
|
@@ -24,6 +24,14 @@ export interface Options {
|
|
|
24
24
|
* (Default: "same-origin"). String | Boolean. Credentials to use for the request. If this is a string, it will be passed straight on. If it's a boolean, true will be "include" and false will not send credentials at all.
|
|
25
25
|
*/
|
|
26
26
|
includeCredentials?: string | boolean;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The billing account to use for the requests.
|
|
30
|
+
*
|
|
31
|
+
* By default the requests are billed on the user's account.
|
|
32
|
+
* Requests can only be billed to an organization the user is a member of, and which has subscribed to Enterprise Hub.
|
|
33
|
+
*/
|
|
34
|
+
billTo?: string;
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
export type InferenceTask = Exclude<PipelineType, "other">;
|
|
@@ -94,19 +102,22 @@ export type RequestArgs = BaseArgs &
|
|
|
94
102
|
};
|
|
95
103
|
|
|
96
104
|
export interface ProviderConfig {
|
|
97
|
-
|
|
105
|
+
makeBaseUrl: ((task?: InferenceTask) => string) | (() => string);
|
|
98
106
|
makeBody: (params: BodyParams) => Record<string, unknown>;
|
|
99
107
|
makeHeaders: (params: HeaderParams) => Record<string, string>;
|
|
100
108
|
makeUrl: (params: UrlParams) => string;
|
|
101
109
|
clientSideRoutingOnly?: boolean;
|
|
102
110
|
}
|
|
103
111
|
|
|
112
|
+
export type AuthMethod = "none" | "hf-token" | "credentials-include" | "provider-key";
|
|
113
|
+
|
|
104
114
|
export interface HeaderParams {
|
|
105
115
|
accessToken?: string;
|
|
106
|
-
authMethod:
|
|
116
|
+
authMethod: AuthMethod;
|
|
107
117
|
}
|
|
108
118
|
|
|
109
119
|
export interface UrlParams {
|
|
120
|
+
authMethod: AuthMethod;
|
|
110
121
|
baseUrl: string;
|
|
111
122
|
model: string;
|
|
112
123
|
task?: InferenceTask;
|
|
@@ -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
|
+
}
|