# Codex 通过 CLIProxyAPI 代理使用 GLM-4.7 配置说明
## 概述
本指南介绍如何配置 Codex 通过 CLIProxyAPI 代理来使用智谱 AI 的 GLM-4.7 模型。
## 架构说明
```
Codex
↓ (Authorization: Bearer codex-zhipu-key)
CLIProxyAPI (localhost:8317)
↓ (x-api-key: 智谱API Key)
智谱 AI (bigmodel.cn)
```
**为什么要用 CLIProxyAPI?**
- Codex 使用 OpenAI 的 Responses API 协议
- 智谱 AI 使用自定义的 API 格式
- CLIProxyAPI 充当协议转换器,将 OpenAI 格式请求转换为智谱 AI 格式
## 前置要求
1. **Codex CLI** - OpenAI 的命令行工具
2. **CLIProxyAPI** - API 代理服务器
3. **智谱 AI API Key** - 从 https://bigmodel.cn/apikey/platform 获取
## 步骤 1:下载并配置 CLIProxyAPI
### 1.1 下载 CLIProxyAPI
```bash
# 访问 GitHub Releases 下载
https://github.com/router-for-me/CLIProxyAPI/releases
# 解压到目录(例如:D:\dev\CLIProxyAPI_6.9.27_windows_amd64)
```
### 1.2 创建 CLIProxyAPI 配置文件
在 CLIProxyAPI 目录下创建或修改 `config.yaml`:
```yaml
# 服务器配置
host: "localhost"
port: 8317
# API 密钥配置
api-keys:
- "codex-zhipu-key" # Codex 连接 CLIProxyAPI 使用的密钥
# OpenAI 兼容提供商配置
openai-compatibility:
- name: "zhipu"
base-url: "https://open.bigmodel.cn/api/paas/v4/"
api-key-entries:
- api-key: "你的智谱API Key" # 例如:f67bc7959efd49c4bf705b4636d59e72.gsgPB9NXHiGMR99P
models:
- name: "glm-4-flash"
alias: "glm-4-flash"
- name: "glm-4-plus"
alias: "glm-4.7" # 将 glm-4-plus 映射为 glm-4.7
- name: "glm-4"
alias: "glm-4"
```
**关键配置说明:**
- `api-keys`: 定义 Codex 连接 CLIProxyAPI 的密钥
- `openai-compatibility`: 配置智谱 AI 作为 OpenAI 兼容提供商
- `models`: 将智谱模型映射为 Codex 可识别的模型名称
### 1.3 启动 CLIProxyAPI
```bash
cd D:\dev\CLIProxyAPI_6.9.27_windows_amd64
./cli-proxy-api.exe
```
验证 CLIProxyAPI 运行正常:
```bash
curl http://localhost:8317/v1/models
```
应该返回模型列表。
## 步骤 2:配置 Codex
### 2.1 修改 Codex config.toml
位置:`C:\Users\Haha\.codex\config.toml`
```toml
# 全局设置
model_provider = "custom"
model = "glm-4.7"
disable_response_storage = true
# 自定义提供商配置
[model_providers.custom]
name = "CLIProxyAPI"
wire_api = "responses" # 使用 Responses API 协议
requires_openai_auth = true # 启用 OpenAI 认证
base_url = "http://localhost:8317/v1"
# 项目信任配置
[projects.'C:\Users\Haha']
trust_level = "trusted"
[projects.'C:\projects\doceater']
trust_level = "trusted"
# Windows 特定配置
[windows]
sandbox = "unelevated"
```
**关键配置说明:**
- `model_provider = "custom"`: 使用自定义提供商
- `wire_api = "responses"`: 使用 OpenAI Responses API
- `requires_openai_auth = true`: **关键配置**,让 Codex 使用 OpenAI 认证格式
- `base_url`: 指向 CLIProxyAPI 本地服务
### 2.2 配置 Codex auth.json
位置:`C:\Users\Haha\.codex\auth.json`
```json
{
"auth_mode": "apikey",
"OPENAI_API_KEY": "codex-zhipu-key"
}
```
**关键配置说明:**
- `auth_mode`: 使用 API Key 认证
- `OPENAI_API_KEY`: 必须与 CLIProxyAPI config.yaml 中的 `api-keys` 一致
## 步骤 3:测试配置
### 3.1 测试 CLIProxyAPI
```bash
# 测试模型列表
Invoke-WebRequest -Headers @{"x-api-key"="codex-zhipu-key"} -Uri "http://localhost:8317/v1/models"
# 测试聊天接口
$body = @{
model = "glm-4.7"
messages = @(
@{
role = "user"
content = "你好"
}
)
} | ConvertTo-Json -Depth 10
Invoke-WebRequest -Headers @{"x-api-key"="codex-zhipu-key"} -Uri "http://localhost:8317/v1/chat/completions" -Method POST -Body $body -ContentType "application/json"
```
### 3.2 测试 Codex
```bash
# 简单测试
codex exec "2+2=?"
# 复杂任务
codex exec "帮我分析当前目录的文件结构"
```
### 3.3 验证工作流程
1. Codex 发送请求到 `http://localhost:8317/v1/responses`
2. 使用 `Authorization: Bearer codex-zhipu-key` 认证
3. CLIProxyAPI 验证密钥
4. CLIProxyAPI 转换请求格式并发送到智谱 AI
5. 智谱 AI 返回结果
6. CLIProxyAPI 转换响应格式
7. Codex 接收处理结果
## 常见问题
### Q1: 提示 "Invalid API key"
**原因:**
- Codex auth.json 中的密钥与 CLIProxyAPI config.yaml 中的密钥不一致
- 智谱 API Key 无效或过期
**解决方案:**
1. 检查 `auth.json` 和 `config.yaml` 中的密钥是否一致
2. 访问 https://bigmodel.cn/apikey/platform 验证智谱 API Key
3. 重新生成智谱 API Key
### Q2: 提示 "Model not found"
**原因:**
- 模型名称映射配置错误
**解决方案:**
1. 检查 CLIProxyAPI config.yaml 中的 `models` 配置
2. 确保模型别名正确映射(如 `glm-4.7` → `glm-4-plus`)
### Q3: 连接超时
**原因:**
- CLIProxyAPI 未启动
- 端口 8317 被占用
**解决方案:**
1. 检查 CLIProxyAPI 是否正在运行
2. 使用 `netstat -ano | findstr 8317` 检查端口占用
3. 修改 config.yaml 中的端口号
### Q4: 401 Unauthorized 错误
**原因:**
- `requires_openai_auth = false` 设置错误
- 认证头格式不匹配
**解决方案:**
1. 确保 config.toml 中 `requires_openai_auth = true`
2. 确保使用 `wire_api = "responses"`
## 高级配置
### 多模型配置
在 CLIProxyAPI config.yaml 中添加多个模型:
```yaml
openai-compatibility:
- name: "zhipu"
base-url: "https://open.bigmodel.cn/api/paas/v4/"
api-key-entries:
- api-key: "你的智谱API Key"
models:
- name: "glm-4-flash"
alias: "glm-4-flash" # 快速模型
- name: "glm-4-plus"
alias: "glm-4.7" # 标准模型
- name: "glm-4"
alias: "glm-4" # 基础模型
- name: "glm-4-air"
alias: "glm-4-air" # 轻量模型
```
在 Codex 中切换模型:
```bash
# 使用 glm-4.7
codex exec "任务" --model glm-4.7
# 使用 glm-4-flash(更快)
codex exec "任务" --model glm-4-flash
```
### 日志调试
启用 CLIProxyAPI 调试日志:
```yaml
# config.yaml
debug: true
logging-to-file: true
```
查看 Codex 日志:
```bash
# Windows
Get-Content ~/.codex/log/codex-tui.log -Tail 50
# 查找错误
Get-Content ~/.codex/log/codex-tui.log | Select-String "error"
```
## 配置文件完整示例
### CLIProxyAPI config.yaml (完整)
```yaml
host: "localhost"
port: 8317
api-keys:
- "codex-zhipu-key"
debug: false
openai-compatibility:
- name: "zhipu"
base-url: "https://open.bigmodel.cn/api/paas/v4/"
api-key-entries:
- api-key: "f67bc7959efd49c4bf705b4636d59e72.gsgPB9NXHiGMR99P"
models:
- name: "glm-4-flash"
alias: "glm-4-flash"
- name: "glm-4-plus"
alias: "glm-4.7"
- name: "glm-4"
alias: "glm-4"
```
### Codex config.toml (完整)
```toml
model_provider = "custom"
model = "glm-4.7"
disable_response_storage = true
[model_providers.custom]
name = "CLIProxyAPI"
wire_api = "responses"
requires_openai_auth = true
base_url = "http://localhost:8317/v1"
[projects.'C:\Users\Haha']
trust_level = "trusted"
[projects.'C:\projects\doceater']
trust_level = "trusted"
[windows]
sandbox = "unelevated"
```
### Codex auth.json (完整)
```json
{
"auth_mode": "apikey",
"OPENAI_API_KEY": "codex-zhipu-key"
}
```
## 总结
通过 CLIProxyAPI 代理使用 GLM-4.7 的关键点:
1. ✅ **必须使用 CLIProxyAPI** 作为中间代理层
2. ✅ **requires_openai_auth = true** 是关键配置
3. ✅ **auth.json** 和 **config.yaml** 中的密钥必须一致
4. ✅ **模型别名映射** 要正确配置
5. ✅ **CLIProxyAPI** 负责协议转换
这套配置让 Codex 可以无缝使用智谱 AI 的各种模型,同时保持 OpenAI 兼容的 API 接口。