|
| 1 | +<template> |
| 2 | + <el-dialog |
| 3 | + :title="$t('views.document.generateQuestion.title')" |
| 4 | + v-model="dialogVisible" |
| 5 | + width="650" |
| 6 | + :close-on-click-modal="false" |
| 7 | + :close-on-press-escape="false" |
| 8 | + > |
| 9 | + <div class="content-height"> |
| 10 | + <el-form |
| 11 | + ref="FormRef" |
| 12 | + :model="form" |
| 13 | + :rules="rules" |
| 14 | + label-position="top" |
| 15 | + require-asterisk-position="right" |
| 16 | + > |
| 17 | + <div class="update-info flex border-r-4 mb-16 p-8-12"> |
| 18 | + <div class="mt-4"> |
| 19 | + <AppIcon iconName="app-warning-colorful" style="font-size: 16px"></AppIcon> |
| 20 | + </div> |
| 21 | + <div class="ml-12 lighter"> |
| 22 | + <p>{{ $t('views.document.generateQuestion.tip1', { data: '{data}' }) }}</p> |
| 23 | + <p> |
| 24 | + {{ $t('views.document.generateQuestion.tip2')+ '<question></question>' + |
| 25 | + $t('views.document.generateQuestion.tip3') }} |
| 26 | + </p> |
| 27 | + <p>{{ $t('views.document.generateQuestion.tip4') }}</p> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + <el-form-item |
| 31 | + :label="$t('views.application.form.aiModel.label')" |
| 32 | + prop="model_id" |
| 33 | + > |
| 34 | + <ModelSelect |
| 35 | + v-model="form.model_id" |
| 36 | + :placeholder="$t('views.application.form.aiModel.placeholder')" |
| 37 | + :options="modelOptions" |
| 38 | + showFooter |
| 39 | + :model-type="'LLM'" |
| 40 | + ></ModelSelect> |
| 41 | + </el-form-item> |
| 42 | + <el-form-item |
| 43 | + :label="$t('views.application.form.prompt.label')" |
| 44 | + prop="prompt" |
| 45 | + > |
| 46 | + <el-input |
| 47 | + v-model="form.prompt" |
| 48 | + :placeholder="$t('views.application.form.prompt.placeholder')" |
| 49 | + :rows="7" |
| 50 | + type="textarea" |
| 51 | + /> |
| 52 | + </el-form-item> |
| 53 | + <el-form-item |
| 54 | + v-if="['document', 'dataset'].includes(apiType)" |
| 55 | + :label="$t('components.selectParagraph.title')" |
| 56 | + prop="state" |
| 57 | + > |
| 58 | + <el-radio-group v-model="state" class="radio-block"> |
| 59 | + <el-radio value="error" size="large">{{ |
| 60 | + $t('components.selectParagraph.error') |
| 61 | + }}</el-radio> |
| 62 | + <el-radio value="all" size="large">{{ $t('components.selectParagraph.all') }}</el-radio> |
| 63 | + </el-radio-group> |
| 64 | + </el-form-item> |
| 65 | + </el-form> |
| 66 | + </div> |
| 67 | + <template #footer> |
| 68 | + <span class="dialog-footer"> |
| 69 | + <el-button @click.prevent="dialogVisible = false"> {{ $t('common.cancel') }} </el-button> |
| 70 | + <el-button type="primary" @click="submitHandle(FormRef)" :disabled="!model || loading"> |
| 71 | + {{ $t('common.confirm') }} |
| 72 | + </el-button> |
| 73 | + </span> |
| 74 | + </template> |
| 75 | + </el-dialog> |
| 76 | +</template> |
| 77 | +<script setup lang="ts"> |
| 78 | +import { reactive, ref, watch } from 'vue' |
| 79 | +import { useRoute } from 'vue-router' |
| 80 | +import documentApi from '@/api/knowledge/document' |
| 81 | +import paragraphApi from '@/api/knowledge/paragraph' |
| 82 | +import knowledgeApi from '@/api/knowledge/knowledge' |
| 83 | +import useStore from '@/stores' |
| 84 | +import { groupBy } from 'lodash' |
| 85 | +import { MsgSuccess } from '@/utils/message' |
| 86 | +import { t } from '@/locales' |
| 87 | +import type { FormInstance } from 'element-plus' |
| 88 | +
|
| 89 | +const route = useRoute() |
| 90 | +const { |
| 91 | + params: { id, documentId } // id为datasetID |
| 92 | +} = route as any |
| 93 | +
|
| 94 | +const { model, prompt, user } = useStore() |
| 95 | +
|
| 96 | +const emit = defineEmits(['refresh']) |
| 97 | +
|
| 98 | +const loading = ref<boolean>(false) |
| 99 | +
|
| 100 | +const dialogVisible = ref<boolean>(false) |
| 101 | +const modelOptions = ref<any>(null) |
| 102 | +const idList = ref<string[]>([]) |
| 103 | +const apiType = ref('') // 文档document或段落paragraph |
| 104 | +const state = ref<'all' | 'error'>('error') |
| 105 | +const stateMap = { |
| 106 | + all: ['0', '1', '2', '3', '4', '5', 'n'], |
| 107 | + error: ['0', '1', '3', '4', '5', 'n'] |
| 108 | +} |
| 109 | +const FormRef = ref() |
| 110 | +const datasetId = ref<string>() |
| 111 | +const userId = user.userInfo?.id as string |
| 112 | +const form = ref(prompt.get(userId)) |
| 113 | +const rules = reactive({ |
| 114 | + model_id: [ |
| 115 | + { |
| 116 | + required: true, |
| 117 | + message: t('views.application.form.aiModel.placeholder'), |
| 118 | + trigger: 'blur' |
| 119 | + } |
| 120 | + ], |
| 121 | + prompt: [ |
| 122 | + { |
| 123 | + required: true, |
| 124 | + message: t('views.application.form.prompt.placeholder'), |
| 125 | + trigger: 'blur' |
| 126 | + } |
| 127 | + ] |
| 128 | +}) |
| 129 | +
|
| 130 | +watch(dialogVisible, (bool) => { |
| 131 | + if (!bool) { |
| 132 | + form.value = prompt.get(userId) |
| 133 | + FormRef.value?.clearValidate() |
| 134 | + } |
| 135 | +}) |
| 136 | +
|
| 137 | +const open = (ids: string[], type: string, _datasetId?: string) => { |
| 138 | + datasetId.value = _datasetId |
| 139 | + getModel() |
| 140 | + idList.value = ids |
| 141 | + apiType.value = type |
| 142 | + dialogVisible.value = true |
| 143 | +} |
| 144 | +
|
| 145 | +const submitHandle = async (formEl: FormInstance) => { |
| 146 | + if (!formEl) { |
| 147 | + return |
| 148 | + } |
| 149 | + await formEl.validate((valid, fields) => { |
| 150 | + if (valid) { |
| 151 | + // 保存提示词 |
| 152 | + prompt.save(user.userInfo?.id as string, form.value) |
| 153 | + if (apiType.value === 'paragraph') { |
| 154 | + const data = { |
| 155 | + ...form.value, |
| 156 | + paragraph_id_list: idList.value |
| 157 | + } |
| 158 | + paragraphApi.batchGenerateRelated(id, documentId, data, loading).then(() => { |
| 159 | + MsgSuccess(t('views.document.generateQuestion.successMessage')) |
| 160 | + emit('refresh') |
| 161 | + dialogVisible.value = false |
| 162 | + }) |
| 163 | + } else if (apiType.value === 'document') { |
| 164 | + const data = { |
| 165 | + ...form.value, |
| 166 | + document_id_list: idList.value, |
| 167 | + state_list: stateMap[state.value] |
| 168 | + } |
| 169 | + documentApi.batchGenerateRelated(id, data, loading).then(() => { |
| 170 | + MsgSuccess(t('views.document.generateQuestion.successMessage')) |
| 171 | + emit('refresh') |
| 172 | + dialogVisible.value = false |
| 173 | + }) |
| 174 | + } else if (apiType.value === 'dataset') { |
| 175 | + const data = { |
| 176 | + ...form.value, |
| 177 | + state_list: stateMap[state.value] |
| 178 | + } |
| 179 | + datasetApi.generateRelated(id ? id : datasetId.value, data, loading).then(() => { |
| 180 | + MsgSuccess(t('views.document.generateQuestion.successMessage')) |
| 181 | + dialogVisible.value = false |
| 182 | + }) |
| 183 | + } |
| 184 | + } |
| 185 | + }) |
| 186 | +} |
| 187 | +
|
| 188 | +function getModel() { |
| 189 | + loading.value = true |
| 190 | + datasetApi |
| 191 | + .getDatasetModel(id ? id : datasetId.value) |
| 192 | + .then((res: any) => { |
| 193 | + modelOptions.value = groupBy(res?.data, 'provider') |
| 194 | + loading.value = false |
| 195 | + }) |
| 196 | + .catch(() => { |
| 197 | + loading.value = false |
| 198 | + }) |
| 199 | +} |
| 200 | +
|
| 201 | +defineExpose({ open }) |
| 202 | +</script> |
| 203 | +<style lang="scss" scoped></style> |
0 commit comments