vue+AI智能机器人回复

操作步骤

  • 引入前端代码

    前端代码是参考github上的一个开源项目,里面包括AI机器人回复和聊天室两个模块,这里只抽取出来一个AI机器人回复的前端,有兴趣的话,可以点击查看

  • 封装好代理与请求

    因为第三方API的请求是外网的,存在跨域问题,所以要配置代理,配置如下:

    文件:vue.config.js

    const vueConfig = {
        //上面还有项目的其他配置
        
        devServer: {
            port: 8000,
            proxy: {
              '/ai': {
                target: 'http://openapi.tuling123.com/',
                changeOrigin: true,
                pathRewrite: {'^/ai': ''}
              }
            }
        },
    }
    module.exports = vueConfig
    

    配完代理后,创建请求实例:

    文件: request.js

    // 创建AI机器人回复请求axios实例
    const aiService = axios.create({
      //VUE_APP_AI_BASE_URL=/ai
      //baseURL: process.env.VUE_APP_AI_BASE_URL,
      baseURL: '/ai',
      timeout: 10000
    })
    
    ……
    
    export {
      aiService as aiAxios
    }
    
  • 调用第三方AI机器人的API

    第三方AI机器人有很多,笔者尝试过阿里和图灵两个,调用方式都差不多,但是阿里的有点小贵,所以这里以图灵的为示例:

    aiAxios.post('/openapi/api/v2', {
          reqType: '0',
          perception: {
            inputText: {
              text: this.inputContent
            }
          },
          userInfo: {
            //图灵上注册后自己的机器人apikey
            apiKey: '****',
            //登录用户用账户ID
            userId: '123456'
          }
        }).then(res => {
          let text= res.data.results[0].values.text;
          this.msgs.push({
            date: moment().format('YYYY-MM-DD HH:mm:ss'),
            from: '智能机器人',
            content: text,
            self: false,
            avatarUrl: aiHeadImg
          })
          this.$refs.chattingContent.scrollTop = this.$refs.chattingContent.scrollHeight
        }).catch(err => {
          this.$message.info(err);
    })
    

整体示例代码

<template lang="html">
  <transition name="slide-right">
    <div class="chatting">
      <!-- 聊天界面头部 -->
      <div class="chatting-header">
        <div class="chatting-back">
          <i class="icon-back"></i>
        </div>
        <div class="chatting-title">
          <h2>AI 智能机器人</h2>
        </div>
        <div class="chatting-menu">
          <i class="icon-menu"></i>
        </div>
      </div>
      <!-- 聊天内容区域 -->
      <div ref="chattingContent" id="chattingContent" class="chatting-content">
        <div v-for="item of msgs">
          <!--用户输入内容-->
          <div v-if="item.self" class="chatting-item self clearfix">
            <div class="msg-date">
              {{ item.date }}
            </div>
            <div class="msg-from">
              <span class="msg-author">{{ item.from}}</span>
              <img :src="item.avatarUrl" alt="">
            </div>
            <div class="msg-content">{{ item.content }}</div>
          </div>
          <!--AI回复内容-->
          <div v-else class="chatting-item other clearfix">
            <div class="msg-date">
              {{ item.date }}
            </div>
            <div class="msg-from">
              <img :src="item.avatarUrl" alt="">
              <span class="msg-author">{{ item.from }}</span>
            </div>
            <div class="msg-content">{{ item.content }}</div>
          </div>
        </div>
      </div>
      <!-- 输入区域 -->
      <div class="chatting-input">
        <input @keyup.enter="send" v-model.trim="inputContent" placeholder="与智能机器人聊些啥">
        <button @click="send">发送</button>
      </div>
    </div>
  </transition>

</template>

<script>
  import {aiAxios} from '../../../utils/request'
  import moment from 'moment'
  //下面两张头像自己从网上随便找两张
  import aiHeadImg from '../../../assets/web/pub/images/head-ai.svg'
  import clientHeadImg from '../../../assets/web/pub/images/pltx.png'

  export default {
    name: 'chatting',
    data() {
      return {
        msgs: localStorage.msgs_ai && JSON.parse(localStorage.msgs_ai) || [],
        inputContent: '',
        oContent: {}
      }
    },
    watch: {
      msgs(val) {
        localStorage.msgs_ai = JSON.stringify(val);
      }
    },

    mounted() {
      this.oContent = document.getElementById('chattingContent');
      setTimeout(() => {
        this.$refs.chattingContent.scrollTop = this.$refs.chattingContent.scrollHeight
      }, 0)
    },
    methods: {
      //发送消息
      send() {
        this.oContent.scrollTop = this.oContent.scrollHeight;
        if (this.inputContent === '') {
          return;
        }

        this.msgs.push({
          date: moment().format('YYYY-MM-DD HH:mm:ss'),
          from: this.userInfo.personname || '匿名',
          content: this.inputContent,
          self: true,
          avatarUrl: clientHeadImg
        });
        setTimeout(() => {
          this.$refs.chattingContent.scrollTop = this.$refs.chattingContent.scrollHeight
        }, 0)

        this.getClientRobotReply()
        this.inputContent = '';
      },
      //图灵AI机器人回复
      getClientRobotReply() {
        aiAxios.post('/openapi/api/v2', {
          reqType: '0',
          perception: {
            inputText: {
              text: this.inputContent
            }
          },
          userInfo: {
            //图灵上注册后自己的机器人apikey
            apiKey: '****',
            //登录用户用账户ID
            userId: '123456'
          }
        }).then(res => {
          let text= res.data.results[0].values.text;
          this.msgs.push({
            date: moment().format('YYYY-MM-DD HH:mm:ss'),
            from: '智能机器人',
            content: text,
            self: false,
            avatarUrl: aiHeadImg
          })
          this.$refs.chattingContent.scrollTop = this.$refs.chattingContent.scrollHeight
        }).catch(err => {
          this.$message.info(err);
        })
      }
    }
  }
</script>

<style  lang="less" scoped>
  .chatting {
    display: flex;
    flex-direction: column;

    width: 100%;
    height: 100%;

    .chatting-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 50px;
      width: 100%;
      background-color: #2196f3;
      color: white;
      padding-left: 10px;
      padding-right: 15px;

      .chatting-back {
        width: 30px;
        height: 30px;
        i.icon-back {
          /*background: url('../../common/icons/icon-group2.svg') no-repeat;*/
          background-size: contain;
        }
      }

      .chatting-title {
        i.icon-group {
          vertical-align: top;
          width: 30px;
          height: 30px;
          //background: url('./images/icon-group.svg') no-repeat;
          background-size: contain;
          margin-right: 3px;
        }
      }

      .chatting-menu {
        width: 30px;
        height: 30px;
        i.icon-menu {
          /*background: url('../../common/icons/icon-index.svg') no-repeat;*/
          background-size: contain;
        }
      }
    }

    .chatting-content {
      flex: 1;
      width: 100%;
      background-color: rgba(0, 0, 0, .1);
      overflow: auto;
      .chatting-item {
        padding: 10px;
        width: 100%;
        .msg-date {
          text-align: center;
          color: gray;
          font-size: 80%;
        }
        .msg-from {
          display: flex;
          align-items: center;
          span.loc {
            color: gray;
            font-size: 60%;
            margin-right: 5px;
          }
          .msg-author {
            font-size: 1.2rem;
          }
          img {
            width: 30px;
            height: 30px;
            border-radius: 15px;
          }
        }
        .msg-content {
          margin-top: 5px;
          background-color: white;
          width: 200px;
          padding: 6px 10px;
          border-radius: 10px;
        }
      }

      .chatting-item + .chatting-item {
        margin-top: 10px;
      }
      .self {
        .msg-from {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          img {
            margin-left: 10px;
          }
        }

        .msg-content {
          float: right;
          word-wrap: break-word;
          word-break: break-all;
          margin-right: 10px;
        }


      }

      .other {
        .msg-from {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          img {
            margin-right: 10px;
          }
        }

        .msg-content {
          float: left;
          margin-left: 10px;
          word-wrap: break-word;
          word-break: break-all;
        }

      }

      .online {
        width: 200px;
        // max-width: 100%;
        margin: 3px auto;
        border-radius: 4px;
        text-align: center;
        background-color: #FFFDE7;
      }
    }

    .chatting-input {
      display: flex;
      height: 40px;
      width: 100%;
      input {
        flex: 1;
        padding-left: 10px;
        // padding-top: 10px;
        height: 100%;
        font-size: 1.3rem;
      }
      button {
        width: 60px;
        height: 100%;
        background-color: #2196f3;
        color: white;
        font-size: 1.2rem;
      }
    }
  }
