AI聊天-如何将消息发送给机器人 第4集

这篇具有很好参考价值的文章主要介绍了AI聊天-如何将消息发送给机器人 第4集。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

AI聊天-如何将消息发送给机器人

一、前言

上一篇我们讲解了如何让我们的消息发送框悬浮在固定位置,本编讲为大家讲解如何讲消息发送给机器人,这里需要用到http请求,后端需要提供对应的接口

二、源码资源

1、服务端代码已为大家准备好,请参考springboot的使用技巧
2、测试页面源码,请参考从零开始搭建AI聊天

三、实现思路

1、集成 sse客户端,并配置后端的服务请求地址

2、页面监听enter键,发送消息

3、讲返回的消息流拼接到dom并展示

四、重难度讲解

1、集成 sse客户端,并配置后端的服务请求地址

npm install eventsource

2、页面监听enter键

      <chat-send @sendMsg="sendMsg" ref="send"  @keydown.enter.native="sendText($event)"></chat-send>
    sendText(event){
      if (event.keyCode === 13) {
        this.$refs.send.sendMessages()
        return false
      }
    },

3、index组件最终源码

<template>
  <div class="container">
    <div class="aside">
      <div class="logo">logo区域</div>
      <div>聊天选择区域</div>
    </div>
    <div class="main-right">
      <div class="header">标题区域</div>
      <chat-main ref="main"></chat-main>
      <chat-send @sendMsg="sendMsg" ref="send"  @keydown.enter.native="sendText($event)"></chat-send>
    </div>
  </div>
</template>

<script>
import EventSource from 'eventsource';
import {setDeviceInfo} from "@/utils/storage";
import ChatMain from "@/components/chat-main";
import ChatSend from "@/components/chat-send";
import {chat} from "@/api/sse";
export default {
name: "index",
  components: {ChatSend, ChatMain},
  data(){
  return{
    deviceType:1, //1pc 2h5
    uid:123456
  }
  },
  mounted() {
    this.clientWidth()
    this.initSSE();
  },
  methods:{
    initSSE(){
      const url = `${process.env.VUE_APP_API_BASE_URL}/createSse`; // 替换为实际的SSE端点URL
      this.eventSource = new EventSource(url,{headers:{uid:this.uid}});
      this.eventSource.onmessage = (event) => {
        // 处理接收到的SSE数据

        let payload;
        try{
          payload =JSON.parse(event.data);
        }catch (error) {
          // console.error("内容报异常")
        }
        if(undefined!=payload){
          this.$refs.main.appendMsg(payload)
        }
      };
      this.eventSource.onerror = (error) => {
        // 处理连接错误
        console.error(error);
      };
    },
    sendMsg(textarea){
      if(null==this.eventSource){
        this.initSSE()
      }
      chat({question:textarea,uid:this.uid}).then(()=>{
        this.$refs.main.appendQ(textarea)
        this.$refs.send.echoQuestion()
      })
    },

    sendText(event){
      if (event.keyCode === 13) {
        this.$refs.send.sendMessages()
        return false
      }
    },
    clientWidth(){
      //监听宽度,判断是否关闭对应的菜单
      let deviceInfo = 'pc';
      this.deviceType = 1
      if(document.body.clientWidth>document.body.clientHeight){
        setDeviceInfo(deviceInfo);
      }else{
        deviceInfo = 'h5'
        this.deviceType = 2
        setDeviceInfo(deviceInfo);
      }
    },
  },
}
</script>

