uniapp开发中如何实现uni.request流式请求大模型api接口?
网友回复
uniapp进行流式请求chatgpt的api接口的代码需要分平台
1、如果最终导出微信小程序的话,那么就可以直接使用uni.request,其他小程序、app、h5都不支持uni.request进行流式请求数据了。
示例代码
var requestTaskw = uni.request({ url: 'https://api.openai.com/v1/chat/completions', // 请求地址 method: "POST", // 你的请求方法 timeout: 15000, data: JSON.stringify({ model: 'gpt4O', messages: [{ role: 'user', content: 'hello' }], temperature: 0.9, max_tokens: 100, stream: true }), header: { "Content-Type": "application/json", 'Authorization': `Bearer APIKEY`, }, responseType: "text", enableChunked: true, // 开启流传输 success: response => { console.log(response) }, fail: error => {} }); requestTaskw.onHeadersReceived(function(res) { console.log(res.header); }); // 这里监听消息 requestTaskw.onChunkReceived(function(res) { let decoder = new TextDecoder('utf-8'); let text = decoder.decode(new Uint8Array(res.data)); console.log(text) })2、如果最终导出h5页面的话,可以使用fetch
参考代码:点击打开链接
3、如果是andriod或app及其他小程序,有两种办法
3.1 内嵌h5网页,h5网页中使用fetch方式
3.2 服务端统一采用websocket进行中转,这个适合所有的客户端
服务端示例代码如下:
点击查看全文
增加一个enableChunked: true, // 开启流传输,代码如下:
const response = uni.request({ url: 'Your requested address', // 请求地址 method: "Request method", // 你的请求方法 data: data, header: { "Accept": 'tex...
点击查看剩余70%
浏览器中如何将WebM视频转成mp4视频?
parlant如何改成qwen 的apikey与baseurl?
如何写一个chrome插件实现截屏自动生成步骤图文教程转成pdf或网页?
python如何通过阿里云的api对域名进行解析和ecs主机服务器进行启动停止等操作?
Tesla Robotaxi可以让特斯拉车自动无人驾驶跑滴滴为车主赚钱,国内以后也会这样吗?
有没有可以监控安卓手机上的app打开后偷偷摸摸做了啥的监控软件?
webrtc进行p2p连接发送的文本音视频文件是否是加密的?
如何让一个可爱的三维动物通过three在浏览器中有表情动作的自然说话?
go与wails如何开发一个高性能的原生桌面应用?
python如何调用openai的api实现知识讲解类动画讲解视频的合成?