RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: normalize model types in _factory_model_types and update calculate_model_type (#18685)
Sbaaoui Idrissbfe07974dc26
diff --git a/api/apps/services/provider_api_service.py b/api/apps/services/provider_api_service.pyindex 7020d99c84..d1afca95ba 100644--- a/api/apps/services/provider_api_service.py+++ b/api/apps/services/provider_api_service.py@@ -26,7 +26,7 @@ from api.db.joint_services.tenant_model_service import resolve_model_config, delfrom api.db.services.tenant_model_provider_service import TenantModelProviderServicefrom api.db.services.tenant_model_instance_service import TenantModelInstanceServicefrom api.db.services.tenant_model_service import TenantModelService-from api.utils.model_utils import get_model_type_human, calculate_model_type+from api.utils import model_utilsfrom rag.llm import ChatModel, CvModel, EmbeddingModel, ModelMeta, OcrModel, RerankModel, Seq2txtModel, TTSModel@@ -39,9 +39,7 @@ def _to_int(v, default=500):def _factory_model_types(llm: dict) -> list[str]:model_type = llm.get("model_type")- if isinstance(model_type, list):- return model_type- return [model_type] if model_type else []+ return model_utils.normalize_model_types(model_type) if model_type else []def _normalize_provider_base_url(provider_name: str, base_url: str | None):@@ -422,7 +420,7 @@ async def update_provider_instance(# Update existing modelupdate_dict = {}if isinstance(model.get("model_type"), (str, list)):- target_model_type = calculate_model_type(model["model_type"])+ target_model_type = model_utils.calculate_model_type(model["model_type"])if target_model_type != existing_model_names[model_name].model_type:update_dict["model_type"] = target_model_typemerged_extra = json.loads(existing_model_names[model_name].extra) if existing_model_names[model_name].extra else {}@@ -447,7 +445,7 @@ async def update_provider_instance(if llm_name in existing_model_names:# Update existingupdate_dict = {}- target_model_type = calculate_model_type(_factory_model_types(llm))+ target_model_type = model_utils.calculate_model_type(_factory_model_types(llm))if target_model_type != existing_model_names[llm_name].model_type:update_dict["model_type"] = target_model_typedb_extra = json.loads(existing_model_names[llm_name].extra) if existing_model_names[llm_name].extra else {}@@ -1061,7 +1059,7 @@ def _reconcile_nvidia_instance_models(provider_obj, instance_obj, remote_models:for model in normalized:model_name = model["name"]model_types = model.get("model_types") or [LLMType.CHAT.value]- model_type = calculate_model_type(model_types)+ model_type = model_utils.calculate_model_type(model_types)if existing := existing_by_name.pop(model_name, None):extra = json.loads(existing.extra or "{}")_set_discovered_model_metadata(extra, model)@@ -1158,7 +1156,7 @@ async def list_instance_models(tenant_id: str, provider_id_or_name: str, instancmodel_list.append({"name": model.model_name,- "model_type": get_model_type_human(model.model_type),+ "model_type": model_utils.get_model_type_human(model.model_type),"max_tokens": model_extra.get("max_tokens", 8192) if model.extra else 8192,"status": model.status,"verify": model_extra.get("verify", ModelVerifyStatusEnum.UNKNOWN.value),@@ -1196,7 +1194,7 @@ def update_instance_models(tenant_id: str, provider_id_or_name: str, instance_idif not_exist_models:return False, f"Models {not_exist_models} not found for provider '{provider_id_or_name}' and instance '{instance_id_or_name}'"- target_model_type_bin = calculate_model_type(model_types)+ target_model_type_bin = model_utils.calculate_model_type(model_types)to_update = [model_obj.id for model_obj in model_objs if model_obj.model_type != target_model_type_bin and model_obj.model_name in model_names]if to_update:TenantModelService.batch_update_model_type(to_update, target_model_type_bin)@@ -1229,7 +1227,7 @@ def add_model_to_instance(tenant_id: str, provider_id_or_name: str, instance_id_if isinstance(model_type, str):model_type = [model_type]- model_type_bin = calculate_model_type(model_type)+ model_type_bin = model_utils.calculate_model_type(model_type)extra_fields = {"max_tokens": max_tokens}target_model = [llm for llm in llms if llm["llm_name"] == model_name]if target_model:@@ -1298,7 +1296,7 @@ def update_model(tenant_id: str, provider_id_or_name: str, instance_id_or_name:db_extra.update(**new_extra)to_update.update({"extra": json.dumps(db_extra)})if "model_type" in update_dict:- target_model_type = calculate_model_type(update_dict["model_type"])+ target_model_type = model_utils.calculate_model_type(update_dict["model_type"])if target_model_type != model_obj.model_type:to_update.update({"model_type": target_model_type})diff --git a/api/utils/model_utils.py b/api/utils/model_utils.pyindex f27a08044f..918642aaa0 100644--- a/api/utils/model_utils.py+++ b/api/utils/model_utils.py@@ -28,13 +28,25 @@ _LEGACY_MODEL_TYPE_ALIASES = {}-def calculate_model_type(model_type_name_list: List[str] | str) -> int:- model_type = 0+def normalize_model_types(model_type_name_list: List[str] | str) -> List[str]:+ """Return model type names using the canonical API/runtime identifiers."""if isinstance(model_type_name_list, str):model_type_name_list = [model_type_name_list]+ elif not isinstance(model_type_name_list, list):+ return []++ normalized = []+ for model_type in model_type_name_list:+ canonical = _LEGACY_MODEL_TYPE_ALIASES.get(model_type, model_type)+ if canonical not in normalized:+ normalized.append(canonical)+ return normalized+++def calculate_model_type(model_type_name_list: List[str] | str) -> int:+ model_type = 0type_value_map = {mt.name.lower(): mt.value for mt in ModelTypeBinary}- for mt in model_type_name_list:- normalized_mt = _LEGACY_MODEL_TYPE_ALIASES.get(mt, mt)- if normalized_mt in type_value_map:- model_type |= type_value_map[normalized_mt]+ for mt in normalize_model_types(model_type_name_list):+ if mt in type_value_map:+ model_type |= type_value_map[mt]return model_typediff --git a/test/unit_test/api/utils/test_model_utils.py b/test/unit_test/api/utils/test_model_utils.pynew file mode 100644index 0000000000..3e1d88c7fe--- /dev/null+++ b/test/unit_test/api/utils/test_model_utils.py@@ -0,0 +1,19 @@+from api.utils.model_utils import calculate_model_type, normalize_model_types+from common.constants import ModelTypeBinary+++def test_normalize_model_types_collapses_legacy_capability_aliases():+ assert normalize_model_types(["speech2text", "asr"]) == ["asr"]+ assert normalize_model_types(["chat", "speech2text", "image2text"]) == [+ "chat",+ "asr",+ "vision",+ ]+++def test_normalize_model_types_ignores_invalid_metadata():+ assert normalize_model_types(1) == [] # type: ignore[arg-type]+++def test_calculate_model_type_accepts_legacy_aliases():+ assert calculate_model_type(["speech2text", "image2text"]) == (ModelTypeBinary.ASR.value | ModelTypeBinary.VISION.value)diff --git a/web/src/pages/user-setting/setting-model/provider-schema/field-config/utils.ts b/web/src/pages/user-setting/setting-model/provider-schema/field-config/utils.tsindex 2f50b5912a..ca06265d56 100644--- a/web/src/pages/user-setting/setting-model/provider-schema/field-config/utils.ts+++ b/web/src/pages/user-setting/setting-model/provider-schema/field-config/utils.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import { IModelInfo } from '@/interfaces/request/llm';+import { IModelInfo } from "@/interfaces/request/llm";/*** Capitalize the first letter of a string@@ -24,9 +24,9 @@ export function capitalize(s: string): string {}/**- * When model_type contains chat and vision=true, automatically add image2text+ * When model_type contains chat and vision=true, automatically add vision.*/-export function applyChatToImage2Text(+export function applyChatToVision(modelType: string[] | string | undefined,vision?: boolean,): string[] {@@ -35,8 +35,8 @@ export function applyChatToImage2Text(: modelType? [modelType]: [];- if (arr.includes('chat') && vision) {- return [...arr, 'image2text'];+ if (arr.includes("chat") && vision) {+ return [...arr, "vision"];}return arr;}@@ -56,16 +56,16 @@ export function parseApiKeyAsObject(raw: unknown,): Record<string, any> | undefined {let obj: any = raw;- if (typeof raw === 'string') {+ if (typeof raw === "string") {const trimmed = raw.trim();- if (!trimmed.startsWith('{')) return undefined;+ if (!trimmed.startsWith("{")) return undefined;try {obj = JSON.parse(trimmed);} catch {return undefined;}}- return obj && typeof obj === 'object' ? obj : undefined;+ return obj && typeof obj === "object" ? obj : undefined;}/**diff --git a/web/src/pages/user-setting/setting-model/provider-schema/hooks/use-provider-fields.tsx b/web/src/pages/user-setting/setting-model/provider-schema/hooks/use-provider-fields.tsxindex 299103a5fd..49b2188db9 100644--- a/web/src/pages/user-setting/setting-model/provider-schema/hooks/use-provider-fields.tsx+++ b/web/src/pages/user-setting/setting-model/provider-schema/hooks/use-provider-fields.tsx@@ -14,15 +14,15 @@* limitations under the License.*/-import { FormFieldConfig, FormFieldType } from '@/components/dynamic-form';-import { Input } from '@/components/ui/input';-import { InputSelect } from '@/components/ui/input-select';-import { useTranslate } from '@/hooks/common-hooks';-import { useMemo } from 'react';-import { ControllerRenderProps, FieldValues } from 'react-hook-form';-import { LIST_MODEL_FIELD_NAMES, LIST_MODEL_PROVIDERS } from '../constants';-import { FACTORIES_WITH_BASE_URL, getProviderConfig } from '../field-config';-import type { FieldConfig, SelectOption } from '../types';+import { FormFieldConfig, FormFieldType } from "@/components/dynamic-form";+import { Input } from "@/components/ui/input";+import { InputSelect } from "@/components/ui/input-select";+import { useTranslate } from "@/hooks/common-hooks";+import { useMemo } from "react";+import { ControllerRenderProps, FieldValues } from "react-hook-form";+import { LIST_MODEL_FIELD_NAMES, LIST_MODEL_PROVIDERS } from "../constants";+import { FACTORIES_WITH_BASE_URL, getProviderConfig } from "../field-config";+import type { FieldConfig, SelectOption } from "../types";interface UseProviderFieldsParams {llmFactory: string;@@ -45,15 +45,15 @@ const resolveText = (t: (key: string) => string,): string | undefined => {if (!val) return undefined;- const key = typeof val === 'function' ? val(factory) : val;+ const key = typeof val === "function" ? val(factory) : val;return t(key);};/** Set value by nested path (supports paths like 'model_info.model_type'). */-const FORBIDDEN_KEYS = new Set(['__proto__', 'constructor', 'prototype']);+const FORBIDDEN_KEYS = new Set(["__proto__", "constructor", "prototype"]);const setNestedValue = (obj: any, path: string, value: any) => {- const keys = path.split('.');+ const keys = path.split(".");let current = obj;for (let i = 0; i < keys.length - 1; i++) {const key = keys[i];@@ -95,7 +95,7 @@ export const useProviderFields = ({baseUrlOptions,hideWhenInstanceExists,}: UseProviderFieldsParams) => {- const { t } = useTranslate('setting');+ const { t } = useTranslate("setting");const config = useMemo(() => getProviderConfig(llmFactory), [llmFactory]);@@ -110,40 +110,44 @@ export const useProviderFields = ({// Resolve the shouldRender string token to an actual predicate.const resolveShouldRender = useMemo(() => {- return (sr: FieldConfig['shouldRender']) => {+ return (sr: FieldConfig["shouldRender"]) => {if (!sr) return undefined;- if (typeof sr === 'function') return sr;+ if (typeof sr === "function") return sr;switch (sr) {- case 'hideWhenInstanceExists':+ case "hideWhenInstanceExists":return hideWhenInstanceExists;- case 'modelTypeIncludesChat':+ case "modelTypeIncludesChat":return (values: any) => {const mt = values?.model_type;- if (Array.isArray(mt)) return mt.includes('chat');- return mt === 'chat';+ if (Array.isArray(mt)) return mt.includes("chat");+ return mt === "chat";};- case 'modelTypeSupportsToolCall':+ case "modelTypeSupportsToolCall":return (values: any) => {const mt = values?.model_type;if (Array.isArray(mt)) {- return mt.includes('chat') || mt.includes('image2text');+ return (+ mt.includes("chat") ||+ mt.includes("vision") ||+ mt.includes("image2text")+ );}- return mt === 'chat' || mt === 'image2text';+ return mt === "chat" || mt === "vision" || mt === "image2text";};- case 'modelTypeIncludesTtsAndNotExists':+ case "modelTypeIncludesTtsAndNotExists":return (values: any) => {if (!hideWhenInstanceExists(values)) return false;const mt = values?.model_type;- if (Array.isArray(mt)) return mt.includes('tts');- return mt === 'tts';+ if (Array.isArray(mt)) return mt.includes("tts");+ return mt === "tts";};- case 'showBaseUrl':+ case "showBaseUrl":return () =>FACTORIES_WITH_BASE_URL.some((x) => x === llmFactory) ||- llmFactory?.toLowerCase() === 'Anthropic'.toLowerCase();- case 'showGroupId':- return () => llmFactory?.toLowerCase() === 'Minimax'.toLowerCase();+ llmFactory?.toLowerCase() === "Anthropic".toLowerCase();+ case "showGroupId":+ return () => llmFactory?.toLowerCase() === "Minimax".toLowerCase();default:return undefined;}@@ -158,7 +162,7 @@ export const useProviderFields = ({const baseUrlRegionMaps = useMemo(() => {const maps: Record<string, Map<string, string>> = {};config.fields.forEach((field) => {- if (field.type !== 'inputSelect') return;+ if (field.type !== "inputSelect") return;const options =field.options && field.options.length > 0? field.options@@ -197,7 +201,7 @@ export const useProviderFields = ({: undefined,}: undefined;- const baseField: Omit<FormFieldConfig, 'type'> = {+ const baseField: Omit<FormFieldConfig, "type"> = {name: field.name,label: t(field.label),required: field.required,@@ -217,17 +221,17 @@ export const useProviderFields = ({// are rendered as disabled.disabled: !!viewMode && !LIST_MODEL_FIELD_NAMES.has(field.name),dependencies:- field.shouldRender === 'modelTypeIncludesChat' ||- field.shouldRender === 'modelTypeSupportsToolCall' ||- field.shouldRender === 'modelTypeIncludesTtsAndNotExists'- ? ['model_type']- : ['model_type', 'instance_name'].includes(field.name)- ? ['model_type', 'instance_name']+ field.shouldRender === "modelTypeIncludesChat" ||+ field.shouldRender === "modelTypeSupportsToolCall" ||+ field.shouldRender === "modelTypeIncludesTtsAndNotExists"+ ? ["model_type"]+ : ["model_type", "instance_name"].includes(field.name)+ ? ["model_type", "instance_name"]: undefined,};// inputSelect type: use the InputSelect component, options come from baseUrlOptions- if (field.type === 'inputSelect') {+ if (field.type === "inputSelect") {const inputSelectOptions: SelectOption[] =field.options && field.options.length > 0? field.options@@ -240,7 +244,7 @@ export const useProviderFields = ({return inputSelectOptions.length > 0 ? (<InputSelect{...fieldProps}- value={(fieldProps.value ?? '') as string}+ value={(fieldProps.value ?? "") as string}onChange={(value) => fieldProps.onChange(value)}options={inputSelectOptions as any}placeholder={placeholderText}@@ -287,11 +291,11 @@ export const useProviderFields = ({// original key in the URL object is 'default' (e.g.// `availableProviders.url.default`). If no such option exists,// leave the field empty so the user picks one explicitly.- if (f.type === 'inputSelect') {+ if (f.type === "inputSelect") {const urlMap = baseUrlRegionMaps[f.name];if (urlMap) {for (const [url, regionKey] of urlMap.entries()) {- if (regionKey === 'default') {+ if (regionKey === "default") {setNestedValue(result, f.name, url);return;}@@ -300,9 +304,9 @@ export const useProviderFields = ({}});// For configurations without a default model_type, assign an empty array (multi-select field)- const mtField = config.fields.find((f) => f.name === 'model_type');+ const mtField = config.fields.find((f) => f.name === "model_type");if (mtField && mtField.defaultValue === undefined) {- setNestedValue(result, 'model_type', []);+ setNestedValue(result, "model_type", []);}return result;}, [editMode, viewMode, initialValues, config.fields, baseUrlRegionMaps]);