RE: RE: Nuxt开发指南
You are viewing a single comment's thread from:

RE: Nuxt开发指南

Words
32
Reading
1 min
Listen
Play
7M

经测试,只有浏览器端的 fetch 才有 streaming 的能力,其它方法不行!
useFetch, $fetch都不行!

const url = "http://localhost:6200/gptstreaming"
const prompt = "天空为什么是蓝色的?"
let query = [{role: "user", content: prompt}]
let dataObj = {
  method: 'POST',
  headers: {
      'Content-Type': 'application/json',
  },
  body: JSON.stringify({
      query: query,
  })
}
const response = await fetch(url, dataObj)
if (response.ok) {  
  let i = 0
  let getStream = function (reader) {
      return reader.read().then(function (result) {
        // 如果数据已经读取完毕,直接返回
        if (result.done) {
          console.log(889, "result done")
          return
        }
        // 取出本段数据(二进制格式)
        let chunk = result.value
        console.log(226, chunk, typeof chunk)  //unit8array object
        let text = utf8ArrayToStr(chunk) //数据解析
        // 将本段数据追加到网页之中
        messageDiv.innerHTML += text
        // 递归处理下一段数据
        return getStream(reader)
      })
  }
  getStream(response.body.getReader())
}
@lemooljiang: 经测试,只有浏览器端的 fetch | Ecency