Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: #18744 The model order defined in position.yaml in the Model Plugin is not taking effect. (#18756)

bravomark9bcc8041e9d4
diff --git a/api/core/entities/provider_configuration.py b/api/core/entities/provider_configuration.py
index b3affc91a69..86887c9b4a4 100644
--- a/api/core/entities/provider_configuration.py
+++ b/api/core/entities/provider_configuration.py
@@ -798,7 +798,25 @@ class ProviderConfiguration(BaseModel):
provider_models = [m for m in provider_models if m.status == ModelStatus.ACTIVE]
# resort provider_models
- return sorted(provider_models, key=lambda x: x.model_type.value)
+ # Optimize sorting logic: first sort by provider.position order, then by model_type.value
+ # Get the position list for model types (retrieve only once for better performance)
+ model_type_positions = {}
+ if hasattr(self.provider, "position") and self.provider.position:
+ model_type_positions = self.provider.position
+
+ def get_sort_key(model: ModelWithProviderEntity):
+ # Get the position list for the current model type
+ positions = model_type_positions.get(model.model_type.value, [])
+
+ # If the model name is in the position list, use its index for sorting
+ # Otherwise use a large value (list length) to place undefined models at the end
+ position_index = positions.index(model.model) if model.model in positions else len(positions)
+
+ # Return composite sort key: (model_type value, model position index)
+ return (model.model_type.value, position_index)
+
+ # Sort using the composite sort key
+ return sorted(provider_models, key=get_sort_key)
def _get_system_provider_models(
self,
diff --git a/api/core/model_runtime/entities/provider_entities.py b/api/core/model_runtime/entities/provider_entities.py
index 85321bed947..d0f9ee13e5b 100644
--- a/api/core/model_runtime/entities/provider_entities.py
+++ b/api/core/model_runtime/entities/provider_entities.py
@@ -134,6 +134,9 @@ class ProviderEntity(BaseModel):
# pydantic configs
model_config = ConfigDict(protected_namespaces=())
+ # position from plugin _position.yaml
+ position: Optional[dict[str, list[str]]] = {}
+
@field_validator("models", mode="before")
@classmethod
def validate_models(cls, v):