dify/api/core/tools/entities/tool_entities.py

371 lines
11 KiB
Python
Raw Normal View History

2024-09-10 18:13:33 +08:00
import base64
from enum import Enum
2024-09-20 23:48:48 +08:00
from typing import Any, Optional, Union
2024-09-20 23:48:48 +08:00
from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_serializer, field_validator
2024-02-01 18:11:57 +08:00
2024-08-30 14:23:14 +08:00
from core.entities.parameter_entities import AppSelectorScope, CommonParameterType, ModelConfigScope
2024-09-20 23:48:48 +08:00
from core.entities.provider_entities import ProviderConfig
from core.tools.entities.common_entities import I18nObject
class ToolLabelEnum(Enum):
SEARCH = "search"
IMAGE = "image"
VIDEOS = "videos"
WEATHER = "weather"
FINANCE = "finance"
DESIGN = "design"
TRAVEL = "travel"
SOCIAL = "social"
NEWS = "news"
MEDICAL = "medical"
PRODUCTIVITY = "productivity"
EDUCATION = "education"
BUSINESS = "business"
ENTERTAINMENT = "entertainment"
UTILITIES = "utilities"
OTHER = "other"
2024-08-30 18:11:38 +08:00
class ToolProviderType(str, Enum):
"""
Enum class for tool provider
"""
2024-09-20 02:25:14 +08:00
PLUGIN = "plugin"
2024-05-27 22:01:11 +08:00
BUILT_IN = "builtin"
WORKFLOW = "workflow"
API = "api"
APP = "app"
DATASET_RETRIEVAL = "dataset-retrieval"
@classmethod
def value_of(cls, value: str) -> "ToolProviderType":
"""
Get value of given mode.
:param value: mode value
:return: mode
"""
for mode in cls:
if mode.value == value:
return mode
raise ValueError(f"invalid mode value {value}")
class ApiProviderSchemaType(Enum):
"""
Enum class for api provider schema type.
"""
OPENAPI = "openapi"
SWAGGER = "swagger"
OPENAI_PLUGIN = "openai_plugin"
OPENAI_ACTIONS = "openai_actions"
@classmethod
def value_of(cls, value: str) -> "ApiProviderSchemaType":
"""
Get value of given mode.
:param value: mode value
:return: mode
"""
for mode in cls:
if mode.value == value:
return mode
raise ValueError(f"invalid mode value {value}")
class ApiProviderAuthType(Enum):
"""
Enum class for api provider auth type.
"""
NONE = "none"
API_KEY = "api_key"
@classmethod
def value_of(cls, value: str) -> "ApiProviderAuthType":
"""
Get value of given mode.
:param value: mode value
:return: mode
"""
for mode in cls:
if mode.value == value:
return mode
raise ValueError(f"invalid mode value {value}")
class ToolInvokeMessage(BaseModel):
2024-07-29 18:57:34 +08:00
class TextMessage(BaseModel):
text: str
class JsonMessage(BaseModel):
json_object: dict
2024-08-29 13:42:31 +08:00
class BlobMessage(BaseModel):
blob: bytes
2024-09-10 18:13:33 +08:00
class VariableMessage(BaseModel):
variable_name: str = Field(..., description="The name of the variable")
variable_value: str = Field(..., description="The value of the variable")
stream: bool = Field(default=False, description="Whether the variable is streamed")
@field_validator("variable_value", mode="before")
2024-09-19 18:02:24 +08:00
@classmethod
def transform_variable_value(cls, value, values) -> Any:
2024-09-10 18:13:33 +08:00
"""
Only basic types and lists are allowed.
"""
if not isinstance(value, dict | list | str | int | float | bool):
raise ValueError("Only basic types and lists are allowed.")
2024-09-20 23:48:48 +08:00
2024-09-10 18:13:33 +08:00
# if stream is true, the value must be a string
2024-09-20 23:48:48 +08:00
if values.get("stream"):
2024-09-10 18:13:33 +08:00
if not isinstance(value, str):
raise ValueError("When 'stream' is True, 'variable_value' must be a string.")
return value
2024-09-20 23:48:48 +08:00
2024-09-14 01:26:22 +08:00
@field_validator("variable_name", mode="before")
2024-09-19 18:02:24 +08:00
@classmethod
def transform_variable_name(cls, value) -> str:
2024-09-14 01:26:22 +08:00
"""
The variable name must be a string.
"""
2024-09-14 02:47:01 +08:00
if value in {"json", "text", "files"}:
2024-09-14 01:26:22 +08:00
raise ValueError(f"The variable name '{value}' is reserved.")
return value
2024-09-10 18:13:33 +08:00
class MessageType(Enum):
TEXT = "text"
IMAGE = "image"
LINK = "link"
BLOB = "blob"
JSON = "json"
IMAGE_LINK = "image_link"
2024-05-27 22:01:11 +08:00
FILE_VAR = "file_var"
2024-09-10 18:13:33 +08:00
VARIABLE = "variable"
type: MessageType = MessageType.TEXT
"""
plain text, image url or link url
"""
2024-09-10 18:13:33 +08:00
message: JsonMessage | TextMessage | BlobMessage | VariableMessage | None
meta: dict[str, Any] | None = None
save_as: str = ""
2024-09-20 23:48:48 +08:00
@field_validator("message", mode="before")
2024-09-10 18:13:33 +08:00
@classmethod
def decode_blob_message(cls, v):
2024-09-20 23:48:48 +08:00
if isinstance(v, dict) and "blob" in v:
2024-09-10 18:13:33 +08:00
try:
2024-09-20 23:48:48 +08:00
v["blob"] = base64.b64decode(v["blob"])
2024-09-10 18:13:33 +08:00
except Exception:
pass
return v
2024-09-20 23:48:48 +08:00
@field_serializer("message")
2024-09-10 18:13:33 +08:00
def serialize_message(self, v):
if isinstance(v, self.BlobMessage):
2024-09-20 23:48:48 +08:00
return {"blob": base64.b64encode(v.blob).decode("utf-8")}
2024-09-10 18:13:33 +08:00
return v
class ToolInvokeMessageBinary(BaseModel):
mimetype: str = Field(..., description="The mimetype of the binary")
url: str = Field(..., description="The url of the binary")
save_as: str = ""
2024-05-27 22:01:11 +08:00
file_var: Optional[dict[str, Any]] = None
2024-01-31 11:58:07 +08:00
class ToolParameterOption(BaseModel):
value: str = Field(..., description="The value of the option")
label: I18nObject = Field(..., description="The label of the option")
@field_validator("value", mode="before")
@classmethod
def transform_id_to_str(cls, value) -> str:
if not isinstance(value, str):
return str(value)
else:
return value
2024-01-31 11:58:07 +08:00
class ToolParameter(BaseModel):
class ToolParameterType(str, Enum):
2024-08-30 14:23:14 +08:00
STRING = CommonParameterType.STRING.value
NUMBER = CommonParameterType.NUMBER.value
BOOLEAN = CommonParameterType.BOOLEAN.value
SELECT = CommonParameterType.SELECT.value
SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
FILE = CommonParameterType.FILE.value
class ToolParameterForm(Enum):
SCHEMA = "schema" # should be set while adding tool
FORM = "form" # should be set before invoking tool
LLM = "llm" # will be set by LLM
name: str = Field(..., description="The name of the parameter")
label: I18nObject = Field(..., description="The label presented to the user")
2024-08-30 14:23:14 +08:00
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")
2024-08-30 14:23:14 +08:00
scope: AppSelectorScope | ModelConfigScope | None = None
form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
llm_description: Optional[str] = None
required: Optional[bool] = False
default: Optional[Union[float, int, str]] = None
min: Optional[Union[float, int]] = None
max: Optional[Union[float, int]] = None
2024-08-30 14:23:14 +08:00
options: list[ToolParameterOption] = Field(default_factory=list)
@classmethod
def get_simple_instance(
cls,
name: str,
llm_description: str,
type: ToolParameterType,
required: bool,
options: Optional[list[str]] = None,
) -> "ToolParameter":
"""
get a simple tool parameter
:param name: the name of the parameter
:param llm_description: the description presented to the LLM
:param type: the type of the parameter
:param required: if the parameter is required
:param options: the options of the parameter
"""
2024-01-31 11:58:07 +08:00
# convert options to ToolParameterOption
if options:
2024-09-14 02:47:01 +08:00
option_objs = [
ToolParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) for option in options
]
2024-07-29 16:40:04 +08:00
else:
2024-08-30 18:11:38 +08:00
option_objs = []
return cls(
name=name,
2024-09-20 23:48:48 +08:00
label=I18nObject(en_US="", zh_Hans=""),
2024-07-29 16:40:04 +08:00
placeholder=None,
2024-09-20 23:48:48 +08:00
human_description=I18nObject(en_US="", zh_Hans=""),
type=type,
form=cls.ToolParameterForm.LLM,
llm_description=llm_description,
required=required,
2024-07-29 16:40:04 +08:00
options=option_objs,
)
class ToolProviderIdentity(BaseModel):
author: str = Field(..., description="The author of the tool")
name: str = Field(..., description="The name of the tool")
description: I18nObject = Field(..., description="The description of the tool")
icon: str = Field(..., description="The icon of the tool")
label: I18nObject = Field(..., description="The label of the tool")
tags: Optional[list[ToolLabelEnum]] = Field(
default=[],
description="The tags of the tool",
)
2024-09-23 13:09:46 +08:00
class ToolIdentity(BaseModel):
author: str = Field(..., description="The author of the tool")
name: str = Field(..., description="The name of the tool")
label: I18nObject = Field(..., description="The label of the tool")
provider: str = Field(..., description="The provider of the tool")
icon: Optional[str] = None
2024-09-20 23:48:48 +08:00
class ToolDescription(BaseModel):
human: I18nObject = Field(..., description="The description presented to the user")
llm: str = Field(..., description="The description presented to the LLM")
2024-09-23 13:09:46 +08:00
class ToolEntity(BaseModel):
identity: ToolIdentity
parameters: list[ToolParameter] = Field(default_factory=list)
description: Optional[ToolDescription] = None
# pydantic configs
model_config = ConfigDict(protected_namespaces=())
@field_validator("parameters", mode="before")
@classmethod
def set_parameters(cls, v, validation_info: ValidationInfo) -> list[ToolParameter]:
return v or []
class ToolProviderEntity(BaseModel):
identity: ToolProviderIdentity
credentials_schema: dict[str, ProviderConfig] = Field(default_factory=dict)
tools: list[ToolEntity] = Field(default_factory=list)
2024-05-27 22:01:11 +08:00
class WorkflowToolParameterConfiguration(BaseModel):
"""
Workflow tool configuration
"""
2024-05-27 22:01:11 +08:00
name: str = Field(..., description="The name of the parameter")
description: str = Field(..., description="The description of the parameter")
form: ToolParameter.ToolParameterForm = Field(..., description="The form of the parameter")
class ToolInvokeMeta(BaseModel):
"""
Tool invoke meta
"""
time_cost: float = Field(..., description="The time cost of the tool invoke")
error: Optional[str] = None
tool_config: Optional[dict] = None
@classmethod
def empty(cls) -> "ToolInvokeMeta":
"""
Get an empty instance of ToolInvokeMeta
"""
return cls(time_cost=0.0, error=None, tool_config={})
@classmethod
def error_instance(cls, error: str) -> "ToolInvokeMeta":
"""
Get an instance of ToolInvokeMeta with error
"""
return cls(time_cost=0.0, error=error, tool_config={})
def to_dict(self) -> dict:
return {
"time_cost": self.time_cost,
"error": self.error,
"tool_config": self.tool_config,
2024-05-27 22:01:11 +08:00
}
2024-05-27 22:01:11 +08:00
class ToolLabel(BaseModel):
"""
Tool label
"""
2024-05-27 22:01:11 +08:00
name: str = Field(..., description="The name of the tool")
label: I18nObject = Field(..., description="The label of the tool")
icon: str = Field(..., description="The icon of the tool")
2024-05-27 22:01:11 +08:00
class ToolInvokeFrom(Enum):
"""
Enum class for tool invoke
"""
2024-05-27 22:01:11 +08:00
WORKFLOW = "workflow"
AGENT = "agent"
2024-09-20 23:48:48 +08:00