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

# VS Code

> 通过 BYOK 将 VS Code Copilot Chat 接入 HaiRoute

VS Code 支持通过内置的语言模型管理功能，为 Copilot Chat 添加自定义模型。配置完成后，你可以在 VS Code Chat 面板中直接使用 HaiRoute 模型。

## 协议说明

VS Code 的自定义模型（Custom Endpoint）支持 OpenAI Chat Completions 和 OpenAI Responses 两种 API 类型。HaiRoute 均已支持：选择 **Chat Completions** 时使用 `https://api.hairoute.ai/v1/chat/completions`；选择 **Responses** 时使用 `https://api.hairoute.ai/v1/responses`。

## 先准备好这些信息

* 已启用 GitHub Copilot 的 VS Code
* HaiRoute API Key
* 至少一个可用模型 ID，例如 `gpt-5.4`
* 与所选 API 类型匹配的 HaiRoute 完整请求地址

## 接入步骤

### 步骤 1：打开语言模型管理界面

在 VS Code Chat 面板中，点击输入框旁的模型选择器（默认显示 Auto），在弹出的下拉菜单中选择「管理模型」。

也可以打开命令面板（`Ctrl+Shift+P` / `Cmd+Shift+P`）运行 **Chat: Manage Language Models**。

<img src="https://mintcdn.com/hai-token/_6nNjs176oTbHvVD/images/copilot/PixPin_2026-08-17_17-10-03.png?fit=max&auto=format&n=_6nNjs176oTbHvVD&q=85&s=928e1f084dbd4c4f943faace654b9df3" alt="从聊天模型选择器打开「管理模型」" width="1588" height="1108" data-path="images/copilot/PixPin_2026-08-17_17-10-03.png" />

### 步骤 2：添加 Custom Endpoint 模型

在「语言模型」管理页面，点击「添加模型」，从下拉菜单中选择 **Custom Endpoint**。

<img src="https://mintcdn.com/hai-token/_6nNjs176oTbHvVD/images/copilot/PixPin_2026-08-17_17-11-09.png?fit=max&auto=format&n=_6nNjs176oTbHvVD&q=85&s=59a2c20459aac06c83015a9e7265b862" alt="在「添加模型」菜单中选择 Custom Endpoint" width="1588" height="1108" data-path="images/copilot/PixPin_2026-08-17_17-11-09.png" />

在弹出的命名对话框中为这个端点命名（可保持默认 `Custom Endpoint`），按回车确认。

<img src="https://mintcdn.com/hai-token/_6nNjs176oTbHvVD/images/copilot/PixPin_2026-08-17_17-12-00.png?fit=max&auto=format&n=_6nNjs176oTbHvVD&q=85&s=2c6a6a7414eeb5494e8e24ed4f997ca5" alt="为自定义端点命名" width="1588" height="1108" data-path="images/copilot/PixPin_2026-08-17_17-12-00.png" />

### 步骤 3：输入 HaiRoute API Key

在弹出的 API Key 对话框中，粘贴你的 HaiRoute API Key，按回车确认。

<img src="https://mintcdn.com/hai-token/_6nNjs176oTbHvVD/images/copilot/PixPin_2026-08-17_17-12-17.png?fit=max&auto=format&n=_6nNjs176oTbHvVD&q=85&s=ae72b078938da8db2c80c829097b868c" alt="输入 HaiRoute API Key" width="1588" height="1108" data-path="images/copilot/PixPin_2026-08-17_17-12-17.png" />

### 步骤 4：选择 API 类型

在 API Type 对话框中，使用 OpenAI Chat Completions 协议时选择 **Chat Completions**；使用 OpenAI Responses 协议时选择 **Responses**。HaiRoute 已支持这两种 API 类型。

<img src="https://mintcdn.com/hai-token/_6nNjs176oTbHvVD/images/copilot/PixPin_2026-08-17_17-12-58.png?fit=max&auto=format&n=_6nNjs176oTbHvVD&q=85&s=18df235e6f50bbfde151fb6ccd97af36" alt="选择 API 类型为 Chat Completions 或 Responses" width="1588" height="1108" data-path="images/copilot/PixPin_2026-08-17_17-12-58.png" />

### 步骤 5：编辑配置文件填写模型 ID 和 URL

完成上述向导后，VS Code 会自动打开 `chatLanguageModels.json` 配置文件。找到刚才创建的 Custom Endpoint 条目，在 `models` 数组中填写以下字段：

