@huggingface/inference 3.5.1 → 3.6.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 +11 -11
- package/dist/browser/index.cjs +1652 -0
- package/dist/browser/index.js +1652 -0
- package/dist/index.cjs +295 -930
- package/dist/index.js +284 -940
- package/dist/src/{HfInference.d.ts → InferenceClient.d.ts} +12 -7
- package/dist/src/InferenceClient.d.ts.map +1 -0
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/lib/makeRequestOptions.d.ts +16 -1
- package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
- package/dist/src/providers/cohere.d.ts.map +1 -1
- package/dist/src/providers/novita.d.ts.map +1 -1
- package/dist/src/snippets/getInferenceSnippets.d.ts +4 -0
- package/dist/src/snippets/getInferenceSnippets.d.ts.map +1 -0
- package/dist/src/snippets/index.d.ts +1 -4
- package/dist/src/snippets/index.d.ts.map +1 -1
- package/dist/src/tasks/cv/textToVideo.d.ts.map +1 -1
- package/dist/test/InferenceClient.spec.d.ts +2 -0
- package/dist/test/InferenceClient.spec.d.ts.map +1 -0
- package/package.json +17 -8
- package/src/{HfInference.ts → InferenceClient.ts} +12 -7
- package/src/index.ts +2 -2
- package/src/lib/makeRequestOptions.ts +37 -10
- package/src/providers/black-forest-labs.ts +2 -2
- package/src/providers/cohere.ts +0 -1
- package/src/providers/fireworks-ai.ts +4 -4
- package/src/providers/hf-inference.ts +1 -1
- package/src/providers/nebius.ts +3 -3
- package/src/providers/novita.ts +7 -6
- package/src/providers/sambanova.ts +1 -1
- package/src/providers/together.ts +3 -3
- package/src/snippets/getInferenceSnippets.ts +398 -0
- package/src/snippets/index.ts +1 -5
- package/src/snippets/templates/js/fetch/basic.jinja +19 -0
- package/src/snippets/templates/js/fetch/basicAudio.jinja +19 -0
- package/src/snippets/templates/js/fetch/basicImage.jinja +19 -0
- package/src/snippets/templates/js/fetch/textToAudio.jinja +41 -0
- package/src/snippets/templates/js/fetch/textToImage.jinja +19 -0
- package/src/snippets/templates/js/fetch/zeroShotClassification.jinja +22 -0
- package/src/snippets/templates/js/huggingface.js/basic.jinja +11 -0
- package/src/snippets/templates/js/huggingface.js/basicAudio.jinja +13 -0
- package/src/snippets/templates/js/huggingface.js/basicImage.jinja +13 -0
- package/src/snippets/templates/js/huggingface.js/conversational.jinja +11 -0
- package/src/snippets/templates/js/huggingface.js/conversationalStream.jinja +19 -0
- package/src/snippets/templates/js/huggingface.js/textToImage.jinja +11 -0
- package/src/snippets/templates/js/huggingface.js/textToVideo.jinja +10 -0
- package/src/snippets/templates/js/openai/conversational.jinja +13 -0
- package/src/snippets/templates/js/openai/conversationalStream.jinja +22 -0
- package/src/snippets/templates/python/fal_client/textToImage.jinja +11 -0
- package/src/snippets/templates/python/huggingface_hub/basic.jinja +4 -0
- package/src/snippets/templates/python/huggingface_hub/basicAudio.jinja +1 -0
- package/src/snippets/templates/python/huggingface_hub/basicImage.jinja +1 -0
- package/src/snippets/templates/python/huggingface_hub/conversational.jinja +6 -0
- package/src/snippets/templates/python/huggingface_hub/conversationalStream.jinja +8 -0
- package/src/snippets/templates/python/huggingface_hub/documentQuestionAnswering.jinja +5 -0
- package/src/snippets/templates/python/huggingface_hub/imageToImage.jinja +6 -0
- package/src/snippets/templates/python/huggingface_hub/importInferenceClient.jinja +6 -0
- package/src/snippets/templates/python/huggingface_hub/textToImage.jinja +5 -0
- package/src/snippets/templates/python/huggingface_hub/textToVideo.jinja +4 -0
- package/src/snippets/templates/python/openai/conversational.jinja +13 -0
- package/src/snippets/templates/python/openai/conversationalStream.jinja +15 -0
- package/src/snippets/templates/python/requests/basic.jinja +7 -0
- package/src/snippets/templates/python/requests/basicAudio.jinja +7 -0
- package/src/snippets/templates/python/requests/basicImage.jinja +7 -0
- package/src/snippets/templates/python/requests/conversational.jinja +9 -0
- package/src/snippets/templates/python/requests/conversationalStream.jinja +16 -0
- package/src/snippets/templates/python/requests/documentQuestionAnswering.jinja +13 -0
- package/src/snippets/templates/python/requests/imageToImage.jinja +15 -0
- package/src/snippets/templates/python/requests/importRequests.jinja +10 -0
- package/src/snippets/templates/python/requests/tabular.jinja +9 -0
- package/src/snippets/templates/python/requests/textToAudio.jinja +23 -0
- package/src/snippets/templates/python/requests/textToImage.jinja +14 -0
- package/src/snippets/templates/python/requests/zeroShotClassification.jinja +8 -0
- package/src/snippets/templates/python/requests/zeroShotImageClassification.jinja +14 -0
- package/src/snippets/templates/sh/curl/basic.jinja +7 -0
- package/src/snippets/templates/sh/curl/basicAudio.jinja +5 -0
- package/src/snippets/templates/sh/curl/basicImage.jinja +5 -0
- package/src/snippets/templates/sh/curl/conversational.jinja +7 -0
- package/src/snippets/templates/sh/curl/conversationalStream.jinja +7 -0
- package/src/snippets/templates/sh/curl/zeroShotClassification.jinja +5 -0
- package/src/tasks/cv/textToVideo.ts +25 -5
- package/src/vendor/fetch-event-source/LICENSE +21 -0
- package/dist/src/HfInference.d.ts.map +0 -1
- package/dist/src/snippets/curl.d.ts +0 -17
- package/dist/src/snippets/curl.d.ts.map +0 -1
- package/dist/src/snippets/js.d.ts +0 -21
- package/dist/src/snippets/js.d.ts.map +0 -1
- package/dist/src/snippets/python.d.ts +0 -23
- package/dist/src/snippets/python.d.ts.map +0 -1
- package/dist/test/HfInference.spec.d.ts +0 -2
- package/dist/test/HfInference.spec.d.ts.map +0 -1
- package/src/snippets/curl.ts +0 -177
- package/src/snippets/js.ts +0 -475
- package/src/snippets/python.ts +0 -487
|
@@ -8,21 +8,26 @@ type TaskWithNoAccessToken = {
|
|
|
8
8
|
type TaskWithNoAccessTokenNoEndpointUrl = {
|
|
9
9
|
[key in keyof Task]: (args: DistributiveOmit<Parameters<Task[key]>[0], "accessToken" | "endpointUrl">, options?: Parameters<Task[key]>[1]) => ReturnType<Task[key]>;
|
|
10
10
|
};
|
|
11
|
-
export declare class
|
|
11
|
+
export declare class InferenceClient {
|
|
12
12
|
private readonly accessToken;
|
|
13
13
|
private readonly defaultOptions;
|
|
14
14
|
constructor(accessToken?: string, defaultOptions?: Options);
|
|
15
15
|
/**
|
|
16
|
-
* Returns copy of
|
|
16
|
+
* Returns copy of InferenceClient tied to a specified endpoint.
|
|
17
17
|
*/
|
|
18
|
-
endpoint(endpointUrl: string):
|
|
18
|
+
endpoint(endpointUrl: string): InferenceClientEndpoint;
|
|
19
19
|
}
|
|
20
|
-
export declare class
|
|
20
|
+
export declare class InferenceClientEndpoint {
|
|
21
21
|
constructor(endpointUrl: string, accessToken?: string, defaultOptions?: Options);
|
|
22
22
|
}
|
|
23
|
-
export interface
|
|
23
|
+
export interface InferenceClient extends TaskWithNoAccessToken {
|
|
24
24
|
}
|
|
25
|
-
export interface
|
|
25
|
+
export interface InferenceClientEndpoint extends TaskWithNoAccessTokenNoEndpointUrl {
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* For backward compatibility only.
|
|
29
|
+
*/
|
|
30
|
+
export declare class HfInference extends InferenceClient {
|
|
26
31
|
}
|
|
27
32
|
export {};
|
|
28
|
-
//# sourceMappingURL=
|
|
33
|
+
//# sourceMappingURL=InferenceClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InferenceClient.d.ts","sourceRoot":"","sources":["../../src/InferenceClient.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC;AACjC,OAAO,KAAK,EAAE,OAAO,EAAe,MAAM,SAAS,CAAC;AACpD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAKlE,KAAK,IAAI,GAAG,OAAO,KAAK,CAAC;AAEzB,KAAK,qBAAqB,GAAG;KAC3B,GAAG,IAAI,MAAM,IAAI,GAAG,CACpB,IAAI,EAAE,gBAAgB,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,aAAa,CAAC,EAC/D,OAAO,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAC9B,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;CAC1B,CAAC;AAEF,KAAK,kCAAkC,GAAG;KACxC,GAAG,IAAI,MAAM,IAAI,GAAG,CACpB,IAAI,EAAE,gBAAgB,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,aAAa,GAAG,aAAa,CAAC,EAC/E,OAAO,CAAC,EAAE,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAC9B,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;CAC1B,CAAC;AAEF,qBAAa,eAAe;IAC3B,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAS;IACrC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAU;gBAE7B,WAAW,SAAK,EAAE,cAAc,GAAE,OAAY;IAc1D;;OAEG;IACI,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,uBAAuB;CAG7D;AAED,qBAAa,uBAAuB;gBACvB,WAAW,EAAE,MAAM,EAAE,WAAW,SAAK,EAAE,cAAc,GAAE,OAAY;CAa/E;AAED,MAAM,WAAW,eAAgB,SAAQ,qBAAqB;CAAG;AAEjE,MAAM,WAAW,uBAAwB,SAAQ,kCAAkC;CAAG;AAEtF;;GAEG;AACH,qBAAa,WAAY,SAAQ,eAAe;CAAG"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { InferenceClient, InferenceClientEndpoint, HfInference } from "./InferenceClient";
|
|
2
2
|
export { InferenceOutputError } from "./lib/InferenceOutputError";
|
|
3
3
|
export * from "./types";
|
|
4
4
|
export * from "./tasks";
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC1F,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,OAAO,KAAK,QAAQ,MAAM,qBAAqB,CAAC;AAEhD,OAAO,EAAE,QAAQ,EAAE,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { InferenceTask, Options, RequestArgs } from "../types";
|
|
2
2
|
/**
|
|
3
|
-
* Helper that prepares request arguments
|
|
3
|
+
* Helper that prepares request arguments.
|
|
4
|
+
* This async version handle the model ID resolution step.
|
|
4
5
|
*/
|
|
5
6
|
export declare function makeRequestOptions(args: RequestArgs & {
|
|
6
7
|
data?: Blob | ArrayBuffer;
|
|
@@ -13,4 +14,18 @@ export declare function makeRequestOptions(args: RequestArgs & {
|
|
|
13
14
|
url: string;
|
|
14
15
|
info: RequestInit;
|
|
15
16
|
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Helper that prepares request arguments. - for internal use only
|
|
19
|
+
* This sync version skips the model ID resolution step
|
|
20
|
+
*/
|
|
21
|
+
export declare function makeRequestOptionsFromResolvedModel(resolvedModel: string, args: RequestArgs & {
|
|
22
|
+
data?: Blob | ArrayBuffer;
|
|
23
|
+
stream?: boolean;
|
|
24
|
+
}, options?: Options & {
|
|
25
|
+
task?: InferenceTask;
|
|
26
|
+
chatCompletion?: boolean;
|
|
27
|
+
}): {
|
|
28
|
+
url: string;
|
|
29
|
+
info: RequestInit;
|
|
30
|
+
};
|
|
16
31
|
//# sourceMappingURL=makeRequestOptions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"makeRequestOptions.d.ts","sourceRoot":"","sources":["../../../src/lib/makeRequestOptions.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAqB,aAAa,EAAE,OAAO,EAAkB,WAAW,EAAE,MAAM,UAAU,CAAC;AAgCvG
|
|
1
|
+
{"version":3,"file":"makeRequestOptions.d.ts","sourceRoot":"","sources":["../../../src/lib/makeRequestOptions.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAqB,aAAa,EAAE,OAAO,EAAkB,WAAW,EAAE,MAAM,UAAU,CAAC;AAgCvG;;;GAGG;AACH,wBAAsB,kBAAkB,CACvC,IAAI,EAAE,WAAW,GAAG;IACnB,IAAI,CAAC,EAAE,IAAI,GAAG,WAAW,CAAC;IAC1B,MAAM,CAAC,EAAE,OAAO,CAAC;CACjB,EACD,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,oEAAoE;IACpE,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,WAAW,CAAA;CAAE,CAAC,CAoC7C;AAED;;;GAGG;AACH,wBAAgB,mCAAmC,CAClD,aAAa,EAAE,MAAM,EACrB,IAAI,EAAE,WAAW,GAAG;IACnB,IAAI,CAAC,EAAE,IAAI,GAAG,WAAW,CAAC;IAC1B,MAAM,CAAC,EAAE,OAAO,CAAC;CACjB,EACD,OAAO,CAAC,EAAE,OAAO,GAAG;IACnB,IAAI,CAAC,EAAE,aAAa,CAAC;IACrB,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB,GACC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,WAAW,CAAA;CAAE,CA4FpC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cohere.d.ts","sourceRoot":"","sources":["../../../src/providers/cohere.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,cAAc,EAAuC,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"cohere.d.ts","sourceRoot":"","sources":["../../../src/providers/cohere.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,cAAc,EAAuC,MAAM,UAAU,CAAC;AAmBpF,eAAO,MAAM,aAAa,EAAE,cAK3B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"novita.d.ts","sourceRoot":"","sources":["../../../src/providers/novita.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,cAAc,EAAuC,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"novita.d.ts","sourceRoot":"","sources":["../../../src/providers/novita.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,KAAK,EAAE,cAAc,EAAuC,MAAM,UAAU,CAAC;AA0BpF,eAAO,MAAM,aAAa,EAAE,cAK3B,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type InferenceSnippet, type ModelDataMinimal } from "@huggingface/tasks";
|
|
2
|
+
import type { InferenceProvider } from "../types";
|
|
3
|
+
export declare function getInferenceSnippets(model: ModelDataMinimal, accessToken: string, provider: InferenceProvider, providerModelId?: string, opts?: Record<string, unknown>): InferenceSnippet[];
|
|
4
|
+
//# sourceMappingURL=getInferenceSnippets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getInferenceSnippets.d.ts","sourceRoot":"","sources":["../../../src/snippets/getInferenceSnippets.ts"],"names":[],"mappings":"AAEA,OAAO,EACN,KAAK,gBAAgB,EAErB,KAAK,gBAAgB,EAGrB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,KAAK,EAAE,iBAAiB,EAA8B,MAAM,UAAU,CAAC;AAkT9E,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,gBAAgB,EACvB,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,iBAAiB,EAC3B,eAAe,CAAC,EAAE,MAAM,EACxB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,gBAAgB,EAAE,CAIpB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/snippets/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/snippets/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"textToVideo.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToVideo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAqB,OAAO,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAO3D,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,gBAAgB,CAAC;AAE1D,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"textToVideo.d.ts","sourceRoot":"","sources":["../../../../src/tasks/cv/textToVideo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAqB,OAAO,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AAO3D,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,gBAAgB,CAAC;AAE1D,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAoBrC,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAwDtG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InferenceClient.spec.d.ts","sourceRoot":"","sources":["../../test/InferenceClient.spec.ts"],"names":[],"mappings":"AAeA,OAAO,OAAO,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@huggingface/inference",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"packageManager": "pnpm@8.10.5",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"author": "Tim Mikeladze <tim.mikeladze@gmail.com>",
|
|
7
|
-
"description": "Typescript
|
|
6
|
+
"author": "Hugging Face and Tim Mikeladze <tim.mikeladze@gmail.com>",
|
|
7
|
+
"description": "Typescript client for the Hugging Face Inference Providers and Inference Endpoints",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/huggingface/huggingface.js.git"
|
|
@@ -33,13 +33,21 @@
|
|
|
33
33
|
"main": "./dist/index.cjs",
|
|
34
34
|
"module": "./dist/index.js",
|
|
35
35
|
"exports": {
|
|
36
|
-
"
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
".": {
|
|
37
|
+
"types": "./dist/src/index.d.ts",
|
|
38
|
+
"require": "./dist/index.cjs",
|
|
39
|
+
"import": "./dist/index.js"
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"browser": {
|
|
43
|
+
"./src/snippets/index.js": false,
|
|
44
|
+
"./dist/index.js": "./dist/browser/index.js",
|
|
45
|
+
"./dist/index.mjs": "./dist/browser/index.mjs"
|
|
39
46
|
},
|
|
40
47
|
"type": "module",
|
|
41
48
|
"dependencies": {
|
|
42
|
-
"@huggingface/tasks": "^0.17.
|
|
49
|
+
"@huggingface/tasks": "^0.17.8",
|
|
50
|
+
"@huggingface/jinja": "^0.3.3"
|
|
43
51
|
},
|
|
44
52
|
"devDependencies": {
|
|
45
53
|
"@types/node": "18.13.0"
|
|
@@ -54,6 +62,7 @@
|
|
|
54
62
|
"format:check": "prettier --check .",
|
|
55
63
|
"test": "vitest run --config vitest.config.mts",
|
|
56
64
|
"test:browser": "vitest run --browser.name=chrome --browser.headless --config vitest.config.mts",
|
|
57
|
-
"check": "tsc"
|
|
65
|
+
"check": "tsc",
|
|
66
|
+
"dev": "tsup src/index.ts --format cjs,esm --watch"
|
|
58
67
|
}
|
|
59
68
|
}
|
|
@@ -21,7 +21,7 @@ type TaskWithNoAccessTokenNoEndpointUrl = {
|
|
|
21
21
|
) => ReturnType<Task[key]>;
|
|
22
22
|
};
|
|
23
23
|
|
|
24
|
-
export class
|
|
24
|
+
export class InferenceClient {
|
|
25
25
|
private readonly accessToken: string;
|
|
26
26
|
private readonly defaultOptions: Options;
|
|
27
27
|
|
|
@@ -40,14 +40,14 @@ export class HfInference {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
* Returns copy of
|
|
43
|
+
* Returns copy of InferenceClient tied to a specified endpoint.
|
|
44
44
|
*/
|
|
45
|
-
public endpoint(endpointUrl: string):
|
|
46
|
-
return new
|
|
45
|
+
public endpoint(endpointUrl: string): InferenceClientEndpoint {
|
|
46
|
+
return new InferenceClientEndpoint(endpointUrl, this.accessToken, this.defaultOptions);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
-
export class
|
|
50
|
+
export class InferenceClientEndpoint {
|
|
51
51
|
constructor(endpointUrl: string, accessToken = "", defaultOptions: Options = {}) {
|
|
52
52
|
accessToken;
|
|
53
53
|
defaultOptions;
|
|
@@ -63,6 +63,11 @@ export class HfInferenceEndpoint {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
export interface
|
|
66
|
+
export interface InferenceClient extends TaskWithNoAccessToken {}
|
|
67
67
|
|
|
68
|
-
export interface
|
|
68
|
+
export interface InferenceClientEndpoint extends TaskWithNoAccessTokenNoEndpointUrl {}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* For backward compatibility only.
|
|
72
|
+
*/
|
|
73
|
+
export class HfInference extends InferenceClient {}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { InferenceClient, InferenceClientEndpoint, HfInference } from "./InferenceClient";
|
|
2
2
|
export { InferenceOutputError } from "./lib/InferenceOutputError";
|
|
3
3
|
export * from "./types";
|
|
4
4
|
export * from "./tasks";
|
|
5
|
-
|
|
6
5
|
import * as snippets from "./snippets/index.js";
|
|
6
|
+
|
|
7
7
|
export { snippets };
|
|
@@ -45,7 +45,8 @@ const providerConfigs: Record<InferenceProvider, ProviderConfig> = {
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
|
-
* Helper that prepares request arguments
|
|
48
|
+
* Helper that prepares request arguments.
|
|
49
|
+
* This async version handle the model ID resolution step.
|
|
49
50
|
*/
|
|
50
51
|
export async function makeRequestOptions(
|
|
51
52
|
args: RequestArgs & {
|
|
@@ -58,13 +59,13 @@ export async function makeRequestOptions(
|
|
|
58
59
|
chatCompletion?: boolean;
|
|
59
60
|
}
|
|
60
61
|
): Promise<{ url: string; info: RequestInit }> {
|
|
61
|
-
const {
|
|
62
|
+
const { provider: maybeProvider, model: maybeModel } = args;
|
|
62
63
|
const provider = maybeProvider ?? "hf-inference";
|
|
63
64
|
const providerConfig = providerConfigs[provider];
|
|
65
|
+
const { task, chatCompletion } = options ?? {};
|
|
64
66
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (endpointUrl && provider !== "hf-inference") {
|
|
67
|
+
// Validate inputs
|
|
68
|
+
if (args.endpointUrl && provider !== "hf-inference") {
|
|
68
69
|
throw new Error(`Cannot use endpointUrl with a third-party provider.`);
|
|
69
70
|
}
|
|
70
71
|
if (maybeModel && isUrl(maybeModel)) {
|
|
@@ -79,18 +80,44 @@ export async function makeRequestOptions(
|
|
|
79
80
|
if (providerConfig.clientSideRoutingOnly && !maybeModel) {
|
|
80
81
|
throw new Error(`Provider ${provider} requires a model ID to be passed directly.`);
|
|
81
82
|
}
|
|
83
|
+
|
|
82
84
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
83
85
|
const hfModel = maybeModel ?? (await loadDefaultModel(task!));
|
|
84
|
-
const
|
|
86
|
+
const resolvedModel = providerConfig.clientSideRoutingOnly
|
|
85
87
|
? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
86
88
|
removeProviderPrefix(maybeModel!, provider)
|
|
87
|
-
:
|
|
88
|
-
await getProviderModelId({ model: hfModel, provider }, args, {
|
|
89
|
+
: await getProviderModelId({ model: hfModel, provider }, args, {
|
|
89
90
|
task,
|
|
90
91
|
chatCompletion,
|
|
91
92
|
fetch: options?.fetch,
|
|
92
93
|
});
|
|
93
94
|
|
|
95
|
+
// Use the sync version with the resolved model
|
|
96
|
+
return makeRequestOptionsFromResolvedModel(resolvedModel, args, options);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Helper that prepares request arguments. - for internal use only
|
|
101
|
+
* This sync version skips the model ID resolution step
|
|
102
|
+
*/
|
|
103
|
+
export function makeRequestOptionsFromResolvedModel(
|
|
104
|
+
resolvedModel: string,
|
|
105
|
+
args: RequestArgs & {
|
|
106
|
+
data?: Blob | ArrayBuffer;
|
|
107
|
+
stream?: boolean;
|
|
108
|
+
},
|
|
109
|
+
options?: Options & {
|
|
110
|
+
task?: InferenceTask;
|
|
111
|
+
chatCompletion?: boolean;
|
|
112
|
+
}
|
|
113
|
+
): { url: string; info: RequestInit } {
|
|
114
|
+
const { accessToken, endpointUrl, provider: maybeProvider, model, ...remainingArgs } = args;
|
|
115
|
+
|
|
116
|
+
const provider = maybeProvider ?? "hf-inference";
|
|
117
|
+
const providerConfig = providerConfigs[provider];
|
|
118
|
+
|
|
119
|
+
const { includeCredentials, task, chatCompletion, signal } = options ?? {};
|
|
120
|
+
|
|
94
121
|
const authMethod = (() => {
|
|
95
122
|
if (providerConfig.clientSideRoutingOnly) {
|
|
96
123
|
// Closed-source providers require an accessToken (cannot be routed).
|
|
@@ -119,7 +146,7 @@ export async function makeRequestOptions(
|
|
|
119
146
|
authMethod !== "provider-key"
|
|
120
147
|
? HF_HUB_INFERENCE_PROXY_TEMPLATE.replace("{{PROVIDER}}", provider)
|
|
121
148
|
: providerConfig.baseUrl,
|
|
122
|
-
model,
|
|
149
|
+
model: resolvedModel,
|
|
123
150
|
chatCompletion,
|
|
124
151
|
task,
|
|
125
152
|
});
|
|
@@ -150,7 +177,7 @@ export async function makeRequestOptions(
|
|
|
150
177
|
: JSON.stringify(
|
|
151
178
|
providerConfig.makeBody({
|
|
152
179
|
args: remainingArgs as Record<string, unknown>,
|
|
153
|
-
model,
|
|
180
|
+
model: resolvedModel,
|
|
154
181
|
task,
|
|
155
182
|
chatCompletion,
|
|
156
183
|
})
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
|
|
18
18
|
|
|
19
|
-
const BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai
|
|
19
|
+
const BLACK_FOREST_LABS_AI_API_BASE_URL = "https://api.us1.bfl.ai";
|
|
20
20
|
|
|
21
21
|
const makeBody = (params: BodyParams): Record<string, unknown> => {
|
|
22
22
|
return params.args;
|
|
@@ -31,7 +31,7 @@ const makeHeaders = (params: HeaderParams): Record<string, string> => {
|
|
|
31
31
|
};
|
|
32
32
|
|
|
33
33
|
const makeUrl = (params: UrlParams): string => {
|
|
34
|
-
return `${params.baseUrl}/${params.model}`;
|
|
34
|
+
return `${params.baseUrl}/v1/${params.model}`;
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
export const BLACK_FOREST_LABS_CONFIG: ProviderConfig = {
|
package/src/providers/cohere.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
|
|
18
18
|
|
|
19
|
-
const FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai
|
|
19
|
+
const FIREWORKS_AI_API_BASE_URL = "https://api.fireworks.ai";
|
|
20
20
|
|
|
21
21
|
const makeBody = (params: BodyParams): Record<string, unknown> => {
|
|
22
22
|
return {
|
|
@@ -30,10 +30,10 @@ const makeHeaders = (params: HeaderParams): Record<string, string> => {
|
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
const makeUrl = (params: UrlParams): string => {
|
|
33
|
-
if (params.
|
|
34
|
-
return `${params.baseUrl}/v1/chat/completions`;
|
|
33
|
+
if (params.chatCompletion) {
|
|
34
|
+
return `${params.baseUrl}/inference/v1/chat/completions`;
|
|
35
35
|
}
|
|
36
|
-
return params.baseUrl
|
|
36
|
+
return `${params.baseUrl}/inference`;
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
export const FIREWORKS_AI_CONFIG: ProviderConfig = {
|
|
@@ -29,7 +29,7 @@ const makeUrl = (params: UrlParams): string => {
|
|
|
29
29
|
/// when deployed on hf-inference, those two tasks are automatically compatible with one another.
|
|
30
30
|
return `${params.baseUrl}/pipeline/${params.task}/${params.model}`;
|
|
31
31
|
}
|
|
32
|
-
if (params.
|
|
32
|
+
if (params.chatCompletion) {
|
|
33
33
|
return `${params.baseUrl}/models/${params.model}/v1/chat/completions`;
|
|
34
34
|
}
|
|
35
35
|
return `${params.baseUrl}/models/${params.model}`;
|
package/src/providers/nebius.ts
CHANGED
|
@@ -33,10 +33,10 @@ const makeUrl = (params: UrlParams): string => {
|
|
|
33
33
|
if (params.task === "text-to-image") {
|
|
34
34
|
return `${params.baseUrl}/v1/images/generations`;
|
|
35
35
|
}
|
|
36
|
+
if (params.chatCompletion) {
|
|
37
|
+
return `${params.baseUrl}/v1/chat/completions`;
|
|
38
|
+
}
|
|
36
39
|
if (params.task === "text-generation") {
|
|
37
|
-
if (params.chatCompletion) {
|
|
38
|
-
return `${params.baseUrl}/v1/chat/completions`;
|
|
39
|
-
}
|
|
40
40
|
return `${params.baseUrl}/v1/completions`;
|
|
41
41
|
}
|
|
42
42
|
return params.baseUrl;
|
package/src/providers/novita.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
import type { ProviderConfig, UrlParams, HeaderParams, BodyParams } from "../types";
|
|
18
18
|
|
|
19
|
-
const NOVITA_API_BASE_URL = "https://api.novita.ai
|
|
19
|
+
const NOVITA_API_BASE_URL = "https://api.novita.ai";
|
|
20
20
|
|
|
21
21
|
const makeBody = (params: BodyParams): Record<string, unknown> => {
|
|
22
22
|
return {
|
|
@@ -30,11 +30,12 @@ const makeHeaders = (params: HeaderParams): Record<string, string> => {
|
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
const makeUrl = (params: UrlParams): string => {
|
|
33
|
-
if (params.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
33
|
+
if (params.chatCompletion) {
|
|
34
|
+
return `${params.baseUrl}/v3/openai/chat/completions`;
|
|
35
|
+
} else if (params.task === "text-generation") {
|
|
36
|
+
return `${params.baseUrl}/v3/openai/completions`;
|
|
37
|
+
} else if (params.task === "text-to-video") {
|
|
38
|
+
return `${params.baseUrl}/v3/hf/${params.model}`;
|
|
38
39
|
}
|
|
39
40
|
return params.baseUrl;
|
|
40
41
|
};
|
|
@@ -30,7 +30,7 @@ const makeHeaders = (params: HeaderParams): Record<string, string> => {
|
|
|
30
30
|
};
|
|
31
31
|
|
|
32
32
|
const makeUrl = (params: UrlParams): string => {
|
|
33
|
-
if (params.
|
|
33
|
+
if (params.chatCompletion) {
|
|
34
34
|
return `${params.baseUrl}/v1/chat/completions`;
|
|
35
35
|
}
|
|
36
36
|
return params.baseUrl;
|
|
@@ -33,10 +33,10 @@ const makeUrl = (params: UrlParams): string => {
|
|
|
33
33
|
if (params.task === "text-to-image") {
|
|
34
34
|
return `${params.baseUrl}/v1/images/generations`;
|
|
35
35
|
}
|
|
36
|
+
if (params.chatCompletion) {
|
|
37
|
+
return `${params.baseUrl}/v1/chat/completions`;
|
|
38
|
+
}
|
|
36
39
|
if (params.task === "text-generation") {
|
|
37
|
-
if (params.chatCompletion) {
|
|
38
|
-
return `${params.baseUrl}/v1/chat/completions`;
|
|
39
|
-
}
|
|
40
40
|
return `${params.baseUrl}/v1/completions`;
|
|
41
41
|
}
|
|
42
42
|
return params.baseUrl;
|