<style scoped>
.container{
  text-align: center;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.aside{
  width: 13vw;
  height: calc(100vh);
  background: #f1f0f0;
}
.logo{
  width: 13vw;
  height: 12vh;
}
.main-right{
  width: 87vw;
  height: calc(100vh);
}
.header{
  width: 100%;
  height:7vh;
  background: white;
}


</style>

4、chat-main组件源码

<template>
  <div class="chat-main">
    <div class="chat-main-content" v-for="(item,index) in msgList" :key="index">
      <li>
        <span>{{item.createTime}}</span>
        <span style="width: 100%">{{item.question}}</span>
      </li>
      <li>
        <span style="display: flex;text-align: center">
          <img src="../asserts/image/gpt.png" width="36" height="36">
          <span style="padding-left: 10px">{{item.createTime}}</span>
        </span>
        <span>
          <markdown-it-vue :content="item.answer"  class="markdown-body"  :options="options"></markdown-it-vue>
        </span>
      </li>
    </div>
  </div>
</template>

<script>
import MarkdownItVue from 'markdown-it-vue'
import 'markdown-it-vue/dist/markdown-it-vue.css'
import {formatDate} from "@/utils/date";
export default {
name: "chat-main",
  components:{MarkdownItVue},
  data(){
  return{
    msgList:[],
    options: {
      markdownIt: {
        linkify: true,
        highlight:true
      },
      linkAttributes: {
        attrs: {
          target: '_blank',
          rel: 'noopener'
        }
      }
    }
  }
  },
  methods:{
    appendQ(question){
      let param = {id:2,groupId:"148",question:question,answer:"",createTime:this.formatDate(new Date())}
      this.msgList.push(param);
      this.msgIndex = this.msgList.length -1
    },
    appendMsg(data){
      if(data.content){
        this.msgList[this.msgIndex].answer+=data.content
      }
    },
    formatDate(time){
      return formatDate(time, 'yyyy-MM-dd hh:mm:ss')
    },
  }
}
</script>

<style scoped>
.chat-main{
  height: calc(90vh);
  text-align: center;
  width: 100%;
  overflow-y: auto;
}
.markdown-body{
  color: #0c0c0c !important;
  margin-left: 10px;
  border-radius: 5px;
  padding-top: 10px;
}
.chat-main-content li{
  list-style-type: none;
  line-height: 36px;
  /*border:solid 1px white;*/
  margin: auto;
}
li:nth-child(odd) {
  /* 设置奇数项样式 */
  margin-right: 1.5vw;
  text-align: right;
  display: flex;
  flex-direction: column;
}

li:nth-child(even) {
  /* 设置偶数项样式 */
  text-align: left;
  margin-left: 1.5vw;
}

</style>

5、chat-send源码

<template>
  <div class="chat-send">
    <div style="display: flex;justify-content: center;align-content: center;align-items: center">
    <input type="textarea" v-model="textarea" class="send-input" placeholder="  请输入您的问题 按 enter 即发送" />
      <span @click="sendMessages">
        <img src="./../asserts/image/send.png" width="48" height="48">
      </span>
    </div>
  </div>
</template>

<script>
export default {
name: "chat-send",
  data(){
  return{
    textarea:"",
  }
  },
  methods:{
    sendMessages(){
      if(this.textarea==''||this.textarea==null){
        return;
      }
      if(this.textarea.length>2000){
        open("问题过长,请将您的问题多次会话描述")
        return;
      }
      this.$emit("sendMsg",this.textarea,false)
    },
    echoQuestion(){
      this.textarea="";
    }
  }
}
</script>

<style scoped>

.chat-send{
  text-align: center;
  border-radius: 10px;
  left: 56%;
  top: 92%;
  position: absolute;
  transform: translate(-50%,-50%);
}
.chat-send input{
  height: 6vh;
  width: 40vw;
  border-radius: 10px;
  background: rgba(254,255,253,0.5)
}
.chat-send button{
  height: 3vh;
  width: 3vw;
  border-radius: 10px;
}
</style>

演示效果

AI聊天-如何将消息发送给机器人 第4集,智能聊天实战,人工智能,html

五、总结

1、集成sse并初始化连接对象的过程中,需要注意请求头的传入形如一下

     this.eventSource = new EventSource(url,{headers:{uid:this.uid}});

请求头header的传入在实战项目中可以替换为token

2、对于键盘的监听 可以 采用一下事件进行监听

@keydown.enter.native="sendText($event)"

3、在追加文本信息的过程中需要用到,如:

   appendMsg(data){
      if(data.content){
        this.msgList[this.msgIndex].answer+=data.content
      }
    },

本编讲到这里,如果大佬有什么好的建议,欢迎在评论区留言,下编将为大家讲解,如何让我们的会话界面自动回到底部
最后大家请记得点赞收藏哦文章来源地址https://www.toymoban.com/news/detail-794662.html

到了这里,关于AI聊天-如何将消息发送给机器人 第4集的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • ai聊天问答知识库机器人源码,基于gpt实现的本地知识库问答实现,聊天对话效果,发送回复以及流式输出...

    现在基于gpt做自己项目的问答机器人,效果非常的好。可以把自己的文档上传上去,让机器人根据文档来进行回答。 想要实现智能AI问答功能,现在大部分都是基于向量数据库的形式。 整体的流程就是:上传文档===openai向量接口 ==== 存入向量数据库 访客咨询:  咨询问题

    2024年02月10日
    浏览(37)
  • 谷歌的智能AI聊天机器人Bard已免费开放申请,赶紧加入等待列表体验一把

    2023年,真是一个Ai元年,随意ChatGPT的大火,各路网络巨头都按捺不住了,Google更是一度启动了红色警报,这是有史以来,谷歌感受到最大的压力,不过谷歌平时也并不是没有技术沉淀的,其实很最就已经有Ai智能自然语言机器人了,LaMDA甚至都已经有了情感了,所以谷歌与及

    2024年02月06日
    浏览(37)
  • 《花雕学AI》用Edge和chrome浏览器体验GPT-4智能聊天的神奇免费插件,Sider – 聊天机器人的新选择

    你有没有想过和人工智能聊天?你有没有想过用浏览器就能和GPT-4这样的先进的聊天机器人对话?如果你有这样的想法,那么你一定要试试Sider这个神奇的免费插件。 Sider(Sider – AI Sidebar)是一款基于ChatGPT的智能侧边栏插件,是一个可以让你在Edge和chrome浏览器上与GPT-4智能聊

    2024年02月08日
    浏览(45)
  • mxxWechatBot微信机器人主动给机器人发送消息

    大家伙,我是雄雄,欢迎关注微信公众号:雄雄的小课堂。 注意: 免责声明:该工具仅供学习使用,禁止使用该工具从事违法活动,否则永久拉黑​封禁账号。 本工具我不会绝对保证对你的账号没有影响,尽量使用小号去研究学习, mxxWechatBot 不承担任何责任。 经过用户的

    2024年02月02日
    浏览(79)
  • 智能聊天机器人的实现

    ChatGPT近期以强大的对话和信息整合能力风靡全网,可以写代码、改论文、讲故事,几乎无所不能,这让人不禁有个大胆的想法,能否用他的对话模型打造一个智能机器人,可以在与好友对话中给出意想不到的回应,而且再也不用担心女朋友影响我们 打游戏 工作了。 本项目是

    2024年02月15日
    浏览(30)
  • 使用飞书机器人发送消息与文件

    本文默认你已拥有一个机器人,如果没有请点击以下链接创建机器人 检查机器人权限 如果需要跨部门发送消息,检查是否开通跨部门权限 在发布版本时选择作用范围为所有员工 机器人发送消息需要获取以下权限: 通过手机号或邮箱获取用户 ID 查看、评论和下载云空间中所

    2024年04月10日
    浏览(40)
  • JavaDemo——使用机器人发送微信消息

    原理很简单,使用机器人模拟按键,使用剪贴板把内容copy进去发送; 需要先在pc登录微信客户端,然后用机器人按键,使用ctrl+alt+w快捷键打开微信,使用ctrl+f打开搜索,黏贴好友名称进行搜索,然后黏贴要发送的消息发送即可; 此外还需要注意耗时操作,需要等待操作完成

    2024年02月13日
    浏览(34)
  • 使用飞书自定义机器人发送消息

    使用飞书机器人可以很方便的获取自动化任务的反馈: 在群里创建一个机器人: 记住下面的 webhook地址,这个是标识机器人的唯一ID,比如它的webhook地址是: \\\"https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxx-ab01-4427-xxxxx-xxxxx\\\" 然后创建程序: 发送之后的效果如下:

    2024年02月03日
    浏览(54)
  • AI智能机器人的语音识别是如何实现的 ?

    什么是智能语音识别系统?语音识别实际就是将人类说话的内容和意思转化为计算机可读的输入,例如按键、二进制编码或者字符序列等。与说话人的识别不同,后者主要是识别和确认发出语音的人并非其中所包含的内容。语音识别的目的就是让机器人听懂人类所说的语言,

    2024年02月10日
    浏览(30)
  • qq机器人账号不能发送群消息,被风控

    当我们在群里测试机器人时,发现机器人无回应,在cqhttp端可以看到群消息发送失败,账号可能被风控,如下图所示:  针对这种情况,我们可以在手机端登录该qq账号,在qq群内发送一个消息,就会弹出提示,如下图:  接下来只需点击“了解详情及处理”,跟随系统提示进

    2024年02月11日
    浏览(64)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包