Skip to content

Commit 7aee911

Browse files
committed
refactor: 按用户保存提示词缓存
1 parent 0d263a3 commit 7aee911

File tree

4 files changed

+58
-24
lines changed

4 files changed

+58
-24
lines changed

ui/src/stores/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import useApplicationStore from './modules/application'
1010
import useDocumentStore from './modules/document'
1111
import useProblemStore from './modules/problem'
1212
import useLogStore from './modules/log'
13+
import usePromptStore from './modules/prompt'
1314

1415
const useStore = () => ({
1516
common: useCommonStore(),
@@ -20,7 +21,8 @@ const useStore = () => ({
2021
application: useApplicationStore(),
2122
document: useDocumentStore(),
2223
problem: useProblemStore(),
23-
log: useLogStore()
24+
log: useLogStore(),
25+
prompt: usePromptStore(),
2426
})
2527

2628
export default useStore

ui/src/stores/modules/prompt.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { defineStore } from 'pinia'
2+
3+
export interface promptTypes {
4+
user: string
5+
formValue: { model_id: string, prompt: string }
6+
}
7+
8+
const usePromptStore = defineStore({
9+
id: 'prompt',
10+
state: (): promptTypes[] => (JSON.parse(localStorage.getItem('PROMPT_CACHE') || '[]')),
11+
actions: {
12+
save(user: string, formValue: any) {
13+
this.$state.forEach((item: any, index: number) => {
14+
if (item.user === user) {
15+
this.$state.splice(index, 1)
16+
}
17+
})
18+
this.$state.push({ user, formValue })
19+
localStorage.setItem('PROMPT_CACHE', JSON.stringify(this.$state))
20+
},
21+
get(user: string) {
22+
for (let i = 0; i < this.$state.length; i++) {
23+
if (this.$state[i].user === user) {
24+
return this.$state[i].formValue
25+
}
26+
}
27+
return {
28+
model_id: '',
29+
prompt: '内容:{data}\n' +
30+
'\n' +
31+
'请总结上面的内容,并根据内容总结生成 5 个问题。\n' +
32+
'回答要求:\n' +
33+
'- 请只输出问题;\n' +
34+
'- 请将每个问题放置<question></question>标签中。'
35+
}
36+
}
37+
}
38+
})
39+
40+
export default usePromptStore

ui/src/views/document/component/GenerateRelatedDialog.vue

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ const {
134134
params: { id } // id为datasetID
135135
} = route as any
136136
137-
const { model } = useStore()
137+
const { model, prompt, user } = useStore()
138138
139139
140140
const emit = defineEmits(['refresh'])
@@ -147,15 +147,9 @@ const providerOptions = ref<Array<Provider>>([])
147147
const documentIdList = ref<string[]>([])
148148
149149
const FormRef = ref()
150-
const form = ref({
151-
model_id: '',
152-
prompt: '内容:{data}\n' +
153-
'\n' +
154-
'请总结上面的内容,并根据内容总结生成 5 个问题。\n' +
155-
'回答要求:\n' +
156-
'- 请只输出问题;\n' +
157-
'- 请将每个问题放置<question></question>标签中。'
158-
})
150+
const userId = user.userInfo?.id as string
151+
const form = ref(prompt.get(userId))
152+
159153
160154
const rules = reactive({
161155
model_id: [{ required: true, message: '请选择AI 模型', trigger: 'blur' }],
@@ -176,6 +170,8 @@ const submitHandle = async (formEl: FormInstance) => {
176170
}
177171
await formEl.validate((valid, fields) => {
178172
if (valid) {
173+
// 保存提示词
174+
prompt.save(user.userInfo?.id as string, form.value)
179175
const data = { ...form.value, document_id_list: documentIdList.value }
180176
documentApi.batchGenerateRelated(id, data).then(() => {
181177
MsgSuccess('生成关联问题成功')

ui/src/views/paragraph/component/GenerateRelatedDialog.vue

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
</el-dialog>
118118
</template>
119119
<script setup lang="ts">
120-
import { reactive, ref, watch } from 'vue'
120+
import { reactive, ref } from 'vue'
121121
import { useRoute } from 'vue-router'
122122
import paragraphApi from '@/api/paragraph'
123123
@@ -127,16 +127,14 @@ import type { Provider } from '@/api/type/model'
127127
import datasetApi from '@/api/dataset'
128128
import { groupBy } from 'lodash'
129129
import { MsgSuccess } from '@/utils/message'
130-
import { t } from '@/locales'
131130
import type { FormInstance } from 'element-plus'
132131
133132
const route = useRoute()
134133
const {
135134
params: { id, documentId } // id为datasetID
136135
} = route as any
137136
138-
const { model } = useStore()
139-
137+
const { model, prompt, user } = useStore()
140138
141139
const emit = defineEmits(['refresh'])
142140
@@ -148,15 +146,10 @@ const providerOptions = ref<Array<Provider>>([])
148146
const paragraphIdList = ref<string[]>([])
149147
150148
const FormRef = ref()
151-
const form = ref({
152-
model_id: '',
153-
prompt: '内容:{data}\n' +
154-
'\n' +
155-
'请总结上面的内容,并根据内容总结生成 5 个问题。\n' +
156-
'回答要求:\n' +
157-
'- 请只输出问题;\n' +
158-
'- 请将每个问题放置<question></question>标签中。'
159-
})
149+
150+
const userId = user.userInfo?.id as string
151+
const form = ref(prompt.get(userId))
152+
160153
161154
const rules = reactive({
162155
model_id: [{ required: true, message: '请选择AI 模型', trigger: 'blur' }],
@@ -176,6 +169,9 @@ const submitHandle = async (formEl: FormInstance) => {
176169
}
177170
await formEl.validate((valid, fields) => {
178171
if (valid) {
172+
// 保存提示词
173+
prompt.save(user.userInfo?.id as string, form.value)
174+
179175
const data = { ...form.value, paragraph_id_list: paragraphIdList.value }
180176
paragraphApi.batchGenerateRelated(id, documentId, data).then(() => {
181177
MsgSuccess('生成关联问题成功')

0 commit comments

Comments
 (0)