UnisKB/ui/src/components/ai-dialog/index.vue

307 lines
8.4 KiB
Vue
Raw Normal View History

2023-11-17 03:36:16 +00:00
<template>
2023-11-21 10:32:51 +00:00
<div class="ai-dialog p-24">
2023-11-28 09:39:35 +00:00
<el-scrollbar ref="scrollDiv">
<div ref="dialogScrollbar" class="ai-dialog__content">
2023-11-22 03:05:06 +00:00
<div class="item-content mb-16">
<div class="avatar">
<AppAvatar class="avatar-gradient">
<img src="@/assets/icon_robot.svg" style="width: 54%" alt="" />
</AppAvatar>
</div>
2023-11-21 03:43:47 +00:00
2023-11-22 03:05:06 +00:00
<div class="content">
<el-card shadow="always" class="dialog-card">
<h4>您好我是 MaxKB 智能小助手</h4>
2023-11-22 09:04:47 +00:00
<div class="mt-4" v-if="data?.prologue">
<el-text type="info">{{ data?.prologue }}</el-text>
2023-11-22 03:05:06 +00:00
</div>
</el-card>
2023-11-22 10:37:08 +00:00
<el-card shadow="always" class="dialog-card mt-12" v-if="data?.example?.length > 0">
2023-11-22 03:05:06 +00:00
<h4 class="mb-8">您可以尝试输入以下问题</h4>
2023-11-22 09:04:47 +00:00
<el-space wrap>
2023-11-22 10:37:08 +00:00
<template v-for="(item, index) in data?.example" :key="index">
2023-11-24 11:02:52 +00:00
<div
@click="quickProblemHandel(item)"
class="problem-button cursor ellipsis-2"
v-if="item"
>
2023-11-22 10:37:08 +00:00
<el-icon><EditPen /></el-icon>
{{ item }}
</div>
</template>
2023-11-22 09:04:47 +00:00
</el-space>
2023-11-22 03:05:06 +00:00
</el-card>
</div>
</div>
2023-11-28 09:39:35 +00:00
<template v-for="(item, index) in chatList" :key="index">
<!-- 问题 -->
<div class="item-content mb-16 lighter">
<div class="avatar">
<AppAvatar>
<img src="@/assets/user-icon.svg" style="width: 54%" alt="" />
</AppAvatar>
</div>
<div class="content">
<div class="text">
{{ item.problem_text }}
</div>
2023-11-22 03:05:06 +00:00
</div>
</div>
2023-11-28 09:39:35 +00:00
<!-- 回答 -->
<div class="item-content mb-16 lighter">
<div class="avatar">
<AppAvatar class="avatar-gradient">
<img src="@/assets/icon_robot.svg" style="width: 54%" alt="" />
</AppAvatar>
</div>
<div class="content">
2023-11-29 07:42:48 +00:00
<div class="flex" v-if="!item.answer_text">
<el-card shadow="always" class="dialog-card"> 回答中... </el-card>
2023-11-28 09:39:35 +00:00
</div>
2023-11-29 07:38:22 +00:00
<el-card v-else shadow="always" class="dialog-card">
<MarkdownRenderer :source="item.answer_text"></MarkdownRenderer>
</el-card>
2023-11-28 09:39:35 +00:00
</div>
2023-11-22 03:05:06 +00:00
</div>
2023-11-28 09:39:35 +00:00
</template>
2023-11-20 10:56:31 +00:00
</div>
2023-11-21 02:56:25 +00:00
</el-scrollbar>
2023-11-21 10:32:51 +00:00
<div class="ai-dialog__operate p-24">
<div class="operate-textarea flex">
<el-input
v-model="inputValue"
type="textarea"
placeholder="请输入"
:autosize="{ minRows: 1, maxRows: 8 }"
2023-11-28 09:39:35 +00:00
@keydown.enter="sendChatHandle($event)"
:disabled="loading"
2023-11-21 10:32:51 +00:00
/>
2023-11-24 11:02:52 +00:00
<div class="operate" v-loading="loading">
2023-11-29 03:25:14 +00:00
<el-button text class="sent-button" :disabled="isDisabledChart" @click="sendChatHandle">
<img v-show="isDisabledChart" src="@/assets/icon_send.svg" alt="" />
<img v-show="!isDisabledChart" src="@/assets/icon_send_colorful.svg" alt="" />
2023-11-21 10:32:51 +00:00
</el-button>
</div>
2023-11-20 10:56:31 +00:00
</div>
2023-11-17 03:36:16 +00:00
</div>
</div>
</template>
<script setup lang="ts">
2023-11-29 03:25:14 +00:00
import { ref, nextTick, onUpdated, computed } from 'vue'
2023-11-24 11:02:52 +00:00
import applicationApi from '@/api/application'
2023-11-29 05:00:31 +00:00
import { ChatManage, type chatType } from '@/api/type/application'
2023-11-28 09:39:35 +00:00
import { randomId } from '@/utils/utils'
2023-11-29 07:38:22 +00:00
import MarkdownRenderer from '@/components/markdown-renderer/index.vue'
2023-11-17 03:36:16 +00:00
const props = defineProps({
data: {
2023-11-22 09:04:47 +00:00
type: Object,
default: () => {}
2023-11-17 03:36:16 +00:00
}
})
2023-11-28 09:39:35 +00:00
const scrollDiv = ref()
const dialogScrollbar = ref()
2023-11-24 11:02:52 +00:00
const loading = ref(false)
2023-11-20 10:56:31 +00:00
const inputValue = ref('')
2023-11-27 10:57:52 +00:00
const chartOpenId = ref('')
2023-11-28 09:39:35 +00:00
const chatList = ref<chatType[]>([])
2023-11-27 10:57:52 +00:00
2023-11-29 03:25:14 +00:00
const isDisabledChart = computed(
() => !(inputValue.value && props.data?.name && props.data?.model_id)
)
2023-11-24 11:02:52 +00:00
function quickProblemHandel(val: string) {
inputValue.value = val
}
2023-11-27 09:06:39 +00:00
2023-11-28 09:39:35 +00:00
function sendChatHandle(event: any) {
if (!event.ctrlKey) {
// 如果没有按下组合键ctrl则会阻止默认事件
event.preventDefault()
2023-11-29 03:25:14 +00:00
if (!isDisabledChart.value) {
chatMessage()
}
2023-11-28 09:39:35 +00:00
} else {
// 如果同时按下ctrl+回车键,则会换行
inputValue.value += '\n'
}
}
2023-11-27 09:06:39 +00:00
/**
* 对话
*/
2023-11-27 10:57:52 +00:00
function getChartOpenId() {
2023-11-24 11:02:52 +00:00
loading.value = true
const obj = {
model_id: props.data.model_id,
dataset_id_list: props.data.dataset_id_list,
2023-11-27 09:06:39 +00:00
multiple_rounds_dialogue: props.data.multiple_rounds_dialogue
2023-11-24 11:02:52 +00:00
}
applicationApi
.postChatOpen(obj)
.then((res) => {
2023-11-27 10:57:52 +00:00
chartOpenId.value = res.data
chatMessage()
2023-11-24 11:02:52 +00:00
})
.catch(() => {
loading.value = false
})
}
2023-11-27 10:57:52 +00:00
function chatMessage() {
2023-11-28 09:39:35 +00:00
loading.value = true
2023-11-27 10:57:52 +00:00
if (!chartOpenId.value) {
getChartOpenId()
} else {
2023-11-29 05:00:31 +00:00
const problem_text = inputValue.value
inputValue.value = ''
applicationApi.postChatMessage(chartOpenId.value, problem_text).then(async (response) => {
const id = randomId()
2023-11-28 09:39:35 +00:00
chatList.value.push({
2023-11-29 05:00:31 +00:00
id: id,
problem_text: problem_text,
answer_text: '',
buffer: []
2023-11-28 09:39:35 +00:00
})
2023-11-29 05:00:31 +00:00
const row = chatList.value.find((item) => item.id === id)
if (row) {
const chatMange = new ChatManage(row, 50, loading)
chatMange.write()
const reader = response.body.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
chatMange.close()
break
}
try {
const decoder = new TextDecoder('utf-8')
const str = decoder.decode(value, { stream: true })
if (str && str.startsWith('data:')) {
// console.log(JSON?.parse(str.replace('data:', '')))
const content = JSON?.parse(str.replace('data:', ''))?.content
if (content) {
chatMange.append(content)
}
}
} catch (e) {}
2023-11-28 09:39:35 +00:00
}
2023-11-27 10:08:58 +00:00
}
2023-11-27 10:57:52 +00:00
})
}
2023-11-27 09:06:39 +00:00
}
2023-11-28 09:39:35 +00:00
// 滚动到底部
function handleScrollBottom() {
nextTick(() => {
scrollDiv.value.setScrollTop(dialogScrollbar.value.scrollHeight)
})
}
onUpdated(() => {
handleScrollBottom()
})
2023-11-17 03:36:16 +00:00
</script>
2023-11-20 10:56:31 +00:00
<style lang="scss" scoped>
.ai-dialog {
2023-11-21 10:32:51 +00:00
--padding-left: 40px;
2023-11-21 03:43:47 +00:00
height: 100%;
2023-11-20 10:56:31 +00:00
display: flex;
flex-direction: column;
2023-11-21 02:56:25 +00:00
box-sizing: border-box;
2023-11-21 10:32:51 +00:00
position: relative;
padding-right: 20px;
padding-top: 0;
2023-11-28 09:39:35 +00:00
color: var(--app-text-color);
2023-11-20 10:56:31 +00:00
&__content {
2023-11-21 10:32:51 +00:00
width: 99%;
padding-bottom: 96px;
2023-11-21 03:43:47 +00:00
.avatar {
float: left;
}
.content {
2023-11-21 10:32:51 +00:00
padding-left: var(--padding-left);
2023-11-21 03:43:47 +00:00
}
2023-11-22 03:05:06 +00:00
.text {
word-break: break-all;
padding: 6px 0;
}
2023-11-20 10:56:31 +00:00
.problem-button {
2023-11-21 10:32:51 +00:00
width: 100%;
2023-11-20 10:56:31 +00:00
border: none;
border-radius: 8px;
background: var(--app-layout-bg-color);
height: 46px;
2023-11-22 09:04:47 +00:00
padding: 0 12px;
line-height: 46px;
box-sizing: border-box;
color: var(--el-text-color-regular);
-webkit-line-clamp: 1;
word-break: break-all;
2023-11-20 10:56:31 +00:00
&:hover {
background: var(--el-color-primary-light-9);
}
:deep(.el-icon) {
color: var(--el-color-primary);
}
}
}
2023-11-21 10:32:51 +00:00
&__operate {
background: #f3f7f9;
position: absolute;
bottom: 0;
left: 0;
width: 100%;
2023-11-21 02:56:25 +00:00
box-sizing: border-box;
2023-11-21 10:32:51 +00:00
z-index: 10;
padding-top: 16px;
&:before {
background: linear-gradient(0deg, #f3f7f9 0%, rgba(243, 247, 249, 0) 100%);
content: '';
position: absolute;
width: 100%;
top: -16px;
left: 0;
height: 16px;
2023-11-20 10:56:31 +00:00
}
2023-11-21 10:32:51 +00:00
.operate-textarea {
box-shadow: 0px 6px 24px 0px rgba(31, 35, 41, 0.08);
background-color: #ffffff;
border-radius: 8px;
border: 1px solid #ffffff;
box-sizing: border-box;
2023-11-21 02:56:25 +00:00
2023-11-21 10:32:51 +00:00
&:has(.el-textarea__inner:focus) {
border: 1px solid var(--el-color-primary);
}
:deep(.el-textarea__inner) {
border-radius: 8px !important;
box-shadow: none;
resize: none;
padding: 12px 16px;
}
.operate {
2023-11-24 11:02:52 +00:00
padding: 6px 10px;
2023-11-21 10:32:51 +00:00
.sent-button {
2023-11-24 11:02:52 +00:00
max-height: none;
2023-11-21 10:32:51 +00:00
.el-icon {
font-size: 24px;
}
2023-11-20 10:56:31 +00:00
}
2023-11-24 11:02:52 +00:00
:deep(.el-loading-spinner) {
margin-top: -15px;
.circular {
width: 31px;
height: 31px;
}
}
2023-11-20 10:56:31 +00:00
}
}
}
2023-11-21 10:32:51 +00:00
.dialog-card {
border: none;
}
2023-11-20 10:56:31 +00:00
}
</style>