tool_entities.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. from enum import Enum
  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. IMAGE_LINK = "image_link"
  85. FILE_VAR = "file_var"
  86. type: MessageType = MessageType.TEXT
  87. """
  88. plain text, image url or link url
  89. """
  90. message: Union[str, bytes] = None
  91. meta: dict[str, Any] = None
  92. save_as: str = ''
  93. class ToolInvokeMessageBinary(BaseModel):
  94. mimetype: str = Field(..., description="The mimetype of the binary")
  95. url: str = Field(..., description="The url of the binary")
  96. save_as: str = ''
  97. file_var: Optional[dict[str, Any]] = None
  98. class ToolParameterOption(BaseModel):
  99. value: str = Field(..., description="The value of the option")
  100. label: I18nObject = Field(..., description="The label of the option")
  101. @field_validator('value', mode='before')
  102. @classmethod
  103. def transform_id_to_str(cls, value) -> str:
  104. if not isinstance(value, str):
  105. return str(value)
  106. else:
  107. return value
  108. class ToolParameter(BaseModel):
  109. class ToolParameterType(str, Enum):
  110. STRING = "string"
  111. NUMBER = "number"
  112. BOOLEAN = "boolean"
  113. SELECT = "select"
  114. SECRET_INPUT = "secret-input"
  115. FILE = "file"
  116. class ToolParameterForm(Enum):
  117. SCHEMA = "schema" # should be set while adding tool
  118. FORM = "form" # should be set before invoking tool
  119. LLM = "llm" # will be set by LLM
  120. name: str = Field(..., description="The name of the parameter")
  121. label: I18nObject = Field(..., description="The label presented to the user")
  122. human_description: I18nObject = Field(..., description="The description presented to the user")
  123. type: ToolParameterType = Field(..., description="The type of the parameter")
  124. form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
  125. llm_description: Optional[str] = None
  126. required: Optional[bool] = False
  127. default: Optional[Union[int, str]] = None
  128. min: Optional[Union[float, int]] = None
  129. max: Optional[Union[float, int]] = None
  130. options: Optional[list[ToolParameterOption]] = None
  131. @classmethod
  132. def get_simple_instance(cls,
  133. name: str, llm_description: str, type: ToolParameterType,
  134. required: bool, options: Optional[list[str]] = None) -> 'ToolParameter':
  135. """
  136. get a simple tool parameter
  137. :param name: the name of the parameter
  138. :param llm_description: the description presented to the LLM
  139. :param type: the type of the parameter
  140. :param required: if the parameter is required
  141. :param options: the options of the parameter
  142. """
  143. # convert options to ToolParameterOption
  144. if options:
  145. options = [ToolParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) for option in options]
  146. return cls(
  147. name=name,
  148. label=I18nObject(en_US='', zh_Hans=''),
  149. human_description=I18nObject(en_US='', zh_Hans=''),
  150. type=type,
  151. form=cls.ToolParameterForm.LLM,
  152. llm_description=llm_description,
  153. required=required,
  154. options=options,
  155. )
  156. class ToolProviderIdentity(BaseModel):
  157. author: str = Field(..., description="The author of the tool")
  158. name: str = Field(..., description="The name of the tool")
  159. description: I18nObject = Field(..., description="The description of the tool")
  160. icon: str = Field(..., description="The icon of the tool")
  161. label: I18nObject = Field(..., description="The label of the tool")
  162. tags: Optional[list[ToolLabelEnum]] = Field(default=[], description="The tags of the tool", )
  163. class ToolDescription(BaseModel):
  164. human: I18nObject = Field(..., description="The description presented to the user")
  165. llm: str = Field(..., description="The description presented to the LLM")
  166. class ToolIdentity(BaseModel):
  167. author: str = Field(..., description="The author of the tool")
  168. name: str = Field(..., description="The name of the tool")
  169. label: I18nObject = Field(..., description="The label of the tool")
  170. provider: str = Field(..., description="The provider of the tool")
  171. icon: Optional[str] = None
  172. class ToolCredentialsOption(BaseModel):
  173. value: str = Field(..., description="The value of the option")
  174. label: I18nObject = Field(..., description="The label of the option")
  175. class ToolProviderCredentials(BaseModel):
  176. class CredentialsType(Enum):
  177. SECRET_INPUT = "secret-input"
  178. TEXT_INPUT = "text-input"
  179. SELECT = "select"
  180. BOOLEAN = "boolean"
  181. @classmethod
  182. def value_of(cls, value: str) -> "ToolProviderCredentials.CredentialsType":
  183. """
  184. Get value of given mode.
  185. :param value: mode value
  186. :return: mode
  187. """
  188. for mode in cls:
  189. if mode.value == value:
  190. return mode
  191. raise ValueError(f'invalid mode value {value}')
  192. @staticmethod
  193. def default(value: str) -> str:
  194. return ""
  195. name: str = Field(..., description="The name of the credentials")
  196. type: CredentialsType = Field(..., description="The type of the credentials")
  197. required: bool = False
  198. default: Optional[Union[int, str]] = None
  199. options: Optional[list[ToolCredentialsOption]] = None
  200. label: Optional[I18nObject] = None
  201. help: Optional[I18nObject] = None
  202. url: Optional[str] = None
  203. placeholder: Optional[I18nObject] = None
  204. def to_dict(self) -> dict:
  205. return {
  206. 'name': self.name,
  207. 'type': self.type.value,
  208. 'required': self.required,
  209. 'default': self.default,
  210. 'options': self.options,
  211. 'help': self.help.to_dict() if self.help else None,
  212. 'label': self.label.to_dict(),
  213. 'url': self.url,
  214. 'placeholder': self.placeholder.to_dict() if self.placeholder else None,
  215. }
  216. class ToolRuntimeVariableType(Enum):
  217. TEXT = "text"
  218. IMAGE = "image"
  219. class ToolRuntimeVariable(BaseModel):
  220. type: ToolRuntimeVariableType = Field(..., description="The type of the variable")
  221. name: str = Field(..., description="The name of the variable")
  222. position: int = Field(..., description="The position of the variable")
  223. tool_name: str = Field(..., description="The name of the tool")
  224. class ToolRuntimeTextVariable(ToolRuntimeVariable):
  225. value: str = Field(..., description="The value of the variable")
  226. class ToolRuntimeImageVariable(ToolRuntimeVariable):
  227. value: str = Field(..., description="The path of the image")
  228. class ToolRuntimeVariablePool(BaseModel):
  229. conversation_id: str = Field(..., description="The conversation id")
  230. user_id: str = Field(..., description="The user id")
  231. tenant_id: str = Field(..., description="The tenant id of assistant")
  232. pool: list[ToolRuntimeVariable] = Field(..., description="The pool of variables")
  233. def __init__(self, **data: Any):
  234. pool = data.get('pool', [])
  235. # convert pool into correct type
  236. for index, variable in enumerate(pool):
  237. if variable['type'] == ToolRuntimeVariableType.TEXT.value:
  238. pool[index] = ToolRuntimeTextVariable(**variable)
  239. elif variable['type'] == ToolRuntimeVariableType.IMAGE.value:
  240. pool[index] = ToolRuntimeImageVariable(**variable)
  241. super().__init__(**data)
  242. def dict(self) -> dict:
  243. return {
  244. 'conversation_id': self.conversation_id,
  245. 'user_id': self.user_id,
  246. 'tenant_id': self.tenant_id,
  247. 'pool': [variable.model_dump() for variable in self.pool],
  248. }
  249. def set_text(self, tool_name: str, name: str, value: str) -> None:
  250. """
  251. set a text variable
  252. """
  253. for variable in self.pool:
  254. if variable.name == name:
  255. if variable.type == ToolRuntimeVariableType.TEXT:
  256. variable = cast(ToolRuntimeTextVariable, variable)
  257. variable.value = value
  258. return
  259. variable = ToolRuntimeTextVariable(
  260. type=ToolRuntimeVariableType.TEXT,
  261. name=name,
  262. position=len(self.pool),
  263. tool_name=tool_name,
  264. value=value,
  265. )
  266. self.pool.append(variable)
  267. def set_file(self, tool_name: str, value: str, name: str = None) -> None:
  268. """
  269. set an image variable
  270. :param tool_name: the name of the tool
  271. :param value: the id of the file
  272. """
  273. # check how many image variables are there
  274. image_variable_count = 0
  275. for variable in self.pool:
  276. if variable.type == ToolRuntimeVariableType.IMAGE:
  277. image_variable_count += 1
  278. if name is None:
  279. name = f"file_{image_variable_count}"
  280. for variable in self.pool:
  281. if variable.name == name:
  282. if variable.type == ToolRuntimeVariableType.IMAGE:
  283. variable = cast(ToolRuntimeImageVariable, variable)
  284. variable.value = value
  285. return
  286. variable = ToolRuntimeImageVariable(
  287. type=ToolRuntimeVariableType.IMAGE,
  288. name=name,
  289. position=len(self.pool),
  290. tool_name=tool_name,
  291. value=value,
  292. )
  293. self.pool.append(variable)
  294. class ModelToolPropertyKey(Enum):
  295. IMAGE_PARAMETER_NAME = "image_parameter_name"
  296. class ModelToolConfiguration(BaseModel):
  297. """
  298. Model tool configuration
  299. """
  300. type: str = Field(..., description="The type of the model tool")
  301. model: str = Field(..., description="The model")
  302. label: I18nObject = Field(..., description="The label of the model tool")
  303. properties: dict[ModelToolPropertyKey, Any] = Field(..., description="The properties of the model tool")
  304. class ModelToolProviderConfiguration(BaseModel):
  305. """
  306. Model tool provider configuration
  307. """
  308. provider: str = Field(..., description="The provider of the model tool")
  309. models: list[ModelToolConfiguration] = Field(..., description="The models of the model tool")
  310. label: I18nObject = Field(..., description="The label of the model tool")
  311. class WorkflowToolParameterConfiguration(BaseModel):
  312. """
  313. Workflow tool configuration
  314. """
  315. name: str = Field(..., description="The name of the parameter")
  316. description: str = Field(..., description="The description of the parameter")
  317. form: ToolParameter.ToolParameterForm = Field(..., description="The form of the parameter")
  318. class ToolInvokeMeta(BaseModel):
  319. """
  320. Tool invoke meta
  321. """
  322. time_cost: float = Field(..., description="The time cost of the tool invoke")
  323. error: Optional[str] = None
  324. tool_config: Optional[dict] = None
  325. @classmethod
  326. def empty(cls) -> 'ToolInvokeMeta':
  327. """
  328. Get an empty instance of ToolInvokeMeta
  329. """
  330. return cls(time_cost=0.0, error=None, tool_config={})
  331. @classmethod
  332. def error_instance(cls, error: str) -> 'ToolInvokeMeta':
  333. """
  334. Get an instance of ToolInvokeMeta with error
  335. """
  336. return cls(time_cost=0.0, error=error, tool_config={})
  337. def to_dict(self) -> dict:
  338. return {
  339. 'time_cost': self.time_cost,
  340. 'error': self.error,
  341. 'tool_config': self.tool_config,
  342. }
  343. class ToolLabel(BaseModel):
  344. """
  345. Tool label
  346. """
  347. name: str = Field(..., description="The name of the tool")
  348. label: I18nObject = Field(..., description="The label of the tool")
  349. icon: str = Field(..., description="The icon of the tool")
  350. class ToolInvokeFrom(Enum):
  351. """
  352. Enum class for tool invoke
  353. """
  354. WORKFLOW = "workflow"
  355. AGENT = "agent"