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

426 lines
11 KiB
Vue
Raw Normal View History

2023-11-17 03:36:16 +00:00
<template>
2023-12-06 09:30:46 +00:00
<div class="ai-chat">
2023-11-28 09:39:35 +00:00
<el-scrollbar ref="scrollDiv">
2023-12-06 09:30:46 +00:00
<div ref="dialogScrollbar" class="ai-chat__content p-24">
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">
2023-12-04 10:02:00 +00:00
<h4>您好我是 {{ data?.name || '应用名称' }}</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)"
2023-12-07 11:28:38 +00:00
class="problem-button ellipsis-2"
:class="log ? 'disabled' : 'cursor'"
2023-11-24 11:02:52 +00:00
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">
2023-12-07 07:57:00 +00:00
<div class="text break-all">
2023-11-28 09:39:35 +00:00
{{ 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">
2023-12-01 11:17:58 +00:00
<el-card shadow="always" class="dialog-card">
回答中 <span class="dotting"></span>
</el-card>
2023-11-28 09:39:35 +00:00
</div>
2023-12-13 11:06:13 +00:00
2023-11-29 07:38:22 +00:00
<el-card v-else shadow="always" class="dialog-card">
2023-12-13 11:06:13 +00:00
<MdPreview ref="editorRef" editorId="preview-only" :modelValue="item.answer_text" />
2023-11-29 07:38:22 +00:00
</el-card>
2023-12-07 11:28:38 +00:00
<div class="flex-between mt-8" v-if="log">
2023-12-12 08:00:54 +00:00
<LogOperationButton :data="item" :applicationId="appId" />
2023-12-07 11:28:38 +00:00
</div>
<div class="flex-between mt-8" v-else>
2023-11-30 11:14:40 +00:00
<div>
<el-button
type="primary"
v-if="item.is_stop && !item.write_ed"
@click="startChat(item)"
link
>继续</el-button
>
<el-button type="primary" v-else-if="!item.write_ed" @click="stopChat(item)" link
>停止回答</el-button
>
</div>
2023-12-01 10:21:49 +00:00
<div v-if="item.write_ed && props.appId">
2023-12-01 11:17:58 +00:00
<OperationButton
:data="item"
:applicationId="appId"
:chartId="chartOpenId"
@regeneration="regenerationChart(item)"
/>
2023-12-01 10:21:49 +00:00
</div>
2023-11-30 11:14:40 +00:00
</div>
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-12-07 11:28:38 +00:00
<div class="ai-chat__operate p-24" v-if="!log">
2023-11-21 10:32:51 +00:00
<div class="operate-textarea flex">
<el-input
2023-12-04 10:02:00 +00:00
ref="quickInputRef"
2023-11-21 10:32:51 +00:00
v-model="inputValue"
type="textarea"
placeholder="请输入"
:autosize="{ minRows: 1, maxRows: 8 }"
2023-11-28 09:39:35 +00:00
@keydown.enter="sendChatHandle($event)"
2023-11-21 10:32:51 +00:00
/>
2023-12-04 10:10:42 +00:00
<div class="operate">
<el-button
text
class="sent-button"
:disabled="isDisabledChart || loading"
@click="sendChatHandle"
>
<img v-show="isDisabledChart || loading" src="@/assets/icon_send.svg" alt="" />
<img
v-show="!isDisabledChart && !loading"
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-12-07 11:28:38 +00:00
import { ref, nextTick, onUpdated, computed, watch } from 'vue'
2023-12-06 09:30:46 +00:00
import { useRoute } from 'vue-router'
2023-12-07 11:28:38 +00:00
import LogOperationButton from './LogOperationButton.vue'
2023-12-01 03:36:04 +00:00
import OperationButton from './OperationButton.vue'
2023-11-24 11:02:52 +00:00
import applicationApi from '@/api/application'
2023-11-30 09:12:39 +00:00
import { ChatManagement, type chatType } from '@/api/type/application'
2023-11-28 09:39:35 +00:00
import { randomId } from '@/utils/utils'
2023-12-01 10:21:49 +00:00
import useStore from '@/stores'
2023-12-13 11:06:13 +00:00
import { MdPreview } from 'md-editor-v3'
2023-12-07 07:22:07 +00:00
defineOptions({ name: 'AiChat' })
2023-12-01 10:21:49 +00:00
const route = useRoute()
const {
params: { accessToken }
} = route as any
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-30 10:50:42 +00:00
},
2023-12-07 07:22:07 +00:00
appId: String,
2023-12-07 11:28:38 +00:00
log: Boolean,
record: {
type: Array<chatType[]>,
default: () => []
}
2023-11-17 03:36:16 +00:00
})
2023-12-01 10:21:49 +00:00
const { application } = useStore()
2023-11-28 09:39:35 +00:00
2023-12-04 10:02:00 +00:00
const quickInputRef = ref()
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-12-07 11:28:38 +00:00
const chatList = ref<any[]>([])
2023-11-27 10:57:52 +00:00
2023-11-29 03:25:14 +00:00
const isDisabledChart = computed(
2023-11-30 10:50:42 +00:00
() => !(inputValue.value && (props.appId || (props.data?.name && props.data?.model_id)))
2023-11-29 03:25:14 +00:00
)
2023-12-07 11:28:38 +00:00
watch(
() => props.record,
(value) => {
if (props.log) {
chatList.value = value
}
},
{
immediate: true
}
)
2023-11-24 11:02:52 +00:00
function quickProblemHandel(val: string) {
2023-12-07 11:28:38 +00:00
if (!props.log) {
inputValue.value = val
nextTick(() => {
quickInputRef.value?.focus()
})
}
2023-11-24 11:02:52 +00:00
}
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-12-05 06:27:50 +00:00
if (!isDisabledChart.value && !loading.value) {
2023-11-29 03:25:14 +00:00
chatMessage()
}
2023-11-28 09:39:35 +00:00
} else {
// 如果同时按下ctrl+回车键,则会换行
inputValue.value += '\n'
}
}
2023-11-30 09:12:39 +00:00
const stopChat = (chat: chatType) => {
ChatManagement.stop(chat.id)
}
const startChat = (chat: chatType) => {
ChatManagement.write(chat.id)
}
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
}
2023-11-30 10:50:42 +00:00
if (props.appId) {
applicationApi
.getChatOpen(props.appId)
.then((res) => {
chartOpenId.value = res.data
chatMessage()
})
2023-12-01 10:21:49 +00:00
.catch((res) => {
if (res.response.status === 403) {
application.asyncAppAuthentication(accessToken).then(() => {
getChartOpenId()
})
}
2023-11-30 10:50:42 +00:00
loading.value = false
})
} else {
applicationApi
.postChatOpen(obj)
.then((res) => {
chartOpenId.value = res.data
chatMessage()
})
.catch(() => {
loading.value = false
})
}
2023-11-24 11:02:52 +00:00
}
2023-12-01 11:17:58 +00:00
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
2023-11-30 09:12:39 +00:00
const id = randomId()
chatList.value.push({
id: id,
problem_text: problem_text,
answer_text: '',
buffer: [],
write_ed: false,
2023-12-01 10:21:49 +00:00
is_stop: false,
record_id: '',
vote_status: '-1'
2023-11-30 09:12:39 +00:00
})
2023-12-05 06:27:50 +00:00
inputValue.value = ''
2023-11-29 05:00:31 +00:00
applicationApi.postChatMessage(chartOpenId.value, problem_text).then(async (response) => {
const row = chatList.value.find((item) => item.id === id)
2023-12-01 07:59:50 +00:00
2023-11-29 05:00:31 +00:00
if (row) {
2023-11-30 09:12:39 +00:00
ChatManagement.addChatRecord(row, 50, loading)
ChatManagement.write(id)
2023-11-29 05:00:31 +00:00
const reader = response.body.getReader()
2023-12-01 03:36:04 +00:00
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
2023-11-29 05:00:31 +00:00
while (true) {
const { done, value } = await reader.read()
if (done) {
2023-11-30 09:12:39 +00:00
ChatManagement.close(id)
2023-11-29 05:00:31 +00:00
break
}
try {
const decoder = new TextDecoder('utf-8')
const str = decoder.decode(value, { stream: true })
2023-12-01 10:21:49 +00:00
2023-11-29 05:00:31 +00:00
if (str && str.startsWith('data:')) {
2023-12-01 10:21:49 +00:00
row.record_id = JSON?.parse(str.replace('data:', '')).id
2023-11-29 05:00:31 +00:00
const content = JSON?.parse(str.replace('data:', ''))?.content
if (content) {
2023-11-30 09:12:39 +00:00
ChatManagement.append(id, content)
2023-11-29 05:00:31 +00:00
}
}
2023-12-01 03:36:04 +00:00
} catch (e) {
// console
}
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
2023-12-01 11:17:58 +00:00
function regenerationChart(item: chatType) {
inputValue.value = item.problem_text
chatMessage()
}
2023-11-28 09:39:35 +00:00
// 滚动到底部
function handleScrollBottom() {
nextTick(() => {
scrollDiv.value.setScrollTop(dialogScrollbar.value.scrollHeight)
})
}
onUpdated(() => {
2023-12-07 11:28:38 +00:00
if (!props.log) {
handleScrollBottom()
}
2023-11-28 09:39:35 +00:00
})
2023-11-17 03:36:16 +00:00
</script>
2023-11-20 10:56:31 +00:00
<style lang="scss" scoped>
2023-12-06 09:30:46 +00:00
.ai-chat {
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;
2023-11-28 09:39:35 +00:00
color: var(--app-text-color);
2023-11-20 10:56:31 +00:00
&__content {
2023-12-01 11:17:58 +00:00
padding-top: 0;
2023-11-21 10:32:51 +00:00
padding-bottom: 96px;
2023-12-01 11:17:58 +00:00
box-sizing: border-box;
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-12-01 10:21:49 +00:00
:deep(ol) {
2023-12-01 11:17:58 +00:00
margin-left: 16px !important;
2023-12-01 10:21:49 +00:00
}
2023-11-21 03:43:47 +00:00
}
2023-11-22 03:05:06 +00:00
.text {
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);
}
2023-12-07 11:28:38 +00:00
&.disabled {
&:hover {
background: var(--app-layout-bg-color);
}
}
2023-11-20 10:56:31 +00:00
: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;
&: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>