Skip to content

Commit

Permalink
关闭服务器,推荐自己部署 (justjavac#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
justjavac authored Jun 2, 2023
1 parent cc5e0d7 commit d9fcaed
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
37 changes: 21 additions & 16 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
# OpenAI/ChatGPT 免翻墙代理

# **⚠️⚠️⚠️ 由于 https://closeai.deno.dev 访问量巨大,已经超过了免费额度,本代理暂时关闭,请大家参照教程自行部署。⚠️⚠️⚠️**

据很多网友反应,**OpenAI 检测到中国的 API
访问时,会直接封号**。所以我在国外的服务器上搭建了一个代理,用于访问
OpenAI/ChatGPT 的 API。

## 自己部署

### Deno

点击[这个链接][1],可以快速一键部署到 Deno Deploy 上。

然后在 Settings 选项卡里可以设置自定义二级域名,或者绑定自己的域名。

或者,访问 https://deno.new 域名,把 deno.ts 复制到 Playground 中,点击 Play
按钮。

### CloudFlare

将 cloudflare.ts 复制到 CloudFlare Workers 中。

## 使用

使用 OpenAI/ChatGPT 官方 npm 包:
Expand All @@ -13,7 +30,7 @@ import { Configuration } from "openai";

const configuration = new Configuration({
apiKey: OPENAI_API_KEY,
+ basePath: "https://closeai.deno.dev/v1",
+ basePath: "https://xxxxx.deno.dev/v1",
});
```

Expand All @@ -23,7 +40,7 @@ const configuration = new Configuration({
import openai

openai.api_key = os.getenv("OPENAI_API_KEY")
+ openai.api_base = "https://closeai.deno.dev/v1"
+ openai.api_base = "https://xxxxx.deno.dev/v1"
```

## 相关仓库
Expand All @@ -33,19 +50,7 @@ const configuration = new Configuration({
## 本地开发

```bash
deno run --allow-net --allow-read --allow-env --watch main.ts
deno run --allow-net --allow-read --allow-env --watch deno.ts
```

## 自己部署

### Deno

点击[这个链接][1],可以快速一键部署到 Deno Deploy 上。

然后在 Settings 选项卡里可以设置自定义二级域名,或者绑定自己的域名。

[1]: https://dash.deno.com/new?url=https://raw.githubusercontent.com/justjavac/openai-proxy/main/main.ts

### CloudFlare

将 cloudflare.ts 复制到 CloudFlare Workers 中。
[1]: https://dash.deno.com/new?url=https://raw.githubusercontent.com/justjavac/openai-proxy/main/deno.ts
40 changes: 19 additions & 21 deletions cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
//copy this to cloudflare workers
export default {
async fetch(request, env) {
try {
const OPENAI_API_HOST = "api.openai.com";
const oldUrl = new URL(request.url);
async fetch(request, env) {
try {
const OPENAI_API_HOST = "api.openai.com";
const oldUrl = new URL(request.url);

if (oldUrl.pathname === "/") {
return new Response(`https://${oldUrl.hostname}/v1`, { status: 200 });
}

if (oldUrl.pathname === "/") {
return new Response(`https://${oldUrl.hostname}/v1`, {status: 200});
}
const newUrl = new URL(request.url);
newUrl.hostname = OPENAI_API_HOST;

const newUrl = new URL(request.url);
newUrl.hostname = OPENAI_API_HOST;
const modifiedRequest = new Request(newUrl, {
method: request.method,
headers: request.headers,
body: request.body,
});

const modifiedRequest = new Request(newUrl, {
method: request.method,
headers: request.headers,
body: request.body
});

return await fetch(modifiedRequest);
} catch (e) {
return new Response(e.stack, {status: 500});
}
return await fetch(modifiedRequest);
} catch (e) {
return new Response(e.stack, { status: 500 });
}
}

},
};
14 changes: 14 additions & 0 deletions deno.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts";

const OPENAI_API_HOST = "api.openai.com";

serve(async (request) => {
const url = new URL(request.url);

if (url.pathname === "/") {
return fetch(new URL("./Readme.md", import.meta.url));
}

url.host = OPENAI_API_HOST;
return await fetch(url, request);
});
13 changes: 2 additions & 11 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { serve } from "https://deno.land/[email protected]/http/server.ts";

const OPENAI_API_HOST = "api.openai.com";

serve(async (request) => {
const url = new URL(request.url);

if (url.pathname === "/") {
return fetch(new URL("./Readme.md", import.meta.url));
}

url.host = OPENAI_API_HOST;
return await fetch(url, request);
serve(() => {
return fetch(new URL("./Readme.md", import.meta.url));
});

0 comments on commit d9fcaed

Please sign in to comment.