AIQA.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <template>
  2. <div class="ai-content-wrap">
  3. <div class="window-list-wrap">
  4. <div class="window-title">
  5. <div class="title-wrap">
  6. <p style="color: #333333;font-size: 20px;font-weight: 600;">HORIZON INSIGHTS</p>
  7. <span style="color: #666666;font-size: 16px;">弘则研究AI问答小助手</span>
  8. </div>
  9. <div class="icon"><img src="~@/assets/img/icons/horizon.png" /></div>
  10. </div>
  11. <div class="add-btn" @click="handleAddNewWindow"><i class="el-icon-circle-plus-outline"></i>新建对话窗口</div>
  12. <div class="list-wrap hidden-scrollbar">
  13. <Window-List-Item
  14. ref="windowListItem"
  15. v-for="item in windowList" :key="item.AiChatTopicId"
  16. :activeWindowId="activeWindowId"
  17. :item="item"
  18. @click.native="changeActiveWindow(item)"
  19. @changeEdit="changeWindowTitle"
  20. @delete="deleteWindowItem"
  21. />
  22. <div class="empty-list" v-if="windowList.length===0">
  23. <img src="~@/assets/img/ai_m/empty_list.png" />
  24. </div>
  25. </div>
  26. </div>
  27. <div class="content-wrap">
  28. <div class="content-header">
  29. <div class="title-wrap">
  30. <p>{{activeWindowId<=0?'新对话窗口':activeWindow.TopicName||''}}</p>
  31. <span>{{activeWindowId<=0?'弘则AI助手使用说明':`${historyList.length||0} messages`}}</span>
  32. </div>
  33. <div class="select-box">
  34. <!-- <el-select v-model="model" :class="{'hint':showHint}" :disabled="isTyping||(windowContentLoading&&windowContentLoading.visible)" ref="modelSelect"
  35. @click.native="selectClick"
  36. @change="changeModel">
  37. <el-option v-for="item in modelList" :key="item.label"
  38. :label="item.label"
  39. :value="item.label">
  40. <span style="float:left">{{item.label}}</span>
  41. <span style="float:right"><img :src="item.icon" style="margin-top:5px;width:24px;height:24px;"/></span>
  42. </el-option>
  43. </el-select> -->
  44. </div>
  45. </div>
  46. <!-- 仅这一部分滚动 -->
  47. <div class="window-content-wrap hidden-scrollbar">
  48. <div class="content-item" v-for="item in historyList" :key="item.AiChatId">
  49. <!-- user 提问 -->
  50. <Message-Item :messageInfo="formatMsg(item,'user')"
  51. @startTyping="handleStartTyping"
  52. @finishedTyping="handleFinishedTyping"
  53. />
  54. <!-- 模型回答 -->
  55. <Message-Item :messageInfo="formatMsg(item,'model')"
  56. @startTyping="handleStartTyping"
  57. @finishedTyping="handleFinishedTyping"
  58. />
  59. </div>
  60. <New-Window-Hint v-if="activeWindowId===0"/>
  61. </div>
  62. <div class="input-box">
  63. <textarea rows="6" v-model="inputText" placeholder="请输入提问,Shift+Enter换行" @keydown.enter="handleSendMsg"></textarea>
  64. <div class="send-btn" @click="handleSendMsg"><img src="~@/assets/img/ai_m/send.png" />发送</div>
  65. </div>
  66. </div>
  67. </div>
  68. </template>
  69. <script>
  70. /* components */
  71. import MessageItem from './components/messageItem.vue';
  72. import NewWindowHint from './components/newWindowHint.vue';
  73. import WindowListItem from './components/windowListItem';
  74. /* api */
  75. import {aiQAInterence} from '@/api/modules/aiApi.js';
  76. export default {
  77. components: { WindowListItem,NewWindowHint, MessageItem },
  78. data() {
  79. return {
  80. /* window */
  81. activeWindowId:0,//当前激活的窗口id
  82. activeWindow:null,//当前激活窗口
  83. windowList:[],//窗口列表
  84. listWrapLoading:null,
  85. /* window-content*/
  86. historyList:[],//当前窗口历史记录
  87. inputText:'',
  88. model:'GPT-4 Turbo',//当前选择的模型
  89. modelOldValue:'',
  90. modelList:[
  91. {
  92. label:'GPT-4 Turbo',
  93. icon:require('@/assets/img/icons/gpt-4-turbo.png'),
  94. },
  95. {
  96. label:'GPT4',
  97. icon:require('@/assets/img/icons/gpt-4.png'),
  98. },
  99. {
  100. label:'gpt-3.5-turbo-16k',
  101. icon:require('@/assets/img/icons/chat-gpt-16k.png'),
  102. },
  103. {
  104. label:'gpt-3.5-turbo',
  105. icon:require('@/assets/img/icons/chat-gpt.png'),
  106. },
  107. {
  108. label:'eta',
  109. icon:require('@/assets/img/icons/horizon.png'),
  110. },
  111. ],//模型列表
  112. showHint:false,//选择模型提示
  113. isTyping:false,//是否处于打字动画中
  114. windowContentLoading:null,
  115. answerLoading:false,//回答中
  116. };
  117. },
  118. watch:{
  119. model(newVal,oldVal){
  120. this.modelOldValue = oldVal
  121. }
  122. },
  123. computed:{
  124. modelIcon(){
  125. const modelItem = this.modelList.find(i=>i.label===this.model)
  126. return modelItem?modelItem.icon:''
  127. }
  128. },
  129. methods: {
  130. //改变活跃窗口
  131. changeActiveWindow(item){
  132. if(item.AiChatTopicId===0){
  133. this.handleAddNewWindow()
  134. return
  135. }
  136. if(item.AiChatTopicId===this.activeWindowId) return
  137. if(this.windowContentLoading&&this.windowContentLoading.visible){
  138. this.$message.warning('上个窗口未加载完成,请稍等')
  139. return
  140. }
  141. this.activeWindowId=item.AiChatTopicId
  142. this.activeWindow = this.windowList.find(i=>i.AiChatTopicId===item.AiChatTopicId)||{}
  143. this.historyList = []
  144. this.isTyping = false
  145. this.getWindowDetail()
  146. //切换窗口是否重置输入框
  147. //this.inputText=''
  148. },
  149. //获取对话窗口具体信息
  150. getWindowDetail(){
  151. this.windowContentLoading = this.$loading({
  152. target:document.querySelector('.window-content-wrap'),
  153. });
  154. aiQAInterence.getTopicDetail({
  155. AiChatTopicId:this.activeWindowId
  156. }).then(res=>{
  157. if(res.Ret!==200) return
  158. const {List} = res.Data
  159. this.historyList = List||[]
  160. this.windowContentLoading&&this.windowContentLoading.close()
  161. //使用模型
  162. this.model = this.historyList.length?this.historyList[this.historyList.length-1].Model:'GPT-4 Turbo'
  163. //如果有历史记录,则滚动到底部
  164. this.$nextTick(()=>{
  165. const windowContentWrap = document.querySelector('.window-content-wrap')
  166. windowContentWrap.scrollTo({
  167. top:windowContentWrap.scrollHeight,
  168. behavior:'smooth'
  169. })
  170. })
  171. })
  172. },
  173. //格式化对话信息
  174. formatMsg(msg,type){
  175. let msgObj = {
  176. messageId:msg.AiChatId,
  177. messageTime:'',
  178. messageText:'',
  179. messageType:'',
  180. modelName:''
  181. }
  182. const {Ask,Answer,CreateTime,ModifyTime,isPlay,Model} = msg
  183. if(type==='user'){
  184. msgObj.messageText = Ask||''
  185. msgObj.messageType = 'question'
  186. msgObj.messageTime = CreateTime||''
  187. }
  188. else{
  189. msgObj.messageText = Answer||''
  190. msgObj.messageType = 'answer'
  191. msgObj.messageTime = ModifyTime||''
  192. msgObj.modelName = Model||''
  193. msgObj.isPlay = Boolean(isPlay)
  194. }
  195. return msgObj
  196. },
  197. //新建对话窗口
  198. handleAddNewWindow(){
  199. //禁止重复新建
  200. if(this.activeWindowId===0){
  201. this.$message.warning("您已建立对话窗口,请输入提问")
  202. return
  203. }
  204. //将相关值置空
  205. this.activeWindowId=0
  206. this.activeWindow=null
  207. this.historyList=[]
  208. this.model='GPT-4 Turbo'
  209. //this.inputText=''
  210. this.isTyping = false
  211. },
  212. //开始播放动画
  213. handleStartTyping(){
  214. this.isTyping = true
  215. },
  216. //结束播放动画
  217. handleFinishedTyping(item){
  218. this.isTyping = false
  219. const {messageId} = item
  220. const index = this.historyList.findIndex(i=>i.AiChatId===messageId)
  221. index!==-1&&(this.historyList[index].isPlay = false)
  222. },
  223. //编辑对话窗口名称
  224. changeWindowTitle(title){
  225. aiQAInterence.editTopicName({
  226. AiChatTopicId:this.activeWindowId,
  227. TopicName:title
  228. }).then(res=>{
  229. if(res.Ret!==200) return
  230. this.$message.success('保存成功')
  231. this.getWindowList()
  232. })
  233. },
  234. //删除对话窗口
  235. deleteWindowItem(){
  236. const index = this.windowList.findIndex(i=>i.AiChatTopicId===this.activeWindowId)||0
  237. //更改activeWindowId
  238. /**
  239. * 一般情况:指向下一个
  240. * 若当前为最后一个,指向上一个
  241. * 如果是唯一一个,则置为0
  242. */
  243. let AiChatTopicId = 0
  244. if(this.windowList.length===1){
  245. AiChatTopicId=0
  246. }else if(index===this.windowList.length-1){
  247. AiChatTopicId = this.windowList[index-1].AiChatTopicId
  248. }else {
  249. AiChatTopicId = this.windowList[index+1].AiChatTopicId
  250. }
  251. aiQAInterence.deleteTopic({
  252. AiChatTopicId:this.activeWindowId
  253. }).then(res=>{
  254. if(res.Ret!==200) return
  255. this.getWindowList()
  256. this.changeActiveWindow({AiChatTopicId})
  257. })
  258. },
  259. //选择模型
  260. changeModel(value){
  261. //console.log('change',value)
  262. //非新建窗口时,模型之间切换需弹窗提示
  263. if(this.activeWindowId!==0&&value!==''){
  264. //弹窗提示:切换模型
  265. this.$confirm("切换回答模型,则切换后的模型无法联系上下文进行回答,确认切换吗?", "提示", {
  266. type: "warning"
  267. }).then(()=>{
  268. this.$message.success('切换模型成功')
  269. }).catch(()=>{
  270. //this.$message.success('已取消切换模型')
  271. this.model= this.modelOldValue
  272. this.modelOldValue=''
  273. })
  274. }
  275. //新建窗口时,若选择了模型,则移除hint效果
  276. if(this.activeWindowId===0&&value!==''){
  277. this.showHint=false
  278. }
  279. },
  280. //点击模型选择框
  281. selectClick(){
  282. if(this.isTyping){
  283. this.$message.warning('请等待回答完成')
  284. }
  285. },
  286. //发送消息
  287. handleSendMsg(e){
  288. e.preventDefault()
  289. if(e.shiftKey===true) {
  290. this.inputText+='\n'
  291. return
  292. }
  293. if(this.isTyping||this.answerLoading){
  294. this.$message.warning('请等待回答完成')
  295. return
  296. }
  297. //新建窗口,未选择模型
  298. if(this.activeWindowId===0&&this.model===''){
  299. this.showHint = true
  300. this.$message.error('请选择模型')
  301. this.$nextTick(()=>{
  302. this.$refs.modelSelect.focus()
  303. })
  304. return
  305. }
  306. if(this.inputText.length===0){
  307. this.$message.warning('请输入提问')
  308. return
  309. }
  310. this.answerLoading=true
  311. this.activeWindowId===0&&(this.activeWindowId = -1)
  312. //this.activeWindowId!==0&&this.getWindowDetail()
  313. //mock 加入到historyList中
  314. const msgObj = {
  315. AiChatId:-1,
  316. AiChatTopicId:0,
  317. Ask:this.inputText,
  318. Answer:'回答生成中...',
  319. CreateTime:'',
  320. ModifyTime:'',
  321. Model:this.model
  322. }
  323. this.historyList.push(msgObj)
  324. //滚动到底部
  325. this.$nextTick(()=>{
  326. const windowContentWrap = document.querySelector('.window-content-wrap')
  327. windowContentWrap.scrollTo({
  328. top:windowContentWrap.scrollHeight,
  329. behavior:'smooth'
  330. })
  331. })
  332. const inputText = this.inputText
  333. this.inputText = ''
  334. aiQAInterence.sendChatMsg({
  335. AiChatTopicId:this.activeWindowId<=0?0:this.activeWindowId,
  336. Ask:inputText,
  337. Model:this.model
  338. }).then(res=>{
  339. this.answerLoading=false
  340. //在回答未获取前切换了新窗口
  341. if(this.historyList.length===0){
  342. this.getWindowList()
  343. return
  344. }
  345. const msg = this.historyList[this.historyList.length-1]
  346. if(res.Ret!==200){
  347. //提问失败
  348. msg.Answer = res.ErrMsg||res.Msg||''
  349. msg.isPlay = true
  350. this.historyList.splice(this.historyList.length-1,1,msg)
  351. return
  352. }
  353. //提问成功,替换对应数据
  354. const {AiChatTopicId,Answer,Ask,Model} = res.Data
  355. //this.inputText=''
  356. if(this.activeWindowId<=0){
  357. const windowItem = {
  358. AiChatTopicId:AiChatTopicId||0,
  359. TopicName:Ask,
  360. }
  361. this.windowList.push(windowItem)
  362. this.activeWindow = windowItem
  363. this.getWindowList()
  364. }
  365. //在回答未获取前切换了已有窗口
  366. if(this.activeWindowId>0&&this.activeWindowId!==AiChatTopicId) return
  367. this.activeWindowId = AiChatTopicId||0
  368. msg.Answer = Answer||''
  369. msg.Model = Model
  370. msg.isPlay = true
  371. this.historyList.splice(this.historyList.length-1,1,msg)
  372. }).catch(()=>{
  373. this.answerLoading=false
  374. })
  375. },
  376. //获取窗口列表
  377. getWindowList(){
  378. this.listWrapLoading = this.$loading({
  379. target:document.querySelector('.list-wrap'),
  380. background: 'rgba(244, 245, 249, 1)'
  381. });
  382. aiQAInterence.getTopicList().then(res=>{
  383. if(res.Ret!==200) return
  384. this.windowList = res.Data.List||[]
  385. this.listWrapLoading&&this.listWrapLoading.close()
  386. })
  387. }
  388. },
  389. mounted(){
  390. this.getWindowList()
  391. }
  392. };
  393. </script>
  394. <style lang="scss">
  395. .ai-content-wrap{
  396. .el-select.hint{
  397. .el-input.is-focus .el-input__inner{
  398. border-color: red;
  399. }
  400. .el-input__inner:focus{
  401. border-color: red;
  402. }
  403. }
  404. }
  405. </style>
  406. <style scoped lang="scss">
  407. $border-color:#3D52A1;
  408. .ai-content-wrap{
  409. width:calc(100% - 64px);
  410. height:calc(100% - 64px);
  411. display: flex;
  412. position:relative;
  413. margin:30px;
  414. border:1px solid $border-color;
  415. border-radius: 32px;
  416. overflow: hidden;
  417. min-width: 960px;
  418. .window-list-wrap{
  419. width:380px;
  420. border-right: 1px solid #E2E2E2;
  421. padding:30px;
  422. box-sizing: border-box;
  423. background-color: #F4F5F9;
  424. display: flex;
  425. flex-direction: column;
  426. .window-title{
  427. display: flex;
  428. justify-content: space-between;
  429. align-items: center;
  430. .icon img{
  431. width:58px;
  432. height:58px;
  433. }
  434. }
  435. .add-btn{
  436. border:2px dashed $border-color;
  437. border-radius:16px;
  438. padding:14px;
  439. margin-top:40px;
  440. text-align: center;
  441. font-size: 16px;
  442. color:#333333;
  443. cursor: pointer;
  444. background-color: #F4F5F9;
  445. transition: background-color .5s, color .5s;
  446. &:hover{
  447. background-color: white;
  448. color: $border-color;
  449. border-style: solid;
  450. i{
  451. color: $border-color;
  452. }
  453. }
  454. i{
  455. margin-right: 10px;
  456. font-weight: bold;
  457. font-size: 16px;
  458. }
  459. }
  460. .list-wrap{
  461. margin-top:30px;
  462. flex: 1;
  463. overflow-y: scroll;
  464. position:relative;
  465. .empty-list img{
  466. width:150px;
  467. height:150px;
  468. margin:0 auto;
  469. display: block;
  470. }
  471. }
  472. }
  473. .content-wrap{
  474. flex: 1;
  475. display: flex;
  476. flex-direction: column;
  477. min-width: 588px;
  478. .content-header{
  479. display: flex;
  480. justify-content: space-between;
  481. align-items: center;
  482. padding:20px 30px;
  483. box-sizing: border-box;
  484. border-bottom: 1px solid #E2E2E2;
  485. .title-wrap{
  486. p{
  487. color: #333333;
  488. font-size: 18px;
  489. margin-bottom: 5px;
  490. }
  491. span{
  492. color: #999999;
  493. line-height: 18px;
  494. }
  495. }
  496. }
  497. .window-content-wrap{
  498. flex: 1;
  499. /* border-bottom: 1px solid black; */
  500. padding:30px;
  501. text-align: center;
  502. overflow-y: scroll;
  503. overflow-x:hidden;
  504. position:relative;
  505. .content-item{
  506. text-align: left;
  507. .item{
  508. width: 100%;
  509. display: flex;
  510. padding:15px 0;
  511. border-bottom: 1px solid black;
  512. .content-item-avatar{
  513. width: 50px;
  514. margin-right: 20px;
  515. img{
  516. width:38px;
  517. height:38px;
  518. }
  519. }
  520. .text{
  521. flex: 1;
  522. }
  523. }
  524. }
  525. }
  526. .input-box{
  527. padding:30px;
  528. position: relative;
  529. textarea{
  530. width:100%;
  531. border-radius: 16px;
  532. box-sizing: border-box;
  533. padding:20px 85px 20px 20px;
  534. font-size: 16px;
  535. resize: none;
  536. border-color: #E3E3E3;
  537. box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.08);
  538. }
  539. .send-btn{
  540. position:absolute;
  541. bottom:50px;
  542. right:50px;
  543. padding:8px 12px;
  544. background-color: $border-color;
  545. border-radius: 6px;
  546. font-size: 14px;
  547. color: white;
  548. line-height: 14px;
  549. cursor: pointer;
  550. opacity: .7;
  551. transition: opacity .5s;
  552. img{
  553. width:14px;
  554. height:14px;
  555. margin-right: 8px;
  556. }
  557. &:hover,&.canClick{
  558. opacity: 1;
  559. }
  560. }
  561. }
  562. }
  563. .hidden-scrollbar{
  564. &::-webkit-scrollbar-track{
  565. display: none;
  566. }
  567. div::-webkit-scrollbar-track{
  568. display: none;
  569. }
  570. }
  571. }
  572. </style>