123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- <template>
- <div class="app-layout">
- <div class="side-bar">
- <div class="logo-section">
- <img src="@/assets/images/common/ai/logo.png" alt="新基础 小新" width="160" height="160" />
- <span class="logo-text">新基础 小新</span>
- </div>
- <div>
- <ul>
- <li><el-text>我的学习压力大,每天睡不好怎么办?</el-text></li>
- <li><el-text>帮我查一下2024年清华大学在四川省录取分数线</el-text></li>
- </ul>
- </div>
- </div>
- <div class="main-content">
- <div class="chat-container">
- <div class="message-list">
- <div v-for="item in chatRecordList" :key="item.id">
- <!-- 会话图标 -->
- <!--<div :class="item.isUser == 1? 'user-image' : 'system-image' "></div>-->
- <div v-if="item.isUser == 1" class="user-message">
- <img src="@/assets/images/common/ai/user.png" alt="icon"/>
- <span class="user-message-content">{{item.content}}</span>
- </div>
- <div v-else class="message bot-message">
- <img src="@/assets/images/common/ai/system.png" alt="icon"/>
- <div class="bot-message-content" v-html="item.content"></div>
- </div>
- </div>
- </div>
- <div class="message-box">
- <div class="typing-message">
- <span>
- <span v-html="streamHtmlData"></span>
- <span class="loading-dots">
- <span class="dot"></span>
- <span class="dot"></span>
- </span>
- </span>
- </div>
- </div>
- <el-button @click="stopMessage">停止回答</el-button>
- </div>
- <div class="input-container">
- <el-input
- v-model="inputMessage"
- placeholder="请输入消息"
- @keyup.enter="sendMessage"
- ></el-input>
- <el-button @click="sendMessage" :disabled="isSending" type="primary">发送</el-button>
- </div>
- </div>
- <div class="control-bar">
- <div class="control-button-box">
- <div class="control-button">
- <el-button style="width:140px">退出</el-button>
- </div>
- <div class="control-button">
- <el-button style="width:140px">静音</el-button>
- </div>
- <div class="control-button">
- <el-button style="width:140px">新会话</el-button>
- </div>
- </div>
- <div class="session-list-box">
- </div>
- </div>
- </div>
- </template>
- <script setup>
- import {aiChatRecordList} from '@/api/xjc-integratedmachine/common/aiChat.js'
- import { getToken } from '@/utils/auth'
- import { marked } from 'marked'
- // 聊天记录
- let chatRecordList = ref([])
- // md流式数据
- const streamMarkdownData = ref('');
- // html流式数据
- let streamHtmlData = ref('');
- // 流式加载状态
- const isLoading = ref(false);
- // 读取器实例
- let reader = null;
- // AbortController用于中止请求
- let controller = null;
- // 输入的问题
- let inputMessage = ref('')
- // 发送标识
- let isSending = ref(false)
- // 查看所有聊天记录
- list();
- function list() {
- let queryForm = {
- }
- aiChatRecordList(queryForm).then(resp =>{
- console.log(resp)
- chatRecordList.value = resp.rows;
- })
- }
- const sendMessage = () => {
- /* if (inputMessage.value.trim()) {
- sendRequest(inputMessage.value.trim())
- inputMessage.value = ''
- }*/
- }
- const sendRequest = async(message) => {
- // 用户信息
- const userMessage = {
- isUser: true,
- content: message,
- isTyping: false
- }
- // 消息加入聊天记录
- // chatRecordList.value.push(userMessage)
- try{
- isLoading.value = true;
- streamMarkdownData.value = ''; // 清空之前的数据
- // 创建AbortController以便可以中止请求
- controller = new AbortController();
- // 请求体
- let form = {
- "content": message? message: "你是谁?"
- }
- // 发送fetch请求
- const response = await fetch('/dev-api/ai/chat/record/stream', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'Authorization': 'Bearer ' + getToken()
- },
- body: JSON.stringify(form)
- });
- // 获取可读流的读取器
- reader = response.body.getReader();
- const decoder = new TextDecoder('utf-8');
- // 循环读取流数据
- while (true) {
- const { done, value } = await reader.read();
- if (done) break; // 如果流读取完成则退出循环
- // 解码并追加数据
- let chunk = decoder.decode(value, { stream: true });
- chunk = chunk.replace(/\n\n/g, '').replace(/data:/g, '')
- streamMarkdownData.value += chunk;
- streamHtmlData.value = marked(streamMarkdownData.value)
- }
- }catch (error) {
- // 如果是手动中止,不显示错误
- if (error.name !== 'AbortError') {
- console.error('流式读取失败:', error);
- streamMarkdownData.value = 'Error: ' + error.message;
- }
- } finally {
- streamHtmlData.value = marked(streamMarkdownData.value)
- isLoading.value = false;
- console.log("==============");
- }
- }
- const stopMessage = () => {
- if (reader) {
- // 取消读取
- reader.cancel().catch(() => {});
- reader = null;
- }
- if (controller) {
- // 中止请求
- controller.abort();
- controller = null;
- }
- isLoading.value = false;
- }
- onMounted(()=>{
- sendMessage()
- })
- </script>
- <script>
- export default {
- name: "chat"
- }
- </script>
- <style scoped>
- .app-layout {
- display: flex;
- height: 100vh;
- width: 100vw;
- }
- .side-bar {
- width: 20vw;
- background-color: #f4f4f9;
- padding: 20px;
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- .main-content {
- width: 70vw;
- height: 100vh;
- flex: 1;
- padding: 20px;
- overflow-y: auto;
- }
- .chat-container {
- display: flex;
- flex-direction: column;
- height: 90vh;
- /*overflow-y: auto;*/
- }
- .logo-section {
- display: flex;
- flex-direction: column;
- align-items: center;
- }
- /*控制区*/
- .control-bar{
- width: 10vw;
- background-color: #f4f4f9;
- padding: 20px;
- }
- .control-button-box {
- }
- .control-button {
- display: flex;
- justify-content: center;
- margin: 4px 0;
- }
- .session-list-box{
- }
- /*聊天列表*/
- .message-list{
- flex: 1;
- overflow-y: auto;
- padding: 10px;
- border: 1px solid #e0e0e0;
- border-radius: 4px;
- background-color: #fff;
- margin-bottom: 10px;
- display: flex;
- flex-direction: column;
- }
- .every-message {
- margin-bottom: 10px;
- padding: 10px;
- border-radius: 4px;
- display: flex;
- align-items: center;
- width: 40vw;
- /* align-items: center; */
- }
- .user-message {
- display: flex;
- align-self: flex-end;
- flex-direction: row-reverse;
- align-items: center;
- margin: 4px;
- }
- .user-message-content {
- background-color: #e1f5fe;
- padding: 10px;
- border-radius: 4px;
- }
- .bot-message {
- max-width: 70%;
- display: flex;
- align-self: flex-start;
- align-items: flex-start;
- margin: 4px;
- }
- .bot-message-content {
- background-color: #f1f8e9;
- padding: 10px;
- border-radius: 4px;
- }
- .system-image {
- width: 32px;
- height:32px;
- background-image: url('@/assets/images/common/ai/system.png');
- }
- .user-image {
- width: 32px;
- height:32px;
- background-image: url('@/assets/images/common/ai/user.png');
- }
- /* 每个消息的区域 */
- .message-box {
- /*max-height: 300px;*/
- width: 60vw;
- /*height: 30vh;*/
- overflow-y: auto;
- border: 1px solid #eee;
- padding: 10px;
- margin-top: 10px;
- background: #f9f9f9;
- }
- pre {
- white-space: pre-wrap;
- word-wrap: break-word;
- }
- .loading-dots {
- padding-left: 5px;
- }
- .dot {
- display: inline-block;
- margin-left: 5px;
- width: 8px;
- height: 8px;
- background-color: #000000;
- border-radius: 50%;
- animation: pulse 1.2s infinite ease-in-out both;
- }
- .dot:nth-child(2) {
- animation-delay: -0.6s;
- }
- @keyframes pulse {
- 0%,
- 100% {
- transform: scale(0.6);
- opacity: 0.4;
- }
- 50% {
- transform: scale(1);
- opacity: 1;
- }
- }
- /*输入框*/
- .input-container {
- display: flex;
- }
- .input-container .el-input {
- flex: 1;
- margin-right: 10px;
- }
- </style>
|