{"id":13457271,"url":"https://github.com/UNICKCHENG/openai-proxy","last_synced_at":"2025-03-24T12:31:09.697Z","repository":{"id":163534313,"uuid":"638073927","full_name":"UNICKCHENG/openai-proxy","owner":"UNICKCHENG","description":"openai-proxy is a solution provided for Chinese users to address the issue of being unable to make requests to the OpenAI API.","archived":false,"fork":false,"pushed_at":"2024-03-12T19:54:03.000Z","size":2774,"stargazers_count":411,"open_issues_count":23,"forks_count":109,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-21T21:09:23.968Z","etag":null,"topics":["chatgpt","claude","openai","openai-api"],"latest_commit_sha":null,"homepage":"https://openai-proxy.apifox.cn","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/UNICKCHENG.png","metadata":{"files":{"readme":"readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2023-05-09T03:00:42.000Z","updated_at":"2025-03-03T01:08:55.000Z","dependencies_parsed_at":"2023-12-13T16:27:04.979Z","dependency_job_id":"471cde91-a700-4b4e-9f7b-fc927fab303d","html_url":"https://github.com/UNICKCHENG/openai-proxy","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UNICKCHENG%2Fopenai-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UNICKCHENG%2Fopenai-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UNICKCHENG%2Fopenai-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/UNICKCHENG%2Fopenai-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/UNICKCHENG","download_url":"https://codeload.github.com/UNICKCHENG/openai-proxy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245269775,"owners_count":20587843,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["chatgpt","claude","openai","openai-api"],"created_at":"2024-07-31T08:01:37.937Z","updated_at":"2025-03-24T12:31:09.305Z","avatar_url":"https://github.com/UNICKCHENG.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# Hi, there 👋\n\nAPI 文档请查看 👉 https://openai-proxy.apifox.cn\n\n`openai-proxy` 是为中国用户提供的一个方案，目前可以帮你解决的问题有：\n\n- ✅ OpenAI API 请求超时\n- ✅ OpenAI API 不支持查询费用消耗量\n- ✅ Claude API 还在等待列表中\n- ✅ Google Bard 没有提供 API\n- 。。。\n\n---\n\n## OpenAI API 请求超时\n\n现在您只需要将 `https://api.openai.com` 替换成 `https://openai.aihey.cc/openai` 即可食用\n\n```bash\n# openai api\ncurl https://api.openai.com/v1/chat/completions \\\n  -H \"Content-Type: application/json\" \\\n  -H \"Authorization: Bearer $OPENAI_API_KEY\" \\\n  -d '{\n    \"model\": \"gpt-3.5-turbo\",\n    \"messages\": [{\"role\": \"user\", \"content\": \"Hello!\"}]\n  }'\n\n# 替换后\ncurl https://openai.aihey.cc/openai/v1/chat/completions \\\n  -H \"Content-Type: application/json\" \\\n  -H \"Authorization: Bearer $OPENAI_API_KEY\" \\\n  -d '{\n    \"model\": \"gpt-3.5-turbo\",\n    \"messages\": [{\"role\": \"user\", \"content\": \"Hello!\"}]\n  }'\n```\n\n## OpenAI API 不支持查询费用消耗量\n\n\u003e 自从 2023 年 04 月份开始，`https://api.openai.com/dashboard/billing/credit_grants` 只能通过网页登录生成的 session id 来请求，而 key (`sk-*****`) 的方式将无效。**不过您可以通过下述两个方式来请求**。\n\n### 方式 1：使用 `openai-proxy`\n\n缺点：\n\n- 可能和网页端显示的数据存在差异（这个不知道为啥，有了解的小伙伴欢迎在 [ISSUES](https://github.com/UNICKCHENG/openai-proxy/issues) 指出）\n\n```bash\n# 指定统计开始日期和结束日期\ncurl https://openai.aihey.cc/openai/billing/credit_grants?start_date=2022-05-10\u0026end_date=2023-05-10 \\\n  -H \"Content-Type: application/json\" \\\n  -H \"Authorization: Bearer sk-***********\"\n\n# 不指定参数，默认统计最近 90 天的数据\ncurl https://openai.aihey.cc/openai/billing/credit_grants \\\n  -H \"Content-Type: application/json\" \\\n  -H \"Authorization: Bearer sk-**********\"\n```\n\n### 方式 2：使用网页端生成的 `sensitive_id`\n\n缺点：\n\n- 首次需要去网页端获取（按 F12 后刷新网页）\n- 可能随时会失效（但目前来看只要网页端不退出登录，`sensitive_id` 时效都比较长）\n\n![](assets/openai.png)\n\n```bash\ncurl https://openai.aihey.cc/openai/billing/credit_grants\\\n  -H \"Content-Type: application/json\" \\\n  -H \"Authorization: Bearer sess-*****\"\n```\n\n## Claude web API 支持\n\n### 开始之前\n\n- 访问 [claude.ai](https://claude.ai/) 创建账号（暂时只有美国和英国 IP 可访问）\n- 从网页端拿到 orgId\n- 从网页端拿到 sessionKey\n\n\u003e ![](assets/claude_org_id.png) \u003e ![](assets/claude_session.png)\n\n### 获取全部会话\n\n```bash\ncurl -X GET 'https://openai.aihey.cc/claude/organizations/dca2a902-a463-41f0-88cb-b047deb40178/chat_conversations' \\\n--header 'Accept: */*' \\\n--header 'Content-Type: application/json' \\\n--header 'Cookie: sessionKey=sk-***********'\n```\n\n### 生成 AI 回复\n\n```bash\ncurl -X POST 'https://openai.aihey.cc/claude/append_message' \\\n--header 'Accept: text/event-stream' \\\n--header 'Cookie: sessionKey=sk-***********' \\\n--header 'Content-Type: application/json' \\\n--data-raw '{\n  \"completion\": {\n    \"prompt\": \"hello\",\n    \"timezone\": \"Asia/Shanghai\",\n    \"model\": \"claude-2\"\n  },\n  \"organization_uuid\": \"dca2a902-a463-41f0-88cb-b047deb40178\",\n  \"conversation_uuid\": \"5446798e-0e11-4e8f-994c-3d8386f01bd6\",\n  \"text\": \"hello\",\n  \"attachments\": []\n}'\n```\n\n### 生成 AI 回复（类 OpenAI）\n\n已经支持类似 gpt 的请求格式，如果你使用的第三方插件支持自定义 OpenAI 地址，现在可以直接将 `https://api.openai.com` 替换成 `http://openai.aihey.cc/claude/{organization_uuid}`\n\n```bash\ncurl -X POST 'https://openai.aihey.cc/claude/dca2a902-a463-41f0-88cb-b047deb40178/v1/chat/completions' \\\n--header 'Accept: */*' \\\n--header 'Content-Type: application/json' \\\n--header 'Authorization: Bearer your-sessionKey' \\\n--data-raw '{\n    \"model\":\"gpt-3.5-turbo\",\n    \"messages\": [\n        {\"role\": \"user\", \"content\":\"hello\"}\n    ],\n    \"stream\": false\n}'\n```\n\n更多接口请参考 https://openai-proxy.apifox.cn/doc-2721086\n\n## 支持 Google Bard API（非官方）\n\n\u003e **承诺：当 Google Bard 官方开放 API 时，将替换为官方 API 请求**\n\n\u003e 官方 API 申请：https://www.googlecloudcommunity.com/gc/AI-ML/Google-Bard-API/m-p/538517\n\n借助 [PawanOsman/GoogleBard](https://github.com/PawanOsman/GoogleBard) 现成的封装，现支持通过 api 请求 Google Bard\n\n```bash\ncurl -X POST https://openai.aihey.cc/google/bard \\\n  -H \"Content-Type: application/json\" \\\n  -H \"Cookie: __Secure-1PSID=*****;__Secure-1PSIDTS=*******\" \\\n  -d '{\n    \"prompt\": \"hello\"\n  }'\n```\n\n\u003e `__Secure-1PSID` 获取方式如下图，网页端打开 Google Bard 后，F12 查看 Cookies。\n\u003e `__Secure-1PSIDTS` 获取方式同理\n\u003e ![googlebard.png](assets/googlebard.png)\n\n## 🎉 自行部署\n\n你可以使用以下任一方式进行部署，之后只需将前文中的 `https://openai.aihey.cc` 替换成你自己的即可\n\n### 方式 1：使用 vercel 部署\n\n注意：vercel 免费版本对函数执行时间存在限制，具体可参考 https://github.com/UNICKCHENG/openai-proxy/issues/10\n\n[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?demo-title=openai-proxy\u0026repository-name=openai-proxy\u0026repository-url=https://github.com/UNICKCHENG/openai-proxy\u0026from=github)\n\n\u003e 如果你希望也能在国内正常访问，可以试试域名解析的方案\n\u003e\n\u003e 1. 首先请准备一个域名\n\u003e \n\u003e 2. 去域名服务商添加 CNAME\n\u003e \n\u003e ```\n\u003e cname-china.vercel-dns.com\n\u003e ```\n\u003e\n\u003e 3. 自定义域名，注意请将 `openai.aihey.cc` 替换成你自己的域名\n\u003e ![](assets/vercel.png)\n\n\n### 方式 2：使用 Sealos 部署\n\n可参考: https://mp.weixin.qq.com/s/qbyKE_Uf_3xbWWqFKidreA\n\n[![](https://raw.githubusercontent.com/labring-actions/templates/main/Deploy-on-Sealos.svg)](https://cloud.sealos.io/?openapp=system-fastdeploy%3FtemplateName%3Dopenai-proxy)\n\n### 方式 3：使用 Docker\n\n```bash\ndocker run -itd --name openai-proxy -p 13000:3000  unickcheng/openai-proxy\n\n# 查看服务\ndocker ps -a\n```\n\n如果你熟悉 docker compose，可参考 [docker-compose.yml](./docker-compose.yml) 文件，使用 `docker compose up -d` 命令来启动服务\n\n## 👉 一些疑问\n\n### openai api key 会不会被盗用（重点）\n\n**不会，但也不要放下警惕**。市面上也有很多同类**开源**的优秀产品，都不会恶意地盗用使用者的 key，遗憾的是，这并不代表全部。因此，在使用第三方平台的产品，**尽量使用一个定期更新的 key**。\n\n如果有条件的话，建议参考前面内容自行部署，如果遇到问题，可在 Issues 中提问。\n\n### 为什么创建这个项目\n\n因为想入门 [Next.js](https://nextjs.org/docs)，所以就有了这个项目。\n\n另外，这里只是提供一个方案，对于 API 请求速度并没得到多少提升，自行部署还需要额外提供域名，似乎听着就嫌麻烦。\n\n- 如果你是名爱折腾的开发者，或许也可以尝试 Nginx、Cloudflare 等方案\n- 如果你希望有个直接拿来用的方案，那么请放心大胆地白嫖\n\n\u003cdiv align=center \u003e\n    \u003cimg src=\"./assets/support.jpg\" height = \"500\" alt=\"打赏作者\" /\u003e\n\u003c/div\u003e\n\n## 💖 感谢\n\n- \u003chttps://vercel.com/docs\u003e\n- \u003chttps://nextjs.org/docs\u003e\n- \u003chttps://platform.openai.com/docs/api-reference\u003e\n- https://github.com/PawanOsman/GoogleBard\n- https://github.com/vercel-labs/ai\n- https://claude.ai\n- \u003chttps://github.com/Danny-Dasilva/CycleTLS\u003e\n\n \u003ca href=\"https://github.com/UNICKCHENG/openai-proxy/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=UNICKCHENG/openai-proxy\" /\u003e\n \u003c/a\u003e\n\n[![Star History Chart](https://api.star-history.com/svg?repos=UNICKCHENG/openai-proxy\u0026type=Date)](https://star-history.com/#UNICKCHENG/openai-proxy\u0026Date)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FUNICKCHENG%2Fopenai-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FUNICKCHENG%2Fopenai-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FUNICKCHENG%2Fopenai-proxy/lists"}