huggingface.js

huggingfaceMIT活跃维护

Hugging Face 官方 JavaScript 客户端库,让前端和 Node.js 项目直接调用 Hub 模型管理、推理 API 等能力,无需自己封装 HTTP 请求。

Use Hugging Face with JavaScript

tasks-v0.22.4发布于 2026-11-06 20:54 UTC2,605star +4 / 7 天1,060fork同步于 2026-11-10 11:43 UTC

怎么上手

由 README 提炼,执行前请确认
npm install @huggingface/hub
npm install @huggingface/inference
硬件要求按 README 说明

README 未说明;库本身只做 API 调用,算力在 Hugging Face 服务端,本地仅需现代浏览器或 Node.js >= 18 / Bun / Deno。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

在 JS/TS 里调用 Hugging Face Hub 与推理服务的官方库集合

Hugging Face 官方 JavaScript 客户端库,让前端和 Node.js 项目直接调用 Hub 模型管理、推理 API 等能力,无需自己封装 HTTP 请求。

核心能力

  • 用 @huggingface/hub 创建仓库、上传下载文件
  • 用 @huggingface/inference 调用多家推理提供商与专用端点
  • 内置 TS 类型,支持流式 chatCompletion
  • 提供 MCP 客户端与 tiny-agents 构建带工具的 Agent
  • 附带 GGUF、DDUF、LeRobot 等格式解析包

容易踩的坑

需要自行在账号设置里获取 HF access token 并传入;库依赖现代运行时特性,Node 需 >= 18,旧环境无 polyfill;各功能分散在多个子包,用到哪个要单独安装。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本tasks-v0.22.4
  1. tasks-v0.22.4最新正式版2026-11-06 · 2026-11-06 20:54 UTC

    这个版本没有写更新说明。

  2. tasks-v0.22.32026-11-06 · 2026-11-06 17:53 UTC

    这个版本没有写更新说明。

  3. usd-web-viewer-v0.2.12026-11-02 · 2026-11-02 20:37 UTC

    这个版本没有写更新说明。

  4. lerobot-v0.0.142026-11-02 · 2026-11-02 14:50 UTC

    这个版本没有写更新说明。

  5. tasks-v0.22.22026-10-29 · 2026-10-29 10:26 UTC

    这个版本没有写更新说明。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译
    // 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/hub: Interact with huggingface.co to create or delete repos and commit / download files
    • @huggingface/inference: Use all supported (serverless) Inference Providers or switch to Inference Endpoints (dedicated) to make calls to 100,000+ Machine Learning models
    • @huggingface/mcp-client: A Model Context Protocol (MCP) client, and a tiny Agent library, built on top of InferenceClient.
    • @huggingface/gguf: A GGUF parser that works on remotely hosted files.
    • @huggingface/dduf: Similar package for DDUF (DDUF Diffusers Unified Format)
    • @huggingface/lerobot: A parser for LeRobot robotics datasets, reading v2.x and v3.0 layouts over ranged HTTP requests.
    • @huggingface/usd-web-viewer: An OpenUSD viewer for the web: composes .usd, .usda, .usdc and .usdz files in WebAssembly and renders them with three.js.
    • @huggingface/tasks: The definition files and source-of-truth for the Hub's main primitives like pipeline tasks, model libraries, etc.
    • @huggingface/jinja: A minimalistic JS implementation of the Jinja templating engine, to be used for ML chat templates.
    • @huggingface/space-header: Use the Space mini_header outside Hugging Face
    • @huggingface/ollama-utils: Various utilities for maintaining Ollama compatibility with models on the Hugging Face Hub.
    • @huggingface/tiny-agents: A tiny, model-agnostic library for building AI agents that can use tools.

    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:

    npm install @huggingface/hub
    npm install @huggingface/inference
    npm install @huggingface/mcp-client
    

    Then import the libraries in your code:

    import { createRepo, commit, deleteRepo, listFiles } from "@huggingface/hub";
    import { InferenceClient } from "@huggingface/inference";
    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, i.e. <script type="module">, you can import the libraries in your code:

    <script type="module">
        import { InferenceClient } from 'https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm';
        import { createRepo, commit, deleteRepo, listFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/[email protected]/+esm";
    </script>
    

    Deno

    // 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.

    @huggingface/inference examples

    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

    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

    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

    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.