worker aipro 不生效

教程来自https://linux.do/t/topic/65203 再次感谢
@ wo_zu_long 的教程 !

workjs代码

export default {
  async fetch(request, env, ctx) {
   

    addEventListener("fetch", event => {
      event.respondWith(handleRequest(event.request))
    })
    
    async function handleRequest(request) {
    
      if (request.method === "OPTIONS") {
        return new Response(null, {
          headers: {
            'Access-Control-Allow-Origin': '*',
            "Access-Control-Allow-Headers": '*'
          }, status: 204
        })
      }
    
      // 确保请求是 POST 请求,并且路径正确
      if (request.method === "POST" && new URL(request.url).pathname === "/v1/chat/completions") {
        const url = 'https://multillm.ai-pro.org/api/openai-completion'; // 目标 API 地址
        const headers = new Headers(request.headers);
    
        // 添加或修改需要的 headers
        headers.set('Content-Type', 'application/json');
    
        // 获取请求的 body 并解析 JSON
        const requestBody = await request.json();
        const stream = requestBody.stream;  // 获取 stream 参数
    
        // 构造新的请求
        const newRequest = new Request(url, {
          method: 'POST',
          headers: headers,
          body: JSON.stringify(requestBody) // 使用修改后的 body
        });
    
        try {
          // 向目标 API 发送请求并获取响应
          const response = await fetch(newRequest);
          const responseData = await response.json(); // 解析响应数据
    
          // 如果 stream 参数为 true,则使用事件流格式发送响应
          if (stream) {
            return new Response(eventStream(responseData), {
              headers: {
                'Access-Control-Allow-Origin': '*',
                "Access-Control-Allow-Headers": '*',
                'Content-Type': 'text/event-stream',
                'Cache-Control': 'no-cache',
                'Connection': 'keep-alive'
              }
            });
          } else {
            // 如果不是流式响应,则直接返回响应数据
            return new Response(JSON.stringify(responseData), {
              status: response.status,
              headers: response.headers
            });
          }
        } catch (e) {
          // 如果请求失败,返回错误信息
          return new Response(JSON.stringify({ error: 'Unable to reach the backend API' }), { status: 502 });
        }
      } else {
        // 如果请求方法不是 POST 或路径不正确,返回错误
        return new Response('Not found', { status: 404 });
      }
    }
    
    function eventStream(data) {
      // Simplified eventStream function that does not split the content into chunks
      return `data: ${JSON.stringify({
        id: data.id,
        object: 'chat.completion.chunk',
        created: data.created,
        model: data.model,
        system_fingerprint: data.system_fingerprint,
        choices: [{
          index: 0,
          delta: { role: 'assistant', content: data.choices[0].message.content },
          logprobs: null,
          finish_reason: data.choices[0].finish_reason
        }]
      })}\n\n`;
    }


  }
};

然后点击 Deploy部署

并且绑定自定义域名
在lobeChat中检查不通过:


请问是我js代码不正确吗?

image

是我这块worker代码写错了吗?

我好像找到问题所在了

应该删除原有的workjs代码然后放入大佬给的js代码吗?
但是我还是不生效 :sob: :sob: :sob: :sob:
image

成功了,此贴完结,我使用newAPi再中转了一道
image
:grin: :grin:

我是直接生效了呀

Newapi这么设置的

是将原本worker的代码删除然后粘贴入大佬的代码吗?

newAPI直接将部署好的worker地址放入代理地址即可

模型填那个

是的,直接删了再贴上来

模型填gpt-4就行

然后将worker地址放入lobechat的自定义模型就可以使用吗?

需不需要加后缀什么的

不自定义的可以,但是需要翻墙,自定义域名等了几分钟生效了,不要翻墙就行。我用的是nextchat,直接填入就行了

!可以 ,我使用next chat直接放入域名即可了!

不用newapi了吧

这个GPT4,我问他知识截止时间,第一次说是2023年12月,第二次是2024年4月9日,第三次又是2023年12月,第四次变成2021年12月,之后再问几次都是2021年12月

但是lobe好像不行,是我哪里出错了吗?
image