在ExpressJS/NestJS中通过HTTP流发送长字符串的正确方法是什么?

What is the correct way to send a long string by an HTTP stream in ExpressJS/NestJS?

提问人:Shawn White 提问时间:4/22/2023 最后编辑:Shawn White 更新时间:4/24/2023 访问量:849

问:

我正在使用 NestJS 为 openai 聊天完成 API 编写转发服务。我想对原始流进行转换,然后将流转发到客户端。

代码如下,它位于 nestJS 控制器中

const completion = await openai.createChatCompletion(
  {
    model: 'gpt-3.5-turbo',
    messages: messages,
    n: 1,
    stream: true,
    max_tokens: 4000,
  },
  { responseType: 'stream' },
);

class TransformerStream extends Transform {
  _transform(chunk, encoding, callback) {
    // If I directly forward the chunk like this, the client can receive chunk by chunk
    this.push(chunk)
    // However, if I use string, the client can't receive chunk by chunk.
    // My original code is to transform the chunk to string and do some transformation, to simplify the question, just use 'data: ping\n' here
    this.push('data: ping\n', 'utf8')
    callback()
  }
}

const transformer = new TransformerStream()
completion.data.pipe(transformer).pipe(res)

我正在使用 axios 从客户端请求 API,并且我尝试使用onDownloadProgress

axios.post('/api/chat', body, {
  responseType: 'stream',
  onDownloadProgress: progress => {
    console.log(progress)
  }
} )

综上所述,当我直接从 openAI api 发送缓冲区块时,可以多次记录进度。 但是当我发送字符串时,它只能记录一次。

节点.js express openai-api http-streaming

评论


答:

2赞 AngYC 4/24/2023 #1

这可能是由于原始字符串的长度与尝试写入流的字符串长度之间的差异。chunk

您可以考虑在 NestJS 控制器中设置以下标头:

  • Transfer-Encoding:chunked
  • X-Content-Type-Options:nosniff

示例代码:

res.setHeader('Transfer-Encoding', 'chunked');
res.setHeader('X-Content-Type-Options', 'nosniff');

Transfer-Encoding告诉浏览器开始处理数据,而不是等待所有内容先加载

X-Content-Type-Options告诉浏览器尊重标头指定的内容,而不是尝试根据返回内容的头部进行猜测。根据我对最新 Chrome 浏览器的测试,在浏览器正确识别 .Content-TypeContent-Type

您可以在此处阅读有关该行为的更多信息:什么是“X-Content-Type-Options=nosniff”?

参考:

评论

0赞 Shawn White 4/24/2023
谢谢。添加帮助。由服务器自动添加。现在可以触发 xhr 的 progress 事件。X-Content-Type-OptionsTransfer-Encoding
1赞 Shawn White 4/24/2023
我还发现,如果我添加,没有标题,也可以触发进度事件。解释似乎是,如果嗅探会在未指定或错误时发生。Content-Type: application/octet-streamnosniffContent-Type