Skip to content

Commit fa5cead

Browse files
committed
Merge branch 'main' of github.com:huggingface/huggingface.js into google-hai-libraries
2 parents 60a89d5 + 1a07402 commit fa5cead

File tree

52 files changed

+4351
-893
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+4351
-893
lines changed

.github/workflows/test.yml

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ jobs:
4141
run: VCR_MODE=playback pnpm --filter ...[${{ steps.since.outputs.SINCE }}] test
4242
env:
4343
HF_TOKEN: ${{ secrets.HF_TOKEN }}
44+
HF_FAL_KEY: dummy
45+
HF_REPLICATE_KEY: dummy
46+
HF_SAMBANOVA_KEY: dummy
47+
HF_TOGETHER_KEY: dummy
4448

4549
browser:
4650
runs-on: ubuntu-latest
@@ -77,6 +81,10 @@ jobs:
7781
run: VCR_MODE=playback pnpm --filter ...[${{ steps.since.outputs.SINCE }}] test:browser
7882
env:
7983
HF_TOKEN: ${{ secrets.HF_TOKEN }}
84+
HF_FAL_KEY: dummy
85+
HF_REPLICATE_KEY: dummy
86+
HF_SAMBANOVA_KEY: dummy
87+
HF_TOGETHER_KEY: dummy
8088

8189
e2e:
8290
runs-on: ubuntu-latest
@@ -140,3 +148,7 @@ jobs:
140148
env:
141149
NPM_CONFIG_REGISTRY: http://localhost:4874/
142150
HF_TOKEN: ${{ secrets.HF_TOKEN }}
151+
HF_FAL_KEY: dummy
152+
HF_REPLICATE_KEY: dummy
153+
HF_SAMBANOVA_KEY: dummy
154+
HF_TOGETHER_KEY: dummy

README.md

+32-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// Programatically interact with the Hub
1414

1515
await createRepo({
16-
repo: {type: "model", name: "my-user/nlp-model"},
16+
repo: { type: "model", name: "my-user/nlp-model" },
1717
accessToken: HF_TOKEN
1818
});
1919

@@ -27,7 +27,7 @@ await uploadFile({
2727
}
2828
});
2929

30-
// Use Inference API
30+
// Use HF Inference API, or external Inference Providers!
3131

