Skip to content

qymlxin/chatgpt-to-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

chatgpt-to-api

我写这个项目,是为了把 ChatGPT / Codex OAuth 凭证接到本地兼容 API 上,方便本地工具或客户端直接走统一接口。

当前我主要做了这些能力:

  • 支持 OpenAI Codex / ChatGPT Plus/Pro OAuth 登录
  • 支持 GitHub Copilot OAuth 登录
  • 启动本地 HTTP 服务,转发到 ChatGPT Codex Responses 上游
  • 对外暴露兼容接口:
    • POST /v1/chat/completions
    • POST /v1/chat/responses
    • POST /v1/messages
    • POST /anthropic/v1/messages
  • 提供一个简单前端页面,用来测试流式对话

项目结构

.
├── frontend/           # 前端测试页面
├── oauth/              # OAuth 登录、刷新、凭证管理
├── main.go             # CLI 入口 + 本地 API 代理服务
├── go.mod
└── go.sum

运行环境

  • Go 1.25+
  • macOS / Linux / Windows
  • 本机能打开浏览器完成 OAuth 登录

go.mod 当前声明为 go 1.25.0

编译

直接编译

go build -o chatgpt-to-api .

编译后运行:

./chatgpt-to-api

不编译直接运行

go run . server --port 8080

命令行用法

入口命令:

chatgpt-to-api [command]

可用命令

chatgpt-to-api login --provider openai-codex
chatgpt-to-api login --provider github-copilot
chatgpt-to-api refresh --provider openai-codex
chatgpt-to-api logout --provider openai-codex
chatgpt-to-api relogin --provider openai-codex
chatgpt-to-api status
chatgpt-to-api server --port 8080

Provider

当前支持两个 provider:

  • openai-codex
  • github-copilot

默认 provider 是:

  • openai-codex

使用流程

1)先登录

如果要用 ChatGPT / Codex 凭证:

./chatgpt-to-api login --provider openai-codex

登录时会:

  • 自动拉起浏览器
  • 完成 OAuth 授权
  • 把凭证保存到本地

默认凭证路径:

~/.config/chatgpt-to-api/openai-codex.json

如果要用 GitHub Copilot:

./chatgpt-to-api login --provider github-copilot

对应凭证路径:

~/.config/chatgpt-to-api/github-copilot.json

2)检查认证状态

./chatgpt-to-api status

这里会检查:

  • OpenAI Codex 是否已配置
  • GitHub Copilot 是否已配置

3)启动本地代理服务

./chatgpt-to-api server --port 8080

启动后默认地址:

http://localhost:8080

健康检查:

curl http://localhost:8080/health

返回示例:

{"status":"ok"}

服务端点

启动服务后,可用接口如下。

OpenAI Chat Completions 兼容接口

POST /v1/chat/completions

请求示例:

curl -N http://localhost:8080/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-5.4",
    "stream": true,
    "messages": [
      {"role": "system", "content": "你是一个简洁的助手"},
      {"role": "user", "content": "用三句话介绍 Go"}
    ]
  }'

OpenAI Responses 风格接口

POST /v1/chat/responses

Anthropic Messages 兼容接口

POST /v1/messages
POST /anthropic/v1/messages

这两个接口主要是为了兼容 Claude / Anthropic 风格客户端。

模型说明

我这里会把外部传入的模型名映射成 Codex 上游模型。

当前代码里能直接看到的常用模型包括:

  • gpt-5.4
  • gpt-5.3-codex
  • gpt-5.3-codex-spark
  • gpt-5.2-codex
  • gpt-5.1-codex
  • gpt-5.1-codex-max

如果本机存在 ~/.codex/models_cache.json,也会优先读取里面可用的模型。

前端测试页面

项目里带了一个简单页面:

frontend/index.html

这个页面可以用来:

  • 发送流式聊天请求
  • 选择 provider
  • 调用登录 / 重登 / 注销接口
  • 查看流式输出效果

使用方式

这是一个纯静态页面,可以直接用任意静态文件服务器打开。

例如在项目根目录执行:

python3 -m http.server 3000

然后访问:

http://localhost:3000/frontend/index.html

页面默认 API 地址是:

http://localhost:8081/v1/chat/completions

如果后端实际跑在 8080,就在页面设置里改成:

http://localhost:8080/v1/chat/completions

认证相关接口

服务里还留了一组认证管理接口:

查询认证状态

GET /v1/auth/status

执行 OAuth 登录

POST /v1/auth/login

请求示例:

{
  "provider": "openai-codex"
}

重新登录

POST /v1/auth/relogin

注销

POST /v1/auth/logout

手动导入 token

POST /v1/auth/import

请求体示例:

{
  "access_token": "your_access_token",
  "refresh_token": "your_refresh_token"
}

Claude 配置

如果我要把这个服务当成 Claude / Anthropic 风格的本地网关来用,可以把 Claude 侧的 Base URL 指到这个服务。

因为这里提供了:

  • POST /v1/messages
  • POST /anthropic/v1/messages

所以最直接的方式,就是把客户端请求改到本地服务,例如:

http://localhost:8080/anthropic/v1/messages

或:

http://localhost:8080/v1/messages

配置思路

需要在 Claude 使用侧配置:

  • Base URL 指向本地服务
  • Messages API 路径使用这里暴露的接口
  • 模型名可传 sonnet / opus / haiku,代码里会映射到合适的 Codex 模型

当前代码里的 Anthropic 模型映射规则大致是:

  • sonnet / claude-sonnet* -> 首选 Codex 模型
  • opus / claude-opus* -> 更强的 Codex 模型
  • haiku / claude-haiku* -> 更小的 Codex 模型

如果在自定义客户端里使用

可以直接按 Anthropic Messages 格式发请求:

curl -N http://localhost:8080/anthropic/v1/messages \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "sonnet",
    "stream": true,
    "messages": [
      {"role": "user", "content": "你好,介绍一下你自己"}
    ]
  }'

凭证存储位置

默认配置和凭证保存在:

~/.config/chatgpt-to-api/

常见文件包括:

  • openai-codex.json
  • github-copilot.json
  • config.json

注意事项

  1. server 模式启动时,会优先读取:
    • ~/.config/chatgpt-to-api/openai-codex.json
    • 如果没有,再尝试 ~/.config/chatgpt-to-api/config.json
  2. 前端页面默认端口是 8081,而后端命令示例里常用 8080,要注意保持一致。
  3. OAuth 登录依赖本机浏览器与本地回调端口:
    • OpenAI Codex 默认回调地址:http://localhost:1455/auth/callback
  4. 如果 access token 过期,程序会尝试自动 refresh。

一个最小可用示例

# 1. 编译
go build -o chatgpt-to-api .

# 2. 登录
./chatgpt-to-api login --provider openai-codex

# 3. 启动服务
./chatgpt-to-api server --port 8080

# 4. 调用接口
curl -N http://localhost:8080/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "gpt-5.4",
    "stream": true,
    "messages": [
      {"role": "user", "content": "你好"}
    ]
  }'

About

A project that proxies ChatGPT Codex as OpenAI and Anthropic APIs

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors