在nodejs环境下,使用axios发送post请求,在stream开启情况下,每次返回的内容最后一条不完整,必须要和之后第一条数据做合并。第一次使用stream的方式返回数据,不知道这种情况是正常的,还是bug。
这是代码:
async function mainStream() {
const response = await axios({
method: 'post',
url: 'https://open.bigmodel.cn/api/paas/v4/chat/completions',
data: {
model: 'glm-4',
messages: [
{
role: "user",
content: "介绍一下山西"
}
],
stream: true
},
headers: {
Authorization: token
},
responseType: 'stream',
})
let str = ''
let errStr = ''
response.data.on('data', (chunk) => {
const msg = chunk.toString('utf8');
console.log('---- $$$ ---');
console.log(msg);
const allMessages = chunk.toString('utf8').split("\n\n");
for (let index = 0; index < allMessages.length; index++) {
const element = allMessages[index];
let data = element.substring(6);
if (data === '[DONE]') {
break
}
try {
data = JSON.parse(data)
str += data.choices[0].delta.content
// console.log(str);
} catch (error) {
if (index === 0) {
errStr += element
let data1 = errStr.substring(6);
data1 = JSON.parse(data1)
const errData = data1.choices[0].delta.content
str += errData
// console.log(str);
errStr = ''
}
if (index + 1 === allMessages.length) {
errStr = element
}
}
}
})
response.data.on('end', (chunk) => {
console.log(str);
console.log('返回完成');
})
}
这是现象:
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"的"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"de
---- $$$ ---
lta":{"role":"assistant","content":"游客"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"应"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"提前"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"查询"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"最新的"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"限"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"行"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"a
---- $$$ ---
ssistant","content":"政策"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"。\n\n总的来说"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":","}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"山西"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"是一个"}}]}
data: {"id":"8356085236399216072","created":1706939757,"model":"glm-4","choices":[{"index":0,"delta":{"role":"assistant","content":"自然"}}]}