</style>
  • 5
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: Vue2 是一种流行的前端框架,它可以帮助开发人员快速地构建交互式的用户界面和单页应用程序。Vue2 提供了许多实用的功能和工具,例如响应式数据绑定、组件化开发、指令、过滤器和路由等。Vue2 还具有易于理解和使用的 API,因此在开发过程中能够提高工作效率。 腾讯云智能机器人是一种基于人工智能技术的聊天机器人,它能够快速地响应用户的查询,并提供精确的答案。腾讯云智能机器人能够在企业的客户服务、教育、医疗、金融和电商等领域发挥作用。腾讯云智能机器人还提供了丰富的开发接口和平台,使开发人员可以灵活地定制自己的机器人应用程序。 如果将 Vue2 和腾讯云智能机器人聊天相结合,开发人员就可以使用 Vue2 快速构建出具有良好用户交互性的机器人应用程序。Vue2 提供了一种组件化的开发方式,使得开发人员可以将机器人的各个功能模块拆分为独立的组件进行开发、测试和维护。开发人员还可以利用 Vue2 的路由功能,将不同的机器人页面映射到不同的 URL 路径。这样做不仅可提高机器人应用程序的代码可维护性和可扩展性,更能够为用户带来更好的使用体验,增强用户粘性,提高用户满意度。 ### 回答2: Vue2是一款非常流行的前端框架,其主要特点包括简单易学、组件化、数据绑定、虚拟DOM和模板语法等。借助Vue2,开发人员可以更加高效地进行Web开发,并且可以实现更加复杂的Web应用程序。同时,腾讯云智能机器人聊天可以让人们与机器人进行对话,获取有关特定主题的信息或指导。借助人工智能技术,智能机器人可以理解人们的自然语言,并且提供准确的答案和解决方案。 在Vue2和腾讯云智能机器人聊天的结合中,开发人员可以使用Vue2来构建智能机器人聊天的用户界面,并实现与用户的交互。Vue2的组件化和数据绑定特征使得开发人员能够构建可重用的UI组件并轻松管理应用程序的状态。腾讯云智能机器人聊天则提供了一个强大的人工智能引擎,对话管理和意图处理的功能,使得人们可以与机器人进行对话并得到所需的信息和指导。 总之,Vue2和腾讯云智能机器人聊天的结合可以为开发人员提供一种强大的方式来构建现代Web应用程序,同时打造出一种全新的用户交互体验,这对于许多行业,如客服和教育等,都将有着重大的影响。 ### 回答3: Vue2 是一款非常流行的前端框架,它能够帮助开发者快速地构建交互式的 Web 应用程序。而腾讯云智能机器人则是一种创新型的解决方案,它结合了人工智能技术和自然语言处理技术,可以为企业和用户提供高效、便捷的智能客服服务。通过使用 Vue2 和腾讯云智能机器人,我们可以创建出更加智能化的用户界面,让用户可以通过简单的对话和交互来获取所需的服务和信息。 当使用 Vue2 和腾讯云智能机器人进行开发时,开发者可以轻松地编写用户界面,并集成智能客服机器人。这样,用户就可以在使用网站或应用程序时,一边浏览内容,一边进行对话交流,从而获得更加人性化的体验和服务。同时,智能客服机器人还可以根据用户的问题和意图,自动查找相关信息和知识库,并及时给出相应的答案和建议,提高了客户的满意度。 总之,Vue2 和腾讯云智能机器人的结合,为用户带来了更好的人机交互方式和更高效的智能客服服务,是一种非常有价值的开发方案。开发者可以利用这种方案,为用户和企业构建更加优秀的 Web 应用程序和智能化解决方案。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值