{"id":13455363,"url":"https://github.com/x-dr/chatgptProxyAPI","last_synced_at":"2025-03-24T08:32:12.609Z","repository":{"id":99334177,"uuid":"608735752","full_name":"x-dr/chatgptProxyAPI","owner":"x-dr","description":"🔥 使用cloudflare 搭建免费的 OpenAI api代理 ，解决网络无法访问问题。支持流式输出","archived":true,"fork":false,"pushed_at":"2023-11-20T16:54:24.000Z","size":1946,"stargazers_count":2965,"open_issues_count":1,"forks_count":674,"subscribers_count":22,"default_branch":"main","last_synced_at":"2025-01-17T20:38:10.811Z","etag":null,"topics":["chatgpt","cloudflare-pages","cloudflare-workers","free","openai-api"],"latest_commit_sha":null,"homepage":"https://chatai.451024.xyz","language":"HTML","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/x-dr.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2023-03-02T16:22:58.000Z","updated_at":"2025-01-16T10:29:02.000Z","dependencies_parsed_at":"2024-01-13T17:36:19.544Z","dependency_job_id":"e8fe0184-d61c-43cb-b00e-8f4b453ca67c","html_url":"https://github.com/x-dr/chatgptProxyAPI","commit_stats":null,"previous_names":[],"tags_count":0,"template":true,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-dr%2FchatgptProxyAPI","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-dr%2FchatgptProxyAPI/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-dr%2FchatgptProxyAPI/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/x-dr%2FchatgptProxyAPI/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/x-dr","download_url":"https://codeload.github.com/x-dr/chatgptProxyAPI/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245235331,"owners_count":20582204,"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","cloudflare-pages","cloudflare-workers","free","openai-api"],"created_at":"2024-07-31T08:01:04.431Z","updated_at":"2025-03-24T08:32:12.120Z","avatar_url":"https://github.com/x-dr.png","language":"HTML","funding_links":[],"categories":["HTML","A01_文本生成_文本对话","菜单"],"sub_categories":["大语言对话模型及数据","OpenAI代理"],"readme":"*经测试有些ip访问时会显示下图*\n\n\u003cimg src=\"https://image.3001.net/images/20231116/1700131811_6555f3e30a1efc2db761f.png\" height=\"50%\" width=\"50%\" /\u003e\u003c/a\u003e\n\n\n---\n### Demo\n\n[https://chatai.451024.xyz](https://chatai.451024.xyz)\n\n\u003e api\n\n```url\nhttps://openai.451024.xyz\n```\n\n\n```url\nhttps://openai-proxy-api.pages.dev/api\n```\n***\n\n\u003e  新项目 [基于OpenAI的微信机器人](https://github.com/x-dr/wechat-bot)\n\n***\n\n### 演示站为公共服务，如有大规模使用需求请自行部署，演示站有点不堪重负\n\n![worker](./docs/img/worker.png)\n\n\n## 利用Cloudflare Worker中转api.openai.com\n\n1. 新建一个 Cloudflare Worker\n2. 复制 [cf_worker.js](https://cdn.jsdelivr.net/gh/x-dr/chatgptProxyAPI@main/cf_worker.js)  里的代码粘贴到 Worker 中并部署\n3. 给 Worker 绑定一个没有被墙的域名\n4. 使用自己的域名代替 api.openai.com\n\n\n**[详细教程](./docs/cloudflare_workers.md)**\n\n\n\n***\n\n\n\n## 使用CloudFlare Pages进行中转\n\n### 1、部署中转API+ Openai API余额查询 (使用sess-xxxx的Authorization查询，有效时间未知)\n\n\u003e [官方文档](https://developers.cloudflare.com/pages)\n\n1. ~~Fork本项目~~ 点击[Use this template](https://github.com/x-dr/chatgptProxyAPI/generate)按钮创建一个新的代码库。\n2. 登录到[Cloudflare](https://dash.cloudflare.com/)控制台.\n3. 在帐户主页中，选择`pages`\u003e ` Create a project` \u003e `Connect to Git`\n4. 选择你 Fork 的项目存储库，在`Set up builds and deployments`部分中，全部默认即可。\n\n\n5. 点击`Save and Deploy`部署，然后点`Continue to project`即可看到访问域名\n\n\n\u003e 把官方接口的`https://api.openai.com`替换为`https://xxx.pages.dev` 即可\n\n**Demo**\n\n[https://chatai.451024.xyz](https://chatai.451024.xyz)\n\n**[详细教程](./docs/cloudflare_pages.md)**\n\n\n### 2、只部署中转API\n\n\n**[详细教程](./docs/cloudflare_proxy_pages.md)**\n\n\n\n\n## docker 部署（要境外vps） \n\n\u003e 好像不支持sse 所以不建议\n\n\u003cdetails\u003e\n\n\u003csummary\u003ee.g.\u003c/summary\u003e\n\n```bash\ndocker run -itd --name openaiproxy \\\n            -p 3000:3000 \\\n            --restart=always \\\n           gindex/openaiproxy:latest\n```\n\n#### 使用\n\n*api : http://vpsip:3000/proxy/v1/chat/completions*\n\n```bash\ncurl --location 'http://vpsip:3000/proxy/v1/chat/completions' \\\n--header 'Authorization: Bearer sk-xxxxxxxxxxxxxxx' \\\n--header 'Content-Type: application/json' \\\n--data '{\n   \"model\": \"gpt-3.5-turbo\",\n  \"messages\": [{\"role\": \"user\", \"content\": \"Hello!\"}]\n }'\n\n```\n\n\u003c/details\u003e\n\n\n\n## 用法\n\n\n\u003cdetails\u003e\n\n\u003csummary\u003eJavaScript用fetch\u003c/summary\u003e\n\n```javascript\nconst requestOptions = {\n    method: 'POST',\n    headers: {\n        \"Authorization\": \"Bearer sk-xxxxxxxxxxxx\",\n        \"Content-Type\": \"application/json\"\n    },\n    body: JSON.stringify({\n        \"model\": \"gpt-3.5-turbo\",\n        \"messages\": [\n            {\n                \"role\": \"user\",\n                \"content\": \"hello word\"\n            }\n        ]\n    })\n};\n\nfetch(\"https://openai.1rmb.tk/v1/chat/completions\", requestOptions)\n    .then(response =\u003e response.text())\n    .then(result =\u003e console.log(result))\n    .catch(error =\u003e console.log('error', error));\n  \n```\n\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\n\n\u003csummary\u003e用python\u003c/summary\u003e\n\n```python\nimport requests\n\nurl = \"https://openai.1rmb.tk/v1/chat/completions\"\napi_key = 'sk-xxxxxxxxxxxxxxxxxxxx'\n\nheaders = {\n  'Authorization': f'Bearer {api_key}',\n  'Content-Type': 'application/json'\n}\n\npayload = {\n  \"model\": \"gpt-3.5-turbo\",\n  \"messages\": [\n    {\n      \"role\": \"user\",\n      \"content\": \"hello word\"\n    }\n  ]\n}\n\ntry:\n    response = requests.post(url, headers=headers, json=payload)\n    response.raise_for_status() # 抛出异常，如果响应码不是200\n    data = response.json()\n    print(data)\nexcept requests.exceptions.RequestException as e:\n    print(f\"请求错误: {e}\")\nexcept json.JSONDecodeError as e:\n    print(f\"无效的 JSON 响应: {e}\")\n```\n\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\n\u003csummary\u003e用nodejs chatgpt库\u003c/summary\u003e\n\n[transitive-bullshit/chatgpt-api](https://github.com/transitive-bullshit/chatgpt-api)\n\n```javascript\nimport { ChatGPTAPI } from 'chatgpt'\n\nasync function example() {\n  const api = new ChatGPTAPI({\n    apiKey: \"sk-xxxxxxxxxxxxxx\",\n  // proxy+/v1\n    apiBaseUrl:\"https://openai.1rmb.tk/v1\"\n\n\n  })\n\n  const res = await api.sendMessage('Hello World!')\n  console.log(res.text)\n}\n\nexample()\n\n```\n\n\u003c/details\u003e\n\n\n\n\u003cdetails\u003e\n\n\u003csummary\u003e查询余额\u003c/summary\u003e\n\n```javascript\n    const headers = {\n      'content-type': 'application/json',\n      'Authorization': `Bearer sk-xxxxxxxxxxxxxxxxx`\n    }\n    // 查是否订阅\n    const subscription = await fetch(\"https://openai.1rmb.tk/v1/dashboard/billing/subscription\", {\n      method: 'get',\n      headers: headers\n    })\n    if (!subscription.ok) {\n      const data = await subscription.json()\n      // console.log(data);\n      return data\n      // throw new Error('API request failed')\n    } else {\n      const subscriptionData = await subscription.json()\n      const endDate = subscriptionData.access_until\n      const startDate = new Date(endDate - 90 * 24 * 60 * 60);\n      console.log(formatDate(endDate, \"YYYY-MM-DD\"));\n      console.log(formatDate(startDate, \"YYYY-MM-DD\"));\n      const response = await fetch(`https://openai.1rmb.tk/v1/dashboard/billing/usage?start_date=${formatDate(startDate, \"YYYY-MM-DD\")}\u0026end_date=${formatDate(endDate, \"YYYY-MM-DD\")}`, {\n        method: 'get',\n        headers: headers\n      })\n      \n      const usageData = await response.json();\n      // 账号类型\n      const plan = subscriptionData.plan.id\n      console.log(usageData);\n      }\n\n```\n\n\u003c/details\u003e\n\n## Star History\n\n[![Star History Chart](https://api.star-history.com/svg?repos=x-dr/chatgptProxyAPI\u0026type=Date)](https://star-history.com/#x-dr/chatgptProxyAPI\u0026Date)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fx-dr%2FchatgptProxyAPI","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fx-dr%2FchatgptProxyAPI","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fx-dr%2FchatgptProxyAPI/lists"}