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

+10-9
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

+2-2
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

+2-2
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

+2-2
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

+1-1
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

+2-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/agents/src/lib/evalBuilder.ts

+2-2
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

+3-3
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

+7-4
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

+2-2
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 {

packages/agents/test/HfAgent.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from "vitest";
22
import { HfAgent, defaultTools, LLMFromHub, LLMFromEndpoint } from "../src";
33
import type { Data } from "../src/types";
4-
import type { HfInference } from "@huggingface/inference";
4+
import type { InferenceClient } from "@huggingface/inference";
55

66
const env = import.meta.env;
77
if (!env.HF_TOKEN) {
@@ -33,7 +33,7 @@ describe("HfAgent", () => {
3333
},
3434
],
3535
// eslint-disable-next-line @typescript-eslint/no-unused-vars
36-
call: async (input: Promise<Data>, inference: HfInference): Promise<Data> => {
36+
call: async (input: Promise<Data>, inference: InferenceClient): Promise<Data> => {
3737
const data = await input;
3838
if (typeof data !== "string") {
3939
throw new Error("Input must be a string");

packages/gguf/src/cli.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

3-
import { GGMLQuantizationType, gguf, ggufAllShards, GGUFParseOutput } from ".";
3+
import type { GGUFParseOutput } from ".";
4+
import { GGMLQuantizationType, ggufAllShards } from ".";
45
import { GGML_QUANT_SIZES } from "./quant-descriptions";
56

67
interface PrintColumnHeader {

packages/inference/README.md

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

3-
A Typescript powered wrapper for the HF 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.
3+
A Typescript powered wrapper for the HF Inference API (serverless), Inference Endpoints (dedicated), and all supported 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 all supported third-party Inference Providers.
55

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

@@ -25,24 +25,24 @@ yarn add @huggingface/inference
2525

2626
```ts
2727
// esm.sh
28-
import { HfInference } from "https://esm.sh/@huggingface/inference"
28+
import { InferenceClient } from "https://esm.sh/@huggingface/inference"
2929
// or npm:
30-
import { HfInference } from "npm:@huggingface/inference"
30+
import { InferenceClient } from "npm:@huggingface/inference"
3131
```
3232

3333
### Initialize
3434

3535
```typescript
36-
import { HfInference } from '@huggingface/inference'
36+
import { InferenceClient } from '@huggingface/inference'
3737

38-
const hf = new HfInference('your access token')
38+
const hf = new InferenceClient('your access token')
3939
```
4040

4141
**Important note:** Using an access token is optional to get started, however you will be rate limited eventually. Join [Hugging Face](https://huggingface.co/join) and then visit [access tokens](https://huggingface.co/settings/tokens) to generate your access token for **free**.
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-
### Third-party inference providers
45+
### All supported inference providers
4646

4747
You can send inference requests to third-party providers with the inference client.
4848

@@ -63,7 +63,7 @@ To send requests to a third-party provider, you have to pass the `provider` para
6363
```ts
6464
const accessToken = "hf_..."; // Either a HF access token, or an API key from the third-party provider (Replicate in this example)
6565

66-
const client = new HfInference(accessToken);
66+
const client = new InferenceClient(accessToken);
6767
await client.textToImage({
6868
provider: "replicate",
6969
model:"black-forest-labs/Flux.1-dev",
@@ -93,7 +93,7 @@ This is not an issue for LLMs as everyone converged on the OpenAI API anyways, b
9393

9494
### Tree-shaking
9595

96-
You can import the functions you need directly from the module instead of using the `HfInference` class.
96+
You can import the functions you need directly from the module instead of using the `InferenceClient` class.
9797

9898
```ts
9999
import { textGeneration } from "@huggingface/inference";
@@ -165,7 +165,7 @@ for await (const chunk of hf.chatCompletionStream({
165165
It's also possible to call Mistral or OpenAI endpoints directly:
166166

167167
```typescript
168-
const openai = new HfInference(OPENAI_TOKEN).endpoint("https://api.openai.com");
168+
const openai = new InferenceClient(OPENAI_TOKEN).endpoint("https://api.openai.com");
169169

170170
let out = "";
171171
for await (const chunk of openai.chatCompletionStream({
@@ -602,7 +602,7 @@ You can use any Chat Completion API-compatible provider with the `chatCompletion
602602
```typescript
603603
// Chat Completion Example
604604
const MISTRAL_KEY = process.env.MISTRAL_KEY;
605-
const hf = new HfInference(MISTRAL_KEY);
605+
const hf = new InferenceClient(MISTRAL_KEY);
606606
const ep = hf.endpoint("https://api.mistral.ai");
607607
const stream = ep.chatCompletionStream({
608608
model: "mistral-tiny",

packages/inference/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "3.5.1",
44
"packageManager": "[email protected]",
55
"license": "MIT",
6-
"author": "Tim Mikeladze <[email protected]>",
7-
"description": "Typescript wrapper for the Hugging Face Inference Endpoints & Inference API",
6+
"author": "Hugging Face and Tim Mikeladze <[email protected]>",
7+
"description": "Typescript client for the Hugging Face Inference Providers and Inference Endpoints",
88
"repository": {
99
"type": "git",
1010
"url": "https://github.com/huggingface/huggingface.js.git"

packages/inference/scripts/generate-dts.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ for (const dir of dirs) {
6161

6262
appendFileSync(
6363
"./dist/index.d.ts",
64-
`export class HfInference {
64+
`export class InferenceClient {
6565
\tconstructor(accessToken?: string, defaultOptions?: Options);
6666
\t/**
67-
\t * Returns copy of HfInference tied to a specified endpoint.
67+
\t * Returns copy of InferenceClient tied to a specified endpoint.
6868
\t */
69-
\tendpoint(endpointUrl: string): HfInferenceEndpoint;
69+
\tendpoint(endpointUrl: string): InferenceClientEndpoint;
7070
` +
7171
fns
7272
.map(
@@ -84,7 +84,7 @@ appendFileSync(
8484

8585
appendFileSync(
8686
"./dist/index.d.ts",
87-
`export class HfInferenceEndpoint {\n\tconstructor(endpointUrl: string, accessToken?: string, defaultOptions?: Options);\n` +
87+
`export class InferenceClientEndpoint {\n\tconstructor(endpointUrl: string, accessToken?: string, defaultOptions?: Options);\n` +
8888
fns
8989
.map(
9090
(fn) =>

0 commit comments

Comments
 (0)