-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathModelConstructor.vue
More file actions
292 lines (268 loc) · 8.83 KB
/
ModelConstructor.vue
File metadata and controls
292 lines (268 loc) · 8.83 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
288
289
290
291
292
<template>
<el-form-item
:label="$t('views.model.modelForm.model_type.label')"
required
prop="model_type"
:rules="[{ required: true, message: $t('views.model.modelForm.model_type.requiredMessage') }]"
>
<el-select
v-model="formValue.model_type"
:placeholder="$t('views.model.modelForm.model_type.placeholder')"
@change="handleModelTypeChange"
>
<el-option
v-for="item in modelTypeList"
:key="item.value"
:label="item.text"
:value="item.value"
/>
</el-select>
</el-form-item>
<el-form-item
:label="$t('dynamicsForm.ModelConstructor.optionalModel')"
required
prop="provider_list"
:rules="[
{
required: true,
message: $t('dynamicsForm.ModelConstructor.modelPlaceholder'),
type: 'array',
},
]"
>
<div class="flex-between w-full">
<ModelSelect
multiple
v-model="selectedIds"
:placeholder="$t('dynamicsForm.ModelConstructor.modelPlaceholder')"
:options="groupedModelOptions"
@change="handleProviderListChange"
:model-type="formValue.model_type"
>
</ModelSelect>
</div>
</el-form-item>
<el-form-item
:label="$t('dynamicsForm.ModelConstructor.defaultModel')"
prop="default_value.model_id"
:required="formValue.required"
:rules="
formValue.required
? [
{
required: true,
message: $t('dynamicsForm.ModelConstructor.modelPlaceholder'),
},
]
: []
"
v-if="formValue.provider_list && formValue.provider_list.length > 0"
>
<div class="flex-between w-full">
<el-select
v-model="formValue.default_value"
value-key="model_id"
:placeholder="$t('dynamicsForm.ModelConstructor.modelPlaceholder')"
>
<el-option-group
v-for="(modelList, providerName) in selectedModelsOptions"
:key="providerName"
:label="relatedObject(providerOptions, providerName, 'provider')?.name"
>
<el-option
v-for="item in modelList"
:key="item.id"
:label="item.name"
:value="getProviderItem(item.id)"
>
<el-space :size="8">
<span
:innerHTML="relatedObject(providerOptions, providerName, 'provider')?.icon"
class="select-model-icon"
style="margin-top: -7px"
></span>
<span>{{ item.name }}</span>
</el-space>
</el-option>
</el-option-group>
<template #label="{ label, value }">
<el-space :size="8" v-if="value?.model_id">
<span
class="select-model-icon"
:innerHTML="
relatedObject(providerOptions, getModelInfo(value.model_id)?.provider, 'provider')
?.icon
"
>
</span>
<span>
<span>{{
relatedObject(providerOptions, getModelInfo(value.model_id)?.provider, 'provider')
?.name
}}</span>
<span>/</span>
<span>{{ label }}</span>
</span>
</el-space>
</template>
</el-select>
<div class="ml-8">
<el-button @click="openParamSetting" @refreshForm="handleParamRefresh">
<el-icon>
<Operation />
</el-icon>
</el-button>
</div>
</div>
</el-form-item>
<AIModeParamSettingDialog ref="AIModeParamSettingDialogRef" @refresh="handleParamRefresh" />
</template>
<script setup lang="ts">
import { computed, onMounted, inject, ref } from 'vue'
import { modelTypeList } from '@/views/model/component/data'
import AIModeParamSettingDialog from '@/views/application/component/AIModeParamSettingDialog.vue'
import { groupBy } from 'lodash'
import { providerList as providerOptions } from '../../items/model/provider-data'
import { relatedObject } from '@/utils/array'
const getSelectModelList = inject('getSelectModelList') as Function
const getModelParamsForm = inject('getModelParamsForm') as Function
const props = defineProps<{
modelValue: any
}>()
const emit = defineEmits(['update:modelValue'])
const formValue = computed({
set: (item: any) => {
emit('update:modelValue', item)
},
get: () => {
return props.modelValue
},
})
const selectedIds = computed({
get: () => (formValue.value.provider_list || []).map((p: any) => p.model_id),
set: (newIds: string[]) => {
const oldList = formValue.value.provider_list || []
const newList = newIds.map((id: string) => {
const existing = oldList.find((p: any) => p.model_id === id)
return existing || { model_id: id, model_params_setting: {} }
})
formValue.value.provider_list = newList
// find new model then get it default value
const oldIds = oldList.map((p: any) => p.model_id)
const addedIds = newIds.filter((id: string) => !oldIds.includes(id))
addedIds.forEach((id: string) => {
fetchDefaultParams(id)
})
},
})
const selectedModelsOptions = computed(() => {
const ids = (formValue.value.provider_list || []).map((p: any) => p.model_id)
const filtered = rawModelOptions.value.filter((m: any) => ids.includes(m.id))
return groupBy(filtered, 'provider')
})
function fetchDefaultParams(modelId: string) {
if (!getModelParamsForm) return
getModelParamsForm(modelId).then((res: any) => {
const formFields = res?.data || []
const defaults = (res?.data || [])
.map((item: any) => {
if (item.show_default_value === false) {
return { [item.field]: undefined }
} else {
return { [item.field]: item.default_value }
}
})
.reduce((x: any, y: any) => ({ ...x, ...y }), {})
// update to model_params_setting
const target = formValue.value.provider_list.find((p: any) => p.model_id === modelId)
if (target) {
target.model_params_setting = defaults
target.model_form_field = formFields
}
})
}
const AIModeParamSettingDialogRef = ref<InstanceType<typeof AIModeParamSettingDialog>>()
const openParamSetting = () => {
const dv = formValue.value.default_value
if (!dv?.model_id) return
AIModeParamSettingDialogRef.value?.open(dv.model_id, undefined, dv?.model_params_setting)
}
const handleParamRefresh = (paramData: any) => {
const dv = formValue.value.default_value
if (dv?.model_id) {
formValue.value.default_value = { ...dv, model_params_setting: paramData }
const target = formValue.value.provider_list.find((p: any) => p.model_id === dv.model_id)
if (target) {
target.model_params_setting = paramData
}
}
}
const rawModelOptions = ref<any[]>([])
const groupedModelOptions = ref<Record<string, any[]>>({})
const fetchModelByType = (type: string) => {
if (!type || !getSelectModelList) return
getSelectModelList({ model_type: type }).then((res: any) => {
rawModelOptions.value = res?.data || []
groupedModelOptions.value = groupBy(res?.data, 'provider')
})
}
const handleModelTypeChange = (val: string) => {
formValue.value.provider_list = []
formValue.value.default_value = {}
if (val) {
fetchModelByType(val)
} else {
rawModelOptions.value = []
groupedModelOptions.value = {}
}
}
const getModelInfo = (modelId: string) => {
return rawModelOptions.value.find((item: any) => item.id === modelId)
}
// default_value 赋值
const getProviderItem = (modelId: string) => {
const found = formValue.value.provider_list.find((p: any) => p.model_id === modelId)
if (found) {
const { model_form_field, ...rest } = found
return rest
}
return { model_id: modelId, model_params_setting: {} }
}
function handleProviderListChange() {
const ids = (formValue.value.provider_list || []).map((p: any) => p.model_id)
const currentId = formValue.value.default_value?.model_id
if (currentId && !ids.includes(currentId)) {
formValue.value.default_value = {}
}
}
const getData = () => {
const providerList = (formValue.value.provider_list || []).map((p: any) => {
const modelInfo = getModelInfo(p.model_id)
return {
model_id: p.model_id,
model_name: modelInfo?.name || '',
provider: modelInfo?.provider || '',
model_params_setting: p.model_params_setting || {},
model_form_field: p.model_form_field || [],
}
})
return {
input_type: 'Model',
model_type: formValue.value.model_type,
default_value: formValue.value.default_value,
attrs: {
provider_list: providerList,
},
}
}
const rander = (form_data: any) => {
formValue.value.model_type = form_data.model_type
formValue.value.provider_list = form_data.attrs?.provider_list || []
formValue.value.default_value = form_data.default_value || {}
if (form_data.model_type) {
fetchModelByType(form_data.model_type)
}
}
defineExpose({ getData, rander })
</script>
<style lang="scss" scoped></style>