Huggingface.js documentation
مكتبات Hugging Face بلغة JavaScript
// 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 بلغة JavaScript
هذه مجموعة من مكتبات JS للتفاعل مع واجهة Hugging Face API، مع تضمين أنواع TS.
- @huggingface/hub: التفاعل مع huggingface.co لإنشاء أو حذف المستودعات والالتزام / تنزيل الملفات
- @huggingface/inference: استخدم جميع مزوّدي الاستدلال (Inference Providers) المدعومين (بدون خادم) أو انتقل إلى نقاط نهاية الاستدلال (Inference Endpoints) (مخصصة) لإجراء استدعاءات إلى أكثر من 100,000 نموذج تعلّم آلي
- @huggingface/mcp-client: عميل بروتوكول سياق النموذج (MCP)، ومكتبة وكيل صغيرة، مبنية على InferenceClient.
- @huggingface/gguf: محلل GGUF يعمل على الملفات المستضافة عن بُعد.
- @huggingface/dduf: حزمة مشابهة لـ DDUF (تنسيق DDUF الموحّد لـ Diffusers)
- @huggingface/lerobot: محلّل لمجموعات بيانات روبوتات LeRobot، يقرأ تخطيطات الإصدار v2.x وv3.0 عبر طلبات HTTP النطاقية.
- @huggingface/usd-web-viewer: عارض OpenUSD للويب: يجمع ملفات
.usdو.usdaو.usdcو.usdzباستخدام WebAssembly ويعرضها باستخدام three.js. - @huggingface/tasks: ملفات التعريف والمصدر الأساسي للمفاهيم الأساسية الرئيسية في Hub مثل مهام pipeline ومكتبات النماذج وما إلى ذلك.
- @huggingface/jinja: تطبيق مبسّط بلغة JavaScript لمحرك القوالب Jinja، يُستخدم للقوالب الخاصة بالحوارات في التعلم الآلي.
- @huggingface/space-header: استخدام مساحة
mini_headerخارج Hugging Face - @huggingface/ollama-utils: مجموعة متنوعة من الأدوات المساعدة للحفاظ على توافق Ollama مع النماذج الموجودة على Hugging Face Hub.
- @huggingface/tiny-agents: مكتبة صغيرة وغير مرتبطة بنموذج محدد لبناء وكلاء ذكاء اصطناعي يمكنهم استخدام الأدوات.
نستخدم ميزات حديثة لتجنب استخدام أدوات التوافق (polyfills) والتبعيات، لذا فإن المكتبات ستعمل فقط على المتصفحات الحديثة / Node.js >= 18 / Bun / Deno.
لا تزال المكتبات في مراحلها الأولى، لذا نرجو مساعدتنا من خلال فتح تقارير عن المشكلات!
التثبيت
من NPM
لتثبيت المكتبات عبر NPM، يمكنك تنزيل المكتبات حسب الحاجة:
npm install @huggingface/hub npm install @huggingface/inference npm install @huggingface/mcp-client
ثم استورد المكتبات في كودك:
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";من CDN أو الاستضافة الثابتة
يمكنك تشغيل حزمنا باستخدام JS النقي، دون الحاجة إلى أي أداة حزم، من خلال استخدام CDN أو استضافة ثابتة. باستخدام وحدات ES، أي <script type="module">، يمكنك استيراد المكتبات في كودك:
<script type="module">
import { InferenceClient } from 'https://cdn.jsdelivr.net/npm/@huggingface/inference@4.13.32/+esm';
import { createRepo, commit, deleteRepo, listFiles } from "https://cdn.jsdelivr.net/npm/@huggingface/hub@2.17.10/+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"أمثلة على الاستخدام
احصل على رمز وصول HF الخاص بك في إعدادات الحساب.
أمثلة على @huggingface/inference
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://huggingface.co/proxy/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
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
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);
}
}
}توجد ميزات أخرى بالطبع، تحقق من ملف README الخاص بكل مكتبة!
تنسيق واختبار
sudo corepack enable pnpm install pnpm -r format:check pnpm -r lint:check pnpm -r test
البناء
pnpm -r buildسيؤدي هذا إلى إنشاء ملفات جافاسكربت بصيغتي ESM و CJS في packages/*/dist، مثل packages/inference/dist/index.mjs.