码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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

    示例代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    6. <title>Documenttitle>
    7. head>
    8. <body>
    9. <textarea id="input">textarea>
    10. <div>
    11. <button onclick="handleSend()">发送button>
    12. div>
    13. <div id="result">div>
    14. <script>
    15. async function handleSend() {
    16. let messageHot = ''
    17. document.getElementById('result').innerHTML = ''
    18. let content = document.getElementById('input').value
    19. document.getElementById('input').value = ''
    20. const response = await fetch(
    21. `https://chat.xutongbao.top/api/light/chat/createChatCompletion`,
    22. {
    23. method: 'post',
    24. headers: {
    25. 'Content-Type': 'application/json',
    26. Accept: 'text/event-stream',
    27. },
    28. body: JSON.stringify({
    29. model: 'gpt-3.5-turbo-1106',
    30. token: 'sk-3d76d415-dd72-43ff-b7c8-65fb426f1d7b',
    31. messages: [
    32. {
    33. role: 'user',
    34. content,
    35. },
    36. ],
    37. params: {
    38. n: 1,
    39. stream: true,
    40. },
    41. }),
    42. }
    43. )
    44. if (!response.body) return
    45. const reader = response.body.getReader()
    46. // 创建了一个文本解码器
    47. const decoder = new TextDecoder()
    48. let count = 0
    49. reader.read().then(function processText({ done, value }) {
    50. if (done) {
    51. messageHot += '【结束】'
    52. document.getElementById('result').innerHTML = messageHot
    53. return
    54. }
    55. let text = decoder.decode(value)
    56. if (text) {
    57. if (
    58. text.length > 9 &&
    59. text.slice(text.length - 9) === 'undefined'
    60. ) {
    61. text = text.slice(0, text.length - 9)
    62. }
    63. let start852Index = text.indexOf('start852')
    64. let end852Index = text.indexOf('end852')
    65. if (start852Index >= 0 && end852Index >= 0) {
    66. let headerData = text.slice(start852Index + 8, end852Index)
    67. console.log('headerData', headerData)
    68. text = text.slice(end852Index + 6)
    69. }
    70. messageHot += text
    71. document.getElementById('result').innerHTML = messageHot
    72. count++
    73. console.log('次数', count, text)
    74. }
    75. return reader.read().then(processText)
    76. })
    77. }
    78. script>
    79. body>
    80. html>

  • 相关阅读:
    从 WinDbg 角度理解 .NET7 的AOT玩法
    etcd备份恢复原理详解及踩坑实录
    【华为OD机试】服务失效判断【2023 B卷|200分】
    基于PHP+MYSQL宠物领养系统的开发与设计
    【Mysql高级特性】InnoDB Checkpoint与 Redo log
    Windows 10 - 适用于各种服务(Redis、MySQL)的文件迁移到其他目录后,导致的各种服务找不到的问题 - 注册服务 - 关闭服务 - 重启服务
    输出所有最长公共子序列
    SpringCloud详解
    排序算法:计数排序
    【C++】开源:iceoryx通信中间件配置与使用
  • 原文地址:https://blog.csdn.net/xutongbao/article/details/134337330
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号