chatgpt-web icon indicating copy to clipboard operation
chatgpt-web copied to clipboard

Enable SSE api for text-stream

Open liut opened this issue 1 year ago • 3 comments

  • 同官网接口一致,启用SSE请求协议
  • 为了近一步减少流量,简化了stream相关的消息体,其他暂时用不到的字段先忽略不输出
  • 同时提升参数csid(即conversionId)的位置
interface StreamMessage {
  id: string
  csid?: string
  pmid?: string
  delta: string
  text?: string
  finishReason?: string
}
  • 相应的,添加服务接口 /api/chat-sse,用于实现 'text/event-stream' 输出

liut avatar Apr 07 '23 05:04 liut

修订了在接收消息的过程中只使用delta,如图 chatgpt-web-sse-msg-2 text只出现在最后一波

liut avatar Apr 07 '23 18:04 liut

官方API和accessToken(通过代理)方式都已测试通过。 @Chanzhaoyu

liut avatar Apr 12 '23 09:04 liut

应该不需要这么复杂

import { PassThrough } from "stream";
// 请求里
 const steamData = new PassThrough();
  ctx.body = steamData;
steamData.write(firstChunk ? JSON.stringify(chat) : `\n${JSON.stringify(chat)}`);

这样就可以了 拉了你的代码用vercel依旧没法实现打字机效果

pangao66 avatar Apr 25 '23 11:04 pangao66

应该不需要这么复杂

import { PassThrough } from "stream";
// 请求里
 const steamData = new PassThrough();
  ctx.body = steamData;
steamData.write(firstChunk ? JSON.stringify(chat) : `\n${JSON.stringify(chat)}`);

这样就可以了 拉了你的代码用vercel依旧没法实现打字机效果

我的这个版本在SG表现正常(一次一词)、HK表现不太正常(一次约5-20词),应该和网络速度有关。

另外,你这段代码加在哪里?可否方便提个PR看看。

liut avatar May 06 '23 02:05 liut