@OptIn(BetaOpenAI::class) @GetMapping("generate", produces = [MediaType.TEXT_EVENT_STREAM_VALUE]) fun chat(): Flow<String> { return chatService.chat().map { chat -> var content = "" chat.choices.forEach { chatChunk -> chatChunk.delta?.content?.apply { content += this } } // print("content == $content") Base64.getEncoder().encodeToString(content.toByteArray(Charsets.UTF_8)) } } 前端使用base64界面后就可以了 const arrayBuffer = buffer.data; const uint8Array = new Uint8Array(arrayBuffer); let oText = String.fromCharCode.apply(null, uint8Array) let text = oText.replaceAll("data:", "") let res = "" if (text.length > 0) { res = Base64.decode(text) } all += res this.setData({ info: all })
request 中的onChunkReceived 乱码网页,postman ,接口都表现正常,开启 enableChunked: true, 后就乱码,求解 let task = wx.request({ url: 'https://xxx/api/chat/generate', enableChunked: true, method: "GET", }) task.onChunkReceived((buffer) => { // const decoder = new TextDecoder("utf-8"); // const str = decoder.decode(new Uint8Array(buffer.data)) const arrayBuffer = buffer.data; const uint8Array = new Uint8Array(arrayBuffer); let text = String.fromCharCode.apply(null, uint8Array); console.log('onChunkReceived',text) }) chrome表现 [图片] postman [图片] 后台接口 [图片] 乱码 [图片]
2023-05-19