# 如何使用 OpenAI Codex 运行本地 LLM

本分步指南将向你展示如何将开放的 LLM 和 API 连接到 OpenAI Codex **完全在本地**，并附有截图。Codex 只需要一个支持 OpenAI Responses API 的本地端点。可使用任何开放模型运行，如 Qwen、DeepSeek、Gemma 等。

在本教程中，我们将使用这些开放模型： [Gemma 4](/docs/zh/mo-xing/gemma-4.md) 和 [Qwen3.5](/docs/zh/mo-xing/qwen3.5.md) 它们是强大的代理式与编程模型（可在 24GB RAM/统一内存设备上运行）。对于推理，我们将使用 [Unsloth Studio](https://github.com/unslothai/unsloth) 和 [`llama.cpp`](https://github.com/ggml-org/llama.cpp) 可让你在 macOS、Linux 和 Windows 上运行/提供 LLM。你可以替换为任何其他模型，只需更新脚本和 Codex 配置中的模型名称。

<a href="/pages/b71ddea7924324c058a771e5e831c3cb6fc75b18#setup-codex" class="button primary" data-icon="openai">设置 Codex</a><a href="/pages/b71ddea7924324c058a771e5e831c3cb6fc75b18#quickstart-tutorials" class="button primary">📖 本地模型设置教程</a>

对于模型量化版本，我们将使用 Unsloth 的 [**动态 GGUF**](/docs/zh/ji-chu/unsloth-dynamic-2.0-ggufs.md) 这样你就可以运行量化后的 GGUF 模型，同时尽可能保留精度。

{% hint style="info" %}
自 2026 年 1 月以来，Codex 已发生了很大变化。它现在 [**OpenAI Responses API**](https://platform.openai.com/docs/api-reference/responses) **独占使用**，而且 Chat Completions 支持已被弃用。 [Unsloth Studio](#unsloth-tutorial) 同时支持两者，因此我们将使用 `wire_api = "responses"` 贯穿本指南。
{% endhint %}

### &#x20;<i class="fa-openai">:openai:</i> 设置 Codex

[Codex](https://github.com/openai/codex) 是 OpenAI 官方的本地运行编程代理。虽然它是为 ChatGPT 设计的，但它支持 **自定义 API 端点**，这使它能够用于本地 LLM。稍后当 Studio 启动后，我们会将它指向 Unsloth Studio 的 `/v1/responses` 端点。

{% tabs %}
{% tab title="Linux / WSL" %}
在终端中运行：

```bash
apt update
sudo apt install nodejs npm -y
npm install -g @openai/codex
```

{% endtab %}

{% tab title="Windows" %}
在 Windows Powershel 中运行：

```powershell
winget install --id OpenAI.Codex
```

{% hint style="info" %}
**更喜欢 Codex 桌面应用？** 从 Microsoft Store 安装：

```powershell
winget install --id 9PLM9XGG6VKS --source msstore
```

或者通过 [Microsoft 应用商店](https://apps.microsoft.com/detail/9plm9xgg6vks)。该应用读取相同的 `%USERPROFILE%\.codex\config.toml`，因此我们稍后设置的提供方配置两种方式都适用。
{% endhint %}

{% hint style="info" %}
**更喜欢 WSL？** 以管理员身份打开 PowerShell，运行 `wsl --install`，重启后，再在 Ubuntu 内按照上面的 Linux 选项卡操作。你需要一个小型网络技巧来访问运行在 Windows 主机上的 Studio——请参见“将 Codex 连接到 Studio”中的 WSL 提示。
{% endhint %}
{% endtab %}

{% tab title="MacOS" %}
在终端中运行：

<pre class="language-bash"><code class="lang-bash"><strong>bash brew install --cask codex
</strong></code></pre>

{% endtab %}
{% endtabs %}

安装部分就到这里—— **不要运行 `codex` 现在还不要**。直接运行它会把你带到 OpenAI 的“使用 ChatGPT 登录”选择器（这是模态界面——没有退出通道）。一旦我们连接好本地配置， `codex -p unsloth_api` 或者 `codex -p llama_cpp` 将完全跳过该界面，因为自定义提供方默认使用 `requires_openai_auth = false`。先启动本地模型服务器，然后再让 Codex 连接到它。

## 📖 快速开始教程 <a href="#quickstart-tutorials" id="quickstart-tutorials"></a>

在开始之前，我们首先需要完成你将要使用的特定模型的设置。我们使用 [Unsloth](https://unsloth.ai/docs/new/studio) （一个 Web UI）和 llama.cpp，它们都是可在 Mac、Linux、Windows 设备上运行和提供 LLM 的开源框架。

{% columns %}
{% column %}
在开始之前，我们首先需要完成你将要使用的特定模型的设置。我们使用 [Unsloth](/docs/zh/xin/studio.md) （一个 Web UI）和 llama.cpp，它们都是可在 Mac、Linux、Windows 设备上运行和提供 LLM 的开源框架。

Unsloth 还具有独特的自愈式 [工具调用](/docs/zh/xin/studio/chat.md#auto-healing-tool-calling) 和 [网页搜索](/docs/zh/xin/studio/chat.md#code-execution) 能力。右侧可见连接到 Unsloth 的 Claude Code：
{% endcolumn %}

{% column %}

<div data-with-frame="true"><figure><img src="/files/ce3073a68a308e67d9765fb9106097c0bcdecb1a" alt=""><figcaption></figcaption></figure></div>
{% endcolumn %}
{% endcolumns %}

<a href="/pages/b71ddea7924324c058a771e5e831c3cb6fc75b18#unsloth-tutorial" class="button primary">🦥 Unsloth 教程</a><a href="/pages/b71ddea7924324c058a771e5e831c3cb6fc75b18#llama.cpp-tutorial" class="button primary">🦙 llama.cpp 教程</a>

## 🦥 Unsloth 教程

在本教程中，我们将通过使用一个 UI 将本地模型提供/连接到 Claude Code，方法是使用 [Unsloth](https://github.com/unslothai/unsloth)。Unsloth 可在 Windows、WSL、Linux 和 MacOS 上运行。&#x20;

{% columns %}
{% column %}

* 搜索、下载、 [运行 GGUF](/docs/zh/xin/studio.md#run-models-locally) 以及 safetensor 模型
* [**自愈式** 工具调用](/docs/zh/xin/studio.md#execute-code--heal-tool-calling) + **网页搜索**
* [**代码执行**](/docs/zh/xin/studio.md#run-models-locally) （Python、Bash）
* [自动推理](/docs/zh/xin/studio.md#model-arena) 参数调优（temp、top-p 等）
* 通过 llama.cpp 实现快速 CPU + GPU 推理
* [训练 LLM](/docs/zh/xin/studio.md#no-code-training) 速度提升 2 倍且显存减少 70%

安装说明见下方：
{% endcolumn %}

{% column %}

<div data-with-frame="true"><figure><img src="/files/f8801eb887a6a996de4f745df66b4617f383d216" alt=""><figcaption><p>Qwen3.6 2-bit 在 Unsloth 中运行的示例。</p></figcaption></figure></div>
{% endcolumn %}
{% endcolumns %}

{% tabs %}
{% tab title="MacOS" %}

#### 第 1 步：设置 Unsloth

启动 `终端` （在 Mac 上），然后输入以下命令安装 Unsloth。

```bash
curl -fsSL https://unsloth.ai/install.sh | sh
```

Unsloth 将开始设置环境并安装所需的软件包，如下所示。当系统询问你是否允许现在启动 Studio 时，输入 **Y** 并按下 `Enter` 。这将在你的本地启动 Unsloth，使用的 **8888** 端口。

<figure><img src="/files/5f519c6be67e8e04d146b8890e77a97ea1d57b24" alt="" width="375"><figcaption></figcaption></figure>

{% hint style="info" %}
如果你在安装过程中选择不启动 Unsloth，你始终可以使用 `unsloth studio -p 8888` 来启动 Unsloth 应用。如果你希望 PC/计算机外部的客户端也能访问你的 Unsloth 实例，请添加 `-H 0.0.0.0` 到 `unsloth studio` 命令中。
{% endhint %}

#### 第 2 步：启动 Unsloth

打开你选择的浏览器并输入 `http://127.0.0.1:8888`  到 URL 栏中。如果这是你第一次安装 Unsloth，你会被转到密码页面，需要创建一个新密码。之后，Unsloth 应该会如下面所示打开到聊天页面。

<figure><img src="/files/752cee75840c3ad7dcf1d7c341a194e63895ac0e" alt="" width="375"><figcaption></figcaption></figure>
{% endtab %}

{% tab title="Windows" %}

#### 第 1 步：设置 Unsloth

打开开始菜单，搜索 `PowerShell`，然后启动它。复制并输入安装命令：

```powershell
irm https://unsloth.ai/install.ps1 | iex
```

它将自动开始安装。安装完成后，PowerShell 会询问你是否要启动 Unsloth Studi&#x6F;**.**

<figure><img src="/files/5f519c6be67e8e04d146b8890e77a97ea1d57b24" alt="" width="375"><figcaption></figcaption></figure>

你也可以使用以下命令启动它：

```bash
unsloth studio -H 0.0.0.0 -p 8888
```

{% hint style="info" %}
如果你希望你的实例可被 PC/计算机外部的客户端访问。\
添加 `-H 0.0.0.0` 到 `unsloth studio` 命令中。
{% endhint %}

#### 第 2 步：启动 Unsloth

打开 `http://127.0.0.1:8888` 在浏览器中。首次启动时，创建一个新密码以继续进入聊天页面。 **Unsloth Studio** 现已安装并可供使用。

<figure><img src="/files/752cee75840c3ad7dcf1d7c341a194e63895ac0e" alt="" width="375"><figcaption></figcaption></figure>
{% endtab %}

{% tab title="Linux、WSL" %}

#### 第 1 步：设置 Unsloth

{% tabs %}
{% tab title="Linux" %}
打开你的终端应用。你可以按 `Ctrl + Alt + T`来启动它，或者在系统的应用菜单中搜索 `终端` 。
{% endtab %}

{% tab title="WSL" %}
点击 Windows 开始菜单，输入你已安装的发行版名称（例如 `Ubuntu`），然后打开它。

{% hint style="warning" %}
在 **WSL**上，请确保你的 **NVIDIA 驱动程序** 已安装在 **Windows** 上（而不是在 WSL 内部），并且 **CUDA 工具包** 已安装在你的 WSL 发行版内部。详情请参见下方系统要求。
{% endhint %}
{% endtab %}
{% endtabs %}

要安装，请复制并运行安装命令：

```bash
curl -fsSL https://unsloth.ai/install.sh | sh
```

然后：

1. 点击终端窗口内部
2. 使用 `Ctrl + Shift + V`
3. 按下 `Enter`

Unsloth 将开始设置环境并安装所需的软件包，如下所示。当系统询问你是否允许现在启动 Studio 时，输入 **Y** 并按下 `Enter` 当系统询问你是否要允许现在启动 Studio 时。这将在你的本地启动 Unsloth，使用的 **8888** 端口。

<figure><img src="/files/6f5baa1b6cb53a7720aa0f8a23166f0ba32cb456" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
如果你在安装过程中选择不启动 Unsloth，你始终可以使用 `unsloth studio -p 8888` 来启动 Unsloth 应用。如果你希望 PC/计算机外部的客户端也能访问你的 Unsloth 实例，请添加 `-H 0.0.0.0` 到 `unsloth studio` 命令中。
{% endhint %}

#### 第 2 步：启动 Unsloth

打开你选择的浏览器并输入 `http://127.0.0.1:8888`  到 URL 栏中。如果这是你第一次安装 Unsloth，你会被转到密码页面，需要创建一个新密码。之后，Unsloth 应该会如下面所示打开到聊天页面。

<figure><img src="/files/2426649b2fc23e17ba8d2ed31e9afcf2ccc21ee0" alt="" width="375"><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

### 模型加载 + API 指南

{% stepper %}
{% step %}

#### 选择模型

在使用 API 之前，从 **选择模型** 下拉菜单中加载一个模型，该菜单位于聊天页面左上角。

<figure><img src="/files/e7c1a267b0c4f58689066eddfc57a2c2211f1e13" alt=""><figcaption></figcaption></figure>

在本指南中，我们将使用： `unsloth/gemma-4-26B-A4B-it-GGUF` 配合推荐的 `UD-Q4_K_XL` 量化版本。
{% endstep %}

{% step %}

#### 测试模型

在使用客户端之前，先发送一条快速消息：

<div data-with-frame="true"><figure><img src="/files/d1ef1d199c3aee2da86cc3da46a133801d2683ad" alt="" width="563"><figcaption></figcaption></figure></div>

{% hint style="info" %}
这可确认模型已正确加载并准备好响应。
{% endhint %}
{% endstep %}

{% step %}

#### **Unsloth API 密钥**

在 Studio 中，打开 **设置 → API** 以查看或创建你的 API 密钥。

<figure><img src="/files/7d43e8763d1ae72290485151822b1ea2e4fce42a" alt=""><figcaption></figcaption></figure>

请像对待密码一样对待你的 API 密钥，避免在截图或代码仓库中泄露它。
{% endstep %}
{% endstepper %}

## ⚙️ 连接 Codex

无论你使用的是 Unsloth Studio、llama.cpp 还是其他兼容 OpenAI 的本地服务器，这一部分都是一样的。Codex 需要三个值： **API 密钥**、 **基础 URL**，以及 **模型名称**。下面的示例使用 Unsloth Studio；对于 llama.cpp，请在 llama.cpp 部分中使用具有相同结构的 `llama_cpp` 配置文件。

{% stepper %}
{% step %}

#### **配置 Unsloth 提供方**

Codex 会在 `~/.codex/config.toml` （macOS/Linux/WSL）或 `%USERPROFILE%\.codex\config.toml` （Windows）中查找。创建或编辑它：

{% code title="\~/.codex/config.toml" overflow="wrap" %}

```toml
[model_providers.unsloth_api]
name                  = "Unsloth Studio"
base_url              = "http://localhost:8888/v1"
env_key               = "UNSLOTH_STUDIO_AUTH_TOKEN"
wire_api              = "responses"
requires_openai_auth  = false

[profiles.unsloth_api]
model_provider = "unsloth_api"
model          = "unsloth/gemma-4-26B-A4B"
```

{% endcode %}

{% hint style="info" %}
此配置会创建一个名为 `unsloth_api`的 Codex 配置文件，将其指向 Studio，并告诉 Codex 从名为 `UNSLOTH_STUDIO_AUTH_TOKEN`的环境变量中读取 API 密钥。你将在下一步设置真实密钥。
{% endhint %}

| 字段                     | 作用                                                               |
| ---------------------- | ---------------------------------------------------------------- |
| `base_url`             | 你的本地服务器端点 + `/v1`                                                |
| `env_key`              | **名称** ，即 Codex 用来读取你的 API 密钥的环境变量名。这不是密钥本身。                     |
| `wire_api`             | `responses`。Codex 现在仅使用 OpenAI 的 Responses API。                  |
| `requires_openai_auth` | `false` 会使 Codex 跳过此提供方的“使用 ChatGPT 登录”界面。默认已经是 `false`，但最好显式写出。 |
| `model`                | 你的服务器公开的模型 ID。请求 `GET <base_url>/models` 以确认精确字符串。               |

{% hint style="warning" %}
OpenAI 已移除 `wire_api = "chat"` 支持。始终使用 `wire_api = "responses"`。如果你设置了 `wire_api = "chat"`，Codex 将拒绝启动，并显示 `` `wire_api = "chat"` 已不再受支持。修复方法：在你的提供方配置中将 `wire_api = "responses"` 设置好。 ``
{% endhint %}

{% hint style="info" %}
你可以在同一个文件中添加多个配置文件，每个对应一个你在不同 Unsloth 模型之间切换时使用的配置。Codex 会自动识别它们。
{% endhint %}
{% endstep %}

{% step %}

#### 设置 API 密钥环境变量

使用你写在 `env_key`中的同一个环境变量名称。在上面的 Unsloth Studio 示例中， `env_key = "UNSLOTH_STUDIO_AUTH_TOKEN"`，所以请在你将运行 Codex 的同一个终端中设置 `UNSLOTH_STUDIO_AUTH_TOKEN` ：

{% code title="MacOS / Linux / WSL" %}

```bash
export UNSLOTH_STUDIO_AUTH_TOKEN=YOUR_TOKEN
```

{% endcode %}

{% code title="Windows PowerShell" %}

```powershell
$env:UNSLOTH_STUDIO_AUTH_TOKEN = "YOUR_TOKEN"
```

{% endcode %}

如果你重命名了 `env_key`，也请同步重命名命令中的变量。例如，一个使用 `env_key = "LLAMA_CPP_API_KEY"` 的 llama.cpp 配置文件需要 `LLAMA_CPP_API_KEY`，而不是 `UNSLOTH_STUDIO_AUTH_TOKEN`.

**会话 vs 持久化：** 上面的命令仅对当前终端生效。若要持久化：

* **MacOS / Linux / WSL：** 将 `export` 这一行添加到 `~/.bashrc` （bash）或 `~/.zshrc` （zsh）中。
* **Windows：** 运行 `setx UNSLOTH_STUDIO_AUTH_TOKEN "YOUR_TOKEN"` 一次，或将 `$env:` 这一行添加到你的 PowerShell `$PROFILE`.

{% hint style="warning" %}
**在 WSL 内运行 Codex，而 Unsloth 在 Windows 上？** WSL 是独立的网络命名空间，所以 `localhost` 在 WSL 内部无法访问 Unsloth。请编辑你的 `config.toml` 以改用 Windows 主机 IP：

```bash
# 在 WSL 内获取 Windows 主机 IP
ip route | grep default | awk '{print $3}'
```

然后设置 `base_url = "http://<that-ip>:8888/v1"`。如果你启用了 WSL2 镜像网络（`.wslconfig` → `networkingMode=mirrored`), `localhost` 其行为就与原生 Windows 相同。
{% endhint %}
{% endstep %}

{% step %}

#### **启动 Codex**

```bash
mkdir my-project && cd my-project
codex -p unsloth_api
```

{% hint style="info" %}
**在新目录中首次启动** Codex 会询问 *“你信任此目录中的内容吗？”* ——选择 *是，继续。* 这是按当前工作目录的信任提示，不是 ChatGPT 登录提示（后者会因为 \`requires\_openai\_auth = false\` 而被跳过）。之后在同一目录中启动将跳过此提示。
{% endhint %}

<figure><img src="/files/7da155068741d875b0bca7db32447dbe72b9e401" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
**看到 `模型元数据用于`  unsloth/gemma-4-26B-A4B  `未找到。正在回退到默认元数据`?** Codex 内置了一张表，用于记录 OpenAI 自家模型的上下文窗口、工具支持和输入模态。对于其他任何模型——它会回退到安全默认值。这个警告会在每个会话中对每个非 OpenAI slug 触发一次。一切仍然可以正常工作，你可以忽略它。

**要修复它：** 添加 `model_context_window = 131072` 到 `~/.codex/config.toml` 的顶部，这样 Codex 就会使用 Gemma 4 真实的 128K 上下文，而不是其回退猜测值。若要同时完全控制工具支持和输入模态，请将 `model_catalog_json` 在 `[profiles.unsloth_api]` 中指向一个包含你的 slug 自定义 `ModelInfo` 条目的 JSON 文件。
{% endhint %}

这个 `-p unsloth_api` 标志告诉 Codex 使用你刚添加的配置文件。模型名称会出现在 Codex 的状态栏中。

<figure><img src="/files/41a8451ff5a2020dcc71cf9bd6fd2f08d7693d23" alt=""><figcaption></figcaption></figure>

添加 `--search` 以启用网页搜索：

```bash
codex -p unsloth_api --search
```

绕过所有审批提示 **（警告：这会让 Codex 在没有任何审批的情况下按它自己的方式执行和运行代码！）**:

{% code overflow="wrap" %}

```bash
codex -p unsloth_api --search --dangerously-bypass-approvals-and-sandbox
```

{% endcode %}
{% endstep %}
{% endstepper %}

### 尝试一个真实任务

试试这个提示词，安装并运行一个简单的 Unsloth 微调：

{% code overflow="wrap" %}

```
你只能在 cwd project/ 中工作。不要搜索 AGENTS.md——这就是它。
通过 uv 在虚拟环境中安装 Unsloth。参见
https://unsloth.ai/docs/get-started/install/pip-install 了解方法（获取并阅读）。
然后执行一个简单的 Unsloth 微调运行，说明见
https://github.com/unslothai/unsloth。你可以使用 1 张 GPU。
```

{% endcode %}

如果我们再多等一会儿，你将看到一个使用 Unsloth 成功微调的模型！

<figure><img src="/files/5a701761b8bba389849a6a31fb55036ca4d3dbb8" alt=""><figcaption></figcaption></figure>

### 断开连接或还原

启动 Codex 时不要带 `-p unsloth_api` ，它就会使用默认提供方。或者从 `[profiles.unsloth_api]` 和 `[model_providers.unsloth_api]` 中删除这些 `~/.codex/config.toml`.

```bash
unset UNSLOTH_STUDIO_AUTH_TOKEN
```

你可以让 Unsloth Studio 保持运行，也可以关闭它。停止时它不会拦截任何内容。

### 故障排除

| 症状                                         | 可能原因                                 | 修复方法                                                                                  |
| ------------------------------------------ | ------------------------------------ | ------------------------------------------------------------------------------------- |
| `Model metadata for ... not found`         | 非 OpenAI slug，没有内置元数据                | 无害的警告。若要消除其副作用，请设置 `model_context_window = 131072` 在 `~/.codex/config.toml`，或指向       |
| Codex 说它是 GPT                              | Codex 会注入一个引用 OpenAI 的系统提示；本地模型会照着复述 | 这不是路由错误。请通过 Studio 的活动面板验证。若要改变自我报告内容，请覆盖系统提示。                                        |
| `连接被拒绝`                                    | Studio 未运行或端口错误                      | 确认 Studio 已在 `http://localhost:8888`启动；检查 `base_url` 在 `config.toml`                  |
| `wire_api = "chat" is no longer supported` | 遗留的 `wire_api = "chat"` 位于配置中        | 切换到 `wire_api = "responses"`                                                          |
| `找不到模型`                                    | 模型 ID 拼写错误                           | `GET http://localhost:8888/v1/models` 并复制精确的 ID                                       |
| 生成过程中 OOM                                  | 上下文对显存来说太大                           | 在 Studio 中减少上下文 **设置 → 推理**，或使用更小的量化版本                                                |
| Codex 显示“使用 ChatGPT 登录”选择器                 | 直接裸启动了 `codex` （没有 `-p`)             | 退出（Ctrl+C），然后使用 `codex -p unsloth_api`重新启动。自定义提供方会跳过那个                                |
| 工具调用不可靠                                    | 需要自愈回退                               | Studio 的 [自愈式工具调用](file:///1382377/new/studio/#execute-code--heal-tool-calling) 默认已开启 |
| WSL： `连接被拒绝` 到 `localhost`                 | WSL 网络命名空间                           | 在 `base_url`中使用 Windows 主机 IP，或启用 WSL2 镜像网络                                           |

## 🦙 Llama.cpp 教程

我们也可以直接使用 `llama.cpp` 。我们需要部署 `llama-server` ，这是一个用于在 Mac、Linux 和 Windows 设备上高效运行和提供 LLM 的开源框架。模型将通过 **端口 8001** 提供，所有代理工具调用都通过这个单一的兼容 OpenAI 的端点路由。

{% hint style="info" %}
llama.cpp 端点将位于 **端口 8001** 而不是 `8888` （Unsloth Studio 的默认值）。请相应调整你的 Codex `base_url` ，在 `~/.codex/config.toml`.
{% endhint %}

{% stepper %}
{% step %}

#### **安装 llama.cpp**

我们需要安装 `llama.cpp` 以部署/提供本地 LLM 供 Codex 使用。我们遵循官方构建说明，以获得正确的 GPU 绑定和最佳性能。如果你没有 GPU，或者只想使用 CPU 推理，请将 `-DGGML_CUDA=ON` 到 `改为 -DGGML_CUDA=OFF` 。 **对于 Apple Mac / Metal 设备**，设置 `改为 -DGGML_CUDA=OFF` 然后照常继续——默认已启用 Metal 支持。

```bash
apt-get update
apt-get install pciutils build-essential cmake curl libcurl4-openssl-dev git-all -y
git clone https://github.com/ggml-org/llama.cpp
cmake llama.cpp -B llama.cpp/build \
    -DBUILD_SHARED_LIBS=OFF -DGGML_CUDA=ON
cmake --build llama.cpp/build --config Release -j --clean-first \
    --target llama-cli llama-mtmd-cli llama-server llama-gguf-split
cp llama.cpp/build/bin/llama-* llama.cpp
```

{% endstep %}

{% step %}

#### **在本地下载并使用模型**

通过 `hf` CLI 下载模型（`pip install huggingface_hub hf_transfer`）。我们使用 **UD-Q4\_K\_XL** 量化版本，以获得最佳的大小/精度平衡。你可以在我们的 [这里的合集](file:///1382377/get-started/unsloth-model-catalog.md)中找到所有 Unsloth GGUF 上传内容。如果下载卡住，请参见 [https://hugging-face-hub-xet-debugging.md](https://hugging-face-hub-xet-debugging.md "mention").

```bash
hf download unsloth/gemma-4-26B-A4B-it-GGUF \
    --local-dir unsloth/gemma-4-26B-A4B-it-GGUF \
    --include "*UD-Q4_K_XL*"
```

{% hint style="info" %}
**想要视觉支持吗？** 添加 `--include "*mmproj-BF16*"` 以便同时拉取视觉投影器，然后传入 `--mmproj unsloth/gemma-4-26B-A4B-it-GGUF/mmproj-BF16.gguf` 到 `llama-server`。Codex 本身仅支持文本，因此这是可选的。
{% endhint %}

{% hint style="success" %}
我们使用了 `unsloth/gemma-4-26B-A4B-it-GGUF`，但你也可以使用任何类似于 `unsloth/Qwen3.6-35B-A3B-GGUF` - 参见 [Qwen3.6-35B-A3B](/docs/zh/mo-xing/qwen3.6.md).
{% endhint %}
{% endstep %}

{% step %}

#### **启动 Llama-server**

为了为智能体工作负载部署 Gemma-4-26B-A4B，我们使用 `llama-server`。我们应用 Google 推荐的采样参数（`temp 1.0`, `top_p 0.95`, `top_k 64`）并启用 `--jinja` 以获得正确的工具调用支持。

在新的终端中运行此命令（使用 `tmux` 或打开一个新的终端）。以下配置应该 **能轻松适配 24GB 显存的 GPU（RTX 4090）** 在约 18GB 下。 `--fit on` 也会自动卸载，但如果你看到性能不佳，请减小 `--ctx-size`.

```bash
./llama.cpp/llama-server \
    --model unsloth/gemma-4-26B-A4B-it-GGUF/gemma-4-26B-A4B-it-UD-Q4_K_XL.gguf \
    --alias "unsloth/gemma-4-26B-A4B" \
    --temp 1.0 \
    --top-p 0.95 \
    --top-k 64 \
    --port 8001 \
    --kv-unified \
    --cache-type-k q8_0 --cache-type-v q8_0 \
    --batch-size 4096 --ubatch-size 1024
```

{% hint style="info" %}
我们使用了 `--cache-type-k q8_0 --cache-type-v q8_0` 用于 KV 缓存量化以减少 VRAM 占用。如果你看到质量下降，请改用 `bf16` 代替（`--cache-type-k bf16 --cache-type-v bf16`），但 VRAM 占用会翻倍。
{% endhint %}

{% hint style="success" %}
**禁用 thinking** 可以提升智能体编码任务的性能。Gemma 4 通过聊天模板默认启用 thinking——要禁用它，请将以下标志添加到 llama-server 命令中：

**MacOS / Linux / WSL：**

`--chat-template-kwargs '{"enable_thinking":false}'`

**Windows PowerShell：**

`--chat-template-kwargs "{\"enable_thinking\":false}"`
{% endhint %}
{% endstep %}

{% step %}

#### **将 Codex 指向 8001 端口**

编辑你的 `~/.codex/config.toml` 以使用 llama-server 端口：

{% code title="\~/.codex/config.toml" %}

```toml
[model_providers.llama_cpp]
name      = "llama.cpp"
base_url  = "http://localhost:8001/v1"
env_key   = "LLAMA_CPP_API_KEY"
wire_api  = "responses"

[profiles.llama_cpp]
model_provider = "llama_cpp"
model          = "unsloth/gemma-4-26B-A4B"
```

{% endcode %}

然后使用新配置启动：

```bash
codex -p llama_cpp
```

由于 llama-server 不需要真实的密钥，你可以将身份验证令牌设置为任意值：

{% code title="MacOS / Linux / WSL" %}

```bash
export LLAMA_CPP_API_KEY=sk-no-key-required
```

{% endcode %}

{% code title="Windows PowerShell" %}

```powershell
$env:LLAMA_CPP_API_KEY = "sk-no-key-required"
```

{% endcode %}
{% endstep %}
{% endstepper %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://unsloth.ai/docs/zh/ji-chu/codex.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
