Skip to content

Commit 1ae84ba

Browse files
julien-cVaibhavs10
andauthored
Finally rename HfInference to InferenceClient (#1258)
There's no reason the API should be ≠ in Python and JS --------- Co-authored-by: vb <[email protected]>
1 parent e51d001 commit 1ae84ba

File tree

46 files changed

+161
-148
lines changed

Some content is hidden

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

46 files changed

+161
-148
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ await uploadFile({
3131

3232
await inference.chatCompletion({
3333
model: "meta-llama/Llama-3.1-8B-Instruct",
34+
provider: "sambanova", // or together, fal-ai, replicate, cohere …
3435
messages: [
3536
{
3637
role: "user",
@@ -39,11 +40,11 @@ await inference.chatCompletion({
3940
],
4041
max_tokens: 512,
4142
temperature: 0.5,
42-
provider: "sambanova", // or together, fal-ai, replicate, cohere …
4343
});
4444

4545
await inference.textToImage({
4646
model: "black-forest-labs/FLUX.1-dev",
47+
provider: "replicate",
4748
inputs: "a picture of a green bird",
4849
});
4950

@@ -54,7 +55,7 @@ await inference.textToImage({
5455

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

57-
- [@huggingface/inference](packages/inference/README.md): Use HF Inference API (serverless), Inference Endpoints (dedicated) and third-party Inference Providers to make calls to 100,000+ Machine Learning models
58+
- [@huggingface/inference](packages/inference/README.md): Use HF Inference API (serverless), Inference Endpoints (dedicated) and all supported Inference Providers to make calls to 100,000+ Machine Learning models
5859
- [@huggingface/hub](packages/hub/README.md): Interact with huggingface.co to create or delete repos and commit / download files
5960
- [@huggingface/agents](packages/agents/README.md): Interact with HF models through a natural language interface
6061
- [@huggingface/gguf](packages/gguf/README.md): A GGUF parser that works on remotely hosted files.
@@ -84,7 +85,7 @@ npm install @huggingface/agents
8485
Then import the libraries in your code:
8586

8687
```ts
87-
import { HfInference } from "@huggingface/inference";
88+
import { InferenceClient } from "@huggingface/inference";
8889
import { HfAgent } from "@huggingface/agents";
8990
import { createRepo, commit, deleteRepo, listFiles } from "@huggingface/hub";
9091
import type { RepoId } from "@huggingface/hub";
@@ -96,7 +97,7 @@ You can run our packages with vanilla JS, without any bundler, by using a CDN or
9697

9798
```html
9899
<script type="module">
99-
import { HfInference } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm';
100+
import { InferenceClient } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm';
100101
import { createRepo, commit, deleteRepo, listFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm";
101102
</script>
102103
```
@@ -105,12 +106,12 @@ You can run our packages with vanilla JS, without any bundler, by using a CDN or
105106

106107
```ts
107108
// esm.sh
108-
import { HfInference } from "https://esm.sh/@huggingface/inference"
109+
import { InferenceClient } from "https://esm.sh/@huggingface/inference"
109110
import { HfAgent } from "https://esm.sh/@huggingface/agents";
110111

111112
import { createRepo, commit, deleteRepo, listFiles } from "https://esm.sh/@huggingface/hub"
112113
// or npm:
113-
import { HfInference } from "npm:@huggingface/inference"
114+
import { InferenceClient } from "npm:@huggingface/inference"
114115
import { HfAgent } from "npm:@huggingface/agents";
115116

116117
import { createRepo, commit, deleteRepo, listFiles } from "npm:@huggingface/hub"
@@ -123,11 +124,11 @@ Get your HF access token in your [account settings](https://huggingface.co/setti
123124
### @huggingface/inference examples
124125

125126
```ts
126-
import { HfInference } from "@huggingface/inference";
127+
import { InferenceClient } from "@huggingface/inference";
127128

128129
const HF_TOKEN = "hf_...";
129130

130-
const inference = new HfInference(HF_TOKEN);
131+
const inference = new InferenceClient(HF_TOKEN);
131132

132133
// Chat completion API
133134
const out = await inference.chatCompletion({
@@ -179,7 +180,7 @@ await inference.imageToText({
179180

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

184185
// Chat Completion
185186
const llamaEndpoint = inference.endpoint(

e2e/deno/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HfInference } from "npm:@huggingface/inference@*";
1+
import { InferenceClient } from "npm:@huggingface/inference@*";
22
import { whoAmI, listFiles } from "npm:@huggingface/hub@*";
33

44
const info = await whoAmI({ credentials: { accessToken: "hf_hub.js" }, hubUrl: "https://hub-ci.huggingface.co" });
@@ -10,7 +10,7 @@ for await (const file of listFiles({ repo: "gpt2" })) {
1010

1111
const token = Deno.env.get("HF_TOKEN");
1212
if (token) {
13-
const hf = new HfInference(token);
13+
const hf = new InferenceClient(token);
1414

1515
const tokenInfo = await whoAmI({ credentials: { accessToken: token } });
1616
console.log(tokenInfo);

e2e/svelte/src/routes/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script>
22
import { whoAmI, listFiles } from "@huggingface/hub";
3-
import { HfInference } from "@huggingface/inference";
3+
import { InferenceClient } from "@huggingface/inference";
44
5-
const hf = new HfInference();
5+
const hf = new InferenceClient();
66
77
const test = async () => {
88
const info = await whoAmI({ credentials: { accessToken: "hf_hub.js" }, hubUrl: "https://hub-ci.huggingface.co" });

e2e/ts/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { HfInference } from "@huggingface/inference";
1+
import { InferenceClient } from "@huggingface/inference";
22
import { whoAmI } from "@huggingface/hub";
33

44
const hfToken = process.env.token;
55

6-
const hf = new HfInference(hfToken);
6+
const hf = new InferenceClient(hfToken);
77

88
(async () => {
99
const info = await whoAmI({ credentials: { accessToken: "hf_hub.js" }, hubUrl: "https://hub-ci.huggingface.co" });

packages/agents/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@
5656
"@types/node": "^18.13.0"
5757
},
5858
"dependencies": {
59-
"@huggingface/inference": "^2.6.1"
59+
"@huggingface/inference": "workspace:^"
6060
}
6161
}

packages/agents/pnpm-lock.yaml

Lines changed: 2 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/agents/src/lib/evalBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HfInference } from "@huggingface/inference";
1+
import { InferenceClient } from "@huggingface/inference";
22
import type { Data, Tool } from "../types";
33

44
// this function passes the tools & files to the context before calling eval
@@ -17,7 +17,7 @@ export async function evalBuilder(
1717

1818
// add tools to context
1919
for (const tool of tools) {
20-
const toolCall = (input: Promise<Data>) => tool.call?.(input, new HfInference(accessToken ?? ""));
20+
const toolCall = (input: Promise<Data>) => tool.call?.(input, new InferenceClient(accessToken ?? ""));
2121
// @ts-expect-error adding to the scope
2222
globalThis[tool.name] = toolCall;
2323
}

packages/agents/src/llms/LLMHF.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { LLM } from "../types";
2-
import { HfInference } from "@huggingface/inference";
2+
import { InferenceClient } from "@huggingface/inference";
33

44
export function LLMFromHub(accessToken?: string, model?: string): LLM {
5-
const inference = new HfInference(accessToken);
5+
const inference = new InferenceClient(accessToken);
66

77
return async (prompt: string): Promise<string> => {
88
const formattedPrompt = "<|user|>" + prompt + "<|end|><|assistant|>";
@@ -20,7 +20,7 @@ export function LLMFromHub(accessToken?: string, model?: string): LLM {
2020
}
2121

2222
export function LLMFromEndpoint(accessToken: string, endpoint: string): LLM {
23-
const inference = new HfInference(accessToken).endpoint(endpoint);
23+
const inference = new InferenceClient(accessToken).endpoint(endpoint);
2424
return async (prompt: string): Promise<string> => {
2525
const formattedPrompt = "<|user|>" + prompt + "<|end|><|assistant|>";
2626

packages/agents/src/tools/imageToText.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ export const imageToTextTool: Tool = {
1515
if (typeof data === "string") throw "Input must be a blob.";
1616

1717
return (
18-
await inference.imageToText({
19-
data,
20-
})
21-
).generated_text;
18+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
19+
(
20+
await inference.imageToText({
21+
data,
22+
})
23+
).generated_text!
24+
);
2225
},
2326
};

packages/agents/src/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { HfInference } from "@huggingface/inference";
1+
import type { InferenceClient } from "@huggingface/inference";
22

33
export type Data = string | Blob | ArrayBuffer;
44

55
export interface Tool {
66
name: string;
77
description: string;
88
examples: Array<Example>;
9-
call?: (input: Promise<Data>, inference: HfInference) => Promise<Data>;
9+
call?: (input: Promise<Data>, inference: InferenceClient) => Promise<Data>;
1010
}
1111

1212
export interface Example {

0 commit comments

Comments
 (0)