> For the complete documentation index, see [llms.txt](https://unsloth.ai/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://unsloth.ai/docs/zh/ji-chu-zhi-shi/inference-and-deployment/deploying-llms-with-hugging-face-jobs.md).

# 使用 Hugging Face Jobs 部署 LLM

使用 Hugging Face jobs 和 skills，通过一个 SKILL 来结合 Codex / Claude Code 微调 LFM。

本指南介绍如何使用 [Unsloth](https://github.com/unslothai/unsloth) 和 [Liquid LFM2.5](/docs/zh/mo-xing/tutorials/lfm2.5.md) 借助类似于以下的编码代理进行快速 LLM 微调： [Claude Code](/docs/zh/ji-chu-zhi-shi/claude-code.md)。与标准方法相比，Unsloth 提供约 2 倍更快的训练速度和约 60% 更少的 VRAM 占用。

### 你将需要

* 一个 [Hugging Face](https://huggingface.co) 账户（HF Jobs 必需）
* 具有写入权限的 Hugging Face 令牌
* 一个编码代理（Open Code、Claude Code、Codex）
* 阅读我们的 [Claude Code](/docs/zh/ji-chu-zhi-shi/claude-code.md) 设置它们的指南。

### 安装技能

#### Claude Code

Claude Code 通过其 [插件系统](https://code.claude.com/docs/en/discover-plugins).

1. 添加市场：

```bash
/plugin marketplace add huggingface/skills
```

2. 在以下位置浏览可用技能： **Discover** 选项卡：

```bash
/plugin
```

3. 安装模型训练器技能：

```bash
/plugin install hugging-face-model-trainer@huggingface-skills
```

更多详情请参见 [Claude Code 插件文档](https://code.claude.com/docs/en/discover-plugins) 以及 [技能文档](https://code.claude.com/docs/en/skills).

#### Codex

Codex 通过以下方式发现技能： [`AGENTS.md`](https://developers.openai.com/codex/guides/agents-md) 文件和 [`.agents/skills/`](https://developers.openai.com/codex/skills) 目录。

**使用以下命令安装单个技能： `$skill-installer`**

{% code overflow="wrap" %}

```bash
$skill-installer install https://github.com/huggingface/skills/tree/main/skills/hugging-face-model-trainer
```

{% endcode %}

更多详情请参见 [Codex Skills 文档](https://developers.openai.com/codex/skills) 以及 [AGENTS.md 指南](https://developers.openai.com/codex/guides/agents-md).

### 快速开始

安装技能后，让你的编码代理去训练一个模型。我们使用 [Liquid LFM2.5](/docs/zh/mo-xing/tutorials/lfm2.5.md)

{% code overflow="wrap" %}

```
在 HF Jobs 上使用 Unsloth 训练 LiquidAI/LFM2.5-1.2B-Instruct，数据集为 trl-lib/Capybara
```

{% endcode %}

代理将基于技能中的一个 [示例](https://github.com/huggingface/skills/blob/main/skills/hugging-face-model-trainer/scripts/unsloth_sft_example.py)生成训练脚本，将训练提交到 HF Jobs，并通过 Trackio 提供监控链接。

### 使用 Hugging Face Jobs

训练任务将运行在 [Hugging Face Jobs](https://huggingface.co/docs/huggingface_hub/guides/jobs) ——全托管云 GPU。如果你熟悉 Google Colab 额度，Hugging Face Jobs 也提供类似的额度系统。它采用即用即付结构，或者你也可以提前获取额度。代理会：

1. 生成带内联依赖项的 UV 脚本
2. 通过以下方式将其提交到 HF Jobs： `hf` CLI
3. 报告任务 ID 和监控 URL
4. 训练后的模型会推送到你的 Hugging Face Hub 仓库

#### 示例训练脚本

该技能生成如下脚本：

{% code expandable="true" %}

```py
# /// script
# dependencies = ["unsloth", "trl>=0.12.0", "datasets", "trackio"]
# ///

from unsloth import FastLanguageModel
from trl import SFTTrainer, SFTConfig
from datasets import load_dataset

model, tokenizer = FastLanguageModel.from_pretrained(
    "Qwen/Qwen2.5-0.5B",
    load_in_4bit=True,
    max_seq_length=2048,
)

model = FastLanguageModel.get_peft_model(
    model,
    r=16,
    lora_alpha=32,
    lora_dropout=0,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj",
                    "gate_proj", "up_proj", "down_proj"],
)

dataset = load_dataset("trl-lib/Capybara", split="train")

trainer = SFTTrainer(
    model=model,
    tokenizer=tokenizer,
    train_dataset=dataset,
    args=SFTConfig(
        output_dir="./output",
        push_to_hub=True,
        hub_model_id="username/my-model",
        per_device_train_batch_size=4,
        gradient_accumulation_steps=4,
        num_train_epochs=1,
        learning_rate=2e-4,
        report_to="trackio",
    ),
)

trainer.train()
trainer.push_to_hub()
```

{% endcode %}

使用 Hugging Face Jobs 训练的费用如下：

| 模型大小     | 推荐 GPU       | 预计每小时成本 |
| -------- | ------------ | ------- |
| <1B 参数   | `t4-small`   | \~$0.40 |
| 1-3B 参数  | `t4-medium`  | \~$0.60 |
| 3-7B 参数  | `a10g-small` | \~$1.00 |
| 7-13B 参数 | `a10g-large` | \~$3.00 |

有关 Hugging Face 空间定价的完整概览，请查看指南 [这里](https://huggingface.co/docs/hub/en/spaces-overview#hardware-resources).

### 与编码代理协作的技巧

* 明确说明要使用的模型和数据集，并包含 Hub ID（例如， `Qwen/Qwen2.5-0.5B`, `trl-lib/Capybara`）。代理会搜索并验证这些组合。
* 如果你希望使用 Unsloth，请明确提及它。否则代理会根据模型和预算自行决定框架。
* 在启动大型任务前先询问成本估算
* 请求 Trackio 监控以获取实时损失曲线
* 在提交后让代理检查日志以查看任务状态

### 资源

* [Hugging Face Skills 仓库](https://github.com/huggingface/skills)

{% embed url="<https://youtu.be/Gh5P4niIFNA>" %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://unsloth.ai/docs/zh/ji-chu-zhi-shi/inference-and-deployment/deploying-llms-with-hugging-face-jobs.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
