小程序流式输出chunk时中文乱码问题解决
为了解决小程序进行流式输出数据时会出现中文乱码的问题,在客户端传输流式数据时就先将其转换成base64编码
for chunk in response:
text = chunk.choices[0].delta.content.encode('utf8')
base64_text = base64.b64encode(text).decode('utf8')
yield base64_text
服务端先将 arraybuff 格式转为 uint8Array 格式。然后手动对base64编码进行解码操作得到字符串
requestTask.onChunkReceived(function (r) {
console.log('response',r);
const uint8Array = new Uint8Array(r.data);
console.log(uint8Array)
// 手动解析 ArrayBuffer
let decodedString = '';
for (let i = 0; i < uint8Array.length; i++) {
decodedString += String.fromCharCode(uint8Array[i]);
}
console.log(decodedString);
let str = that.base64_decode(decodedString)
that.setData({
answer: that.data.answer + str
})
小程序流式输出chunk时中文乱码问题解决