后端:調用deepseek的api,所以返回數據格式和deepseek相同
{"model": "DeepSeek-R1-Distill-Qwen-1.5B", "choices": [{"index": 0, "delta": {"role": "assistant", "content": ",有什么", "tool_calls": null}, "finish_reason": null, "logprobs": null}], "usage": {"prompt_tokens": 5, "completion_tokens": 11, "total_tokens": 16}, "id": "chatcmpl-203a3024a36e4c02b02200ca47d8901e", "object": "chat.completion.chunk", "created": 1741766893}
前端開發的幾個注意點:
- 將后端返回的文本轉換為html展示在頁面上,注意調整樣式
- 模擬打字形式,頁面隨之滾動
- 撐開的輸入框和對話內容部分樣式調整
- 測試多種文本輸入,例如含有html標簽的
- 記錄思考時間,思考內容模仿deepseek做收起
這只是初步的項目,僅支持文本輸入
懶得分步寫了,直接貼完整代碼吧
有些地方可能寫得比較繁瑣
比如判斷思考內容那段,只能通過think標簽判斷嗎?
請各位多多指點,歡迎交流!!
<template><div class="talk-window"><div class="talk-title"><p>{{ answerTitle }}</p><el-input v-model="choicedTasks.name" placeholder="請選擇任務" class="sangedianBtn" readonly><template #append><el-button class="ec-font icon-sangedian" @click="isShowTask = true" /></template></el-input></div><div class="talk-container"><div class="talk-welcome" v-if="contentList.length == 0"><h1>{{ welcome.title }}</h1><p>{{ welcome.desc }}</p></div><div class="talk-box" v-else :style="{ height: answerContHeight }"><div ref="logContainer" class="talk-content"><el-row v-for="(item, i) in contentList" :key="i" class="chat-assistant"><transition name="fade"><div :class="['answer-cont', item.type === 'send' ? 'end' : 'start']"><img v-if="item.type == 'answer'" :src="welcome.icon" /><div :class="item.type === 'send' ? 'send-item' : 'answer-item'"><div v-if="item.type == 'answer'" class="hashrate-markdown" v-html="item.message" /><div v-else>{{ item.message }}</div></div><!-- 增加復制 --><!-- <div v-if="item.type == 'answer' && !isTalking"><el-tooltip centent="復制"><i class="ec-font icon-ticket" @click="copyMsg(item.message)" /></el-tooltip></div> --></div></transition></el-row></div><div style="text-align: center; margin-top: 10px"><el-button class="chat-add" @click="newChat"><i class="ec-font icon-tianjia1" />新建對話</el-button></div></div><div class="talk-send"><textarea@keydown.enter="enterMessage"ref="input"v-model="inputMessage"@input="adjustInputHeight"placeholder="輸入消息...":rows="2" /><!-- <el-inputv-model="inputMessage":autosize="{ minRows: 2, maxRows: 5 }"type="textarea"@keyup.enter="enterMessage"placeholder="Please input" /> --><div class="talk-btn-cont" style="text-align: right"><img @click="sendMessage" :src="iconImg" /></div></div></div><copyrightContent :systemNameOption="systemNameOption" /><taskDialog v-if="isShowTask" :choicedTasks="choicedTasks" v-model:isShow="isShowTask" @confirm="confirmTask" /></div>
</template><script>
import taskDialog from '@/views/chat/task/taskDialog.vue'
import copyrightContent from '@/views/chat/talking/components/copyright.vue'
import hljs from 'highlight.js'
import 'highlight.js/styles/a11y-dark.css'
import MarkdownIt from 'markdown-it'
import { VSConfig } from '/config/envConfig'window.hiddenThink = function (index) {// 隱藏思考內容if (document.getElementById(`think_content_${index}`).style.display == 'none') {document.getElementById(`think_content_${index}`).style.display = 'block'document.getElementById(`think_icon_${index}`).classList.replace('icon-a-xiangshang3', 'icon-a-xiangxia3')} else {document.getElementById(`think_content_${index}`).style.display = 'none'document.getElementById(`think_icon_${index}`).classList.replace('icon-a-xiangxia3', 'icon-a-xiangshang3')}
}export default {props: {isCollapsed: {type: Boolean,default: false},activeChat: String,activeTitle: String,chat_style_setting: {type: Object,default: function () {return {}}},systemNameOption: {type: Object,default: function () {return {}}}},components: { taskDialog, copyrightContent },data() {return {inputMessage: '',messages: [],choicedTasks: {uuid: '',name: ''},isShowTask: false,contentList: [],eventSourceChat: null,markdownIt: {},startAnwer: false,startTime: null,endTime: null,thinkTime: null,answerTitle: '',talkUUID: '',refreshHistoryFlag: false, // 是否已經生成了對話uuid,生成了的話就刷新歷史列表msgHight: null,isTalking: false, //是否在對話中,處于對話中則展示休止按鈕welcome: {title: '很高興見到你!',desc: '我可以幫你寫代碼、讀文件、寫作各種創意內容,請把你的任務交給我吧~',icon: require('@/assets/image/AI.png')},store: {}}},watch: {activeTitle(val) {// 重命名了對話this.answerTitle = val},activeChat(val) {this.answerTitle = ''this.talkUUID = val || ''this.inputMessage = ''this.refreshHistoryFlag = falsethis.isTalking = falseif (val) {// 滾動回到頂部const logContainer = this.$refs.logContainerif (logContainer) {logContainer.scrollTop = 0}this.getTalkDetail()} else {this.contentList = []}this.eventSourceChat && this.eventSourceChat.close()},chat_style_setting(val) {this.welcome.title = val.welcome_speech_style || this.welcome.titlethis.welcome.desc = val.description_style || this.welcome.descthis.welcome.icon = val.icon_image || this.welcome.icon}},computed: {iconImg() {// 對話中可能有輸入if (this.isTalking) {return require('/src/assets/image/chat/stop.png')} else {if (!this.inputMessage ||this.inputMessage.trim() === '' ||this.inputMessage.split(/\r?\n/).every((line) => line.trim() === '')) {return require('/src/assets/image/chat/unsend.png')} else {return require('/src/assets/image/chat/send.png')}}},answerContHeight() {// 回到初始值return this.msgHight == '56px' ? 'calc(100% - 140px)' : `calc(100% - 140px - ${this.msgHight} + 56px)`}},mounted() {this.store = mainStore()this.markdownIt = MarkdownIt({html: true,linkify: true,highlight: function (str, lang) {if (lang && hljs.getLanguage(lang)) {try {return hljs.highlight(str, { language: lang }).value} catch (__) {}}return '' // use external default escaping}})},methods: {getTalkDetail() {chat.historyDetail({uuid: this.activeChat}).then((res) => {this.contentList = []if (res.code == 0) {res.info.history_meta &&res.info.history_meta.forEach((item, index) => {if (item.conversation_type == 'Answer') {// 增加一個歷史思考記錄收起吧item.context = item.context.replace(/<think>\n\n<\/think>/g, '').replaceAll('<think>',`<div class="think-time">歷史思考<i id="think_icon_${index}" onclick="hiddenThink(${index})" class="ec-font icon-a-xiangxia3"></i></div><section id="think_content_${index}">`).replaceAll('</think>', '</section>')item.context = this.markdownIt.render(item.context)}this.contentList.push({type: item.conversation_type == 'Question' ? 'send' : 'answer',message: item.context})})this.answerTitle = res.info.name.substring(0, 20)}})},enterMessage(event) {if (event.key === 'Enter' && !event.shiftKey) {event.preventDefault()this.sendMessage()}},sendMessage() {// 終止當前對話if (this.isTalking) {this.eventSourceChat && this.eventSourceChat.close()// 關閉后,處理正在對話的"思考中"let curAnswer = this.contentList[this.contentList.length - 1].messagecurAnswer = curAnswer.replaceAll('<div class="think-time">思考中……</div>', '對話中止')this.contentList[this.contentList.length - 1].message = curAnswerthis.isTalking = false// 再獲取一下歷史記錄// 暫時不獲取歷史記錄,因為中止對話時,歷史UUID可能還沒返回// this.$emit('getHistoryList')return}if (!this.inputMessage ||this.inputMessage.trim() === '' ||this.inputMessage.split(/\r?\n/).every((line) => line.trim() === '')) {this.inputMessage = ''return false}if (!this.choicedTasks.name) {this.$message({ message: '請選擇推理任務', type: 'warning' })return false}// 回到初始高度const textarea = this.$refs.inputtextarea.style.height = '56px'this.msgHight = '56px'this.eventSourceChat && this.eventSourceChat.close()// let markedText = this.markdownIt.render(this.inputMessage)this.contentList.push({ type: 'send', message: this.inputMessage })this.contentList.push({ type: 'answer', message: `<div class="think-time">思考中……</div>` })this.answerTitle = this.answerTitle || this.contentList[0].message.substring(0, 20)this.scrollToBottom()this.initSSEChat()},initSSEChat() {const url = `${VSConfig.isHttps ? 'https' : 'http'}://${this.store.leader}/v1/intelligent_computing/task/chat/stream?uuid=${this.choicedTasks.uuid}&message=${encodeURIComponent(this.inputMessage)}&token=${this.store.token}&conversation_uuid=${this.talkUUID}`this.inputMessage = ''this.eventSourceChat = new EventSource(url)let buffer = ''this.startTime = nullthis.endTime = nullthis.thinkTime = nulllet len = this.contentList.lengthlet index = len % 2 === 0 ? len - 1 : lenthis.isTalking = truethis.eventSourceChat.onmessage = async (event) => {await this.sleep(10)if (event.data == '[DONE]') {return false}// 接收 Delta 數據// 最后一條是UUID,第二次發對話的時候要傳參try {var { choices, created } = JSON.parse(event.data)} catch (e) {// 新對話在歷史列表補充數據this.talkUUID = event.dataif (!this.refreshHistoryFlag) {this.refreshHistoryFlag = event.datathis.$emit('refreshHistory', this.refreshHistoryFlag)}}// const { choices, created } = JSON.parse(event.data)if (choices && choices[0].delta?.content) {buffer += choices[0].delta.content// think標簽內是思考內容,單獨記錄思考時間if (choices[0].delta.content.includes('<think>')) {choices[0].delta.content = `<div class="think-time">思考中……</div><section id="think_content_${index}">`buffer = buffer.replaceAll('<think>', choices[0].delta.content)this.startTime = Math.floor(new Date().getTime() / 1000)}if (choices[0].delta.content.includes('</think>')) {// console.log("結束時間賦值的判斷")choices[0].delta.content = `</section>`this.endTime = Math.floor(new Date().getTime() / 1000)// 獲取到結束時間后,直接展示收起按鈕this.thinkTime = this.endTime - this.startTimebuffer = buffer.replaceAll('<div class="think-time">思考中……</div>',`<div class="think-time">已深度思考(${this.thinkTime}S)<i id="think_icon_${index}" onclick="hiddenThink(${index})" class="ec-font icon-a-xiangxia3"></i></div>`).replaceAll('</think>', choices[0].delta.content).replaceAll(`<section id="think_content_${index}"></section>`, '')}let markedText = this.markdownIt.render(buffer)this.contentList[index] = { type: 'answer', message: markedText }this.scrollToBottomIfAtBottom()}}this.eventSourceChat.onerror = (event) => {console.log('錯誤觸發===》', event)this.contentList[index] = { type: 'answer', message: `<div class="think-time">對話服務連接失敗</div>` }this.eventSourceChat.close()this.isTalking = false}this.eventSourceChat.onclose = (event) => {// 關閉事件console.log('關閉事件--->')this.isTalking = false}},sleep(ms) {return new Promise((resolve) => setTimeout(resolve, ms))},scrollToBottomIfAtBottom() {this.$nextTick(() => {const logContainer = this.$refs.logContainerif (logContainer) {const threshold = 100const distanceToBottom = logContainer.scrollHeight - logContainer.scrollTop - logContainer.clientHeightif (distanceToBottom <= threshold) logContainer.scrollTop = logContainer.scrollHeight}})},scrollToBottom() {this.$nextTick(() => {const logContainer = this.$refs.logContainerif (logContainer) {logContainer.scrollTop = logContainer.scrollHeight}})},confirmTask(val) {this.choicedTasks = val},clearWindow() {this.eventSourceChat && this.eventSourceChat.close()this.contentList = []this.answerTitle = ''this.talkUUID = ''this.inputMessage = ''this.refreshHistoryFlag = falsethis.isTalking = falseconst textarea = this.$refs.inputtextarea.style.height = '56px'this.msgHight = '56px'},newChat() {this.clearWindow()this.$emit('clearChat')},adjustInputHeight(event) {// enter鍵盤按下的換行賦值為空if (event.key === 'Enter' && !event.shiftKey) {this.inputMessage = ''event.preventDefault()return}this.$nextTick(() => {const textarea = this.$refs.inputtextarea.style.height = 'auto'// 最高200pxtextarea.style.height = Math.min(textarea.scrollHeight, 200) + 'px'this.msgHight = textarea.style.height})},copyMsg(txt) {// 復制功能// 創建一個臨時的 textarea 元素const textarea = document.createElement('textarea')textarea.value = txt.replace(/<[^>]+>/g, '') // 去掉html標簽textarea.style.position = 'fixed'document.body.appendChild(textarea)textarea.select() // 選中文本try {document.execCommand('copy') // 執行復制ElMessage({message: '復制成功',type: 'success'})} catch (err) {ElMessage({message: '復制失敗',type: 'error'})} finally {document.body.removeChild(textarea) // 移除臨時元素}}},beforeDestroy() {this.eventSourceChat && this.eventSourceChat.close()}
}
</script><style scoped lang="scss">
.talk-window {height: 100%;transition: margin 0.2s ease;position: relative;
}
.talk-container {height: calc(100% - 58px);position: relative;
}
.talk-welcome {text-align: center;// margin-bottom: 25px;padding: 10% 20% 25px;box-sizing: border-box;h1 {margin-bottom: 30px;font-size: 21px;}p {color: #8f9aad;}
}
.messages {padding: 20px;overflow-y: auto;
}.message {display: flex;margin: 12px 0;
}.message.user {justify-content: flex-end;
}.bubble {max-width: 70%;padding: 12px 16px;border-radius: 12px;background: #fff;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}.message.user .bubble {background: #e8f4ff;
}.time {font-size: 12px;color: #666;margin-top: 4px;
}.talk-send {background: #f1f2f7;border-radius: 10px;border: 1px solid #e9e9eb;padding: 5px 10px;margin: 0px 20%;img {cursor: pointer;}textarea {width: 100%;padding: 10px;resize: none;overflow: auto;// min-height: 48px;max-height: 200px;line-height: 1.5;box-sizing: border-box;font-family: inherit;border: 0px;background: #f1f2f7;}textarea:focus {outline: none !important;}
}input {flex: 1;padding: 12px;border: 1px solid #ddd;border-radius: 8px;
}.talk-title {height: 56px;line-height: 56px;p {color: #000000;font-size: 15px;font-weight: 550;text-align: center;}.sangedianBtn {width: 225px;height: 32px;position: absolute;top: 15px;right: 30px;}
}.send-item {max-width: 60%;word-break: break-all;padding: 10px;background: #eef6ff;border-radius: 10px;color: #000000;white-space: pre-wrap;font-size: 13px;
}
.msg-row {margin-bottom: 10px;
}
.talk-box {height: calc(100% - 140px);.talk-content {background-color: #fff;color: #324659;overflow-y: auto;height: calc(100% - 50px);box-sizing: border-box;padding: 0px 20%;// &:hover {// overflow-y: auto;// }.chat-assistant {display: flex;margin-bottom: 10px;.answer-item {line-height: 30px;color: #324659;}}.answer-cont {position: relative;display: flex;width: 100%;> img {width: 30px;height: 30px;margin-right: 10px;}&.end {justify-content: flex-end;}&.start {justify-content: flex-start;}}}.chat-sse {min-height: 100px;max-height: 460px;}.chat-message {height: calc(100vh - 276px);}.thinking-bubble {height: calc(100vh - 296px);}
}
.chat-add {width: 111px;height: 33px;background: #dbeafe;border-radius: 6px !important;font-size: 14px !important;border: 0px;color: #516ffe !important;&:hover {background: #ebf0f7;}.icon-tianjia1 {margin-right: 10px;font-size: 14px;}
}
.talk-btn-cont {text-align: right;height: 30px;margin-top: 5px;
}
</style><style lang="scss">
@use './markdown.scss';
</style>
最終頁面: