chat.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <template>
  2. <div class="app-layout">
  3. <div class="side-bar">
  4. <div class="logo-section">
  5. <img src="@/assets/images/common/ai/logo.png" alt="新基础 小新" width="160" height="160" />
  6. <span class="logo-text">新基础 小新</span>
  7. </div>
  8. <div>
  9. <ul>
  10. <li><el-text>我的学习压力大,每天睡不好怎么办?</el-text></li>
  11. <li><el-text>帮我查一下2024年清华大学在四川省录取分数线</el-text></li>
  12. </ul>
  13. </div>
  14. </div>
  15. <div class="main-content">
  16. <div class="chat-container">
  17. <div class="message-list">
  18. <div v-for="item in chatRecordList" :key="item.id">
  19. <!-- 会话图标 -->
  20. <!--<div :class="item.isUser == 1? 'user-image' : 'system-image' "></div>-->
  21. <div v-if="item.isUser == 1" class="user-message">
  22. <img src="@/assets/images/common/ai/user.png" alt="icon"/>
  23. <span class="user-message-content">{{item.content}}</span>
  24. </div>
  25. <div v-else class="message bot-message">
  26. <img src="@/assets/images/common/ai/system.png" alt="icon"/>
  27. <div class="bot-message-content" v-html="item.content"></div>
  28. </div>
  29. </div>
  30. </div>
  31. <div class="message-box">
  32. <div class="typing-message">
  33. <span>
  34. <span v-html="streamHtmlData"></span>
  35. <span class="loading-dots">
  36. <span class="dot"></span>
  37. <span class="dot"></span>
  38. </span>
  39. </span>
  40. </div>
  41. </div>
  42. <el-button @click="stopMessage">停止回答</el-button>
  43. </div>
  44. <div class="input-container">
  45. <el-input
  46. v-model="inputMessage"
  47. placeholder="请输入消息"
  48. @keyup.enter="sendMessage"
  49. ></el-input>
  50. <el-button @click="sendMessage" :disabled="isSending" type="primary">发送</el-button>
  51. </div>
  52. </div>
  53. <div class="control-bar">
  54. <div class="control-button-box">
  55. <div class="control-button">
  56. <el-button style="width:140px">退出</el-button>
  57. </div>
  58. <div class="control-button">
  59. <el-button style="width:140px">静音</el-button>
  60. </div>
  61. <div class="control-button">
  62. <el-button style="width:140px">新会话</el-button>
  63. </div>
  64. </div>
  65. <div class="session-list-box">
  66. </div>
  67. </div>
  68. </div>
  69. </template>
  70. <script setup>
  71. import {aiChatRecordList} from '@/api/xjc-integratedmachine/common/aiChat.js'
  72. import { getToken } from '@/utils/auth'
  73. import { marked } from 'marked'
  74. // 聊天记录
  75. let chatRecordList = ref([])
  76. // md流式数据
  77. const streamMarkdownData = ref('');
  78. // html流式数据
  79. let streamHtmlData = ref('');
  80. // 流式加载状态
  81. const isLoading = ref(false);
  82. // 读取器实例
  83. let reader = null;
  84. // AbortController用于中止请求
  85. let controller = null;
  86. // 输入的问题
  87. let inputMessage = ref('')
  88. // 发送标识
  89. let isSending = ref(false)
  90. // 查看所有聊天记录
  91. list();
  92. function list() {
  93. let queryForm = {
  94. }
  95. aiChatRecordList(queryForm).then(resp =>{
  96. console.log(resp)
  97. chatRecordList.value = resp.rows;
  98. })
  99. }
  100. const sendMessage = () => {
  101. /* if (inputMessage.value.trim()) {
  102. sendRequest(inputMessage.value.trim())
  103. inputMessage.value = ''
  104. }*/
  105. }
  106. const sendRequest = async(message) => {
  107. // 用户信息
  108. const userMessage = {
  109. isUser: true,
  110. content: message,
  111. isTyping: false
  112. }
  113. // 消息加入聊天记录
  114. // chatRecordList.value.push(userMessage)
  115. try{
  116. isLoading.value = true;
  117. streamMarkdownData.value = ''; // 清空之前的数据
  118. // 创建AbortController以便可以中止请求
  119. controller = new AbortController();
  120. // 请求体
  121. let form = {
  122. "content": message? message: "你是谁?"
  123. }
  124. // 发送fetch请求
  125. const response = await fetch('/dev-api/ai/chat/record/stream', {
  126. method: 'POST',
  127. headers: {
  128. 'Content-Type': 'application/json',
  129. 'Authorization': 'Bearer ' + getToken()
  130. },
  131. body: JSON.stringify(form)
  132. });
  133. // 获取可读流的读取器
  134. reader = response.body.getReader();
  135. const decoder = new TextDecoder('utf-8');
  136. // 循环读取流数据
  137. while (true) {
  138. const { done, value } = await reader.read();
  139. if (done) break; // 如果流读取完成则退出循环
  140. // 解码并追加数据
  141. let chunk = decoder.decode(value, { stream: true });
  142. chunk = chunk.replace(/\n\n/g, '').replace(/data:/g, '')
  143. streamMarkdownData.value += chunk;
  144. streamHtmlData.value = marked(streamMarkdownData.value)
  145. }
  146. }catch (error) {
  147. // 如果是手动中止,不显示错误
  148. if (error.name !== 'AbortError') {
  149. console.error('流式读取失败:', error);
  150. streamMarkdownData.value = 'Error: ' + error.message;
  151. }
  152. } finally {
  153. streamHtmlData.value = marked(streamMarkdownData.value)
  154. isLoading.value = false;
  155. console.log("==============");
  156. }
  157. }
  158. const stopMessage = () => {
  159. if (reader) {
  160. // 取消读取
  161. reader.cancel().catch(() => {});
  162. reader = null;
  163. }
  164. if (controller) {
  165. // 中止请求
  166. controller.abort();
  167. controller = null;
  168. }
  169. isLoading.value = false;
  170. }
  171. onMounted(()=>{
  172. sendMessage()
  173. })
  174. </script>
  175. <script>
  176. export default {
  177. name: "chat"
  178. }
  179. </script>
  180. <style scoped>
  181. .app-layout {
  182. display: flex;
  183. height: 100vh;
  184. width: 100vw;
  185. }
  186. .side-bar {
  187. width: 20vw;
  188. background-color: #f4f4f9;
  189. padding: 20px;
  190. display: flex;
  191. flex-direction: column;
  192. align-items: center;
  193. }
  194. .main-content {
  195. width: 70vw;
  196. height: 100vh;
  197. flex: 1;
  198. padding: 20px;
  199. overflow-y: auto;
  200. }
  201. .chat-container {
  202. display: flex;
  203. flex-direction: column;
  204. height: 90vh;
  205. /*overflow-y: auto;*/
  206. }
  207. .logo-section {
  208. display: flex;
  209. flex-direction: column;
  210. align-items: center;
  211. }
  212. /*控制区*/
  213. .control-bar{
  214. width: 10vw;
  215. background-color: #f4f4f9;
  216. padding: 20px;
  217. }
  218. .control-button-box {
  219. }
  220. .control-button {
  221. display: flex;
  222. justify-content: center;
  223. margin: 4px 0;
  224. }
  225. .session-list-box{
  226. }
  227. /*聊天列表*/
  228. .message-list{
  229. flex: 1;
  230. overflow-y: auto;
  231. padding: 10px;
  232. border: 1px solid #e0e0e0;
  233. border-radius: 4px;
  234. background-color: #fff;
  235. margin-bottom: 10px;
  236. display: flex;
  237. flex-direction: column;
  238. }
  239. .every-message {
  240. margin-bottom: 10px;
  241. padding: 10px;
  242. border-radius: 4px;
  243. display: flex;
  244. align-items: center;
  245. width: 40vw;
  246. /* align-items: center; */
  247. }
  248. .user-message {
  249. display: flex;
  250. align-self: flex-end;
  251. flex-direction: row-reverse;
  252. align-items: center;
  253. margin: 4px;
  254. }
  255. .user-message-content {
  256. background-color: #e1f5fe;
  257. padding: 10px;
  258. border-radius: 4px;
  259. }
  260. .bot-message {
  261. max-width: 70%;
  262. display: flex;
  263. align-self: flex-start;
  264. align-items: flex-start;
  265. margin: 4px;
  266. }
  267. .bot-message-content {
  268. background-color: #f1f8e9;
  269. padding: 10px;
  270. border-radius: 4px;
  271. }
  272. .system-image {
  273. width: 32px;
  274. height:32px;
  275. background-image: url('@/assets/images/common/ai/system.png');
  276. }
  277. .user-image {
  278. width: 32px;
  279. height:32px;
  280. background-image: url('@/assets/images/common/ai/user.png');
  281. }
  282. /* 每个消息的区域 */
  283. .message-box {
  284. /*max-height: 300px;*/
  285. width: 60vw;
  286. /*height: 30vh;*/
  287. overflow-y: auto;
  288. border: 1px solid #eee;
  289. padding: 10px;
  290. margin-top: 10px;
  291. background: #f9f9f9;
  292. }
  293. pre {
  294. white-space: pre-wrap;
  295. word-wrap: break-word;
  296. }
  297. .loading-dots {
  298. padding-left: 5px;
  299. }
  300. .dot {
  301. display: inline-block;
  302. margin-left: 5px;
  303. width: 8px;
  304. height: 8px;
  305. background-color: #000000;
  306. border-radius: 50%;
  307. animation: pulse 1.2s infinite ease-in-out both;
  308. }
  309. .dot:nth-child(2) {
  310. animation-delay: -0.6s;
  311. }
  312. @keyframes pulse {
  313. 0%,
  314. 100% {
  315. transform: scale(0.6);
  316. opacity: 0.4;
  317. }
  318. 50% {
  319. transform: scale(1);
  320. opacity: 1;
  321. }
  322. }
  323. /*输入框*/
  324. .input-container {
  325. display: flex;
  326. }
  327. .input-container .el-input {
  328. flex: 1;
  329. margin-right: 10px;
  330. }
  331. </style>