【接口已失效】aipro不知道如何使用?oaipro配置

佬友原帖(怎么都不能落下V佬):

1. 修改worker代码,其实就佬友代码基础上加了 /chat/completions 支持

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
    })
  }

  const path = new URL(request.url).pathname

  // 确保请求是 POST 请求,并且路径正确
  if (request.method === "POST" && (path === "/v1/chat/completions" || path === '/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`;
}

2. 打开 New API

3. 按图来操作即可,endpoint不用加路径

4. enjoy

22 个赞

这绝对是看了我帖子在搞我心态了 :rofl:

14 个赞

常规话题软件分享

哈哈哈哈或 oaipro

1 个赞

谢谢大佬 :joy:

1 个赞

始皇:好好好这么玩是吧

1 个赞

哈哈哈,佬是真滴皮,搞始皇心态!!!说个小秘密佬你好像搞错啦,看代码是应该是在另外一个佬友的基础上改的,不是在我的基础上哦~

1 个赞

你让始皇很慌

害怕

膜拜佬

玩出花了

不错

能识图

试了下返回结果要10+s,可能是什么问题?

佬 看帖子说支持4,为啥我弄好之后切成4 感觉蠢蠢

挂的我的代码,放的snaily佬的帖子 :rofl:,不过毕竟昨天晚上撞车了,估计大家都分不清了,能帮助到大家就好,发扬linux.do分享精神 :star_struck:

1 个赞

这个是减速器吗,doge

那必须把你带上

1 个赞

感谢,又能水到点数了 :money_mouth_face:

可以可以,已经使用上了