Cloudflare Worker 中转

使用 Cloudflare Worker 实现中转

参考 “不做了睡觉” 的教程Cloudflare Worker 中转 - 图1

workers 配置文件

  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. // 安全校验
  7. if (request.headers.get('auth') !== 'auth_code') {
  8. return new Response('UnAuthorization', { status: 403 });
  9. }
  10. const url = new URL(request.url);
  11. url.host = TELEGRAPH_URL.replace(/^https?:\/\//, '');
  12. const modifiedRequest = new Request(url.toString(), {
  13. headers: request.headers,
  14. method: request.method,
  15. body: request.body,
  16. redirect: 'follow'
  17. });
  18. const response = await fetch(modifiedRequest);
  19. const modifiedResponse = new Response(response.body, response);
  20. // 添加允许跨域访问的响应头
  21. modifiedResponse.headers.set('Access-Control-Allow-Origin', '*');
  22. return modifiedResponse;
  23. }

修改 FastGPT 的环境变量

务必别忘了填 v1!

  1. OPENAI_BASE_URL=https://xxxxxx/v1
  2. OPENAI_BASE_URL_AUTH=auth_code