{"id":50797103,"url":"https://github.com/aurorax-neo/chat2api","last_synced_at":"2026-06-12T15:30:32.006Z","repository":{"id":231491052,"uuid":"781548686","full_name":"aurorax-neo/chat2api","owner":"aurorax-neo","description":"chat2api","archived":false,"fork":false,"pushed_at":"2026-06-05T12:06:45.000Z","size":346,"stargazers_count":517,"open_issues_count":0,"forks_count":86,"subscribers_count":7,"default_branch":"master","last_synced_at":"2026-06-05T13:09:06.561Z","etag":null,"topics":["api","chat","gpt"],"latest_commit_sha":null,"homepage":"https://chat2api-seven.vercel.app","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aurorax-neo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2024-04-03T15:36:45.000Z","updated_at":"2026-06-05T12:06:49.000Z","dependencies_parsed_at":"2024-11-20T13:46:17.975Z","dependency_job_id":null,"html_url":"https://github.com/aurorax-neo/chat2api","commit_stats":null,"previous_names":["aurorax-neo/free-gpt3.5-2api","aurorax-neo/chat2api"],"tags_count":44,"template":false,"template_full_name":null,"purl":"pkg:github/aurorax-neo/chat2api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aurorax-neo%2Fchat2api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aurorax-neo%2Fchat2api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aurorax-neo%2Fchat2api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aurorax-neo%2Fchat2api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aurorax-neo","download_url":"https://codeload.github.com/aurorax-neo/chat2api/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aurorax-neo%2Fchat2api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34251773,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-12T02:00:06.859Z","response_time":109,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","chat","gpt"],"created_at":"2026-06-12T15:30:30.618Z","updated_at":"2026-06-12T15:30:31.999Z","avatar_url":"https://github.com/aurorax-neo.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chat2api\n\n秽土转生 `free-gpt3.5-2api` -\u003e `chat2api`.\n\n把 ChatGPT Web 侧能力转换为兼容 OpenAI 风格的 HTTP API。\n\n## 支持能力\n\n- `POST /v1/chat/completions`：兼容 Chat Completions，请求支持普通 JSON 与 stream。\n- `POST /v1/responses`：兼容 Responses API 文本链路，请求支持普通 JSON 与 stream。\n- Responses 流会先发 `response.created` 和 `response.in_progress`，再推 `output_item.added` / `delta` / `completed`，更适合 CLI / Codex 这类依赖状态事件的客户端。\n- Function Calling：兼容 OpenAI `tools`/`tool_choice`、旧版 `functions`/`function_call`，支持多工具调用、工具结果回填与流式 tool calls。\n- `GET /v1/accTokens`：查看配置账号池可用数量。\n- 本地 `sk-` auth key：使用配置文件中的 `chatgpts` 账号池请求上游。\n- 可选私有前缀直传 access token：配置 `auth.access_token_prefix` 后，可跳过账号池直接请求上游；未配置时默认关闭。\n\n说明：当前版本不再做模型映射，`model` 会直接传给上游；请求中未传 `model` 时默认使用 `auto`。\n\n## 配置\n\n服务读取 `conf/app.\u003cENV\u003e.yaml`，`ENV` 默认为 `dev`，因此本地默认读取 `conf/app.dev.yaml`。\n\n仓库提供了配置模板 `conf/app.demo.yaml`。本地运行时可以复制为 `conf/app.dev.yaml`；Docker Compose 运行时可以复制为 `.chat2api/conf/app.dev.yaml`，因为 compose 会把 `.chat2api/conf` 映射到容器内的 `/app/conf`。\n\n当前版本的业务配置以 YAML 文件为准，环境变量只用于选择配置文件：\n\n| 环境变量 | 默认值 | 作用 |\n| --- | --- | --- |\n| `ENV` | `dev` | 决定读取哪个配置文件，例如 `ENV=prod` 会读取 `conf/app.prod.yaml`。 |\n\n常见配置文件对应关系：\n\n| 启动方式 | 读取文件 |\n| --- | --- |\n| `go run ./cmd` | `conf/app.dev.yaml` |\n| `ENV=test go run ./cmd` | `conf/app.test.yaml` |\n| `ENV=prod go run ./cmd` | `conf/app.prod.yaml` |\n\n```yaml\nlog_level: debug\nlog_path: logs\nlog_file: app.dev.log\nbind: 127.0.0.1\nport: 3040\n\nauth:\n  access_tokens:\n    - sk-your-local-key\n  # 默认关闭直传真实 access token。\n  # 如需启用，请配置私有且难猜的前缀，例如：\n  # access_token_prefix:\n  #   - your-private-prefix-\n  access_token_prefix: []\n\nproxy: http://127.0.0.1:7890\nchatgpt_base_url: https://chatgpt.com\n\nchatgpts:\n  - id_token: optional_id_token\n    access_token: real_access_token\n    refresh_token: optional_refresh_token\n    account_id: optional_account_id\n    last_refresh: \"\"\n    email: optional_email\n    type: codex\n    expired: \"\"\n    proxy: \"\"\n```\n\n关键规则：\n\n- `auth.access_tokens` 保存裸 token，不要写 `Bearer`；请求时仍使用标准的 `Authorization: Bearer \u003ctoken\u003e`。\n- `auth.access_token_prefix` 配置直传真实 access token 的前缀；默认空列表会关闭直传模式。启用后，请求头里的 `Bearer \u003cprefix\u003e\u003creal_access_token\u003e` 会跳过账号池，并把去掉 `\u003cprefix\u003e` 后的真实 access token 传给上游。前缀务必使用私有且难猜的值。\n- 如果 `auth.access_tokens` 为空，服务启动时会随机生成一个 `sk-` token，写回配置文件，并在日志中打印 `current auth: ...`。\n- `chatgpts` 是账号池配置，每个账号只有 `access_token` 是必要配置；`proxy`、`id_token`、`refresh_token`、`email` 等字段都是可选字段。\n- `chatgpts[].access_token` 是账号池的真实上游 access token。通过本地 `sk-` key 请求时会从这里选择账号。\n- 代理优先级为账号代理优先：`chatgpts[].proxy` 不为空时使用账号代理；为空时回退到全局 `proxy`。\n- `chatgpt_base_url` 为空时默认使用 `https://chatgpt.com`。\n\n## 运行\n\n本地运行：\n\n```bash\ngo run ./cmd\n```\n\n## Web 管理\n\n服务启动后，可以打开 `http://\u003chost\u003e:\u003cport\u003e/admin` 管理 YAML 配置里的本地 API key、access token 直传前缀、全局代理，以及 `chatgpts` 账号池。\n\n- 管理 API 只接受配置文件中的 `auth.access_tokens` 作为登录密钥，不接受 `access_token_prefix` 直传 token。\n- 页面不会回显真实 token，已保存的密钥只显示遮罩值；输入框留空会保留原值，填新值才会覆盖。\n- 保存时会先写出 `conf/app.\u003cENV\u003e.yaml.bak` 备份，再更新当前 `conf/app.\u003cENV\u003e.yaml`。本地和 Docker 运行时配置监听器会重载账号池；serverless/Vercel 环境不支持在页面中写回配置文件。\n\n指定环境运行，例如读取 `conf/app.prod.yaml`：\n\n```bash\nENV=prod go run ./cmd\n```\n\nDocker Compose：\n\n```bash\ndocker compose up -d\n```\n\n### Linux 升级\n\n已经装过旧版本时，通常这样更新：\n\n```bash\n# 源码目录\ngit pull --ff-only\ngo build -o chat2api ./cmd\n# 然后重启你的服务进程或 systemd 服务\n```\n\n```bash\n# Docker Compose\ndocker compose pull\ndocker compose up -d\n```\n\n```bash\n# 二进制部署\n# 停掉旧进程后，替换成最新发布版二进制，再启动\n```\n\n升级时保留 `conf/` 和 `logs/` 即可，配置和日志都在挂载卷里。源码目录更新后，重启进程最稳。\n\nVercel 运行时不会写入配置文件，也不会默认读取仓库里的 `conf/app.dev.yaml`，避免把本地代理或本地账号配置带到云端。下面这些业务环境变量只在 Vercel/serverless 初始化时读取；本地运行和 Docker Compose 仍以 YAML 配置为准。\n\n请在 Vercel 环境变量中配置：\n\n| 环境变量 | 作用 |\n| --- | --- |\n| `AUTH_TOKENS` | 本地 API key，多个值用逗号、分号或换行分隔 |\n| `ACCESS_TOKEN_PREFIX` / `ACCESS_TOKEN_PREFIXES` | 可选，直传真实 access token 的私有前缀，多个值用逗号、分号或换行分隔；不配置则关闭直传模式 |\n| `CHATGPT_ACCESS_TOKENS` | 上游 ChatGPT access token，多个值用逗号、分号或换行分隔 |\n| `PROXY` | 可选，全局代理 |\n| `CHATGPT_BASE_URL` | 可选，默认 `https://chatgpt.com` |\n| `LOG_LEVEL` | 可选，默认 `debug` |\n| `VERCEL_CONFIG_FILE` | 可选，显式指定要读取的 YAML 配置文件路径，例如 `conf/app.prod.yaml` |\n\n`ACCESS_TOKEN_PREFIXES` 示例：\n\n```text\nACCESS_TOKEN_PREFIXES=your-private-prefix-,another-private-prefix-\n```\n\n如果配置了上面的第一个前缀，请求时这样使用：\n\n```bash\ncurl https://your-vercel-domain.vercel.app/v1/chat/completions \\\n  -H 'Authorization: Bearer your-private-prefix-\u003creal_access_token\u003e' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"model\":\"auto\",\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}'\n```\n\n默认 `compose.yaml` 将容器 `3040` 端口映射到宿主机 `7846`，并映射本地配置与日志目录：\n\n```yaml\nvolumes:\n  - .chat2api/conf:/app/conf\n  - .chat2api/logs:/app/logs\n```\n\n容器内工作目录是 `/app`，因此默认会读取 `/app/conf/app.dev.yaml`，也就是宿主机的 `.chat2api/conf/app.dev.yaml`。如需让容器读取其他环境配置，可以在 `compose.yaml` 中增加 `ENV`：\n\n```yaml\nenvironment:\n  - ENV=prod\n```\n\n此时容器会读取宿主机映射进去的 `.chat2api/conf/app.prod.yaml`。\n\n## 接口示例\n\n下面示例以本地开发配置 `127.0.0.1:3040` 为例。\n\n### 查看账号池\n\n```bash\ncurl http://127.0.0.1:3040/v1/accTokens \\\n  -H 'Authorization: Bearer sk-your-local-key'\n```\n\n返回中的 `count` 是账号池账号数量，`canUseCount` 是当前可用账号数量。\n\n### Chat Completions\n\n使用配置账号池：\n\n```bash\ncurl http://127.0.0.1:3040/v1/chat/completions \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"model\":\"auto\",\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}'\n```\n\n配置私有前缀后，可以直传真实 access token 并跳过账号池：\n\n```yaml\nauth:\n  access_token_prefix:\n    - your-private-prefix-\n```\n\n```bash\ncurl http://127.0.0.1:3040/v1/chat/completions \\\n  -H 'Authorization: Bearer your-private-prefix-\u003creal_access_token\u003e' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"model\":\"auto\",\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}'\n```\n\n流式返回：\n\n```bash\ncurl http://127.0.0.1:3040/v1/chat/completions \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"model\":\"auto\",\"stream\":true,\"messages\":[{\"role\":\"user\",\"content\":\"ping\"}]}'\n```\n\nFunction Calling：\n\n下面示例用 `tool_choice` 强制调用 `get_weather`，适合快速验证 Function Calling 是否生效。\n\n```bash\ncurl http://127.0.0.1:3040/v1/chat/completions \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\n    \"model\":\"auto\",\n    \"messages\":[{\"role\":\"user\",\"content\":\"查一下杭州天气\"}],\n    \"tools\":[{\n      \"type\":\"function\",\n      \"function\":{\n        \"name\":\"get_weather\",\n        \"description\":\"查询指定城市天气\",\n        \"parameters\":{\n          \"type\":\"object\",\n          \"properties\":{\"city\":{\"type\":\"string\"}},\n          \"required\":[\"city\"]\n        }\n      }\n    }],\n    \"tool_choice\":{\"type\":\"function\",\"function\":{\"name\":\"get_weather\"}}\n  }'\n```\n\n如果想让模型自行判断是否需要调用工具，把 `tool_choice` 改为：\n\n```json\n\"auto\"\n```\n\n成功时返回里会出现：\n\n```json\n{\n  \"choices\": [\n    {\n      \"message\": {\n        \"role\": \"assistant\",\n        \"content\": null,\n        \"tool_calls\": [\n          {\n            \"id\": \"call_xxx\",\n            \"type\": \"function\",\n            \"function\": {\n              \"name\": \"get_weather\",\n              \"arguments\": \"{\\\"city\\\":\\\"杭州\\\"}\"\n            }\n          }\n        ]\n      },\n      \"finish_reason\": \"tool_calls\"\n    }\n  ]\n}\n```\n\n客户端执行工具后，把工具结果带回下一轮即可让模型继续生成最终回答。第二轮可以继续带 `tools`，也可以只带消息历史；服务会把 `assistant.tool_calls` 和 `role=tool` 转成 ChatGPT Web 侧可理解的普通上下文文本再发给上游。\n\n注意：与 Toolify 保持一致，`role=tool` 必须带 `tool_call_id`，并且消息历史里必须包含对应的上一轮 `assistant.tool_calls`；否则会返回 400，避免工具结果失去工具名和参数上下文。\n\n```bash\ncurl http://127.0.0.1:3040/v1/chat/completions \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\n    \"model\":\"auto\",\n    \"messages\":[\n      {\"role\":\"user\",\"content\":\"杭州现在天气怎么样？\"},\n      {\n        \"role\":\"assistant\",\n        \"content\":null,\n        \"tool_calls\":[{\n          \"id\":\"call_demo_weather\",\n          \"type\":\"function\",\n          \"function\":{\n            \"name\":\"get_weather\",\n            \"arguments\":\"{\\\"city\\\":\\\"杭州\\\"}\"\n          }\n        }]\n      },\n      {\n        \"role\":\"tool\",\n        \"tool_call_id\":\"call_demo_weather\",\n        \"content\":\"杭州晴，26℃，东风 2 级。\"\n      }\n    ],\n    \"tools\":[{\n      \"type\":\"function\",\n      \"function\":{\n        \"name\":\"get_weather\",\n        \"description\":\"查询指定城市天气\",\n        \"parameters\":{\n          \"type\":\"object\",\n          \"properties\":{\"city\":{\"type\":\"string\"}},\n          \"required\":[\"city\"]\n        }\n      }\n    }]\n  }'\n```\n\n流式 Function Calling 同样支持：\n\n```bash\ncurl -N http://127.0.0.1:3040/v1/chat/completions \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\n    \"model\":\"auto\",\n    \"stream\":true,\n    \"messages\":[{\"role\":\"user\",\"content\":\"杭州现在天气怎么样？\"}],\n    \"tools\":[{\n      \"type\":\"function\",\n      \"function\":{\n        \"name\":\"get_weather\",\n        \"description\":\"查询指定城市天气\",\n        \"parameters\":{\n          \"type\":\"object\",\n          \"properties\":{\"city\":{\"type\":\"string\"}},\n          \"required\":[\"city\"]\n        }\n      }\n    }],\n    \"tool_choice\":{\"type\":\"function\",\"function\":{\"name\":\"get_weather\"}}\n  }'\n```\n\n兼容旧版 Chat Completions Function Calling 参数：`functions` 会自动转换为 `tools`，`function_call` 会自动转换为 `tool_choice`。\n\n### Responses\n\n普通文本请求：\n\n```bash\ncurl http://127.0.0.1:3040/v1/responses \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"model\":\"auto\",\"input\":\"ping\"}'\n```\n\n带 instructions：\n\n```bash\ncurl http://127.0.0.1:3040/v1/responses \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"model\":\"auto\",\"instructions\":\"用中文回答\",\"input\":\"ping\"}'\n```\n\n流式返回：\n\n```bash\ncurl http://127.0.0.1:3040/v1/responses \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\"model\":\"auto\",\"stream\":true,\"input\":\"ping\"}'\n```\n\n`/v1/responses` 文本链路同样支持 `function` tools；`image_generation` 工具会走图片生成兼容链路。\n\nResponses Function Calling：\n\n```bash\ncurl http://127.0.0.1:3040/v1/responses \\\n  -H 'Authorization: Bearer sk-your-local-key' \\\n  -H 'Content-Type: application/json' \\\n  -d '{\n    \"model\":\"auto\",\n    \"input\":\"杭州现在天气怎么样？\",\n    \"tools\":[{\n      \"type\":\"function\",\n      \"name\":\"get_weather\",\n      \"description\":\"查询指定城市天气\",\n      \"parameters\":{\n        \"type\":\"object\",\n        \"properties\":{\"city\":{\"type\":\"string\"}},\n        \"required\":[\"city\"]\n      }\n    }],\n    \"tool_choice\":{\"type\":\"function\",\"name\":\"get_weather\"}\n  }'\n```\n\n返回的 `output` 中会包含 `type=function_call` 的条目，字段包括 `call_id`、`name` 和 `arguments`。\n\n## 错误排查\n\n- `401 Incorrect API key`：本地 key 模式检查请求头是否为 `Authorization: Bearer sk-your-local-key`，以及配置里的 `auth.access_tokens` 是否保存裸 token；`access_token_prefix` 模式检查请求头是否为 `Authorization: Bearer \u003cconfigured-prefix\u003e\u003creal_access_token\u003e`，并确认私有前缀已配置且拼接正确。\n- `turnstile token is required` 或 `turnstile token failed`：上游要求 Turnstile 校验，需确认账号 token、代理和上游访问环境是否可用。\n- 账号池不可用：检查 `chatgpts[].access_token` 是否为空、是否过期，以及账号是否处于冷却时间。\n- 代理不生效：先检查账号自己的 `chatgpts[].proxy`，它会优先于全局 `proxy`。\n\n## 参考项目\n\n- https://github.com/aurora-develop/aurora\n- https://github.com/xqdoo00o/ChatGPT-to-API\n- https://github.com/basketikun/chatgpt2api\n- https://github.com/funnycups/Toolify\n\n## Powered By\n\n- codex\n- [aurorax-neo](https://github.com/aurorax-neo)\n\n## Friend Links\n\n- [linux.do](https://linux.do/)\n- [xiaozhou26](https://github.com/xiaozhou26)\n\n## Sponsor\n\n\u003ca href=\"https://edgeone.ai/?from=github\"\u003e\u003cimg width=\"200\" src=\"https://edgeone.ai/media/34fe3a45-492d-4ea4-ae5d-ea1087ca7b4b.png\"\u003e\u003c/a\u003e\n\nCDN acceleration and security protection for this project are sponsored by Tencent EdgeOne.\n\n## Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=aurorax-neo/chat2api\u0026type=Date)](https://star-history.com/#aurorax-neo/chat2api\u0026Date)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faurorax-neo%2Fchat2api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faurorax-neo%2Fchat2api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faurorax-neo%2Fchat2api/lists"}