tool_entities.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. from enum import Enum, StrEnum
  2. from typing import Any, Optional, Union, cast
  3. from pydantic import BaseModel, Field, field_validator
  4. from core.tools.entities.common_entities import I18nObject
  5. class ToolLabelEnum(Enum):
  6. SEARCH = "search"
  7. IMAGE = "image"
  8. VIDEOS = "videos"
  9. WEATHER = "weather"
  10. FINANCE = "finance"
  11. DESIGN = "design"
  12. TRAVEL = "travel"
  13. SOCIAL = "social"
  14. NEWS = "news"
  15. MEDICAL = "medical"
  16. PRODUCTIVITY = "productivity"
  17. EDUCATION = "education"
  18. BUSINESS = "business"
  19. ENTERTAINMENT = "entertainment"
  20. UTILITIES = "utilities"
  21. OTHER = "other"
  22. class ToolProviderType(Enum):
  23. """
  24. Enum class for tool provider
  25. """
  26. BUILT_IN = "builtin"
  27. WORKFLOW = "workflow"
  28. API = "api"
  29. APP = "app"
  30. DATASET_RETRIEVAL = "dataset-retrieval"
  31. @classmethod
  32. def value_of(cls, value: str) -> "ToolProviderType":
  33. """
  34. Get value of given mode.
  35. :param value: mode value
  36. :return: mode
  37. """
  38. for mode in cls:
  39. if mode.value == value:
  40. return mode
  41. raise ValueError(f"invalid mode value {value}")
  42. class ApiProviderSchemaType(Enum):
  43. """
  44. Enum class for api provider schema type.
  45. """
  46. OPENAPI = "openapi"
  47. SWAGGER = "swagger"
  48. OPENAI_PLUGIN = "openai_plugin"
  49. OPENAI_ACTIONS = "openai_actions"
  50. @classmethod
  51. def value_of(cls, value: str) -> "ApiProviderSchemaType":
  52. """
  53. Get value of given mode.
  54. :param value: mode value
  55. :return: mode
  56. """
  57. for mode in cls:
  58. if mode.value == value:
  59. return mode
  60. raise ValueError(f"invalid mode value {value}")
  61. class ApiProviderAuthType(Enum):
  62. """
  63. Enum class for api provider auth type.
  64. """
  65. NONE = "none"
  66. API_KEY = "api_key"
  67. @classmethod
  68. def value_of(cls, value: str) -> "ApiProviderAuthType":
  69. """
  70. Get value of given mode.
  71. :param value: mode value
  72. :return: mode
  73. """
  74. for mode in cls:
  75. if mode.value == value:
  76. return mode
  77. raise ValueError(f"invalid mode value {value}")
  78. class ToolInvokeMessage(BaseModel):
  79. class MessageType(Enum):
  80. TEXT = "text"
  81. IMAGE = "image"
  82. LINK = "link"
  83. BLOB = "blob"
  84. JSON = "json"
  85. IMAGE_LINK = "image_link"
  86. FILE = "file"
  87. type: MessageType = MessageType.TEXT
  88. """
  89. plain text, image url or link url
  90. """
  91. message: str | bytes | dict | None = None
  92. # TODO: Use a BaseModel for meta
  93. meta: dict[str, Any] = Field(default_factory=dict)
  94. save_as: str = ""
  95. class ToolInvokeMessageBinary(BaseModel):
  96. mimetype: str = Field(..., description="The mimetype of the binary")
  97. url: str = Field(..., description="The url of the binary")
  98. save_as: str = ""
  99. file_var: Optional[dict[str, Any]] = None
  100. class ToolParameterOption(BaseModel):
  101. value: str = Field(..., description="The value of the option")
  102. label: I18nObject = Field(..., description="The label of the option")
  103. @field_validator("value", mode="before")
  104. @classmethod
  105. def transform_id_to_str(cls, value) -> str:
  106. if not isinstance(value, str):
  107. return str(value)
  108. else:
  109. return value
  110. class ToolParameter(BaseModel):
  111. class ToolParameterType(StrEnum):
  112. STRING = "string"
  113. NUMBER = "number"
  114. BOOLEAN = "boolean"
  115. SELECT = "select"
  116. SECRET_INPUT = "secret-input"
  117. FILE = "file"
  118. FILES = "files"
  119. # deprecated, should not use.
  120. SYSTEM_FILES = "systme-files"
  121. def as_normal_type(self):
  122. if self in {
  123. ToolParameter.ToolParameterType.SECRET_INPUT,
  124. ToolParameter.ToolParameterType.SELECT,
  125. }:
  126. return "string"
  127. return self.value
  128. def cast_value(self, value: Any, /):
  129. try:
  130. match self:
  131. case (
  132. ToolParameter.ToolParameterType.STRING
  133. | ToolParameter.ToolParameterType.SECRET_INPUT
  134. | ToolParameter.ToolParameterType.SELECT
  135. ):
  136. if value is None:
  137. return ""
  138. else:
  139. return value if isinstance(value, str) else str(value)
  140. case ToolParameter.ToolParameterType.BOOLEAN:
  141. if value is None:
  142. return False
  143. elif isinstance(value, str):
  144. # Allowed YAML boolean value strings: https://yaml.org/type/bool.html
  145. # and also '0' for False and '1' for True
  146. match value.lower():
  147. case "true" | "yes" | "y" | "1":
  148. return True
  149. case "false" | "no" | "n" | "0":
  150. return False
  151. case _:
  152. return bool(value)
  153. else:
  154. return value if isinstance(value, bool) else bool(value)
  155. case ToolParameter.ToolParameterType.NUMBER:
  156. if isinstance(value, int | float):
  157. return value
  158. elif isinstance(value, str) and value:
  159. if "." in value:
  160. return float(value)
  161. else:
  162. return int(value)
  163. case (
  164. ToolParameter.ToolParameterType.SYSTEM_FILES
  165. | ToolParameter.ToolParameterType.FILE
  166. | ToolParameter.ToolParameterType.FILES
  167. ):
  168. return value
  169. case _:
  170. return str(value)
  171. except Exception:
  172. raise ValueError(f"The tool parameter value {value} is not in correct type.")
  173. class ToolParameterForm(Enum):
  174. SCHEMA = "schema" # should be set while adding tool
  175. FORM = "form" # should be set before invoking tool
  176. LLM = "llm" # will be set by LLM
  177. name: str = Field(..., description="The name of the parameter")
  178. label: I18nObject = Field(..., description="The label presented to the user")
  179. human_description: Optional[I18nObject] = Field(None, description="The description presented to the user")
  180. placeholder: Optional[I18nObject] = Field(None, description="The placeholder presented to the user")
  181. type: ToolParameterType = Field(..., description="The type of the parameter")
  182. form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
  183. llm_description: Optional[str] = None
  184. required: Optional[bool] = False
  185. default: Optional[Union[float, int, str]] = None
  186. min: Optional[Union[float, int]] = None
  187. max: Optional[Union[float, int]] = None
  188. options: Optional[list[ToolParameterOption]] = None
  189. @classmethod
  190. def get_simple_instance(
  191. cls,
  192. name: str,
  193. llm_description: str,
  194. type: ToolParameterType,
  195. required: bool,
  196. options: Optional[list[str]] = None,
  197. ) -> "ToolParameter":
  198. """
  199. get a simple tool parameter
  200. :param name: the name of the parameter
  201. :param llm_description: the description presented to the LLM
  202. :param type: the type of the parameter
  203. :param required: if the parameter is required
  204. :param options: the options of the parameter
  205. """
  206. # convert options to ToolParameterOption
  207. # FIXME fix the type error
  208. if options:
  209. options = [
  210. ToolParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) # type: ignore
  211. for option in options # type: ignore
  212. ]
  213. return cls(
  214. name=name,
  215. label=I18nObject(en_US="", zh_Hans=""),
  216. human_description=I18nObject(en_US="", zh_Hans=""),
  217. placeholder=None,
  218. type=type,
  219. form=cls.ToolParameterForm.LLM,
  220. llm_description=llm_description,
  221. required=required,
  222. options=options, # type: ignore
  223. )
  224. class ToolProviderIdentity(BaseModel):
  225. author: str = Field(..., description="The author of the tool")
  226. name: str = Field(..., description="The name of the tool")
  227. description: I18nObject = Field(..., description="The description of the tool")
  228. icon: str = Field(..., description="The icon of the tool")
  229. label: I18nObject = Field(..., description="The label of the tool")
  230. tags: Optional[list[ToolLabelEnum]] = Field(
  231. default=[],
  232. description="The tags of the tool",
  233. )
  234. class ToolDescription(BaseModel):
  235. human: I18nObject = Field(..., description="The description presented to the user")
  236. llm: str = Field(..., description="The description presented to the LLM")
  237. class ToolIdentity(BaseModel):
  238. author: str = Field(..., description="The author of the tool")
  239. name: str = Field(..., description="The name of the tool")
  240. label: I18nObject = Field(..., description="The label of the tool")
  241. provider: str = Field(..., description="The provider of the tool")
  242. icon: Optional[str] = None
  243. class ToolCredentialsOption(BaseModel):
  244. value: str = Field(..., description="The value of the option")
  245. label: I18nObject = Field(..., description="The label of the option")
  246. class ToolProviderCredentials(BaseModel):
  247. class CredentialsType(Enum):
  248. SECRET_INPUT = "secret-input"
  249. TEXT_INPUT = "text-input"
  250. SELECT = "select"
  251. BOOLEAN = "boolean"
  252. @classmethod
  253. def value_of(cls, value: str) -> "ToolProviderCredentials.CredentialsType":
  254. """
  255. Get value of given mode.
  256. :param value: mode value
  257. :return: mode
  258. """
  259. for mode in cls:
  260. if mode.value == value:
  261. return mode
  262. raise ValueError(f"invalid mode value {value}")
  263. @staticmethod
  264. def default(value: str) -> str:
  265. return ""
  266. name: str = Field(..., description="The name of the credentials")
  267. type: CredentialsType = Field(..., description="The type of the credentials")
  268. required: bool = False
  269. default: Optional[Union[int, str]] = None
  270. options: Optional[list[ToolCredentialsOption]] = None
  271. label: Optional[I18nObject] = None
  272. help: Optional[I18nObject] = None
  273. url: Optional[str] = None
  274. placeholder: Optional[I18nObject] = None
  275. def to_dict(self) -> dict:
  276. return {
  277. "name": self.name,
  278. "type": self.type.value,
  279. "required": self.required,
  280. "default": self.default,
  281. "options": self.options,
  282. "help": self.help.to_dict() if self.help else None,
  283. "label": self.label.to_dict() if self.label else None,
  284. "url": self.url,
  285. "placeholder": self.placeholder.to_dict() if self.placeholder else None,
  286. }
  287. class ToolRuntimeVariableType(Enum):
  288. TEXT = "text"
  289. IMAGE = "image"
  290. class ToolRuntimeVariable(BaseModel):
  291. type: ToolRuntimeVariableType = Field(..., description="The type of the variable")
  292. name: str = Field(..., description="The name of the variable")
  293. position: int = Field(..., description="The position of the variable")
  294. tool_name: str = Field(..., description="The name of the tool")
  295. class ToolRuntimeTextVariable(ToolRuntimeVariable):
  296. value: str = Field(..., description="The value of the variable")
  297. class ToolRuntimeImageVariable(ToolRuntimeVariable):
  298. value: str = Field(..., description="The path of the image")
  299. class ToolRuntimeVariablePool(BaseModel):
  300. conversation_id: str = Field(..., description="The conversation id")
  301. user_id: str = Field(..., description="The user id")
  302. tenant_id: str = Field(..., description="The tenant id of assistant")
  303. pool: list[ToolRuntimeVariable] = Field(..., description="The pool of variables")
  304. def __init__(self, **data: Any):
  305. pool = data.get("pool", [])
  306. # convert pool into correct type
  307. for index, variable in enumerate(pool):
  308. if variable["type"] == ToolRuntimeVariableType.TEXT.value:
  309. pool[index] = ToolRuntimeTextVariable(**variable)
  310. elif variable["type"] == ToolRuntimeVariableType.IMAGE.value:
  311. pool[index] = ToolRuntimeImageVariable(**variable)
  312. super().__init__(**data)
  313. def dict(self) -> dict: # type: ignore
  314. """
  315. FIXME: just ignore the type check for now
  316. """
  317. return {
  318. "conversation_id": self.conversation_id,
  319. "user_id": self.user_id,
  320. "tenant_id": self.tenant_id,
  321. "pool": [variable.model_dump() for variable in self.pool],
  322. }
  323. def set_text(self, tool_name: str, name: str, value: str) -> None:
  324. """
  325. set a text variable
  326. """
  327. for variable in self.pool:
  328. if variable.name == name:
  329. if variable.type == ToolRuntimeVariableType.TEXT:
  330. variable = cast(ToolRuntimeTextVariable, variable)
  331. variable.value = value
  332. return
  333. variable = ToolRuntimeTextVariable(
  334. type=ToolRuntimeVariableType.TEXT,
  335. name=name,
  336. position=len(self.pool),
  337. tool_name=tool_name,
  338. value=value,
  339. )
  340. self.pool.append(variable)
  341. def set_file(self, tool_name: str, value: str, name: Optional[str] = None) -> None:
  342. """
  343. set an image variable
  344. :param tool_name: the name of the tool
  345. :param value: the id of the file
  346. """
  347. # check how many image variables are there
  348. image_variable_count = 0
  349. for variable in self.pool:
  350. if variable.type == ToolRuntimeVariableType.IMAGE:
  351. image_variable_count += 1
  352. if name is None:
  353. name = f"file_{image_variable_count}"
  354. for variable in self.pool:
  355. if variable.name == name:
  356. if variable.type == ToolRuntimeVariableType.IMAGE:
  357. variable = cast(ToolRuntimeImageVariable, variable)
  358. variable.value = value
  359. return
  360. variable = ToolRuntimeImageVariable(
  361. type=ToolRuntimeVariableType.IMAGE,
  362. name=name,
  363. position=len(self.pool),
  364. tool_name=tool_name,
  365. value=value,
  366. )
  367. self.pool.append(variable)
  368. class ModelToolPropertyKey(Enum):
  369. IMAGE_PARAMETER_NAME = "image_parameter_name"
  370. class ModelToolConfiguration(BaseModel):
  371. """
  372. Model tool configuration
  373. """
  374. type: str = Field(..., description="The type of the model tool")
  375. model: str = Field(..., description="The model")
  376. label: I18nObject = Field(..., description="The label of the model tool")
  377. properties: dict[ModelToolPropertyKey, Any] = Field(..., description="The properties of the model tool")
  378. class ModelToolProviderConfiguration(BaseModel):
  379. """
  380. Model tool provider configuration
  381. """
  382. provider: str = Field(..., description="The provider of the model tool")
  383. models: list[ModelToolConfiguration] = Field(..., description="The models of the model tool")
  384. label: I18nObject = Field(..., description="The label of the model tool")
  385. class WorkflowToolParameterConfiguration(BaseModel):
  386. """
  387. Workflow tool configuration
  388. """
  389. name: str = Field(..., description="The name of the parameter")
  390. description: str = Field(..., description="The description of the parameter")
  391. form: ToolParameter.ToolParameterForm = Field(..., description="The form of the parameter")
  392. class ToolInvokeMeta(BaseModel):
  393. """
  394. Tool invoke meta
  395. """
  396. time_cost: float = Field(..., description="The time cost of the tool invoke")
  397. error: Optional[str] = None
  398. tool_config: Optional[dict] = None
  399. @classmethod
  400. def empty(cls) -> "ToolInvokeMeta":
  401. """
  402. Get an empty instance of ToolInvokeMeta
  403. """
  404. return cls(time_cost=0.0, error=None, tool_config={})
  405. @classmethod
  406. def error_instance(cls, error: str) -> "ToolInvokeMeta":
  407. """
  408. Get an instance of ToolInvokeMeta with error
  409. """
  410. return cls(time_cost=0.0, error=error, tool_config={})
  411. def to_dict(self) -> dict:
  412. return {
  413. "time_cost": self.time_cost,
  414. "error": self.error,
  415. "tool_config": self.tool_config,
  416. }
  417. class ToolLabel(BaseModel):
  418. """
  419. Tool label
  420. """
  421. name: str = Field(..., description="The name of the tool")
  422. label: I18nObject = Field(..., description="The label of the tool")
  423. icon: str = Field(..., description="The icon of the tool")
  424. class ToolInvokeFrom(Enum):
  425. """
  426. Enum class for tool invoke
  427. """
  428. WORKFLOW = "workflow"
  429. AGENT = "agent"