├── .vscode └── settings.json ├── Readme.md ├── cloudflare.ts ├── deno.ts └── main.ts /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "deno.enable": true, 3 | "deno.lint": true, 4 | "deno.unstable": false 5 | } 6 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # OpenAI/ChatGPT 免翻墙代理 2 | 3 | 据很多网友反应,**OpenAI 检测到中国的 API 4 | 访问时,会直接封号**。所以我在国外的服务器上搭建了一个代理,用于访问 5 | OpenAI/ChatGPT 的 API。 6 | 7 | --- 8 | 9 | **⚠️⚠️⚠️⚠️⚠️⚠️\ 10 | 由于 https://closeai.deno.dev 访问量巨大,已经超过了 Deno Deploy 11 | 的额度,本代理暂时关闭,请大家参照教程自行部署。\ 12 | ⚠️⚠️⚠️⚠️⚠️⚠️** 13 | 14 | ## 自己部署 15 | 16 | ### Deno 17 | 18 | 点击[这个链接][1],可以快速一键部署到 Deno Deploy 上。 19 | 20 | 然后在 Settings 选项卡里可以设置自定义二级域名,或者绑定自己的域名。 21 | 22 | 或者,访问 https://deno.new 域名,把 deno.ts 复制到 Playground 中,点击 Play 23 | 按钮。 24 | 25 | ### CloudFlare 26 | 27 | 将 cloudflare.ts 复制到 CloudFlare Workers 中。 28 | 29 | ## 使用 30 | 31 | 使用 OpenAI/ChatGPT 官方 npm 包: 32 | 33 | ```diff 34 | import { Configuration } from "openai"; 35 | 36 | const configuration = new Configuration({ 37 | apiKey: OPENAI_API_KEY, 38 | + basePath: "https://xxxxx.deno.dev/v1", 39 | }); 40 | ``` 41 | 42 | 使用 OpenAI/ChatGPT 官方 Python 包: 43 | 44 | ```diff 45 | import openai 46 | 47 | openai.api_key = os.getenv("OPENAI_API_KEY") 48 | + openai.api_base = "https://xxxxx.deno.dev/v1" 49 | ``` 50 | 51 | ## 相关仓库 52 | 53 | - [ChatGPT 从入门到精通](https://github.com/justjavac/chatgpt) 54 | 55 | ## 本地开发 56 | 57 | ```bash 58 | deno run --allow-net --allow-read --allow-env --watch deno.ts 59 | ``` 60 | 61 | [1]: https://dash.deno.com/new?url=https://raw.githubusercontent.com/justjavac/openai-proxy/main/deno.ts 62 | -------------------------------------------------------------------------------- /cloudflare.ts: -------------------------------------------------------------------------------- 1 | //copy this to cloudflare workers 2 | export default { 3 | async fetch(request, env) { 4 | try { 5 | const OPENAI_API_HOST = "api.openai.com"; 6 | const oldUrl = new URL(request.url); 7 | 8 | if (oldUrl.pathname === "/") { 9 | return new Response(`https://${oldUrl.hostname}/v1`, { status: 200 }); 10 | } 11 | 12 | const newUrl = new URL(request.url); 13 | newUrl.hostname = OPENAI_API_HOST; 14 | 15 | const modifiedRequest = new Request(newUrl, { 16 | method: request.method, 17 | headers: request.headers, 18 | body: request.body, 19 | }); 20 | 21 | return await fetch(modifiedRequest); 22 | } catch (e) { 23 | return new Response(e.stack, { status: 500 }); 24 | } 25 | }, 26 | }; 27 | -------------------------------------------------------------------------------- /deno.ts: -------------------------------------------------------------------------------- 1 | const OPENAI_API_HOST = "api.openai.com"; 2 | 3 | Deno.serve(async (request) => { 4 | const url = new URL(request.url); 5 | url.host = OPENAI_API_HOST; 6 | 7 | const newRequest = new Request(url.toString(), { 8 | headers: request.headers, 9 | method: request.method, 10 | body: request.body, 11 | redirect: "follow", 12 | }); 13 | return await fetch(newRequest); 14 | }); 15 | -------------------------------------------------------------------------------- /main.ts: -------------------------------------------------------------------------------- 1 | Deno.serve(() => { 2 | return fetch(new URL("./Readme.md", import.meta.url)); 3 | }); 4 | --------------------------------------------------------------------------------