@huggingface/inference 2.8.1 → 3.0.1
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/LICENSE +1 -1
- package/README.md +45 -17
- package/dist/index.cjs +388 -134
- package/dist/index.js +383 -134
- package/dist/src/config.d.ts +3 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/lib/getDefaultTask.d.ts +0 -1
- package/dist/src/lib/getDefaultTask.d.ts.map +1 -1
- package/dist/src/lib/makeRequestOptions.d.ts.map +1 -1
- package/dist/src/providers/fal-ai.d.ts +6 -0
- package/dist/src/providers/fal-ai.d.ts.map +1 -0
- package/dist/src/providers/replicate.d.ts +6 -0
- package/dist/src/providers/replicate.d.ts.map +1 -0
- package/dist/src/providers/sambanova.d.ts +6 -0
- package/dist/src/providers/sambanova.d.ts.map +1 -0
- package/dist/src/providers/together.d.ts +12 -0
- package/dist/src/providers/together.d.ts.map +1 -0
- package/dist/src/providers/types.d.ts +4 -0
- package/dist/src/providers/types.d.ts.map +1 -0
- package/dist/src/tasks/audio/automaticSpeechRecognition.d.ts.map +1 -1
- package/dist/src/tasks/audio/textToSpeech.d.ts.map +1 -1
- package/dist/src/tasks/custom/request.d.ts +1 -1
- package/dist/src/tasks/custom/request.d.ts.map +1 -1
- package/dist/src/tasks/custom/streamingRequest.d.ts.map +1 -1
- package/dist/src/tasks/cv/textToImage.d.ts +8 -0
- package/dist/src/tasks/cv/textToImage.d.ts.map +1 -1
- package/dist/src/tasks/nlp/chatCompletion.d.ts.map +1 -1
- package/dist/src/tasks/nlp/textGeneration.d.ts.map +1 -1
- package/dist/src/types.d.ts +16 -2
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/config.ts +2 -0
- package/src/index.ts +5 -0
- package/src/lib/getDefaultTask.ts +1 -1
- package/src/lib/makeRequestOptions.ts +201 -59
- package/src/providers/fal-ai.ts +23 -0
- package/src/providers/replicate.ts +16 -0
- package/src/providers/sambanova.ts +23 -0
- package/src/providers/together.ts +60 -0
- package/src/providers/types.ts +6 -0
- package/src/tasks/audio/automaticSpeechRecognition.ts +10 -1
- package/src/tasks/audio/textToSpeech.ts +17 -2
- package/src/tasks/custom/request.ts +12 -6
- package/src/tasks/custom/streamingRequest.ts +18 -3
- package/src/tasks/cv/textToImage.ts +44 -1
- package/src/tasks/nlp/chatCompletion.ts +2 -2
- package/src/tasks/nlp/textGeneration.ts +43 -9
- package/src/types.ts +20 -2
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2022 Tim Mikeladze
|
|
3
|
+
Copyright (c) 2022 Tim Mikeladze and the Hugging Face team
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# 🤗 Hugging Face Inference Endpoints
|
|
2
2
|
|
|
3
|
-
A Typescript powered wrapper for the Hugging Face Inference
|
|
4
|
-
It works with
|
|
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.
|
|
5
5
|
|
|
6
6
|
Check out the [full documentation](https://huggingface.co/docs/huggingface.js/inference/README).
|
|
7
7
|
|
|
@@ -42,7 +42,40 @@ const hf = new HfInference('your access token')
|
|
|
42
42
|
|
|
43
43
|
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.
|
|
44
44
|
|
|
45
|
-
|
|
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
|
|
46
79
|
|
|
47
80
|
You can import the functions you need directly from the module instead of using the `HfInference` class.
|
|
48
81
|
|
|
@@ -91,23 +124,21 @@ Using the `chatCompletion` method, you can generate text with models compatible
|
|
|
91
124
|
```typescript
|
|
92
125
|
// Non-streaming API
|
|
93
126
|
const out = await hf.chatCompletion({
|
|
94
|
-
model: "
|
|
95
|
-
messages: [{ role: "user", content: "
|
|
96
|
-
max_tokens:
|
|
127
|
+
model: "meta-llama/Llama-3.1-8B-Instruct",
|
|
128
|
+
messages: [{ role: "user", content: "Hello, nice to meet you!" }],
|
|
129
|
+
max_tokens: 512,
|
|
97
130
|
temperature: 0.1,
|
|
98
|
-
seed: 0,
|
|
99
131
|
});
|
|
100
132
|
|
|
101
133
|
// Streaming API
|
|
102
134
|
let out = "";
|
|
103
135
|
for await (const chunk of hf.chatCompletionStream({
|
|
104
|
-
model: "
|
|
136
|
+
model: "meta-llama/Llama-3.1-8B-Instruct",
|
|
105
137
|
messages: [
|
|
106
|
-
{ role: "user", content: "
|
|
138
|
+
{ role: "user", content: "Can you help me solve an equation?" },
|
|
107
139
|
],
|
|
108
|
-
max_tokens:
|
|
140
|
+
max_tokens: 512,
|
|
109
141
|
temperature: 0.1,
|
|
110
|
-
seed: 0,
|
|
111
142
|
})) {
|
|
112
143
|
if (chunk.choices && chunk.choices.length > 0) {
|
|
113
144
|
out += chunk.choices[0].delta.content;
|
|
@@ -396,11 +427,8 @@ Creates an image from a text prompt.
|
|
|
396
427
|
|
|
397
428
|
```typescript
|
|
398
429
|
await hf.textToImage({
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
parameters: {
|
|
402
|
-
negative_prompt: 'blurry',
|
|
403
|
-
}
|
|
430
|
+
model: 'black-forest-labs/FLUX.1-dev',
|
|
431
|
+
inputs: 'a picture of a green bird'
|
|
404
432
|
})
|
|
405
433
|
```
|
|
406
434
|
|
|
@@ -583,7 +611,7 @@ const { generated_text } = await gpt2.textGeneration({inputs: 'The answer to the
|
|
|
583
611
|
|
|
584
612
|
// Chat Completion Example
|
|
585
613
|
const ep = hf.endpoint(
|
|
586
|
-
"https://api-inference.huggingface.co/models/
|
|
614
|
+
"https://api-inference.huggingface.co/models/meta-llama/Llama-3.1-8B-Instruct"
|
|
587
615
|
);
|
|
588
616
|
const stream = ep.chatCompletionStream({
|
|
589
617
|
model: "tgi",
|