<img src="https://mintcdn.com/hai-token/_6nNjs176oTbHvVD/images/copilot/PixPin_2026-08-17_17-13-53.png?fit=max&auto=format&n=_6nNjs176oTbHvVD&q=85&s=6d043bf3bf6c59ec3f53e98152ebc313" alt="编辑 chatLanguageModels.json 配置文件" width="1588" height="1108" data-path="images/copilot/PixPin_2026-08-17_17-13-53.png" />

| 字段        | 填写值                                                                                                                   |
| --------- | --------------------------------------------------------------------------------------------------------------------- |
| `id`      | HaiRoute 可用模型 ID，例如 `gpt-5.4`                                                                                         |
| `name`    | 模型在选择器中显示的名称，例如 `gpt-5.4`                                                                                             |
| `apiType` | `chat-completions` 或 `responses`，与选择的 API 类型一致                                                                        |
| `url`     | Chat Completions 使用 `https://api.hairoute.ai/v1/chat/completions`；Responses 使用 `https://api.hairoute.ai/v1/responses` |

保存文件后，返回 Chat 面板，在模型选择器中即可看到并切换到刚才添加的 HaiRoute 模型。

## 验证步骤

1. 打开 VS Code Chat 面板。
2. 在模型选择器中选择 HaiRoute 模型。
3. 发送一条简单消息，例如 `hello`。
4. 如果可以正常返回结果，说明接入成功。

## 补充说明

| 项目                  | 推荐值                                                               |
| ------------------- | ----------------------------------------------------------------- |
| Base URL            | `https://api.hairoute.ai/v1`                                      |
| Chat Completions 地址 | `https://api.hairoute.ai/v1/chat/completions`                     |
| Responses 地址        | `https://api.hairoute.ai/v1/responses`                            |
| 实际请求路径              | 与 `apiType` 对应：`POST /v1/chat/completions` 或 `POST /v1/responses` |
| 鉴权头                 | `Authorization: Bearer YOUR_API_KEY`                              |

<Warning>
  此 BYOK 路径主要覆盖 Copilot Chat 及相关 Agent 体验。标准代码补全（行内建议）不在当前 BYOK 覆盖范围内，可能会继续使用 Copilot 官方模型。
</Warning>

## 如何切换模型

在 VS Code 中打开聊天模型选择器，选择另一个 HaiRoute 模型即可。如果列表中没有你想要的模型，回到 **Manage Language Models**，按步骤 2–5 再添加一个，或在 `chatLanguageModels.json` 中为已有 Custom Endpoint 的 `models` 数组追加一个模型对象。

## 常见问题

| 问题                     | 解决方案                                                                                                                                                                                      |
| ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 看不到 Custom Endpoint 入口 | 不同 VS Code 版本、Copilot 套餐和更新渠道可见的入口不完全一致。如果看不到，请尝试更新到最新稳定版或 Insiders 版本，或参考 [Copilot BYOK 官方文档](https://docs.github.com/zh/copilot/bring-your-own-model) 确认当前版本支持的方式。                      |
| 聊天能用了，但代码补全没有变化        | 这是正常现象。官方 BYOK 文档覆盖的是 Chat 和 Agent 相关功能，不覆盖行内代码补全。                                                                                                                                        |
| 提示 `401` 或鉴权失败         | 确认 API Key 填写正确，没有多余空格。VS Code 会以 `Authorization: Bearer YOUR_API_KEY` 的方式发送。                                                                                                             |
| 提示 `model not found`   | 确认 `chatLanguageModels.json` 中 `models[].id` 与 HaiRoute 暴露的模型 ID 完全一致。可以用 `curl https://api.hairoute.ai/v1/models -H "Authorization: Bearer YOUR_API_KEY"` 确认可用模型。                        |
| 配置已保存，但请求未经过 HaiRoute  | 确认 `models[].url` 与 `apiType` 匹配：Chat Completions 使用 `https://api.hairoute.ai/v1/chat/completions`，Responses 使用 `https://api.hairoute.ai/v1/responses`；在聊天模型选择器中重新选择一次模型，或重启 VS Code 后再试。 |

## 下一步

* 查看 [获取 API Key](/docs/zh/quickstart/get-api-key)
* 查看 [OpenAI 格式 API 文档](/docs/zh/api-reference/chat/openai-format)
* 查看 [模型列表](/docs/zh/api-reference/models/list-models)
