tool_entities.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. import base64
  2. from enum import Enum
  3. from typing import Any, Optional, Union
  4. from pydantic import BaseModel, ConfigDict, Field, ValidationInfo, field_serializer, field_validator
  5. from core.entities.parameter_entities import (
  6. AppSelectorScope,
  7. CommonParameterType,
  8. ModelSelectorScope,
  9. ToolSelectorScope,
  10. )
  11. from core.entities.provider_entities import ProviderConfig
  12. from core.tools.entities.common_entities import I18nObject
  13. class ToolLabelEnum(Enum):
  14. SEARCH = "search"
  15. IMAGE = "image"
  16. VIDEOS = "videos"
  17. WEATHER = "weather"
  18. FINANCE = "finance"
  19. DESIGN = "design"
  20. TRAVEL = "travel"
  21. SOCIAL = "social"
  22. NEWS = "news"
  23. MEDICAL = "medical"
  24. PRODUCTIVITY = "productivity"
  25. EDUCATION = "education"
  26. BUSINESS = "business"
  27. ENTERTAINMENT = "entertainment"
  28. UTILITIES = "utilities"
  29. OTHER = "other"
  30. class ToolProviderType(str, Enum):
  31. """
  32. Enum class for tool provider
  33. """
  34. PLUGIN = "plugin"
  35. BUILT_IN = "builtin"
  36. WORKFLOW = "workflow"
  37. API = "api"
  38. APP = "app"
  39. DATASET_RETRIEVAL = "dataset-retrieval"
  40. @classmethod
  41. def value_of(cls, value: str) -> "ToolProviderType":
  42. """
  43. Get value of given mode.
  44. :param value: mode value
  45. :return: mode
  46. """
  47. for mode in cls:
  48. if mode.value == value:
  49. return mode
  50. raise ValueError(f"invalid mode value {value}")
  51. class ApiProviderSchemaType(Enum):
  52. """
  53. Enum class for api provider schema type.
  54. """
  55. OPENAPI = "openapi"
  56. SWAGGER = "swagger"
  57. OPENAI_PLUGIN = "openai_plugin"
  58. OPENAI_ACTIONS = "openai_actions"
  59. @classmethod
  60. def value_of(cls, value: str) -> "ApiProviderSchemaType":
  61. """
  62. Get value of given mode.
  63. :param value: mode value
  64. :return: mode
  65. """
  66. for mode in cls:
  67. if mode.value == value:
  68. return mode
  69. raise ValueError(f"invalid mode value {value}")
  70. class ApiProviderAuthType(Enum):
  71. """
  72. Enum class for api provider auth type.
  73. """
  74. NONE = "none"
  75. API_KEY = "api_key"
  76. @classmethod
  77. def value_of(cls, value: str) -> "ApiProviderAuthType":
  78. """
  79. Get value of given mode.
  80. :param value: mode value
  81. :return: mode
  82. """
  83. for mode in cls:
  84. if mode.value == value:
  85. return mode
  86. raise ValueError(f"invalid mode value {value}")
  87. class ToolInvokeMessage(BaseModel):
  88. class TextMessage(BaseModel):
  89. text: str
  90. class JsonMessage(BaseModel):
  91. json_object: dict
  92. class BlobMessage(BaseModel):
  93. blob: bytes
  94. class FileMessage(BaseModel):
  95. pass
  96. class VariableMessage(BaseModel):
  97. variable_name: str = Field(..., description="The name of the variable")
  98. variable_value: str = Field(..., description="The value of the variable")
  99. stream: bool = Field(default=False, description="Whether the variable is streamed")
  100. @field_validator("variable_value", mode="before")
  101. @classmethod
  102. def transform_variable_value(cls, value, values) -> Any:
  103. """
  104. Only basic types and lists are allowed.
  105. """
  106. if not isinstance(value, dict | list | str | int | float | bool):
  107. raise ValueError("Only basic types and lists are allowed.")
  108. # if stream is true, the value must be a string
  109. if values.get("stream"):
  110. if not isinstance(value, str):
  111. raise ValueError("When 'stream' is True, 'variable_value' must be a string.")
  112. return value
  113. @field_validator("variable_name", mode="before")
  114. @classmethod
  115. def transform_variable_name(cls, value) -> str:
  116. """
  117. The variable name must be a string.
  118. """
  119. if value in {"json", "text", "files"}:
  120. raise ValueError(f"The variable name '{value}' is reserved.")
  121. return value
  122. class MessageType(Enum):
  123. TEXT = "text"
  124. IMAGE = "image"
  125. LINK = "link"
  126. BLOB = "blob"
  127. JSON = "json"
  128. IMAGE_LINK = "image_link"
  129. VARIABLE = "variable"
  130. FILE = "file"
  131. type: MessageType = MessageType.TEXT
  132. """
  133. plain text, image url or link url
  134. """
  135. message: JsonMessage | TextMessage | BlobMessage | VariableMessage | FileMessage | None
  136. meta: dict[str, Any] | None = None
  137. save_as: str = ""
  138. @field_validator("message", mode="before")
  139. @classmethod
  140. def decode_blob_message(cls, v):
  141. if isinstance(v, dict) and "blob" in v:
  142. try:
  143. v["blob"] = base64.b64decode(v["blob"])
  144. except Exception:
  145. pass
  146. return v
  147. @field_serializer("message")
  148. def serialize_message(self, v):
  149. if isinstance(v, self.BlobMessage):
  150. return {"blob": base64.b64encode(v.blob).decode("utf-8")}
  151. return v
  152. class ToolInvokeMessageBinary(BaseModel):
  153. mimetype: str = Field(..., description="The mimetype of the binary")
  154. url: str = Field(..., description="The url of the binary")
  155. save_as: str = ""
  156. file_var: Optional[dict[str, Any]] = None
  157. class ToolParameterOption(BaseModel):
  158. value: str = Field(..., description="The value of the option")
  159. label: I18nObject = Field(..., description="The label of the option")
  160. @field_validator("value", mode="before")
  161. @classmethod
  162. def transform_id_to_str(cls, value) -> str:
  163. if not isinstance(value, str):
  164. return str(value)
  165. else:
  166. return value
  167. class ToolParameter(BaseModel):
  168. class ToolParameterType(str, Enum):
  169. STRING = CommonParameterType.STRING.value
  170. NUMBER = CommonParameterType.NUMBER.value
  171. BOOLEAN = CommonParameterType.BOOLEAN.value
  172. SELECT = CommonParameterType.SELECT.value
  173. SECRET_INPUT = CommonParameterType.SECRET_INPUT.value
  174. FILE = CommonParameterType.FILE.value
  175. FILES = CommonParameterType.FILES.value
  176. APP_SELECTOR = CommonParameterType.APP_SELECTOR.value
  177. TOOL_SELECTOR = CommonParameterType.TOOL_SELECTOR.value
  178. MODEL_SELECTOR = CommonParameterType.MODEL_SELECTOR.value
  179. # deprecated, should not use.
  180. SYSTEM_FILES = CommonParameterType.SYSTEM_FILES.value
  181. def as_normal_type(self):
  182. if self in {
  183. ToolParameter.ToolParameterType.SECRET_INPUT,
  184. ToolParameter.ToolParameterType.SELECT,
  185. }:
  186. return "string"
  187. return self.value
  188. def cast_value(self, value: Any, /):
  189. try:
  190. match self:
  191. case (
  192. ToolParameter.ToolParameterType.STRING
  193. | ToolParameter.ToolParameterType.SECRET_INPUT
  194. | ToolParameter.ToolParameterType.SELECT
  195. ):
  196. if value is None:
  197. return ""
  198. else:
  199. return value if isinstance(value, str) else str(value)
  200. case ToolParameter.ToolParameterType.BOOLEAN:
  201. if value is None:
  202. return False
  203. elif isinstance(value, str):
  204. # Allowed YAML boolean value strings: https://yaml.org/type/bool.html
  205. # and also '0' for False and '1' for True
  206. match value.lower():
  207. case "true" | "yes" | "y" | "1":
  208. return True
  209. case "false" | "no" | "n" | "0":
  210. return False
  211. case _:
  212. return bool(value)
  213. else:
  214. return value if isinstance(value, bool) else bool(value)
  215. case ToolParameter.ToolParameterType.NUMBER:
  216. if isinstance(value, int | float):
  217. return value
  218. elif isinstance(value, str) and value:
  219. if "." in value:
  220. return float(value)
  221. else:
  222. return int(value)
  223. case ToolParameter.ToolParameterType.SYSTEM_FILES | ToolParameter.ToolParameterType.FILES:
  224. if not isinstance(value, list):
  225. return [value]
  226. return value
  227. case ToolParameter.ToolParameterType.FILE:
  228. if isinstance(value, list):
  229. if len(value) != 1:
  230. raise ValueError(
  231. "This parameter only accepts one file but got multiple files while invoking."
  232. )
  233. else:
  234. return value[0]
  235. return value
  236. case (
  237. ToolParameter.ToolParameterType.TOOL_SELECTOR
  238. | ToolParameter.ToolParameterType.MODEL_SELECTOR
  239. | ToolParameter.ToolParameterType.APP_SELECTOR
  240. ):
  241. if not isinstance(value, dict):
  242. raise ValueError("The selector must be a dictionary.")
  243. return value
  244. case _:
  245. return str(value)
  246. except Exception:
  247. raise ValueError(f"The tool parameter value {value} is not in correct type of {self.as_normal_type()}.")
  248. class ToolParameterForm(Enum):
  249. SCHEMA = "schema" # should be set while adding tool
  250. FORM = "form" # should be set before invoking tool
  251. LLM = "llm" # will be set by LLM
  252. name: str = Field(..., description="The name of the parameter")
  253. label: I18nObject = Field(..., description="The label presented to the user")
  254. human_description: Optional[I18nObject] = Field(default=None, description="The description presented to the user")
  255. placeholder: Optional[I18nObject] = Field(default=None, description="The placeholder presented to the user")
  256. type: ToolParameterType = Field(..., description="The type of the parameter")
  257. scope: AppSelectorScope | ModelSelectorScope | ToolSelectorScope | None = None
  258. form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
  259. llm_description: Optional[str] = None
  260. required: Optional[bool] = False
  261. default: Optional[Union[float, int, str]] = None
  262. min: Optional[Union[float, int]] = None
  263. max: Optional[Union[float, int]] = None
  264. options: list[ToolParameterOption] = Field(default_factory=list)
  265. @field_validator("options", mode="before")
  266. @classmethod
  267. def transform_options(cls, v):
  268. if not isinstance(v, list):
  269. return []
  270. return v
  271. @classmethod
  272. def get_simple_instance(
  273. cls,
  274. name: str,
  275. llm_description: str,
  276. type: ToolParameterType,
  277. required: bool,
  278. options: Optional[list[str]] = None,
  279. ) -> "ToolParameter":
  280. """
  281. get a simple tool parameter
  282. :param name: the name of the parameter
  283. :param llm_description: the description presented to the LLM
  284. :param type: the type of the parameter
  285. :param required: if the parameter is required
  286. :param options: the options of the parameter
  287. """
  288. # convert options to ToolParameterOption
  289. if options:
  290. option_objs = [
  291. ToolParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) for option in options
  292. ]
  293. else:
  294. option_objs = []
  295. return cls(
  296. name=name,
  297. label=I18nObject(en_US="", zh_Hans=""),
  298. placeholder=None,
  299. human_description=I18nObject(en_US="", zh_Hans=""),
  300. type=type,
  301. form=cls.ToolParameterForm.LLM,
  302. llm_description=llm_description,
  303. required=required,
  304. options=option_objs,
  305. )
  306. class ToolProviderIdentity(BaseModel):
  307. author: str = Field(..., description="The author of the tool")
  308. name: str = Field(..., description="The name of the tool")
  309. description: I18nObject = Field(..., description="The description of the tool")
  310. icon: str = Field(..., description="The icon of the tool")
  311. label: I18nObject = Field(..., description="The label of the tool")
  312. tags: Optional[list[ToolLabelEnum]] = Field(
  313. default=[],
  314. description="The tags of the tool",
  315. )
  316. class ToolIdentity(BaseModel):
  317. author: str = Field(..., description="The author of the tool")
  318. name: str = Field(..., description="The name of the tool")
  319. label: I18nObject = Field(..., description="The label of the tool")
  320. provider: str = Field(..., description="The provider of the tool")
  321. icon: Optional[str] = None
  322. class ToolDescription(BaseModel):
  323. human: I18nObject = Field(..., description="The description presented to the user")
  324. llm: str = Field(..., description="The description presented to the LLM")
  325. class ToolEntity(BaseModel):
  326. identity: ToolIdentity
  327. parameters: list[ToolParameter] = Field(default_factory=list)
  328. description: Optional[ToolDescription] = None
  329. output_schema: Optional[dict] = None
  330. has_runtime_parameters: bool = Field(default=False, description="Whether the tool has runtime parameters")
  331. # pydantic configs
  332. model_config = ConfigDict(protected_namespaces=())
  333. @field_validator("parameters", mode="before")
  334. @classmethod
  335. def set_parameters(cls, v, validation_info: ValidationInfo) -> list[ToolParameter]:
  336. return v or []
  337. class ToolProviderEntity(BaseModel):
  338. identity: ToolProviderIdentity
  339. plugin_id: Optional[str] = Field(None, description="The id of the plugin")
  340. credentials_schema: list[ProviderConfig] = Field(default_factory=list)
  341. class ToolProviderEntityWithPlugin(ToolProviderEntity):
  342. tools: list[ToolEntity] = Field(default_factory=list)
  343. class WorkflowToolParameterConfiguration(BaseModel):
  344. """
  345. Workflow tool configuration
  346. """
  347. name: str = Field(..., description="The name of the parameter")
  348. description: str = Field(..., description="The description of the parameter")
  349. form: ToolParameter.ToolParameterForm = Field(..., description="The form of the parameter")
  350. class ToolInvokeMeta(BaseModel):
  351. """
  352. Tool invoke meta
  353. """
  354. time_cost: float = Field(..., description="The time cost of the tool invoke")
  355. error: Optional[str] = None
  356. tool_config: Optional[dict] = None
  357. @classmethod
  358. def empty(cls) -> "ToolInvokeMeta":
  359. """
  360. Get an empty instance of ToolInvokeMeta
  361. """
  362. return cls(time_cost=0.0, error=None, tool_config={})
  363. @classmethod
  364. def error_instance(cls, error: str) -> "ToolInvokeMeta":
  365. """
  366. Get an instance of ToolInvokeMeta with error
  367. """
  368. return cls(time_cost=0.0, error=error, tool_config={})
  369. def to_dict(self) -> dict:
  370. return {
  371. "time_cost": self.time_cost,
  372. "error": self.error,
  373. "tool_config": self.tool_config,
  374. }
  375. class ToolLabel(BaseModel):
  376. """
  377. Tool label
  378. """
  379. name: str = Field(..., description="The name of the tool")
  380. label: I18nObject = Field(..., description="The label of the tool")
  381. icon: str = Field(..., description="The icon of the tool")
  382. class ToolInvokeFrom(Enum):
  383. """
  384. Enum class for tool invoke
  385. """
  386. WORKFLOW = "workflow"
  387. AGENT = "agent"
  388. PLUGIN = "plugin"