> 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-cheng/connections.md).

# 将 API 提供商和模型服务器连接到 Unsloth

了解如何通过一个本地 UI 界面使用 [Unsloth](/docs/zh/xin-de/studio.md)，一个用于运行和训练 LLM 的开源仓库。

{% columns %}
{% column %}
连接后，你就可以在同一个 Unsloth 聊天界面中运行模型，使用代码执行、工具调用、图像生成和其他功能；该界面同时适用于本地模型和云端模型。

Unsloth 独家支持 [提示缓存](#prompt-caching) （可在不降低准确性的情况下为你节省大量 token），同时保留对提供方原生能力的访问，例如 OpenAI 内置的 [网页搜索](#web-search-and-thinking) 和 [代码执行](#code-execution).
{% endcolumn %}

{% column %}
{% embed url="<https://github.com/user-attachments/assets/70456395-e016-4273-8256-35adb206267e>" %}
{% endcolumn %}
{% endcolumns %}

### 连接

连接分为两类：替你运行模型的托管 API 提供商，以及由你运行或控制的模型服务器。

**云提供商 -** 使用账户 API 密钥的托管 API：

| 连接         | 功能                   | 设置指南                                                             |
| ---------- | -------------------- | ---------------------------------------------------------------- |
| OpenAI     | 图像、搜索、代码、思考          | [OpenAI →](/docs/zh/ji-cheng/connections/openai.md)              |
| Anthropic  | 图像、搜索、代码、思考          | [Anthropic →](/docs/zh/ji-cheng/connections/anthropic-claude.md) |
| OpenRouter | 通过一个 API 密钥访问许多托管模型。 | [OpenRouter →](/docs/zh/ji-cheng/connections/openrouter.md)      |

**模型服务器 -** 在本地、你的网络中或你的远程机器上运行的推理服务器：

| 服务器       | 说明            | 指南                                                                                                                      |
| --------- | ------------- | ----------------------------------------------------------------------------------------------------------------------- |
| Llama.cpp | 高效的 GGUF 模型服务 | [Llama.cpp →](/docs/zh/ji-cheng/connections/jiang-llama.cpp-lian-jie-dao-unsloth-shi-yong-llamaserver-yun-xing-gguf.md) |
| vLLM      | 高吞吐量服务        | [vLLM →](/docs/zh/ji-cheng/connections/vllm.md)                                                                         |
| Ollama    | 简单的本地模型服务器    | [Ollama →](/docs/zh/ji-cheng/connections/ollama.md)                                                                     |

### 快速开始

要运行外部提供商的模型，请添加 API 密钥并选择 Unsloth 应显示哪些模型。在此示例中，我们将使用 [OpenAI](https://platform.openai.com/api-keys)。相同的设置也适用于 Anthropic 和其他提供商。

{% stepper %}
{% step %}

#### 创建 API

从提供商的控制面板创建一个新的 API 密钥并复制它。

<figure><img src="/files/d6de545bddd5a99f3ef7d177d5c153ccb2c6ef14" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 设置 Unsloth Studio

现在我们需要安装并设置 [Unsloth](/docs/zh/xin-de/studio.md)，这将使你能够在 UI 界面中运行云端模型。 [请看这里](/docs/zh/xin-de/studio/install.md) 以获取更详细的说明。

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

#### 步骤 1：设置 Unsloth

启动 `终端` ，然后通过输入下面的命令安装 Unsloth。

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

环境和所需软件包现在将被安装。输入 `Y` 并在提示时按 Enter 继续。设置完成后，服务器将在本地端口 `8888`.

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

{% hint style="info" %}
如果你在安装过程中跳过了启动应用，可以稍后使用以下命令启动它： `unsloth studio -p 8888`。若要允许你网络中的其他设备连接，请使用 `unsloth studio -H 0.0.0.0 -p 8888` 替代。
{% endhint %}

#### 步骤 2：启动 Unsloth

打开你喜欢的浏览器并输入 `http://127.0.0.1:8888`  到 URL 输入框中。如果这是你第一次安装 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 将开始设置环境并安装所需的软件包，如下所示。输入 **Y** 并按 `Enter` 当被询问是否允许 Unsloth 立即启动时。这将把 Unsloth 启动在你的本地 **8888** 端口上。

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

{% hint style="info" %}
如果你在安装过程中选择不启动 Unsloth，也可以随时使用以下命令启动 Unsloth 应用： `unsloth studio -p 8888` 。如果你希望你的 Unsloth 实例可被 PC/计算机外部的客户端访问，请添加 `-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 %}
{% endstep %}

{% step %}

#### 配置连接

接下来，将你的提供商连接到 Unsloth。

1. 打开 **设置** → **连接**，然后点击 **添加连接。**
2. 选择你要添加的提供商，然后粘贴你之前复制的 API 密钥。
3. 点击 **重新加载模型** 以刷新列表，显示你账户可用的模型。
4. 选择你要启用的模型，然后点击保存。&#x20;

<figure><img src="/files/a0ff542ae56e9f58b29f9ddcc71b0efd0c74cdd5" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 准备聊天

你启用的模型现在将显示在 **已连接** 在 **选择模型** 下拉菜单中。

<figure><img src="/files/1952f178410c905ec63ebcb843c7b1dae8de802c" alt="" width="563"><figcaption></figcaption></figure>

Unsloth 会动态显示适用于不同模型的兼容推理级别和生成控制。
{% endstep %}
{% endstepper %}

### 连接模型服务器

按此流程用于 [**llama.cpp**](/docs/zh/ji-cheng/connections/jiang-llama.cpp-lian-jie-dao-unsloth-shi-yong-llamaserver-yun-xing-gguf.md), [**vLLM**](/docs/zh/ji-cheng/connections/vllm.md)，以及 [**Ollama**](/docs/zh/ji-cheng/connections/ollama.md)。启动或找到你要连接的服务器。 &#x20;

{% tabs %}
{% tab title="llama.cpp " %}
启动 `llama-server` 并使用你想要提供服务的模型：

```bash
llama-server \\
  --model /path/to/model.gguf \\
  --host 0.0.0.0 \\
  --port 8080
```

这会公开一个 API 端点： `http://localhost:8080/v1`

若要要求 API 密钥，请添加：

```bash
--api-key 1234-myapi-key
```

{% endtab %}

{% tab title="vLLM" %}
启动 `vLLM` 使用你想要提供服务的模型的服务器：

```bash
  vllm serve unsloth/gemma-4-26B-A4B-it \\
  --dtype auto \\
```

若要要求 API 密钥，请添加：

```bash
  --api-key token-abc123
```

这会公开一个 API 端点： `http://localhost:8000/v1`
{% endtab %}

{% tab title="Ollama" %}
启动 `Ollama`，然后拉取你要使用的模型：

```bash
ollama serve
ollama pull qwen3:14b
```

这会公开一个 API 端点： `http://localhost:11434/v1`
{% endtab %}
{% endtabs %}

{% columns %}
{% column %}
现在我们可以连接模型服务器了。打开 **设置 → 连接**，然后点击 **添加提供商**.

选择 llama.cpp、vLLM 或 Ollama，然后粘贴服务器 **基础 URL**.

* llama.cpp 示例： `http://localhost:8080/v1`
* Ollama 示例： `http://localhost:11434/v1`
  {% endcolumn %}

{% column %}

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

点击 **加载模型** 以获取可用的模型 ID；如果你的服务器未公开则可手动输入模型 ID `/models`.

然后，在你点击 **添加提供商后，** 你启用的模型现在将显示在 **外部** 在 **选择模型** 下拉菜单中。

### 代码执行

启用后，受支持的 OpenAI 和 Anthropic 模型可以在提供商的沙箱中运行代码，以解决问题、分析数据并处理文件。\
\
Anthropic 模型使用 Claude 侧的提供商代码执行工具。OpenAI 使用可重复使用的容器，你可以在设置中创建、删除并从中选择。 **代码执行** 设置。若要继续使用其文件和状态，请在新线程中选择同一个容器。

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

### 提示缓存

当请求重复使用相同的长前缀时，提示缓存可降低延迟和成本。它受兼容的提供商和服务器支持，包括 OpenAI、Anthropic 和 llama.cpp。使用 **提示缓存** 侧边栏中的设置来控制受支持连接的缓存行为。

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

对于 llama.cpp，提示缓存默认启用，并可在启动时通过以下方式禁用 `llama-server` ：

```bash
--no-cache-prompt
```

### 网页搜索与思考

提供商侧网页搜索适用于 OpenAI、Anthropic、OpenRouter、Mistral、Gemini 和 Kimi 的受支持模型。Think 控件会根据所选模型进行调整：某些模型使用开/关切换，而推理强度模型使用模型特定的思考级别。

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

### 图像生成

就像 GPT 和 Gemini 一样，Unsloth 也支持图像生成。你可以通过点击“编辑图像”按钮并输入新的提示词来直接编辑图像，以优化或重新生成它。系统会在需要时自动生成图像，但你也可以关闭此行为。还提供下载按钮，可让你以原始完整分辨率保存图像。

<div><figure><img src="/files/992af313182ce1bfb26abe80be33754c4a620bca" alt=""><figcaption></figcaption></figure> <figure><img src="/files/a322cfb7cd9de5a40a53f03a07bb05985985d524" alt=""><figcaption></figcaption></figure></div>

### 故障排除

如果某个提供商连接失败，请检查 API 密钥是否属于所选提供商，并且是否有权访问你选择的模型。

如果某个模型在点击后没有出现 **重新加载模型**，则它可能不适用于你的账户。你仍然可以使用 Unsloth 的默认模型列表，或选择其他模型。


---

# 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 dynamically by asking a question.

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

```
GET https://unsloth.ai/docs/zh/ji-cheng/connections.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
