ChatGPT、GPT-4 Turbo接口调用(stream模式)

接口地址

https://chat.xutongbao.top/api/light/chat/createChatCompletion

请求方式

post

请求参数

model可选值:

"gpt-3.5-turbo-1106"、 "gpt-3.5-turbo-16k" 、 "gpt-4"、"gpt-4-1106-preview"。 默认值为: "gpt-3.5-turbo-1106"

token获取方式:

访问:https://chat.xutongbao.top/

使用邮箱注册账号

点击【我的】

点击【API】

前端发起请求的方法

fetch

示例代码

html 复制代码
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button onclick="handleSend()">发送</button>
    <div id="result"></div>
    <script>
      async function handleSend() {
        let messageHot = ''
        document.getElementById('result').innerHTML = ''

        const response = await fetch(
          `https://chat.xutongbao.top/api/light/chat/createChatCompletion`,
          {
            method: 'post',
            headers: {
              'Content-Type': 'application/json',
              Accept: 'text/event-stream',
            },
            body: JSON.stringify({
              model: 'gpt-3.5-turbo-1106',
              token: 'sk-3d76d415-dd72-43ff-b7c8-65fb426f1d7b',
              messages: [
                {
                  role: 'user',
                  content: '你好',
                },
              ],
              params: {
                n: 1,
                stream: true,
              },
            }),
          }
        )

        if (!response.body) return

        const reader = response.body.getReader()
        // 创建了一个文本解码器
        const decoder = new TextDecoder()

        let count = 0
        reader.read().then(function processText({ done, value }) {
          if (done) {
            messageHot += '【结束】'
            document.getElementById('result').innerHTML = messageHot
            return
          }
          let text = decoder.decode(value)
          if (text) {
            if (
              text.length > 9 &&
              text.slice(text.length - 9) === 'undefined'
            ) {
              text = text.slice(0, text.length - 9)
            }
            let start852Index = text.indexOf('start852')
            let end852Index = text.indexOf('end852')

            if (start852Index >= 0 && end852Index >= 0) {
              let headerData = text.slice(start852Index + 8, end852Index)
              console.log('headerData', headerData)
              text = text.slice(end852Index + 6)
            }

            messageHot += text
            document.getElementById('result').innerHTML = messageHot
            count++
            console.log('次数', count, text)
          }

          return reader.read().then(processText)
        })
      }
    </script>
  </body>
</html>
相关推荐
海天胜景2 分钟前
vue3 el-table 列增加 自定义排序逻辑
javascript·vue.js·elementui
今晚打老虎z6 分钟前
dotnet-env: .NET 开发者的环境变量加载工具
前端·chrome·.net
用户38022585982412 分钟前
vue3源码解析:diff算法之patchChildren函数分析
前端·vue.js
烛阴17 分钟前
XPath 进阶:掌握高级选择器与路径表达式
前端·javascript
Naiva20 分钟前
【小技巧】Python + PyCharm 小智AI配置MCP接入点使用说明(内测)( PyInstaller打包成 .exe 可执行文件)
开发语言·python·pycharm
小鱼小鱼干21 分钟前
【JS/Vue3】关于Vue引用透传
前端
JavaDog程序狗22 分钟前
【前端】HTML+JS 实现超燃小球分裂全过程
前端
独立开阀者_FwtCoder27 分钟前
URL地址末尾加不加 "/" 有什么区别
前端·javascript·github
梦子要转行29 分钟前
matlab/Simulink-全套50个汽车性能建模与仿真源码模型9
开发语言·matlab·汽车
独立开阀者_FwtCoder30 分钟前
Vue3 新特性:原来watch 也能“暂停”和“恢复”了!
前端·javascript·github