Skip to content

Update client SDK snippets #3207

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

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</a>

A Rust, Python and gRPC server for text generation inference. Used in production at [Hugging Face](https://huggingface.co)
to power Hugging Chat, the Inference API and Inference Endpoint.
to power Hugging Chat, the Inference API and Inference Endpoints.

</div>

Expand Down
16 changes: 8 additions & 8 deletions docs/source/basic_tutorials/visual_language_models.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ To infer with vision language models through Python, you can use the [`huggingfa
```python
from huggingface_hub import InferenceClient

client = InferenceClient("http://127.0.0.1:3000")
client = InferenceClient(base_url="http://127.0.0.1:3000")
image = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png"
prompt = f"![]({image})What is this a picture of?\n\n"
for token in client.text_generation(prompt, max_new_tokens=16, stream=True):
Expand All @@ -37,7 +37,7 @@ import base64
import requests
import io

client = InferenceClient("http://127.0.0.1:3000")
client = InferenceClient(base_url="http://127.0.0.1:3000")

# read image from local file
image_path = "rabbit.png"
Expand All @@ -58,7 +58,7 @@ or via the `chat_completion` endpoint:
```python
from huggingface_hub import InferenceClient

client = InferenceClient("http://127.0.0.1:3000")
client = InferenceClient(base_url="http://127.0.0.1:3000")

chat = client.chat_completion(
messages=[
Expand Down Expand Up @@ -137,19 +137,19 @@ First, we need to install the `@huggingface/inference` library.
npm install @huggingface/inference
```

If you're using the free Inference API, you can use [Huggingface.js](https://huggingface.co/docs/huggingface.js/inference/README)'s `HfInference`. If you're using inference endpoints, you can use `HfInferenceEndpoint` class to easily interact with the Inference API.
Whether you use Inference Providers (our serverless API), or Inference Endpoints, you can call `InferenceClient`.

We can create a `HfInferenceEndpoint` providing our endpoint URL and We can create a `HfInferenceEndpoint` providing our endpoint URL and [Hugging Face access token](https://huggingface.co/settings/tokens).
We can create a `InferenceClient` providing our endpoint URL and [Hugging Face access token](https://huggingface.co/settings/tokens).

```js
import { HfInferenceEndpoint } from "@huggingface/inference";
import { InferenceClient } from "@huggingface/inference";

const hf = new HfInferenceEndpoint("http://127.0.0.1:3000", "HF_TOKEN");
const client = new InferenceClient('hf_YOUR_TOKEN', { endpointUrl: 'https://YOUR_ENDPOINT.endpoints.huggingface.cloud' });

const prompt =
"![](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rabbit.png)What is this a picture of?\n\n";

const stream = hf.textGenerationStream({
const stream = client.textGenerationStream({
inputs: prompt,
parameters: { max_new_tokens: 16, seed: 42 },
});
Expand Down
18 changes: 10 additions & 8 deletions docs/source/conceptual/streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,26 @@ curl localhost:8080/v1/chat/completions \
### Streaming with JavaScript

First, we need to install the `@huggingface/inference` library.
`npm install @huggingface/inference`

If you're using the free Inference API, you can use `HfInference`. If you're using inference endpoints, you can use `HfInferenceEndpoint`.
```bash
npm install @huggingface/inference
```

Whether you use Inference Providers (our serverless API), or Inference Endpoints, you can call `InferenceClient`.

We can create a `HfInferenceEndpoint` providing our endpoint URL and credential.

```js
import { HfInferenceEndpoint } from '@huggingface/inference'
import { InferenceClient } from '@huggingface/inference';

const hf = new HfInferenceEndpoint('https://YOUR_ENDPOINT.endpoints.huggingface.cloud', 'hf_YOUR_TOKEN')
const client = new InferenceClient('hf_YOUR_TOKEN', { endpointUrl: 'https://YOUR_ENDPOINT.endpoints.huggingface.cloud' });

// prompt
const prompt = 'What can you do in Nuremberg, Germany? Give me 3 Tips'
const prompt = 'What can you do in Nuremberg, Germany? Give me 3 Tips';

const stream = hf.textGenerationStream({ inputs: prompt })
const stream = client.textGenerationStream({ inputs: prompt });
for await (const r of stream) {
// yield the generated token
process.stdout.write(r.token.text)
process.stdout.write(r.token.text);
}
```

Expand Down
Loading