tool_entities.py 12 KB

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