tool_entities.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. from enum import Enum
  2. from typing import Any, Optional, Union, cast
  3. from pydantic import BaseModel, Field
  4. from core.tools.entities.common_entities import I18nObject
  5. class ToolProviderType(Enum):
  6. """
  7. Enum class for tool provider
  8. """
  9. BUILT_IN = "builtin"
  10. WORKFLOW = "workflow"
  11. API = "api"
  12. APP = "app"
  13. DATASET_RETRIEVAL = "dataset-retrieval"
  14. @classmethod
  15. def value_of(cls, value: str) -> 'ToolProviderType':
  16. """
  17. Get value of given mode.
  18. :param value: mode value
  19. :return: mode
  20. """
  21. for mode in cls:
  22. if mode.value == value:
  23. return mode
  24. raise ValueError(f'invalid mode value {value}')
  25. class ApiProviderSchemaType(Enum):
  26. """
  27. Enum class for api provider schema type.
  28. """
  29. OPENAPI = "openapi"
  30. SWAGGER = "swagger"
  31. OPENAI_PLUGIN = "openai_plugin"
  32. OPENAI_ACTIONS = "openai_actions"
  33. @classmethod
  34. def value_of(cls, value: str) -> 'ApiProviderSchemaType':
  35. """
  36. Get value of given mode.
  37. :param value: mode value
  38. :return: mode
  39. """
  40. for mode in cls:
  41. if mode.value == value:
  42. return mode
  43. raise ValueError(f'invalid mode value {value}')
  44. class ApiProviderAuthType(Enum):
  45. """
  46. Enum class for api provider auth type.
  47. """
  48. NONE = "none"
  49. API_KEY = "api_key"
  50. @classmethod
  51. def value_of(cls, value: str) -> 'ApiProviderAuthType':
  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 ToolInvokeMessage(BaseModel):
  62. class MessageType(Enum):
  63. TEXT = "text"
  64. IMAGE = "image"
  65. LINK = "link"
  66. BLOB = "blob"
  67. IMAGE_LINK = "image_link"
  68. FILE_VAR = "file_var"
  69. type: MessageType = MessageType.TEXT
  70. """
  71. plain text, image url or link url
  72. """
  73. message: Union[str, bytes] = None
  74. meta: dict[str, Any] = None
  75. save_as: str = ''
  76. class ToolInvokeMessageBinary(BaseModel):
  77. mimetype: str = Field(..., description="The mimetype of the binary")
  78. url: str = Field(..., description="The url of the binary")
  79. save_as: str = ''
  80. file_var: Optional[dict[str, Any]] = None
  81. class ToolParameterOption(BaseModel):
  82. value: str = Field(..., description="The value of the option")
  83. label: I18nObject = Field(..., description="The label of the option")
  84. class ToolParameter(BaseModel):
  85. class ToolParameterType(Enum):
  86. STRING = "string"
  87. NUMBER = "number"
  88. BOOLEAN = "boolean"
  89. SELECT = "select"
  90. SECRET_INPUT = "secret-input"
  91. FILE = "file"
  92. class ToolParameterForm(Enum):
  93. SCHEMA = "schema" # should be set while adding tool
  94. FORM = "form" # should be set before invoking tool
  95. LLM = "llm" # will be set by LLM
  96. name: str = Field(..., description="The name of the parameter")
  97. label: I18nObject = Field(..., description="The label presented to the user")
  98. human_description: I18nObject = Field(..., description="The description presented to the user")
  99. type: ToolParameterType = Field(..., description="The type of the parameter")
  100. form: ToolParameterForm = Field(..., description="The form of the parameter, schema/form/llm")
  101. llm_description: Optional[str] = None
  102. required: Optional[bool] = False
  103. default: Optional[Union[int, str]] = None
  104. min: Optional[Union[float, int]] = None
  105. max: Optional[Union[float, int]] = None
  106. options: Optional[list[ToolParameterOption]] = None
  107. @classmethod
  108. def get_simple_instance(cls,
  109. name: str, llm_description: str, type: ToolParameterType,
  110. required: bool, options: Optional[list[str]] = None) -> 'ToolParameter':
  111. """
  112. get a simple tool parameter
  113. :param name: the name of the parameter
  114. :param llm_description: the description presented to the LLM
  115. :param type: the type of the parameter
  116. :param required: if the parameter is required
  117. :param options: the options of the parameter
  118. """
  119. # convert options to ToolParameterOption
  120. if options:
  121. options = [ToolParameterOption(value=option, label=I18nObject(en_US=option, zh_Hans=option)) for option in options]
  122. return cls(
  123. name=name,
  124. label=I18nObject(en_US='', zh_Hans=''),
  125. human_description=I18nObject(en_US='', zh_Hans=''),
  126. type=type,
  127. form=cls.ToolParameterForm.LLM,
  128. llm_description=llm_description,
  129. required=required,
  130. options=options,
  131. )
  132. class ToolProviderIdentity(BaseModel):
  133. author: str = Field(..., description="The author of the tool")
  134. name: str = Field(..., description="The name of the tool")
  135. description: I18nObject = Field(..., description="The description of the tool")
  136. icon: str = Field(..., description="The icon of the tool")
  137. label: I18nObject = Field(..., description="The label of the tool")
  138. class ToolDescription(BaseModel):
  139. human: I18nObject = Field(..., description="The description presented to the user")
  140. llm: str = Field(..., description="The description presented to the LLM")
  141. class ToolIdentity(BaseModel):
  142. author: str = Field(..., description="The author of the tool")
  143. name: str = Field(..., description="The name of the tool")
  144. label: I18nObject = Field(..., description="The label of the tool")
  145. provider: str = Field(..., description="The provider of the tool")
  146. icon: Optional[str] = None
  147. class ToolCredentialsOption(BaseModel):
  148. value: str = Field(..., description="The value of the option")
  149. label: I18nObject = Field(..., description="The label of the option")
  150. class ToolProviderCredentials(BaseModel):
  151. class CredentialsType(Enum):
  152. SECRET_INPUT = "secret-input"
  153. TEXT_INPUT = "text-input"
  154. SELECT = "select"
  155. BOOLEAN = "boolean"
  156. @classmethod
  157. def value_of(cls, value: str) -> "ToolProviderCredentials.CredentialsType":
  158. """
  159. Get value of given mode.
  160. :param value: mode value
  161. :return: mode
  162. """
  163. for mode in cls:
  164. if mode.value == value:
  165. return mode
  166. raise ValueError(f'invalid mode value {value}')
  167. @staticmethod
  168. def default(value: str) -> str:
  169. return ""
  170. name: str = Field(..., description="The name of the credentials")
  171. type: CredentialsType = Field(..., description="The type of the credentials")
  172. required: bool = False
  173. default: Optional[Union[int, str]] = None
  174. options: Optional[list[ToolCredentialsOption]] = None
  175. label: Optional[I18nObject] = None
  176. help: Optional[I18nObject] = None
  177. url: Optional[str] = None
  178. placeholder: Optional[I18nObject] = None
  179. def to_dict(self) -> dict:
  180. return {
  181. 'name': self.name,
  182. 'type': self.type.value,
  183. 'required': self.required,
  184. 'default': self.default,
  185. 'options': self.options,
  186. 'help': self.help.to_dict() if self.help else None,
  187. 'label': self.label.to_dict(),
  188. 'url': self.url,
  189. 'placeholder': self.placeholder.to_dict() if self.placeholder else None,
  190. }
  191. class ToolRuntimeVariableType(Enum):
  192. TEXT = "text"
  193. IMAGE = "image"
  194. class ToolRuntimeVariable(BaseModel):
  195. type: ToolRuntimeVariableType = Field(..., description="The type of the variable")
  196. name: str = Field(..., description="The name of the variable")
  197. position: int = Field(..., description="The position of the variable")
  198. tool_name: str = Field(..., description="The name of the tool")
  199. class ToolRuntimeTextVariable(ToolRuntimeVariable):
  200. value: str = Field(..., description="The value of the variable")
  201. class ToolRuntimeImageVariable(ToolRuntimeVariable):
  202. value: str = Field(..., description="The path of the image")
  203. class ToolRuntimeVariablePool(BaseModel):
  204. conversation_id: str = Field(..., description="The conversation id")
  205. user_id: str = Field(..., description="The user id")
  206. tenant_id: str = Field(..., description="The tenant id of assistant")
  207. pool: list[ToolRuntimeVariable] = Field(..., description="The pool of variables")
  208. def __init__(self, **data: Any):
  209. pool = data.get('pool', [])
  210. # convert pool into correct type
  211. for index, variable in enumerate(pool):
  212. if variable['type'] == ToolRuntimeVariableType.TEXT.value:
  213. pool[index] = ToolRuntimeTextVariable(**variable)
  214. elif variable['type'] == ToolRuntimeVariableType.IMAGE.value:
  215. pool[index] = ToolRuntimeImageVariable(**variable)
  216. super().__init__(**data)
  217. def dict(self) -> dict:
  218. return {
  219. 'conversation_id': self.conversation_id,
  220. 'user_id': self.user_id,
  221. 'tenant_id': self.tenant_id,
  222. 'pool': [variable.dict() for variable in self.pool],
  223. }
  224. def set_text(self, tool_name: str, name: str, value: str) -> None:
  225. """
  226. set a text variable
  227. """
  228. for variable in self.pool:
  229. if variable.name == name:
  230. if variable.type == ToolRuntimeVariableType.TEXT:
  231. variable = cast(ToolRuntimeTextVariable, variable)
  232. variable.value = value
  233. return
  234. variable = ToolRuntimeTextVariable(
  235. type=ToolRuntimeVariableType.TEXT,
  236. name=name,
  237. position=len(self.pool),
  238. tool_name=tool_name,
  239. value=value,
  240. )
  241. self.pool.append(variable)
  242. def set_file(self, tool_name: str, value: str, name: str = None) -> None:
  243. """
  244. set an image variable
  245. :param tool_name: the name of the tool
  246. :param value: the id of the file
  247. """
  248. # check how many image variables are there
  249. image_variable_count = 0
  250. for variable in self.pool:
  251. if variable.type == ToolRuntimeVariableType.IMAGE:
  252. image_variable_count += 1
  253. if name is None:
  254. name = f"file_{image_variable_count}"
  255. for variable in self.pool:
  256. if variable.name == name:
  257. if variable.type == ToolRuntimeVariableType.IMAGE:
  258. variable = cast(ToolRuntimeImageVariable, variable)
  259. variable.value = value
  260. return
  261. variable = ToolRuntimeImageVariable(
  262. type=ToolRuntimeVariableType.IMAGE,
  263. name=name,
  264. position=len(self.pool),
  265. tool_name=tool_name,
  266. value=value,
  267. )
  268. self.pool.append(variable)
  269. class ModelToolPropertyKey(Enum):
  270. IMAGE_PARAMETER_NAME = "image_parameter_name"
  271. class ModelToolConfiguration(BaseModel):
  272. """
  273. Model tool configuration
  274. """
  275. type: str = Field(..., description="The type of the model tool")
  276. model: str = Field(..., description="The model")
  277. label: I18nObject = Field(..., description="The label of the model tool")
  278. properties: dict[ModelToolPropertyKey, Any] = Field(..., description="The properties of the model tool")
  279. class ModelToolProviderConfiguration(BaseModel):
  280. """
  281. Model tool provider configuration
  282. """
  283. provider: str = Field(..., description="The provider of the model tool")
  284. models: list[ModelToolConfiguration] = Field(..., description="The models of the model tool")
  285. label: I18nObject = Field(..., description="The label of the model tool")
  286. class WorkflowToolParameterConfiguration(BaseModel):
  287. """
  288. Workflow tool configuration
  289. """
  290. name: str = Field(..., description="The name of the parameter")
  291. description: str = Field(..., description="The description of the parameter")
  292. form: ToolParameter.ToolParameterForm = Field(..., description="The form of the parameter")
  293. class ToolInvokeMeta(BaseModel):
  294. """
  295. Tool invoke meta
  296. """
  297. time_cost: float = Field(..., description="The time cost of the tool invoke")
  298. error: Optional[str] = None
  299. tool_config: Optional[dict] = None
  300. @classmethod
  301. def empty(cls) -> 'ToolInvokeMeta':
  302. """
  303. Get an empty instance of ToolInvokeMeta
  304. """
  305. return cls(time_cost=0.0, error=None, tool_config={})
  306. @classmethod
  307. def error_instance(cls, error: str) -> 'ToolInvokeMeta':
  308. """
  309. Get an instance of ToolInvokeMeta with error
  310. """
  311. return cls(time_cost=0.0, error=error, tool_config={})
  312. def to_dict(self) -> dict:
  313. return {
  314. 'time_cost': self.time_cost,
  315. 'error': self.error,
  316. 'tool_config': self.tool_config,
  317. }
  318. class ToolLabel(BaseModel):
  319. """
  320. Tool label
  321. """
  322. name: str = Field(..., description="The name of the tool")
  323. label: I18nObject = Field(..., description="The label of the tool")
  324. icon: str = Field(..., description="The icon of the tool")
  325. class ToolInvokeFrom(Enum):
  326. """
  327. Enum class for tool invoke
  328. """
  329. WORKFLOW = "workflow"
  330. AGENT = "agent"