当前位置: 首页 > article >正文

微信小程序课设(基于云开发)

微信小程序通过Laf云平台接入ChatGPT实现聊天,回答方式采用流式回答。

以下是图片展示其页面

回答次数通过卡密兑换

以下是对话页面的代码

<!--pages/content/content.wxml-->

<view class="container">




<view class="div" hidden="{{AnswerList.length>0}}">

  <view>

    <image src="../../images/editor.png" mode=""/>

    <text class="one-text">AI创作</text>

  </view>

  <view>

    <image src="../../images/tip.png" mode=""/>

    <text class="one-text">有趣的提问</text>

  </view>

  <view>

    <image src="../../images/book.png" mode=""/>

    <text class="one-text">AI百科</text>

  </view>

</view>

<view class="div-two" hidden="{{AnswerList.length>0}}">

  <view class="div-two-one">

    <text bindtap="handleTextClick" data-message="写一首赞美祖国的诗">写一首赞美祖国的诗→</text>

    <text bindtap="handleTextClick" data-message="写一篇科幻小说" >写一篇科幻小说→</text>

    <text bindtap="handleTextClick" data-message="安排一场发布会流程" >安排一场发布会流程→</text>

  </view>

  <view class="div-two-one">

    <view class="div-two-one">

      <text bindtap="handleTextClick" data-message="有哪些有趣的科学实验" >有哪些有趣的科学实验→</text>

      <text bindtap="handleTextClick" data-message="一个AI也回答不出来的问题" >问一个AI也回答不出来的问题→</text>

      <text bindtap="handleTextClick" data-message="AI会替代人类工作吗?" >AI会替代人类工作吗?→</text>

    </view>

  </view>

  <view class="div-two-one">

      <text bindtap="handleTextClick" data-message="用简单的术语来解释人工智能" >用简单的术语来解释人工智能→</text>

      <text bindtap="handleTextClick" data-message="红烧牛肉的做法" >红烧牛肉的做法→</text>

      <text bindtap="handleTextClick" data-message="请介绍一下你自己" >请介绍一下你自己→</text>

  </view>

</view>



<scroll-view scroll-y="true" hidden="{{AnswerList.length==0}}">

<view class="div-answer" wx:for="{{AnswerList}}" wx:key="index">

  <view class="answer">

    <image src="../../images/49.webp"></image>

    <rich-text>{{item.message}}</rich-text>

  </view>



  <view class="answer answer-one">

    <image src="../../images/chatgpt.jpeg"></image>

    <rich-text>{{item.answer || '...'}}</rich-text>

  </view>



  </view>

</scroll-view>




<view class="question">

  <view class="question-one"><textarea disabled="{{isDisable}}" class="texta" value="{{question}}" placeholder="请输入内容" style="width: 80%;" bindinput="inputChange" ></textarea>

  <image src="../../images/send.png" bind:tap="submitQuestion"></image></view>

</view>






</view>
//流式响应格式 
const requestTask=wx.request({
        url: ,
        method:'POST',
        data:{
          message:this.data.AnswerList[this.data.AnswerList.length-1].message,
          parentMessageId:this.data.conversationId
        },
        enableChunked:true,
        success:(res)=>{
          //流式输出完成后,修改并保存balance
          wx.request({
            url: ,
            method:'POST',
            data:{
              balance:this.data.balance,
              _id:this.data._id
            },
            success:(res)=>{
              wx.setStorageSync('user', res.data)
              console.log(res.data);
            }
          })
        }
      })
      //通过流式输出响应到页面上,并保存对话id
      requestTask.onChunkReceived((response)=>{
        const arrayBuffer = response.data;
        const uint8Array = new Uint8Array(arrayBuffer);
        let text = wx.arrayBufferToBase64(uint8Array);
        text = Base64.decode(text);
       
        const t1=text.split("--!")
        if (t1.length>1) {
          this.setData({
            parentMessageId:t1[1]
          })
        }
        const len=this.data.AnswerList.length
          this.data.AnswerList[len-1]={
            message:this.data.AnswerList[len-1].message,
            answer:this.data.AnswerList[len-1].answer+t1[0]
          }
          this.setData({
            AnswerList:this.data.AnswerList,
            isDisable:false
          })
          console.log(t1);
      })

详细代码通过加企鹅获取

493305086


http://www.kler.cn/a/227731.html

相关文章:

  • 使用国内镜像加速器解决 Docker Hub 拉取镜像慢或被屏蔽的问题
  • minimind - 从零开始训练小型语言模型
  • Redis 基础命令
  • AI大模型开发原理篇-1:语言模型雏形之N-Gram模型
  • 【PyTorch】6.张量运算函数:一键开启!PyTorch 张量函数的宝藏工厂
  • 6.进程的使用方式
  • 蓝桥杯备战(AcWing算法基础课)-高精度-减-高精度
  • 01-Java工厂模式 ( Factory Pattern )
  • node + express 学生信息管理
  • MongoDB复制集实战及原理分析
  • JAVA后端上传图片至企微临时素材
  • C语言如何认识 printf()函数的格式字符?
  • 车载测试Vector工具CANape——常见问题汇总(上)
  • hdf5文件node name长度可以支持多长?
  • Tailwind CSS
  • 第十二篇【传奇开心果系列】Python的OpenCV技术点案例示例:视频流处理
  • 代码随想录训练营第三十四天|860.柠檬水找零406.根据身高重建队列
  • 蓝桥杯刷题day05——2023
  • Leetcode刷题笔记题解(C++):99. 恢复二叉搜索树
  • 【QT+QGIS跨平台编译】之二十:【xerces+Qt跨平台编译】(一套代码、一套框架,跨平台编译)
  • 寒假 day1
  • 实时聊天系统
  • 网络原理TCP/IP(4)
  • 老版本labelme如何不保存imagedata
  • 【TCP】四次挥手(终止连接)
  • Logback学习