-
Notifications
You must be signed in to change notification settings - Fork 242
[DRAFT] docs(genapi): add hugging face library #4748
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
bene2k1
wants to merge
1
commit into
main
Choose a base branch
from
MTA-5809
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
pages/generative-apis/how-to/connect-through-hugging-face-library.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,108 @@ | ||||||
--- | ||||||
meta: | ||||||
title: How to connect using Hugging Face library | ||||||
description: Learn how to interact with Generative APIs using the Hugging Face library | ||||||
content: | ||||||
h1: How to Connect using Hugging Face library | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
paragraph: Learn how to interact with Generative APIs using the Hugging Face library. | ||||||
tags: generative-apis hugging-face library | ||||||
dates: | ||||||
validation: 2025-04-01 | ||||||
posted: 2025-04-01 | ||||||
--- | ||||||
<Macro id="requirements" /> | ||||||
|
||||||
- A Scaleway account logged into the [console](https://console.scaleway.com) | ||||||
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization | ||||||
- A valid [API key](/iam/how-to/create-api-keys/) for API authentication | ||||||
- Node.js installed on your local machine | ||||||
- Scaleway credentials or Hugging Face credentials with the proper access rights (two methods of connection are available) | ||||||
|
||||||
## Steps to Connect Using Hugging Face Library | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
1. Create a new directory on your local machine where you will store all your project files. | ||||||
|
||||||
2. Open a terminal in your project directory and run the following command to install the Hugging Face inference library: | ||||||
```bash | ||||||
npm install @huggingface/inference | ||||||
``` | ||||||
|
||||||
3. Create a new file named `main.js` in your project directory and add the following code to it: | ||||||
```js | ||||||
import { InferenceClient } from '@huggingface/inference'; | ||||||
|
||||||
const client = new InferenceClient({ apiKey: process.env.SCW_SECRET_KEY }); | ||||||
|
||||||
const out = await client.chatCompletion({ | ||||||
provider: "scaleway", | ||||||
// endpointUrl is not supported with third-party providers | ||||||
// endpointUrl: "https://api.scaleway.ai/b409cb09-756c-430f-a8e8-748f88ef4bad", | ||||||
// model: "meta-llama/Meta-Llama-3-8B-Instruct", | ||||||
model: "meta-llama/Llama-3.3-70B-Instruct", | ||||||
messages: [{ role: "user", content: "Tell me about Scaleway." }], | ||||||
max_tokens: 512, | ||||||
temperature: 0.1, | ||||||
}); | ||||||
|
||||||
console.log(out.choices[0].message.content); | ||||||
``` | ||||||
|
||||||
4. Execute the script by running the following command in your terminal: | ||||||
```bash | ||||||
node main.js | ||||||
``` | ||||||
The model's response should be displayed in your terminal. | ||||||
|
||||||
### Using stream completion | ||||||
|
||||||
To use stream completion, you can modify your script as follows: | ||||||
```js | ||||||
import { InferenceClient } from '@huggingface/inference'; | ||||||
|
||||||
const client = new InferenceClient({ apiKey: process.env.SCW_SECRET_KEY }); | ||||||
|
||||||
for await (const chunk of client.chatCompletionStream({ | ||||||
model: "meta-llama/Llama-3.3-70B-Instruct", | ||||||
provider: "scaleway", | ||||||
messages: [{ role: "user", content: "Tell me about Scaleway." }], | ||||||
max_tokens: 512, | ||||||
})) { | ||||||
console.log(chunk.choices[0].delta.content); | ||||||
} | ||||||
``` | ||||||
|
||||||
### Using Hugging Face tokens | ||||||
|
||||||
You can also authenticate using Hugging Face tokens. Set the `HF_TOKEN` environment variable and modify your script slightly: | ||||||
```js | ||||||
import { InferenceClient } from '@huggingface/inference'; | ||||||
|
||||||
const client = new InferenceClient({ apiKey: process.env.HF_TOKEN }); | ||||||
|
||||||
const out = await client.chatCompletion({ | ||||||
provider: "scaleway", | ||||||
model: "meta-llama/Llama-3.3-70B-Instruct", | ||||||
messages: [{ role: "user", content: "Tell me about Scaleway." }], | ||||||
max_tokens: 512, | ||||||
temperature: 0.1, | ||||||
}); | ||||||
|
||||||
console.log(out.choices[0].message.content); | ||||||
``` | ||||||
|
||||||
In some cases, providing the token directly in the `InferenceClient` constructor might not be necessary if the environment variable is set correctly: | ||||||
```js | ||||||
import { InferenceClient } from '@huggingface/inference'; | ||||||
|
||||||
const client = new InferenceClient(); | ||||||
|
||||||
const out = await client.chatCompletion({ | ||||||
provider: "scaleway", | ||||||
model: "meta-llama/Llama-3.3-70B-Instruct", | ||||||
messages: [{ role: "user", content: "Tell me about Scaleway." }], | ||||||
max_tokens: 512, | ||||||
temperature: 0.1, | ||||||
}); | ||||||
|
||||||
console.log(out.choices[0].message.content); | ||||||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.