refactor(model): streamline model filtering and SSRF validation
- Simplified model filtering logic by removing the deduplication function and directly filtering models based on type. - Updated SSRF validation to preserve backend-managed fields when parameters are not provided by the frontend. - Clarified comments and improved code readability in the ModelEditorDialog and ModelSettings components.
This commit is contained in:
@@ -230,8 +230,8 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Chat/VLLM: supports vision toggle -->
|
<!-- Chat: supports vision toggle (VLLM models are inherently multimodal) -->
|
||||||
<div v-if="modelType === 'chat' || modelType === 'vllm'" class="form-item">
|
<div v-if="modelType === 'chat'" class="form-item">
|
||||||
<label class="form-label">{{ $t('model.editor.supportsVisionLabel') }}</label>
|
<label class="form-label">{{ $t('model.editor.supportsVisionLabel') }}</label>
|
||||||
<div style="display: flex; align-items: center; gap: 8px;">
|
<div style="display: flex; align-items: center; gap: 8px;">
|
||||||
<t-switch v-model="formData.supportsVision" />
|
<t-switch v-model="formData.supportsVision" />
|
||||||
|
|||||||
@@ -249,37 +249,29 @@ const loading = ref(true)
|
|||||||
// 模型列表数据
|
// 模型列表数据
|
||||||
const allModels = ref<ModelConfig[]>([])
|
const allModels = ref<ModelConfig[]>([])
|
||||||
|
|
||||||
// 根据类型过滤并去重模型
|
// 根据类型过滤模型
|
||||||
const chatModels = computed(() =>
|
const chatModels = computed(() =>
|
||||||
deduplicateModels(
|
allModels.value
|
||||||
allModels.value
|
.filter(m => m.type === 'KnowledgeQA')
|
||||||
.filter(m => m.type === 'KnowledgeQA')
|
.map(convertToLegacyFormat)
|
||||||
.map(convertToLegacyFormat)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const embeddingModels = computed(() =>
|
const embeddingModels = computed(() =>
|
||||||
deduplicateModels(
|
allModels.value
|
||||||
allModels.value
|
.filter(m => m.type === 'Embedding')
|
||||||
.filter(m => m.type === 'Embedding')
|
.map(convertToLegacyFormat)
|
||||||
.map(convertToLegacyFormat)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const rerankModels = computed(() =>
|
const rerankModels = computed(() =>
|
||||||
deduplicateModels(
|
allModels.value
|
||||||
allModels.value
|
.filter(m => m.type === 'Rerank')
|
||||||
.filter(m => m.type === 'Rerank')
|
.map(convertToLegacyFormat)
|
||||||
.map(convertToLegacyFormat)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const vllmModels = computed(() =>
|
const vllmModels = computed(() =>
|
||||||
deduplicateModels(
|
allModels.value
|
||||||
allModels.value
|
.filter(m => m.type === 'VLLM')
|
||||||
.filter(m => m.type === 'VLLM')
|
.map(convertToLegacyFormat)
|
||||||
.map(convertToLegacyFormat)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// 将后端模型格式转换为旧的前端格式
|
// 将后端模型格式转换为旧的前端格式
|
||||||
@@ -298,30 +290,6 @@ function convertToLegacyFormat(model: ModelConfig) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 去重函数:比较除id外的所有字段,相同的只保留第一个
|
|
||||||
function deduplicateModels(models: any[]) {
|
|
||||||
const seen = new Map<string, any>()
|
|
||||||
|
|
||||||
return models.filter(model => {
|
|
||||||
// 创建一个不包含id的签名用于比较
|
|
||||||
const signature = JSON.stringify({
|
|
||||||
name: model.name,
|
|
||||||
source: model.source,
|
|
||||||
modelName: model.modelName,
|
|
||||||
baseUrl: model.baseUrl,
|
|
||||||
apiKey: model.apiKey,
|
|
||||||
dimension: model.dimension
|
|
||||||
})
|
|
||||||
|
|
||||||
if (seen.has(signature)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
seen.set(signature, model)
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 加载模型列表
|
// 加载模型列表
|
||||||
const loadModels = async () => {
|
const loadModels = async () => {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
@@ -410,7 +378,9 @@ const handleModelSave = async (modelData: any) => {
|
|||||||
truncate_prompt_tokens: 0
|
truncate_prompt_tokens: 0
|
||||||
}
|
}
|
||||||
} : {}),
|
} : {}),
|
||||||
...((currentModelType.value === 'chat' || currentModelType.value === 'vllm') ? {
|
...(currentModelType.value === 'vllm' ? {
|
||||||
|
supports_vision: true
|
||||||
|
} : currentModelType.value === 'chat' ? {
|
||||||
supports_vision: modelData.supportsVision ?? false
|
supports_vision: modelData.supportsVision ?? false
|
||||||
} : {})
|
} : {})
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-10
@@ -301,18 +301,22 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
|
|||||||
model.Name = req.Name
|
model.Name = req.Name
|
||||||
}
|
}
|
||||||
model.Description = req.Description
|
model.Description = req.Description
|
||||||
// Check if any Parameters field is set (can't use struct comparison due to map field)
|
|
||||||
if req.Parameters.BaseURL != "" || req.Parameters.APIKey != "" || req.Parameters.Provider != "" {
|
// SSRF validation for updated model BaseURL
|
||||||
// SSRF validation for updated model BaseURL
|
if req.Parameters.BaseURL != "" {
|
||||||
if req.Parameters.BaseURL != "" {
|
if err := secutils.ValidateURLForSSRF(req.Parameters.BaseURL); err != nil {
|
||||||
if err := secutils.ValidateURLForSSRF(req.Parameters.BaseURL); err != nil {
|
logger.Warnf(ctx, "SSRF validation failed for model BaseURL: %v", err)
|
||||||
logger.Warnf(ctx, "SSRF validation failed for model BaseURL: %v", err)
|
c.Error(errors.NewBadRequestError(fmt.Sprintf("Base URL 未通过安全校验: %v", err)))
|
||||||
c.Error(errors.NewBadRequestError(fmt.Sprintf("Base URL 未通过安全校验: %v", err)))
|
return
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
model.Parameters = req.Parameters
|
|
||||||
}
|
}
|
||||||
|
// Preserve backend-managed fields not sent by frontend
|
||||||
|
req.Parameters.ParameterSize = model.Parameters.ParameterSize
|
||||||
|
if req.Parameters.ExtraConfig == nil {
|
||||||
|
req.Parameters.ExtraConfig = model.Parameters.ExtraConfig
|
||||||
|
}
|
||||||
|
model.Parameters = req.Parameters
|
||||||
|
|
||||||
model.Source = req.Source
|
model.Source = req.Source
|
||||||
model.Type = req.Type
|
model.Type = req.Type
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user