ChatSetting.vue
7.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<template>
<a-card title="智能助手配置" class="config-card">
<a-form
:model="formState"
layout="horizontal"
ref="formRef"
:rules="rules"
:labelCol="{ span: 3 }"
:wrapperCol="{ span: 16 }"
class="config-form"
>
<!-- 向量模型 -->
<a-form-item label="向量模型" class="form-item required" name="embeddingId">
<a-select
v-model:value="formState.embeddingId"
placeholder="请选择向量模型"
:options="options.embeddingOptions"
show-search
option-filter-prop="label"
/>
</a-form-item>
<!-- 大语言模型 -->
<a-form-item label="大语言模型" class="form-item required" name="llmId">
<a-select
v-model:value="formState.llmId"
placeholder="请选择大语言模型"
:options="options.llmOptions"
show-search
option-filter-prop="label"
/>
</a-form-item>
<!-- 知识库 -->
<a-form-item label="知识库" class="form-item required" name="knowledgeId">
<a-select
v-model:value="formState.knowledgeId"
placeholder="请选择知识库"
:options="options.knowledgeOptions"
show-search
option-filter-prop="label"
/>
</a-form-item>
<!-- 按钮(多选) -->
<a-form-item label="功能按钮" class="form-item required" name="buttonIds">
<a-select
v-model:value="formState.buttonIds"
mode="multiple"
placeholder="请选择按钮"
:options="options.buttonOptions"
show-search
option-filter-prop="label"
:max-tag-count="6"
:max="5"
/>
<div class="hint">最多可选择5个按钮</div>
</a-form-item>
<!-- 提示词 -->
<a-form-item label="提示词" class="form-item required" name="prompt">
<a-textarea
v-model:value="formState.prompt"
placeholder="请输入提示词,例如:你是一个专业的AI助手..."
:rows="9"
:maxlength="500"
show-count
/>
</a-form-item>
<!-- 操作按钮 -->
<a-form-item class="action-buttons">
<a-space>
<a-button @click="resetForm">
<template #icon><UndoOutlined /></template>
重置
</a-button>
<a-button type="primary" @click="saveConfigData" :loading="saving">
<template #icon><SaveOutlined /></template>
保存配置
</a-button>
</a-space>
</a-form-item>
</a-form>
</a-card>
</template>
<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue';
import { getConfigData, saveConfig } from './Chatsetting.api';
import { FormInstance, message } from 'ant-design-vue';
import { SaveOutlined, UndoOutlined } from '@ant-design/icons-vue';
// 表单数据结构
const formState = reactive({
id: '',
embeddingId: undefined,
llmId: undefined,
knowledgeId: undefined,
buttonIds: [],
prompt: '',
});
const formRef = ref<FormInstance>(); // 添加表单引用
const rules = reactive({
embeddingId: [{ required: true, message: '请选择向量模型', trigger: 'change' }],
llmId: [{ required: true, message: '请选择大语言模型', trigger: 'change' }],
knowledgeId: [{ required: true, message: '请选择知识库', trigger: 'change' }],
buttonIds: [
{
required: true,
message: '请至少选择一个按钮',
trigger: 'change',
type: 'array',
},
],
prompt: [
{ required: true, message: '请输入提示词', trigger: 'blur' },
{ max: 500, message: '提示词长度不能超过500个字符', trigger: 'blur' },
],
});
// 选项数据
const options = reactive({
embeddingOptions: [],
llmOptions: [],
knowledgeOptions: [],
buttonOptions: [],
});
const saving = ref(false);
// 加载配置数据
const loadConfig = async () => {
try {
const res = await getConfigData();
if (res.success) {
const data = res.result;
// 填充表单
Object.keys(formState).forEach((key) => {
if (data.airagChatsettingConfig[key] !== undefined) {
if (key === 'buttonIds') {
formState[key] = data.airagChatsettingConfig[key] || [];
} else {
formState[key] = data.airagChatsettingConfig[key];
}
console.log('shuju' + formState[key]);
}
});
// 填充选项
options.embeddingOptions = data.embeddingOptions || [];
console.log('向量' + options.embeddingOptions);
options.llmOptions = data.llmOptions || [];
options.knowledgeOptions = data.knowledgeOptions || [];
options.buttonOptions = data.buttonOptions || [];
}
} catch (error) {
console.error('配置加载失败', error);
message.error('配置加载失败');
}
};
// 保存配置
const saveConfigData = async () => {
try {
await formRef.value?.validateFields();
} catch (error) {
message.error('请填写所有必填字段');
return;
}
const params = {
...formState,
id: formState.id || '',
buttonIds: formState.buttonIds.filter((id) => id && id !== 'undefined'),
};
if (formState.buttonIds.length > 5) {
message.error('最多只能选择五个按钮');
return;
}
saving.value = true;
try {
const res = await saveConfig(params);
console.log(res.success);
if (res.success) {
message.success('配置保存成功');
loadConfig(); // 重新加载最新数据
} else {
message.error('保存失败: ' + res.message);
}
} catch (error) {
console.error('保存失败', error);
message.error('保存失败');
} finally {
saving.value = false;
}
};
// 重置表单
const resetForm = () => {
formRef.value?.resetFields();
loadConfig();
};
// 初始化加载
onMounted(() => {
loadConfig();
});
</script>
<style lang="less" scoped>
.config-card { margin: 30px auto;
border: 1px solid #f0f0f0;
border-radius: 6px;
overflow: hidden;
/* 标题在左上角,放大,背景与表单一致 */
:deep(.ant-card-head) {
background-color: #fff;
border-bottom: 1px solid #f0f0f0;
text-align: left;
font-size: 18px;
font-weight: 400;
padding-left: 24px;
}
}
.config-form {
margin-left: 16%;
width: 60%;
margin-top: 2%;
}
/* 表单项增加更大间距 */
:deep(.ant-form-item) {
margin-bottom: 32px; /* 增加间距 */
}
/* Label 样式 */
:deep(.ant-form-item-label > label) {
font-weight: 500;
color: #333;
}
/* 输入框/选择框100%宽度 */
:deep(.ant-form-item-control input),
:deep(.ant-form-item-control textarea),
:deep(.ant-form-item-control .ant-select),
:deep(.ant-form-item-control .ant-select-selector) {
width: 100% !important;
}
/* 输入框高度适中 */
:deep(.ant-input),
:deep(.ant-select-selector),
:deep(.ant-input-textarea) {
min-height: 38px;
}
/* 提示文字 */
.hint {
font-size: 12px;
color: #999;
margin-top: 4px;
}
/* 按钮区域居中 */
.action-buttons {
margin-top: 24px;
margin-left: 10%;
text-align: center;
.ant-space {
justify-content: center;
}
.ant-btn {
min-width: 120px;
}
}
</style>