> 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).

你也可以将你的 ChatGPT/Codex 订阅直接连接到 Unsloth。

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

### 设置

{% stepper %}
{% step %}

#### 创建 OpenAI API 密钥

从以下位置创建 API 密钥： [OpenAI 控制台](https://platform.openai.com/api-keys).

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FNbLWLZa1OBYQrwX0mQVv%2Funsloth_openai_api.gif?alt=media&amp;token=28187d50-8638-4e60-ba33-729efd13f1c1" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 配置连接

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

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

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FlAXYV1wS2lxeS36Lo8Dj%2Fexport-1779457818713.gif?alt=media&amp;token=99c6f0b2-2fb1-40e4-a15a-325bcef8c2f7" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}

#### 准备聊天

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

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FgKCJ3zsZzZuDqBAigsfG%2FScreenshot%202026-05-26%20at%201.31.03%E2%80%AFAM.png?alt=media&amp;token=c8df4285-ba69-4091-8978-e4197f6a12d2" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}

### 代码执行

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

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fn65DzRsTGrflcqJBv67y%2FScreenshot%202026-05-26%20at%206.11.54%E2%80%AFAM.png?alt=media&amp;token=f620e467-2202-4843-ae16-e169a89f0a57" alt="" width="563"><figcaption></figcaption></figure>

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

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

{% column width="50%" %}

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FBR0zCMzeC4IwPSmlx12A%2Fimage.png?alt=media&amp;token=3d9e80ee-4ceb-4454-9620-165650a29c0a" alt="" width="288"><figcaption></figcaption></figure>
{% endcolumn %}
{% endcolumns %}

### 提示缓存

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

{% column width="33.33333333333334%" %}

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FKA6iU3qCFhUiq0KI16aU%2FScreenshot%202026-05-26%20at%203.28.44%E2%80%AFAM.png?alt=media&amp;token=41a43794-792d-48f4-8774-8b9d85702dfc" alt=""><figcaption></figcaption></figure>

{% endcolumn %}
{% endcolumns %}

<figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2Fq4csmTX5rS9isMkNkhTX%2FPrompt%20Caching%20Diagram%20(1).png?alt=media&amp;token=ade433bf-5eaf-4146-a266-525a85a6c98d" alt="" width="375"><figcaption></figcaption></figure>

### 网页搜索与思考

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

<div data-with-frame="true"><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FC0Ed4kzN9h6c0ogEn6NT%2Fwebsearch%20api.png?alt=media&amp;token=d3335222-1d9e-4021-9bf9-734c6acf1fc0" alt="" width="563"><figcaption></figcaption></figure></div>

### 图像生成

与 GPT 类似，Unsloth 也支持图像生成。你可以通过点击“编辑图像”按钮并输入新的提示词来直接编辑图像，以优化或重新生成它。请求后图像会自动生成，但你可以关闭此行为。系统还提供下载按钮，让你能够以原始全分辨率保存图像。

<div><figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2F5ZLU2BY19MN4yiYLx2Br%2FScreenshot%202026-05-26%20at%206.04.34%E2%80%AFAM.png?alt=media&amp;token=44327a8f-c14d-4541-9a02-f19f375b4326" alt=""><figcaption></figcaption></figure> <figure><img src="https://2657992854-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxhOjnexMCB3dmuQFQ2Zq%2Fuploads%2FDQ1bJ5KF8CiyItrfOrIt%2FScreenshot%202026-05-26%20at%206.06.07%E2%80%AFAM.png?alt=media&amp;token=ff6b00bc-0a2f-4375-a454-49429cf839f2" 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 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-cheng/connections/openai.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.
