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

254 lines
6.7 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-21 02:56:25 +00:00
<el-scrollbar>
2023-11-21 03:43:47 +00:00
<div 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>
<!-- <AppAvatar>
2023-11-21 03:43:47 +00:00
<img src="@/assets/user-icon.svg" style="width: 54%" alt="" />
2023-11-21 10:32:51 +00:00
</AppAvatar> -->
2023-11-22 03:05:06 +00:00
</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>
<div class="item-content mb-16">
<div class="avatar">
<AppAvatar>
<img src="@/assets/user-icon.svg" style="width: 54%" alt="" />
</AppAvatar>
</div>
<div class="content">
<div class="text">
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
</div>
</div>
</div>
<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>
<div class="content">
<el-card shadow="always" class="dialog-card"> XXXXXXXXX </el-card>
</div>
2023-11-21 02:56:25 +00:00
</div>
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-24 11:02:52 +00:00
<div class="operate" v-loading="loading">
<el-button
text
class="sent-button"
:disabled="!(inputValue && data?.name && data?.model_id)"
2023-11-27 10:57:52 +00:00
@click="chatMessage"
2023-11-24 11:02:52 +00:00
>
<img
v-show="!(inputValue && data?.name && data?.model_id)"
src="@/assets/icon_send.svg"
alt=""
/>
<img
v-show="inputValue && data?.name && data?.model_id"
src="@/assets/icon_send_colorful.svg"
alt=""
/>
<!-- <AppIcon iconName="app-send"></AppIcon> -->
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-20 10:56:31 +00:00
import { ref } from 'vue'
2023-11-24 11:02:52 +00:00
import applicationApi from '@/api/application'
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-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-24 11:02:52 +00:00
function quickProblemHandel(val: string) {
inputValue.value = val
}
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() {
if (!chartOpenId.value) {
getChartOpenId()
} else {
applicationApi.postChatMessage(chartOpenId.value, inputValue.value).then(async (response) => {
const reader = response.body.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
loading.value = false
break
}
const decoder = new TextDecoder('utf-8')
const str = decoder.decode(value, { stream: true })
console.log('value', JSON.parse(str.replace('data:', '')))
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-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-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>