利用Cloudflare搭建ChatGPT API 代理服务器 傻瓜教程

✈ChatGPT的API接口为 https://api.openai.com 但是很多小伙伴不是国家区域不对,就试没有一个好的代理,经常chatgpt出现无法聊天的情况,那么自己搭建一个代理服务器那不是很香?还是免费的,直接整起来!图文教程,无缝链接!

教程

1.首先注册并且登录cloudflare

https://www.cloudflare.com/zh-cn/

2. 打开Workers和Pages,并且创建应用程序

 创建Worker

部署程序

3. 编辑代码:

编辑代码:

const TELEGRAPH_URL = 'https://api.openai.com';

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  const url = new URL(request.url);
  const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
  url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');
  const modifiedRequest = new Request(url.toString(), {
    headers: request.headers,
    method: request.method,
    body: request.body,
    redirect: 'follow'
  });
  const response = await fetch(modifiedRequest);
  const modifiedResponse = new Response(response.body, response);
  // 添加允许跨域访问的响应头
  modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
  return modifiedResponse;
}

基本部署完成

1. 测试连接

https://你自定义的项目名.workers.dev/v1/chat/completions

2. 成功示例

{
    "error": {
        "message": "You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY), or as the password field (with blank username) if you're accessing the API from your browser and are prompted for a username and password. You can obtain an API key from https://platform.openai.com/account/api-keys.",
        "type": "invalid_request_error",
        "param": null,
        "code": null
    }
}

上面的基本就算部署成功了,但是你还需要部署你自己的域名来访问,成为你自己专属的第一无二的api接口,所以你还需要绑定域名。

部署域名

1. 点开触发器

添加自定义域

2. 输入自定义域名 

输入你在Cloudflare托管的域名,自定义一个未使用的二级域名地址,并点添加自定义域

 3. 最终成品

评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值