欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue+AI智能機器人回復功能實現(xiàn)

 更新時間:2020年07月16日 09:44:42   作者:暴走的小陌  
這篇文章主要為大家詳細介紹了vue+AI智能機器人回復功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue+AI智能機器人回復的具體代碼,供大家參考,具體內容如下

操作步驟

  • 引入前端代碼

前端代碼是參考github上的一個開源項目,里面包括AI機器人回復和聊天室兩個模塊,這里只抽取出來一個AI機器人回復的前端,有興趣的話,可以點擊查看

  • 封裝好代理與請求

因為第三方API的請求是外網(wǎng)的,存在跨域問題,所以要配置代理,配置如下:

文件:vue.config.js

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

配完代理后,創(chuàng)建請求實例:

文件: request.js

// 創(chuàng)建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>
 <!-- 聊天內容區(qū)域 -->
 <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>
 <!-- 輸入?yún)^(qū)域 -->
 <div class="chatting-input">
 <input @keyup.enter="send" v-model.trim="inputContent" placeholder="與智能機器人聊些啥">
 <button @click="send">發(fā)送</button>
 </div>
 </div>
 </transition>

</template>

<script>
 import {aiAxios} from '../../../utils/request'
 import moment from 'moment'
 //下面兩張頭像自己從網(wǎng)上隨便找兩張
 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: {
 //發(fā)送消息
 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>

關于vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 淺談Vue中render中的h箭頭函數(shù)

    淺談Vue中render中的h箭頭函數(shù)

    今天小編就為大家分享一篇淺談Vue中render中的h箭頭函數(shù),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue輕松實現(xiàn)水印效果

    vue輕松實現(xiàn)水印效果

    這篇文章主要為大家詳細介紹了vue輕松實現(xiàn)水印效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • mpvue小程序循環(huán)動畫開啟暫停的實現(xiàn)方法

    mpvue小程序循環(huán)動畫開啟暫停的實現(xiàn)方法

    這篇文章主要介紹了mpvue小程序循環(huán)動畫開啟暫停的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-05-05
  • 手把手教你創(chuàng)建vue3項目的最佳方式

    手把手教你創(chuàng)建vue3項目的最佳方式

    如今的Vue3已經(jīng)勢不可擋,當然搭建一個全新的Vue3項目也有了全新的方式,下面這篇文章主要給大家介紹了關于如何手把手教你創(chuàng)建vue3項目的最佳方式,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • vue中進行微博分享的實例講解

    vue中進行微博分享的實例講解

    在本篇文章里小編給大家整理的是一篇關于vue中進行微博分享的實例內容,有需要的朋友們可以參考下。
    2019-10-10
  • Echarts之圖例legend基本配置方式

    Echarts之圖例legend基本配置方式

    這篇文章主要介紹了Echarts只圖例legend基本配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue-quill-editor二次封裝,實現(xiàn)自定義文件上傳方式

    vue-quill-editor二次封裝,實現(xiàn)自定義文件上傳方式

    這篇文章主要介紹了vue-quill-editor二次封裝,實現(xiàn)自定義文件上傳方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • vue 實現(xiàn)的樹形菜的實例代碼

    vue 實現(xiàn)的樹形菜的實例代碼

    這篇文章主要介紹了vue 實現(xiàn)的樹形菜單,需要的朋友可以參考下
    2018-03-03
  • Vuejs開發(fā)環(huán)境搭建及熱更新【推薦】

    Vuejs開發(fā)環(huán)境搭建及熱更新【推薦】

    Vue.js是目前很火的一個前端框架,采用MVVM模式設計,它是以數(shù)據(jù)驅動和組件化的思想構建的。本文重點給大家介紹Vuejs開發(fā)環(huán)境搭建及熱更新的相關知識,需要的朋友參考下吧
    2018-09-09
  • Vue element-UI el-select循環(huán)選中的問題

    Vue element-UI el-select循環(huán)選中的問題

    這篇文章主要介紹了Vue element-UI el-select循環(huán)選中的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論