tool_entities.py 16 KB

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