当前位置:   article > 正文

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

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

教程

1.首先注册并且登录cloudflare

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

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

 创建Worker

部署程序

3. 编辑代码:

编辑代码:

  1. const TELEGRAPH_URL = 'https://api.openai.com';
  2. addEventListener('fetch', event => {
  3. event.respondWith(handleRequest(event.request))
  4. })
  5. async function handleRequest(request) {
  6. const url = new URL(request.url);
  7. const headers_Origin = request.headers.get("Access-Control-Allow-Origin") || "*"
  8. url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');
  9. const modifiedRequest = new Request(url.toString(), {
  10. headers: request.headers,
  11. method: request.method,
  12. body: request.body,
  13. redirect: 'follow'
  14. });
  15. const response = await fetch(modifiedRequest);
  16. const modifiedResponse = new Response(response.body, response);
  17. // 添加允许跨域访问的响应头
  18. modifiedResponse.headers.set('Access-Control-Allow-Origin', headers_Origin);
  19. return modifiedResponse;
  20. }

基本部署完成

1. 测试连接

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

2. 成功示例

  1. {
  2. "error": {
  3. "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.",
  4. "type": "invalid_request_error",
  5. "param": null,
  6. "code": null
  7. }
  8. }

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

部署域名

1. 点开触发器

添加自定义域

2. 输入自定义域名 

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

 3. 最终成品

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号