字符串的正确方法是什么?"/>
在 ExpressJS/NestJS 中通过 HTTP 流发送长字符串的正确方法是什么?
我正在使用 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 发送缓冲区块时,可以多次记录进度。 但是当我发送字符串时,它只能被记录一次。
回答如下:这可能是由于原始
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
告诉浏览器尊重标头指定的 Content-Type
,而不是尝试根据返回内容的标头进行猜测。根据我对最新 Chrome 浏览器的测试,在浏览器正确识别 Content-Type
. 之前,最初的 1024 个字节似乎被“阻止”了
您可以在此处阅读有关此行为的更多信息:什么是“X-Content-Type-Options=nosniff”?
参考:
- https://developer.mozilla/en-US/docs/Web/HTTP/Headers/Transfer-Encoding
- https://developer.mozilla/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
更多推荐
在 ExpressJS/NestJS 中通过 HTTP 流发送长字符串的正确方法是什么?
发布评论