From 6baa98f16605e3ff0f3c7684e2ea97a8df300509 Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Wed, 6 Nov 2024 17:13:05 +0800 Subject: [PATCH] feat: support app-selector, model-selector and tool-selector as parameters --- api/core/entities/parameter_entities.py | 12 ++++++++++-- api/core/entities/provider_entities.py | 4 ++-- api/core/tools/entities/tool_entities.py | 20 ++++++++++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/api/core/entities/parameter_entities.py b/api/core/entities/parameter_entities.py index 74d052ad11..990e84867e 100644 --- a/api/core/entities/parameter_entities.py +++ b/api/core/entities/parameter_entities.py @@ -12,7 +12,8 @@ class CommonParameterType(Enum): SYSTEM_FILES = "system-files" BOOLEAN = "boolean" APP_SELECTOR = "app-selector" - MODEL_CONFIG = "model-config" + TOOL_SELECTOR = "tool-selector" + MODEL_SELECTOR = "model-selector" class AppSelectorScope(Enum): @@ -22,7 +23,7 @@ class AppSelectorScope(Enum): COMPLETION = "completion" -class ModelConfigScope(Enum): +class ModelSelectorScope(Enum): LLM = "llm" TEXT_EMBEDDING = "text-embedding" RERANK = "rerank" @@ -30,3 +31,10 @@ class ModelConfigScope(Enum): SPEECH2TEXT = "speech2text" MODERATION = "moderation" VISION = "vision" + + +class ToolSelectorScope(Enum): + ALL = "all" + CUSTOM = "custom" + BUILTIN = "builtin" + WORKFLOW = "workflow" diff --git a/api/core/entities/provider_entities.py b/api/core/entities/provider_entities.py index 596a841e74..76fcff149e 100644 --- a/api/core/entities/provider_entities.py +++ b/api/core/entities/provider_entities.py @@ -3,7 +3,7 @@ from typing import Optional, Union from pydantic import BaseModel, ConfigDict, Field -from core.entities.parameter_entities import AppSelectorScope, CommonParameterType, ModelConfigScope +from core.entities.parameter_entities import AppSelectorScope, CommonParameterType, ModelSelectorScope from core.model_runtime.entities.model_entities import ModelType from core.tools.entities.common_entities import I18nObject @@ -168,7 +168,7 @@ class ProviderConfig(BasicProviderConfig): value: str = Field(..., description="The value of the option") label: I18nObject = Field(..., description="The label of the option") - scope: AppSelectorScope | ModelConfigScope | None = None + scope: AppSelectorScope | ModelSelectorScope | None = None required: bool = False default: Optional[Union[int, str]] = None options: Optional[list[Option]] = None diff --git a/api/core/tools/entities/tool_entities.py b/api/core/tools/entities/tool_entities.py index 8b86ba7473..26c9caaa43 100644 --- a/api/core/tools/entities/tool_entities.py +++ b/api/core/tools/entities/tool_entities.py @@ -5,7 +5,12 @@ from typing import Any, Optional, Union from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_serializer, field_validator -from core.entities.parameter_entities import AppSelectorScope, CommonParameterType, ModelConfigScope +from core.entities.parameter_entities import ( + AppSelectorScope, + CommonParameterType, + ModelSelectorScope, + ToolSelectorScope, +) from core.entities.provider_entities import ProviderConfig from core.tools.entities.common_entities import I18nObject @@ -209,6 +214,9 @@ class ToolParameter(BaseModel): SECRET_INPUT = CommonParameterType.SECRET_INPUT.value FILE = CommonParameterType.FILE.value FILES = CommonParameterType.FILES.value + APP_SELECTOR = CommonParameterType.APP_SELECTOR.value + TOOL_SELECTOR = CommonParameterType.TOOL_SELECTOR.value + MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value # deprecated, should not use. SYSTEM_FILES = CommonParameterType.SYSTEM_FILES.value @@ -271,6 +279,14 @@ class ToolParameter(BaseModel): else: return value[0] return value + case ( + ToolParameter.ToolParameterType.TOOL_SELECTOR + | ToolParameter.ToolParameterType.MODEL_SELECTOR + | ToolParameter.ToolParameterType.APP_SELECTOR + ): + if not isinstance(value, dict): + raise ValueError("The selector must be a dictionary.") + return value case _: return str(value) @@ -287,7 +303,7 @@ class ToolParameter(BaseModel): human_description: Optional[I18nObject] = Field(default=None, description="The description presented to the user") placeholder: Optional[I18nObject] = Field(default=None, description="The placeholder presented to the user") type: ToolParameterType = Field(..., description="The type of the parameter") - scope: AppSelectorScope | ModelConfigScope | None = None + scope: AppSelectorScope | ModelSelectorScope | ToolSelectorScope | None = None form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm") llm_description: Optional[str] = None required: Optional[bool] = False