base on Utilities to use the Hugging Face Hub API <p align="center"> <br/> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://huggingface.co/datasets/huggingface/documentation-images/raw/main/huggingfacejs-dark.svg"> <source media="(prefers-color-scheme: light)" srcset="https://huggingface.co/datasets/huggingface/documentation-images/raw/main/huggingfacejs-light.svg"> <img alt="huggingface javascript library logo" src="https://huggingface.co/datasets/huggingface/documentation-images/raw/main/huggingfacejs-light.svg" width="376" height="59" style="max-width: 100%;"> </picture> <br/> <br/> </p> ```ts // Programmatically interact with the Hub await createRepo({ repo: { type: "model", name: "my-user/nlp-model" }, accessToken: HF_TOKEN }); await uploadFile({ repo: "my-user/nlp-model", accessToken: HF_TOKEN, // Can work with native File in browsers file: { path: "pytorch_model.bin", content: new Blob(...) } }); // Use all supported Inference Providers! await inference.chatCompletion({ model: "meta-llama/Llama-3.1-8B-Instruct", provider: "sambanova", // or together, fal-ai, replicate, cohere … messages: [ { role: "user", content: "Hello, nice to meet you!", }, ], max_tokens: 512, temperature: 0.5, }); await inference.textToImage({ model: "black-forest-labs/FLUX.1-dev", provider: "replicate", inputs: "a picture of a green bird", }); // and much more… ``` # Hugging Face JS libraries This is a collection of JS libraries to interact with the Hugging Face API, with TS types included. - [@huggingface/inference](packages/inference/README.md): Use all supported (serverless) Inference Providers or switch to Inference Endpoints (dedicated) to make calls to 100,000+ Machine Learning models - [@huggingface/hub](packages/hub/README.md): Interact with huggingface.co to create or delete repos and commit / download files - [@huggingface/mcp-client](packages/mcp-client/README.md): A Model Context Protocol (MCP) client, and a tiny Agent library, built on top of InferenceClient. - [@huggingface/gguf](packages/gguf/README.md): A GGUF parser that works on remotely hosted files. - [@huggingface/dduf](packages/dduf/README.md): Similar package for DDUF (DDUF Diffusers Unified Format) - [@huggingface/tasks](packages/tasks/README.md): The definition files and source-of-truth for the Hub's main primitives like pipeline tasks, model libraries, etc. - [@huggingface/jinja](packages/jinja/README.md): A minimalistic JS implementation of the Jinja templating engine, to be used for ML chat templates. - [@huggingface/space-header](packages/space-header/README.md): Use the Space `mini_header` outside Hugging Face - [@huggingface/ollama-utils](packages/ollama-utils/README.md): Various utilities for maintaining Ollama compatibility with models on the Hugging Face Hub. We use modern features to avoid polyfills and dependencies, so the libraries will only work on modern browsers / Node.js >= 18 / Bun / Deno. The libraries are still very young, please help us by opening issues! ## Installation ### From NPM To install via NPM, you can download the libraries as needed: ```bash npm install @huggingface/inference npm install @huggingface/hub npm install @huggingface/mcp-client ``` Then import the libraries in your code: ```ts import { InferenceClient } from "@huggingface/inference"; import { createRepo, commit, deleteRepo, listFiles } from "@huggingface/hub"; import { McpClient } from "@huggingface/mcp-client"; import type { RepoId } from "@huggingface/hub"; ``` ### From CDN or Static hosting You can run our packages with vanilla JS, without any bundler, by using a CDN or static hosting. Using [ES modules](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/), i.e. `<script type="module">`, you can import the libraries in your code: ```html <script type="module"> import { InferenceClient } from 'https://cdn.jsdelivr.net/npm/@huggingface/inference@4.0.4/+esm'; import { createRepo, commit, deleteRepo, listFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/hub@2.2.0/+esm"; </script> ``` ### Deno ```ts // esm.sh import { InferenceClient } from "https://esm.sh/@huggingface/inference" import { createRepo, commit, deleteRepo, listFiles } from "https://esm.sh/@huggingface/hub" // or npm: import { InferenceClient } from "npm:@huggingface/inference" import { createRepo, commit, deleteRepo, listFiles } from "npm:@huggingface/hub" ``` ## Usage examples Get your HF access token in your [account settings](https://huggingface.co/settings/tokens). ### @huggingface/inference examples ```ts import { InferenceClient } from "@huggingface/inference"; const HF_TOKEN = "hf_..."; const client = new InferenceClient(HF_TOKEN); // Chat completion API const out = await client.chatCompletion({ model: "meta-llama/Llama-3.1-8B-Instruct", messages: [{ role: "user", content: "Hello, nice to meet you!" }], max_tokens: 512 }); console.log(out.choices[0].message); // Streaming chat completion API for await (const chunk of client.chatCompletionStream({ model: "meta-llama/Llama-3.1-8B-Instruct", messages: [{ role: "user", content: "Hello, nice to meet you!" }], max_tokens: 512 })) { console.log(chunk.choices[0].delta.content); } /// Using a third-party provider: await client.chatCompletion({ model: "meta-llama/Llama-3.1-8B-Instruct", messages: [{ role: "user", content: "Hello, nice to meet you!" }], max_tokens: 512, provider: "sambanova", // or together, fal-ai, replicate, cohere … }) await client.textToImage({ model: "black-forest-labs/FLUX.1-dev", inputs: "a picture of a green bird", provider: "fal-ai", }) // You can also omit "model" to use the recommended model for the task await client.translation({ inputs: "My name is Wolfgang and I live in Amsterdam", parameters: { src_lang: "en", tgt_lang: "fr", }, }); // pass multimodal files or URLs as inputs await client.imageToText({ model: 'nlpconnect/vit-gpt2-image-captioning', data: await (await fetch('https://picsum.photos/300/300')).blob(), }) // Using your own dedicated inference endpoint: https://hf.co/docs/inference-endpoints/ const gpt2Client = client.endpoint('https://xyz.eu-west-1.aws.endpoints.huggingface.cloud/gpt2'); const { generated_text } = await gpt2Client.textGeneration({ inputs: 'The answer to the universe is' }); // Chat Completion const llamaEndpoint = client.endpoint( "https://router.huggingface.co/hf-inference/models/meta-llama/Llama-3.1-8B-Instruct" ); const out = await llamaEndpoint.chatCompletion({ model: "meta-llama/Llama-3.1-8B-Instruct", messages: [{ role: "user", content: "Hello, nice to meet you!" }], max_tokens: 512, }); console.log(out.choices[0].message); ``` ### @huggingface/hub examples ```ts import { createRepo, uploadFile, deleteFiles } from "@huggingface/hub"; const HF_TOKEN = "hf_..."; await createRepo({ repo: "my-user/nlp-model", // or { type: "model", name: "my-user/nlp-test" }, accessToken: HF_TOKEN }); await uploadFile({ repo: "my-user/nlp-model", accessToken: HF_TOKEN, // Can work with native File in browsers file: { path: "pytorch_model.bin", content: new Blob(...) } }); await deleteFiles({ repo: { type: "space", name: "my-user/my-space" }, // or "spaces/my-user/my-space" accessToken: HF_TOKEN, paths: ["README.md", ".gitattributes"] }); ``` ### @huggingface/mcp-client example ```ts import { Agent } from '@huggingface/mcp-client'; const HF_TOKEN = "hf_..."; const agent = new Agent({ provider: "auto", model: "Qwen/Qwen2.5-72B-Instruct", apiKey: HF_TOKEN, servers: [ { // Playwright MCP command: "npx", args: ["@playwright/mcp@latest"], }, ], }); await agent.loadTools(); for await (const chunk of agent.run("What are the top 5 trending models on Hugging Face?")) { if ("choices" in chunk) { const delta = chunk.choices[0]?.delta; if (delta.content) { console.log(delta.content); } } } ``` There are more features of course, check each library's README! ## Formatting & testing ```console sudo corepack enable pnpm install pnpm -r format:check pnpm -r lint:check pnpm -r test ``` ## Building ``` pnpm -r build ``` This will generate ESM and CJS javascript files in `packages/*/dist`, eg `packages/inference/dist/index.mjs`. ", Assign "at most 3 tags" to the expected json: {"id":"10335","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"