• 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>

  • 相关阅读:
    Linux查看程序和动态库依赖的动态库
    JAVA面试题总结基础篇(三)
    简易人工智能入门
    【笔试强训选择题】Day40.习题(错题)解析
    【再探】设计模式-设计原则
    NAND价格第4季度回暖,现在是SSD入手时机吗?
    37.普利姆(Prim)算法
    基于马尔可夫随机场的图像去噪算法matlab仿真
    让Pegasus天马座开发板用上OLED屏
    spring
  • 原文地址:https://blog.csdn.net/xutongbao/article/details/134337330