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

# 将 OpenAI 连接到 Unsloth：在本地聊天中运行 GPT 模型

了解如何连接 OpenAI 模型，包括 GPT-5.5 到 [Unsloth](https://github.com/unslothai/unsloth) 这样你就可以在一个开源的本地 UI 聊天界面中与它们全部聊天。通过连接你的 OpenAI API 密钥，你可以在 Unsloth 中运行 GPT 模型，并使用诸如 [网页搜索](#web-search-and-thinking)、工具调用、 [代码执行](#code-execution), [图像生成](#image-generation)、可复用代码容器，以及 [提示缓存](#prompt-caching).

本指南将带你创建 OpenAI API 密钥、将 OpenAI 作为提供方连接、加载可用模型，并排查常见的设置问题。

### 设置

{% stepper %}
{% step %}

#### 创建 OpenAI API 密钥

从 [OpenAI 仪表板](https://platform.openai.com/api-keys).

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

{% step %}

#### 配置连接

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

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

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

{% step %}

#### 准备聊天

你启用的模型现在会显示在“选择模型”下拉菜单的“已连接”中。受支持的 GPT 模型可以提供额外控制，包括图像生成、思考、网页搜索和代码执行。

<figure><img src="/files/1952f178410c905ec63ebcb843c7b1dae8de802c" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

### 代码执行

启用后，受支持的 OpenAI 模型可以在提供方沙箱中运行代码，以解决问题、分析数据并处理文件。

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

{% columns %}
{% column width="50%" %}
OpenAI 使用可复用的 shell 容器。在 **代码执行** 设置中，你可以设置空闲超时、创建容器、选择活动容器、刷新列表或删除旧容器。

在新的线程中选择相同的容器，以继续使用其中的文件和状态。
{% endcolumn %}

{% column width="50%" %}

<figure><img src="/files/df95cb5301ddd817456b86bc231da14209b26c37" alt="" width="288"><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

### 提示缓存

{% columns %}
{% column width="66.66666666666666%" %}
当请求重复使用相同的长前缀时，提示缓存可降低延迟和成本。兼容的提供方和服务器（包括 OpenAI）都支持它。使用侧边栏中的 **提示缓存** 设置来控制受支持连接的缓存行为。
{% endcolumn %}

{% column width="33.33333333333334%" %}

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

{% endcolumn %}
{% endcolumns %}

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

### 网页搜索与思考

OpenAI 的受支持模型可使用提供方侧网页搜索。 **思考** 控件会根据所选模型进行适配：某些模型使用开/关切换，而推理努力型模型则使用模型特定的思考级别。&#x20;

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

### 图像生成

就像 GPT 一样，Unsloth 也支持图像生成。你可以点击“编辑图像”按钮并输入新提示来直接编辑图像，以优化或重新生成它。请求时会自动生成图像，但你可以关闭此行为。还提供下载按钮，允许你以原始全分辨率保存图像。

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

### 故障排查

如果 OpenAI 连接失败，请检查 API 密钥是否有效，以及是否属于正确的 OpenAI 账户。

如果点击后某个模型没有显示 **加载模型**，它可能不适用于你的账户。你可以手动输入模型 ID，或选择其他模型。


---

# 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/openai.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.
