> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monostate.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 安装

> 在您的系统上设置 AI Training

# 安装 AI Training

本指南将引导您在计算机上安装 AI Training。该过程大约需要 5-10 分钟。

## 您将获得什么

当您使用 `pip` 或 `uv` 安装 AI Training 时，您将获得：

* **命令行界面** - 用于训练模型的 `aitraining` 命令
* **聊天界面** - 通过浏览器测试和与训练模型交互
* **Python API** - 在您的 Python 脚本中导入和使用

所有界面都包含在单个软件包安装中。

## 先决条件

在安装 AI Training 之前，您的系统上需要 Python。Python 是 AI Training 运行的编程语言。

### 检查是否已安装 Python

打开您的终端（Mac）或命令提示符（Windows）并输入：

```bash theme={null}
python --version
```

如果您看到像 `Python 3.8.0` 或更高版本的版本号，您就可以开始了。AI Training 需要 Python 3.8 或更新版本（推荐 3.10+）。

如果您收到错误或版本较旧，请按照下面的安装步骤操作。

## 安装 Python

<Tabs>
  <Tab title="Windows">
    ### Windows 安装

    1. **下载 Python**
       * 访问 [python.org/downloads](https://www.python.org/downloads/)
       * 点击黄色的"Download Python"按钮
       * 保存安装程序

    2. **运行安装程序**
       * 双击下载的文件
       * **重要**：勾选底部的"Add Python to PATH"
       * 点击"Install Now"
       * 等待安装完成

    3. **验证安装**
       * 打开命令提示符（在开始菜单中搜索"cmd"）
       * 输入：`python --version`
       * 您应该看到 Python 版本

    4. **安装 pip（如需要）**
       * pip 通常随 Python 一起提供，但请验证：
       ```bash theme={null}
       pip --version
       ```
       * 如果未找到，运行：
       ```bash theme={null}
       python -m ensurepip --upgrade
       ```
  </Tab>

  <Tab title="Mac">
    ### Mac 安装

    1. **使用 Homebrew（推荐）**

       如果您没有 Homebrew，请先安装它：

       ```bash theme={null}
       /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
       ```

       然后安装 Python：

       ```bash theme={null}
       brew install python@3.11
       ```

    2. **替代方案：直接下载**
       * 访问 [python.org/downloads](https://www.python.org/downloads/)
       * 下载 macOS 安装程序
       * 运行安装程序

    3. **验证安装**
       * 打开终端（在应用程序 > 实用工具中找到）
       * 输入：`python3 --version`
       * 您应该看到 Python 版本

    4. **安装 pip（如需要）**
       * pip 通常随 Python 一起提供，但请验证：
       ```bash theme={null}
       pip3 --version
       ```
  </Tab>

  <Tab title="Linux">
    ### Linux 安装

    大多数 Linux 发行版都包含 Python。如果没有：

    **Ubuntu/Debian:**

    ```bash theme={null}
    sudo apt update
    sudo apt install python3 python3-pip
    ```

    **Fedora:**

    ```bash theme={null}
    sudo dnf install python3 python3-pip
    ```

    **Arch:**

    ```bash theme={null}
    sudo pacman -S python python-pip
    ```
  </Tab>
</Tabs>

## 安装 AI Training

Python 准备就绪后，您有两种安装 AI Training 的选项。我们推荐使用 `uv` 以获得更快的安装速度。

### 选项 1：使用 uv（推荐）

uv 是一个现代的 Python 软件包安装程序，比 pip 快得多，并自动处理虚拟环境。

#### 首先安装 uv：

<Tabs>
  <Tab title="Windows">
    ```bash theme={null}
    # 使用 PowerShell（作为管理员）
    powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
    ```
  </Tab>

  <Tab title="Mac/Linux">
    ```bash theme={null}
    curl -LsSf https://astral.sh/uv/install.sh | sh
    ```
  </Tab>
</Tabs>

#### 然后安装 AI Training：

```bash theme={null}
# uv 自动创建和管理虚拟环境
uv pip install aitraining
```

就是这样！uv 会自动为您处理虚拟环境。

### 选项 2：使用 pip（传统方法）

如果您更喜欢传统方法或无法安装 uv：

#### 步骤 1：创建虚拟环境

虚拟环境将 AI Training 的文件与其他 Python 项目分开。

<Tabs>
  <Tab title="Windows">
    ```bash theme={null}
    python -m venv aitraining-env
    aitraining-env\Scripts\activate
    ```
  </Tab>

  <Tab title="Mac/Linux">
    ```bash theme={null}
    python3 -m venv aitraining-env
    source aitraining-env/bin/activate
    ```
  </Tab>
</Tabs>

当环境激活时，您会在终端中看到 `(aitraining-env)`。

#### 步骤 2：安装 AI Training

```bash theme={null}
pip install aitraining
```

这将从 PyPI 下载并安装 AI Training 及其所有依赖项（包括 PyTorch）。

### 步骤 3：验证安装

检查一切是否正确安装：

```bash theme={null}
aitraining --version
```

您应该看到 AI Training 版本号。

## 选择您的界面

AI Training 提供三种工作方式：

<CardGroup cols={3}>
  <Card title="命令行" icon="terminal" href="/cli/installation-setup">
    **最适合训练**

    使用命令训练模型。适合脚本和自动化。

    开始使用：`aitraining --help`
  </Card>

  <Card title="聊天界面" icon="message" href="/chat/launching">
    **最适合测试**

    在浏览器中交互式测试您的训练模型。

    开始使用：`aitraining chat`
  </Card>

  <Card title="Python API" icon="code" href="/api/introduction">
    **最适合开发者**

    将训练集成到您的 Python 应用程序中。

    开始使用：`from autotrain.trainers.clm.params import LLMTrainingParams`
  </Card>
</CardGroup>

## 可选依赖项

某些功能需要额外的软件包：

| 功能                | 软件包            | 安装                       | 平台                                     |
| ----------------- | -------------- | ------------------------ | -------------------------------------- |
| 更快的 LoRA 训练       | `unsloth`      | `pip install unsloth`    | Linux（仅 SFT，llama/mistral/gemma/qwen2） |
| Flash Attention 2 | `flash-attn`   | `pip install flash-attn` | Linux (CUDA)                           |
| 分布式训练             | `accelerate`   | 已包含                      | Linux/Mac/Windows                      |
| DeepSpeed Zero-3  | `deepspeed`    | `pip install deepspeed`  | Linux                                  |
| 4/8 位量化           | `bitsandbytes` | 已包含                      | **仅 Linux**                            |

<Note>
  **Accelerate 是必需的**用于多 GPU 和分布式训练。它已包含在 AITraining 中。DeepSpeed 是可选的，为非常大的模型提供 Zero-3 内存优化。
</Note>

<Warning>
  **量化（int4/int8）仅在 Linux 上工作。** `--quantization int4` 或 `--quantization int8` 所需的 `bitsandbytes` 库在 Windows 或 macOS 上不可用。
</Warning>

### GPU 设置（可选）

对于 NVIDIA GPU 训练，您可能需要使用特定的 CUDA 版本重新安装 PyTorch：

```bash theme={null}
# CUDA 11.8
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

# CUDA 12.1
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
```

### Weights & Biases 设置（可选）

AITraining 默认使用 W\&B 进行日志记录。要启用：

```bash theme={null}
pip install wandb
wandb login
```

或设置环境变量：`WANDB_API_KEY=your_key`

## 常见问题

<AccordionGroup>
  <Accordion title="'python' 无法识别（Windows）">
    Python 不在您的系统 PATH 中。尝试：

    * 使用 `python3` 而不是 `python`
    * 重新安装 Python 并勾选"Add Python to PATH"
    * 在系统环境变量中手动将 Python 添加到 PATH
  </Accordion>

  <Accordion title="权限被拒绝错误">
    在 Mac/Linux 上，您可能需要使用 `sudo`：

    ```bash theme={null}
    sudo pip install aitraining
    ```

    或者更好的是，使用虚拟环境（如上所示）。
  </Accordion>

  <Accordion title="pip: 找不到命令">
    pip 未随 Python 安装。安装它：

    ```bash theme={null}
    python -m ensurepip --upgrade
    ```

    或在 Linux 上：

    ```bash theme={null}
    sudo apt install python3-pip
    ```
  </Accordion>

  <Accordion title="PyTorch 安装失败">
    * 确保您有足够的磁盘空间（PyTorch 很大）
    * 尝试先升级 pip：`pip install --upgrade pip`
    * 对于 GPU 支持，确保安装了 CUDA 驱动程序
  </Accordion>
</AccordionGroup>

## 下一步

安装完成！现在您已准备好训练您的第一个模型：

<Card title="快速开始教程" icon="play" href="/foundations/quickstart">
  在 10 分钟内训练您的第一个 AI 模型
</Card>

## 需要帮助？

* 加入我们的 [Discord 社区](https://discord.gg/monostate) 获取支持
* 查看 [GitHub issues](https://github.com/monostate/aitraining) 了解已知问题
* 发送邮件至 [support@monostate.ai](mailto:support@monostate.ai) 获取直接帮助
