refactor: 按用户保存提示词缓存

This commit is contained in:
CaptainB 2024-10-29 15:04:34 +08:00 committed by 刘瑞斌
parent 0d263a311c
commit 7aee911430
4 changed files with 58 additions and 24 deletions

View File

@ -10,6 +10,7 @@ import useApplicationStore from './modules/application'
import useDocumentStore from './modules/document'
import useProblemStore from './modules/problem'
import useLogStore from './modules/log'
import usePromptStore from './modules/prompt'
const useStore = () => ({
common: useCommonStore(),
@ -20,7 +21,8 @@ const useStore = () => ({
application: useApplicationStore(),
document: useDocumentStore(),
problem: useProblemStore(),
log: useLogStore()
log: useLogStore(),
prompt: usePromptStore(),
})
export default useStore

View File

@ -0,0 +1,40 @@
import { defineStore } from 'pinia'
export interface promptTypes {
user: string
formValue: { model_id: string, prompt: string }
}
const usePromptStore = defineStore({
id: 'prompt',
state: (): promptTypes[] => (JSON.parse(localStorage.getItem('PROMPT_CACHE') || '[]')),
actions: {
save(user: string, formValue: any) {
this.$state.forEach((item: any, index: number) => {
if (item.user === user) {
this.$state.splice(index, 1)
}
})
this.$state.push({ user, formValue })
localStorage.setItem('PROMPT_CACHE', JSON.stringify(this.$state))
},
get(user: string) {
for (let i = 0; i < this.$state.length; i++) {
if (this.$state[i].user === user) {
return this.$state[i].formValue
}
}
return {
model_id: '',
prompt: '内容:{data}\n' +
'\n' +
'请总结上面的内容,并根据内容总结生成 5 个问题。\n' +
'回答要求:\n' +
'- 请只输出问题;\n' +
'- 请将每个问题放置<question></question>标签中。'
}
}
}
})
export default usePromptStore

View File

@ -134,7 +134,7 @@ const {
params: { id } // iddatasetID
} = route as any
const { model } = useStore()
const { model, prompt, user } = useStore()
const emit = defineEmits(['refresh'])
@ -147,15 +147,9 @@ const providerOptions = ref<Array<Provider>>([])
const documentIdList = ref<string[]>([])
const FormRef = ref()
const form = ref({
model_id: '',
prompt: '内容:{data}\n' +
'\n' +
'请总结上面的内容,并根据内容总结生成 5 个问题。\n' +
'回答要求:\n' +
'- 请只输出问题;\n' +
'- 请将每个问题放置<question></question>标签中。'
})
const userId = user.userInfo?.id as string
const form = ref(prompt.get(userId))
const rules = reactive({
model_id: [{ required: true, message: '请选择AI 模型', trigger: 'blur' }],
@ -176,6 +170,8 @@ const submitHandle = async (formEl: FormInstance) => {
}
await formEl.validate((valid, fields) => {
if (valid) {
//
prompt.save(user.userInfo?.id as string, form.value)
const data = { ...form.value, document_id_list: documentIdList.value }
documentApi.batchGenerateRelated(id, data).then(() => {
MsgSuccess('生成关联问题成功')

View File

@ -117,7 +117,7 @@
</el-dialog>
</template>
<script setup lang="ts">
import { reactive, ref, watch } from 'vue'
import { reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import paragraphApi from '@/api/paragraph'
@ -127,7 +127,6 @@ import type { Provider } from '@/api/type/model'
import datasetApi from '@/api/dataset'
import { groupBy } from 'lodash'
import { MsgSuccess } from '@/utils/message'
import { t } from '@/locales'
import type { FormInstance } from 'element-plus'
const route = useRoute()
@ -135,8 +134,7 @@ const {
params: { id, documentId } // iddatasetID
} = route as any
const { model } = useStore()
const { model, prompt, user } = useStore()
const emit = defineEmits(['refresh'])
@ -148,15 +146,10 @@ const providerOptions = ref<Array<Provider>>([])
const paragraphIdList = ref<string[]>([])
const FormRef = ref()
const form = ref({
model_id: '',
prompt: '内容:{data}\n' +
'\n' +
'请总结上面的内容,并根据内容总结生成 5 个问题。\n' +
'回答要求:\n' +
'- 请只输出问题;\n' +
'- 请将每个问题放置<question></question>标签中。'
})
const userId = user.userInfo?.id as string
const form = ref(prompt.get(userId))
const rules = reactive({
model_id: [{ required: true, message: '请选择AI 模型', trigger: 'blur' }],
@ -176,6 +169,9 @@ const submitHandle = async (formEl: FormInstance) => {
}
await formEl.validate((valid, fields) => {
if (valid) {
//
prompt.save(user.userInfo?.id as string, form.value)
const data = { ...form.value, paragraph_id_list: paragraphIdList.value }
paragraphApi.batchGenerateRelated(id, documentId, data).then(() => {
MsgSuccess('生成关联问题成功')