另外,在小程序上。开启了右下角的调试模式,就可以正常收到完整的chunk。但一关闭调试模式,接收的chunk就会粘连在一起。
关于1.06.2412050 Stable SSE/Checked请求严重BUG?const req = wx.request({ url: BASE_URL + '/api/chat/createAndChat', method: 'POST', responseType: 'text', enableChunked: true, timeout: 1800000, header: { 'Authorization': uni.getStorageSync('token'), 'Accept': 'text/event-stream', 'Content-Type': 'application/json', }, data: { conversation_id: this.conversation_id, bot_id: this.botId, content: this.inputMessage, }, complete: (res) => { console.log(res); } }) req.onChunkReceived((response) => { const arrayBuffer = response.data; console.log(response.data); const uint8Array = new Uint8Array(arrayBuffer); // let text = uni.arrayBufferToBase64(uint8Array) let item = this.Utf8ArrayToStr(uint8Array); let json = item.split('\r\n'); json.forEach((item) => { if (item) { let obj = JSON.parse(item); //后面代码省略 该代码段在最新的开发者工具中无法调试,一直运行不出来。但我把版本回退到1.06.2407120 就可以了。找问题找了好几个晚上!!!!
03-16请问你解决了吗?
真机调试 onChunkReceived 调用流式接口无返回?但是开发者工具可以async sendMessageStream({ messages, onMessage }) { const token = uni.getStorageSync('token') if (!token) { throw new Error('请先登录') } const balance = await this.checkBalance() if (balance <= 0) { throw new Error('余额不足,请充值') } try { let hasStopSignal = false; let hasError = false; const requestTask = uni.request({ url: 'https://api.xxx.com/v1/chat/completions', method: 'POST', timeout: 100000, header: { 'Content-Type': 'application/json', 'Authorization': `Bearer xxx`, 'Accept': 'text/event-stream' }, data: { model: this.config.model || 'gpt-3.5-turbo', messages: messages, stream: true }, enableChunked: true, success: (res) => { console.log('请求成功:', res) if (res.statusCode !== 200) { hasError = true; onMessage({ content: '', done: true, error: `请求失败: ${res.statusCode}` }); } }, fail: (err) => { console.error('请求失败:', err); hasError = true; onMessage({ content: '', done: true, error: err.errMsg || '请求失败' }); }, complete: () => { console.log('请求完成'); // 如果没有收到停止信号且没有错误,则发送完成信号 if (!hasStopSignal && !hasError) { onMessage({ content: '', done: true }); } } }); // 添加分块接收监听 requestTask.onChunkReceived((res) => { if (hasError) return; // 如果有错误,不处理数据块 console.log('收到数据块:', res.data) const result = this.parseSSEChunk(res.data); // 检查是否是停止信号 if (result.done) { hasStopSignal = true; } onMessage(result); }); return requestTask; } catch (error) { console.error('请求错误:', error) throw error } } // 添加SSE解析方法 parseSSEChunk(chunk) { // 处理二进制数据 if (typeof chunk !== 'string') { try { chunk = new TextDecoder().decode(chunk); } catch(e) { console.error('数据解码失败:', e); return { content: '', done: false }; } } const lines = chunk.split('\n').filter(line => line.trim()); console.log('解析后的数据:', lines) for (const line of lines) { const [key, ...values] = line.split(': '); const value = values.join(': '); // 重新组合可能包含 : 的值 if (key === 'data') { try { const data = JSON.parse(value); console.log('解析后的数据:', data) // 返回内容 if (data.choices?.[0]?.delta?.content) { return { content: data.choices[0].delta.content, done: false }; } // 检查是否是结束标记 if (data.choices?.[0]?.finish_reason === 'stop') { return { content: data.choices?.[0]?.delta?.content || '', done: true }; } } catch(e) { console.error('JSON解析失败:', e, '数据:', value); } } } return { content: '', done: false }; }
03-162023年了,请问解决了吗?
Android端textarea组件使用auto-height属性后,高度变得非常大获取string数组后,通过v-for循环生成多个textarea(auto-height=true)。 期望列表中每个textarea都能根据文本长度自动调整高度,但Android端微信textarea高度异常,变得非常的高。如下图所示。 [图片] 如果点击textarea获取焦点,弹出输出法后再关闭输入法,textarea的高度恢复正常,如下图 [图片] [视频]
2023-01-01