【寄了】接力v佬,aipro流式支持(移步reno佬的真流式 https://linux.do/t/topic/65696)

是的,感觉 很棒。

1 个赞

马克

1 个赞

太厉害了哈

1 个赞

太强了大佬

2 个赞

太强了大佬

2 个赞

已经用上,感谢

1 个赞

nextchat,好像没有流式输出哦。

1 个赞

嗯嗯,确实是这样的,我是这样考虑的,由于数据已经到cf了,再用流式单个字的输出方式,我觉得反而慢了,我就想把数据部分全部传过来,只是利用了流式的格式

1 个赞

薅一会儿

1 个赞

马克一下

1 个赞

真是厉害啊

1 个赞

我自己部署了一个,开启流式配合chatX美滋滋。 :partying_face:

1 个赞

这是真的太舒胡啦~

1 个赞

佬们,cloudflare是不是还得要梯子才能访问呀

1 个赞

绑定自己的域名就行了,没有的话,就需要梯子了

2 个赞

mark一下

1 个赞

始皇慌了

1 个赞

:face_with_hand_over_mouth::face_with_hand_over_mouth::face_with_hand_over_mouth:第一次见到始皇慌了

1 个赞

NextChat可以用,但不是流式输出,咋整:

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; charset=UTF-8',
            '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) {
  const chunks = data.choices[0].message.content.split(' '); // 将内容分割成单词数组
  let events = '';

  events += `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: '' },
      logprobs: null,
      finish_reason: null
    }]
  })}\n\n`;

  for (const chunk of chunks) {
    events += `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: { content: chunk + " " },
        logprobs: null,
        finish_reason: null
      }]
    })}\n\n`;
  }

  events += `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: {},
      logprobs: null,
      finish_reason: 'stop'
    }]
  })}\n\n`;

  return events;
}
2 个赞

这段代码我测试了可以流式啊

1 个赞