tool.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. from abc import ABC, abstractmethod
  2. from enum import Enum
  3. from typing import Any, Dict, List, Optional, Union
  4. from core.callback_handler.agent_tool_callback_handler import DifyAgentCallbackHandler
  5. from core.tools.entities.tool_entities import (ToolDescription, ToolIdentity, ToolInvokeMessage, ToolParameter,
  6. ToolRuntimeImageVariable, ToolRuntimeVariable, ToolRuntimeVariablePool)
  7. from core.tools.tool_file_manager import ToolFileManager
  8. from pydantic import BaseModel
  9. class Tool(BaseModel, ABC):
  10. identity: ToolIdentity = None
  11. parameters: Optional[List[ToolParameter]] = None
  12. description: ToolDescription = None
  13. is_team_authorization: bool = False
  14. agent_callback: Optional[DifyAgentCallbackHandler] = None
  15. use_callback: bool = False
  16. class Runtime(BaseModel):
  17. """
  18. Meta data of a tool call processing
  19. """
  20. def __init__(self, **data: Any):
  21. super().__init__(**data)
  22. if not self.runtime_parameters:
  23. self.runtime_parameters = {}
  24. tenant_id: str = None
  25. tool_id: str = None
  26. credentials: Dict[str, Any] = None
  27. runtime_parameters: Dict[str, Any] = None
  28. runtime: Runtime = None
  29. variables: ToolRuntimeVariablePool = None
  30. def __init__(self, **data: Any):
  31. super().__init__(**data)
  32. if not self.agent_callback:
  33. self.use_callback = False
  34. else:
  35. self.use_callback = True
  36. class VARIABLE_KEY(Enum):
  37. IMAGE = 'image'
  38. def fork_tool_runtime(self, meta: Dict[str, Any], agent_callback: DifyAgentCallbackHandler = None) -> 'Tool':
  39. """
  40. fork a new tool with meta data
  41. :param meta: the meta data of a tool call processing, tenant_id is required
  42. :return: the new tool
  43. """
  44. return self.__class__(
  45. identity=self.identity.copy() if self.identity else None,
  46. parameters=self.parameters.copy() if self.parameters else None,
  47. description=self.description.copy() if self.description else None,
  48. runtime=Tool.Runtime(**meta),
  49. agent_callback=agent_callback
  50. )
  51. def load_variables(self, variables: ToolRuntimeVariablePool):
  52. """
  53. load variables from database
  54. :param conversation_id: the conversation id
  55. """
  56. self.variables = variables
  57. def set_image_variable(self, variable_name: str, image_key: str) -> None:
  58. """
  59. set an image variable
  60. """
  61. if not self.variables:
  62. return
  63. self.variables.set_file(self.identity.name, variable_name, image_key)
  64. def set_text_variable(self, variable_name: str, text: str) -> None:
  65. """
  66. set a text variable
  67. """
  68. if not self.variables:
  69. return
  70. self.variables.set_text(self.identity.name, variable_name, text)
  71. def get_variable(self, name: Union[str, Enum]) -> Optional[ToolRuntimeVariable]:
  72. """
  73. get a variable
  74. :param name: the name of the variable
  75. :return: the variable
  76. """
  77. if not self.variables:
  78. return None
  79. if isinstance(name, Enum):
  80. name = name.value
  81. for variable in self.variables.pool:
  82. if variable.name == name:
  83. return variable
  84. return None
  85. def get_default_image_variable(self) -> Optional[ToolRuntimeVariable]:
  86. """
  87. get the default image variable
  88. :return: the image variable
  89. """
  90. if not self.variables:
  91. return None
  92. return self.get_variable(self.VARIABLE_KEY.IMAGE)
  93. def get_variable_file(self, name: Union[str, Enum]) -> Optional[bytes]:
  94. """
  95. get a variable file
  96. :param name: the name of the variable
  97. :return: the variable file
  98. """
  99. variable = self.get_variable(name)
  100. if not variable:
  101. return None
  102. if not isinstance(variable, ToolRuntimeImageVariable):
  103. return None
  104. message_file_id = variable.value
  105. # get file binary
  106. file_binary = ToolFileManager.get_file_binary_by_message_file_id(message_file_id)
  107. if not file_binary:
  108. return None
  109. return file_binary[0]
  110. def list_variables(self) -> List[ToolRuntimeVariable]:
  111. """
  112. list all variables
  113. :return: the variables
  114. """
  115. if not self.variables:
  116. return []
  117. return self.variables.pool
  118. def list_default_image_variables(self) -> List[ToolRuntimeVariable]:
  119. """
  120. list all image variables
  121. :return: the image variables
  122. """
  123. if not self.variables:
  124. return []
  125. result = []
  126. for variable in self.variables.pool:
  127. if variable.name.startswith(self.VARIABLE_KEY.IMAGE.value):
  128. result.append(variable)
  129. return result
  130. def invoke(self, user_id: str, tool_parameters: Dict[str, Any]) -> List[ToolInvokeMessage]:
  131. # update tool_parameters
  132. if self.runtime.runtime_parameters:
  133. tool_parameters.update(self.runtime.runtime_parameters)
  134. # hit callback
  135. if self.use_callback:
  136. self.agent_callback.on_tool_start(
  137. tool_name=self.identity.name,
  138. tool_inputs=tool_parameters
  139. )
  140. try:
  141. result = self._invoke(
  142. user_id=user_id,
  143. tool_parameters=tool_parameters,
  144. )
  145. except Exception as e:
  146. if self.use_callback:
  147. self.agent_callback.on_tool_error(e)
  148. raise e
  149. if not isinstance(result, list):
  150. result = [result]
  151. # hit callback
  152. if self.use_callback:
  153. self.agent_callback.on_tool_end(
  154. tool_name=self.identity.name,
  155. tool_inputs=tool_parameters,
  156. tool_outputs=self._convert_tool_response_to_str(result)
  157. )
  158. return result
  159. def _convert_tool_response_to_str(self, tool_response: List[ToolInvokeMessage]) -> str:
  160. """
  161. Handle tool response
  162. """
  163. result = ''
  164. for response in tool_response:
  165. if response.type == ToolInvokeMessage.MessageType.TEXT:
  166. result += response.message
  167. elif response.type == ToolInvokeMessage.MessageType.LINK:
  168. result += f"result link: {response.message}. please tell user to check it."
  169. elif response.type == ToolInvokeMessage.MessageType.IMAGE_LINK or \
  170. response.type == ToolInvokeMessage.MessageType.IMAGE:
  171. result += f"image has been created and sent to user already, you should tell user to check it now."
  172. elif response.type == ToolInvokeMessage.MessageType.BLOB:
  173. if len(response.message) > 114:
  174. result += str(response.message[:114]) + '...'
  175. else:
  176. result += str(response.message)
  177. else:
  178. result += f"tool response: {response.message}."
  179. return result
  180. @abstractmethod
  181. def _invoke(self, user_id: str, tool_parameters: Dict[str, Any]) -> Union[ToolInvokeMessage, List[ToolInvokeMessage]]:
  182. pass
  183. def validate_credentials(self, credentials: Dict[str, Any], parameters: Dict[str, Any]) -> None:
  184. """
  185. validate the credentials
  186. :param credentials: the credentials
  187. :param parameters: the parameters
  188. """
  189. pass
  190. def get_runtime_parameters(self) -> List[ToolParameter]:
  191. """
  192. get the runtime parameters
  193. interface for developer to dynamic change the parameters of a tool depends on the variables pool
  194. :return: the runtime parameters
  195. """
  196. return self.parameters
  197. def is_tool_available(self) -> bool:
  198. """
  199. check if the tool is available
  200. :return: if the tool is available
  201. """
  202. return True
  203. def create_image_message(self, image: str, save_as: str = '') -> ToolInvokeMessage:
  204. """
  205. create an image message
  206. :param image: the url of the image
  207. :return: the image message
  208. """
  209. return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.IMAGE,
  210. message=image,
  211. save_as=save_as)
  212. def create_link_message(self, link: str, save_as: str = '') -> ToolInvokeMessage:
  213. """
  214. create a link message
  215. :param link: the url of the link
  216. :return: the link message
  217. """
  218. return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.LINK,
  219. message=link,
  220. save_as=save_as)
  221. def create_text_message(self, text: str, save_as: str = '') -> ToolInvokeMessage:
  222. """
  223. create a text message
  224. :param text: the text
  225. :return: the text message
  226. """
  227. return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.TEXT,
  228. message=text,
  229. save_as=save_as
  230. )
  231. def create_blob_message(self, blob: bytes, meta: dict = None, save_as: str = '') -> ToolInvokeMessage:
  232. """
  233. create a blob message
  234. :param blob: the blob
  235. :return: the blob message
  236. """
  237. return ToolInvokeMessage(type=ToolInvokeMessage.MessageType.BLOB,
  238. message=blob, meta=meta,
  239. save_as=save_as
  240. )