3232
await inference.chatCompletion({
3333
model: "meta-llama/Llama-3.1-8B-Instruct",
@@ -39,6 +39,7 @@ await inference.chatCompletion({
3939
],
4040
max_tokens: 512,
4141
temperature: 0.5,
42+
provider: "sambanova", // or together, fal-ai, replicate, …
4243
});
4344

4445
await inference.textToImage({
@@ -53,11 +54,13 @@ await inference.textToImage({
5354

5455
This is a collection of JS libraries to interact with the Hugging Face API, with TS types included.
5556

56-
- [@huggingface/inference](packages/inference/README.md): Use Inference Endpoints (dedicated) and Inference API (serverless) to make calls to 100,000+ Machine Learning models
57+
- [@huggingface/inference](packages/inference/README.md): Use Inference API (serverless), Inference Endpoints (dedicated) and third-party Inference providers to make calls to 100,000+ Machine Learning models
5758
- [@huggingface/hub](packages/hub/README.md): Interact with huggingface.co to create or delete repos and commit / download files
5859
- [@huggingface/agents](packages/agents/README.md): Interact with HF models through a natural language interface
5960
- [@huggingface/gguf](packages/gguf/README.md): A GGUF parser that works on remotely hosted files.
61+
- [@huggingface/dduf](packages/dduf/README.md): Similar package for DDUF (DDUF Diffusers Unified Format)
6062
- [@huggingface/tasks](packages/tasks/README.md): The definition files and source-of-truth for the Hub's main primitives like pipeline tasks, model libraries, etc.
63+
- [@huggingface/jinja](packages/jinja/README.md): A minimalistic JS implementation of the Jinja templating engine, to be used for ML chat templates.
6164
- [@huggingface/space-header](packages/space-header/README.md): Use the Space `mini_header` outside Hugging Face
6265

6366

@@ -92,7 +95,7 @@ You can run our packages with vanilla JS, without any bundler, by using a CDN or
9295

9396
```html
9497
<script type="module">
95-
import { HfInference } from 'https://cdn.jsdelivr.net/npm/@huggingface/inference@2.8.1/+esm';
98+
import { HfInference } from 'https://cdn.jsdelivr.net/npm/@huggingface/inference@3.0.0/+esm';
9699
import { createRepo, commit, deleteRepo, listFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm";
97100
</script>
98101
```
@@ -142,6 +145,22 @@ for await (const chunk of inference.chatCompletionStream({
142145
console.log(chunk.choices[0].delta.content);
143146
}
144147

148+
/// Using a third-party provider:
149+
await inference.chatCompletion({
150+
model: "meta-llama/Llama-3.1-8B-Instruct",
151+
messages: [{ role: "user", content: "Hello, nice to meet you!" }],
152+
max_tokens: 512,
153+
provider: "sambanova", // or together, fal-ai, replicate, …
154+
})
155+
156+
await inference.textToImage({
157+
model: "black-forest-labs/FLUX.1-dev",
158+
inputs: "a picture of a green bird",
159+
provider: "fal-ai",
160+
})
161+
162+
163+
145164
// You can also omit "model" to use the recommended model for the task
146165
await inference.translation({
147166
inputs: "My name is Wolfgang and I live in Amsterdam",
@@ -151,28 +170,24 @@ await inference.translation({
151170
},
152171
});
153172

154-
await inference.textToImage({
155-
model: 'black-forest-labs/FLUX.1-dev',
156-
inputs: 'a picture of a green bird',
157-
})
158-
173+
// pass multimodal files or URLs as inputs
159174
await inference.imageToText({
175+
model: 'nlpconnect/vit-gpt2-image-captioning',
160176
data: await (await fetch('https://picsum.photos/300/300')).blob(),
161-
model: 'nlpconnect/vit-gpt2-image-captioning',
162177
})
163178

164179
// Using your own dedicated inference endpoint: https://hf.co/docs/inference-endpoints/
165180
const gpt2 = inference.endpoint('https://xyz.eu-west-1.aws.endpoints.huggingface.cloud/gpt2');
166181
const { generated_text } = await gpt2.textGeneration({inputs: 'The answer to the universe is'});
167182

168-
//Chat Completion
183+
// Chat Completion
169184
const llamaEndpoint = inference.endpoint(
170185
"https://api-inference.huggingface.co/models/meta-llama/Llama-3.1-8B-Instruct"
171186
);
172187
const out = await llamaEndpoint.chatCompletion({
173-
model: "meta-llama/Llama-3.1-8B-Instruct",
174-
messages: [{ role: "user", content: "Hello, nice to meet you!" }],
175-
max_tokens: 512,
188+
model: "meta-llama/Llama-3.1-8B-Instruct",
189+
messages: [{ role: "user", content: "Hello, nice to meet you!" }],
190+
max_tokens: 512,
176191
});
177192
console.log(out.choices[0].message);
178193
```
@@ -185,7 +200,7 @@ import { createRepo, uploadFile, deleteFiles } from "@huggingface/hub";
185200
const HF_TOKEN = "hf_...";
186201

187202
await createRepo({
188-
repo: "my-user/nlp-model", // or {type: "model", name: "my-user/nlp-test"},
203+
repo: "my-user/nlp-model", // or { type: "model", name: "my-user/nlp-test" },
189204
accessToken: HF_TOKEN
190205
});
191206

@@ -200,7 +215,7 @@ await uploadFile({
200215
});
201216

202217
await deleteFiles({
203-
repo: {type: "space", name: "my-user/my-space"}, // or "spaces/my-user/my-space"
218+
repo: { type: "space", name: "my-user/my-space" }, // or "spaces/my-user/my-space"
204219
accessToken: HF_TOKEN,
205220
paths: ["README.md", ".gitattributes"]
206221
});
@@ -209,7 +224,7 @@ await deleteFiles({
209224
### @huggingface/agents example
210225

211226
```ts
212-
import {HfAgent, LLMFromHub, defaultTools} from '@huggingface/agents';
227+
import { HfAgent, LLMFromHub, defaultTools } from '@huggingface/agents';
213228

214229
const HF_TOKEN = "hf_...";
215230

packages/inference/LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Tim Mikeladze
3+
Copyright (c) 2022 Tim Mikeladze and the Hugging Face team
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

packages/inference/README.md

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# 🤗 Hugging Face Inference Endpoints
22

3-
A Typescript powered wrapper for the Hugging Face Inference Endpoints API. Learn more about Inference Endpoints at [Hugging Face](https://huggingface.co/inference-endpoints).
4-
It works with both [Inference API (serverless)](https://huggingface.co/docs/api-inference/index) and [Inference Endpoints (dedicated)](https://huggingface.co/docs/inference-endpoints/index).
3+
A Typescript powered wrapper for the Hugging Face Inference API (serverless), Inference Endpoints (dedicated), and third-party Inference Providers.
4+
It works with [Inference API (serverless)](https://huggingface.co/docs/api-inference/index) and [Inference Endpoints (dedicated)](https://huggingface.co/docs/inference-endpoints/index), and even with supported third-party Inference Providers.
55

66
Check out the [full documentation](https://huggingface.co/docs/huggingface.js/inference/README).
77

@@ -42,7 +42,40 @@ const hf = new HfInference('your access token')
4242

4343
Your access token should be kept private. If you need to protect it in front-end applications, we suggest setting up a proxy server that stores the access token.
4444

45-
#### Tree-shaking
45+
### Third-party inference providers
46+
47+
You can send inference requests to third-party providers with the inference client.
48+
49+
Currently, we support the following providers: [Fal.ai](https://fal.ai), [Replicate](https://replicate.com), [Together](https://together.xyz) and [Sambanova](https://sambanova.ai).
50+
51+
To send requests to a third-party provider, you have to pass the `provider` parameter to the inference function. Make sure your request is authenticated with an access token.
52+
```ts
53+
const accessToken = "hf_..."; // Either a HF access token, or an API key from the third-party provider (Replicate in this example)
54+
55+
const client = new HfInference(accessToken);
56+
await client.textToImage({
57+
provider: "replicate",
58+
model:"black-forest-labs/Flux.1-dev",
59+
inputs: "A black forest cake"
60+
})
61+
```
62+
63+
When authenticated with a Hugging Face access token, the request is routed through https://huggingface.co.
64+
When authenticated with a third-party provider key, the request is made directly against that provider's inference API.
65+
66+
Only a subset of models are supported when requesting third-party providers. You can check the list of supported models per pipeline tasks here:
67+
- [Fal.ai supported models](./src/providers/fal-ai.ts)
68+
- [Replicate supported models](./src/providers/replicate.ts)
69+
- [Sambanova supported models](./src/providers/sambanova.ts)
70+
- [Together supported models](./src/providers/together.ts)
71+
- [HF Inference API (serverless)](https://huggingface.co/models?inference=warm&sort=trending)
72+
73+
**Important note:** To be compatible, the third-party API must adhere to the "standard" shape API we expect on HF model pages for each pipeline task type.
74+
This is not an issue for LLMs as everyone converged on the OpenAI API anyways, but can be more tricky for other tasks like "text-to-image" or "automatic-speech-recognition" where there exists no standard API. Let us know if any help is needed or if we can make things easier for you!
75+
76+
👋**Want to add another provider?** Get in touch if you'd like to add support for another Inference provider, and/or request it on https://huggingface.co/spaces/huggingface/HuggingDiscussions/discussions/49
77+
78+
### Tree-shaking
4679

4780
You can import the functions you need directly from the module instead of using the `HfInference` class.
4881

packages/inference/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@huggingface/inference",
3-
"version": "2.8.1",
3+
"version": "3.0.0",
44
"packageManager": "[email protected]",
55
"license": "MIT",
66
"author": "Tim Mikeladze <[email protected]>",

packages/inference/src/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const HF_HUB_URL = "https://huggingface.co";
2+
export const HF_INFERENCE_API_URL = "https://api-inference.huggingface.co";

packages/inference/src/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
export type { ProviderMapping } from "./providers/types";
12
export { HfInference, HfInferenceEndpoint } from "./HfInference";
23
export { InferenceOutputError } from "./lib/InferenceOutputError";
4+
export { FAL_AI_SUPPORTED_MODEL_IDS } from "./providers/fal-ai";
5+
export { REPLICATE_SUPPORTED_MODEL_IDS } from "./providers/replicate";
6+
export { SAMBANOVA_SUPPORTED_MODEL_IDS } from "./providers/sambanova";
7+
export { TOGETHER_SUPPORTED_MODEL_IDS } from "./providers/together";
38
export * from "./types";
49
export * from "./tasks";

packages/inference/src/lib/getDefaultTask.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HF_HUB_URL } from "../config";
12
import { isUrl } from "./isUrl";
23

34
/**
@@ -8,7 +9,6 @@ import { isUrl } from "./isUrl";
89
const taskCache = new Map<string, { task: string; date: Date }>();
910
const CACHE_DURATION = 10 * 60 * 1000;
1011
const MAX_CACHE_ITEMS = 1000;
11-
export const HF_HUB_URL = "https://huggingface.co";
1212

1313
export interface DefaultTaskOptions {
1414
fetch?: typeof fetch;

0 commit comments

Comments
 (0)