您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tool_engine.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. import contextlib
  2. import json
  3. from collections.abc import Generator, Iterable
  4. from copy import deepcopy
  5. from datetime import UTC, datetime
  6. from mimetypes import guess_type
  7. from typing import Any, Optional, Union, cast
  8. from yarl import URL
  9. from core.app.entities.app_invoke_entities import InvokeFrom
  10. from core.callback_handler.agent_tool_callback_handler import DifyAgentCallbackHandler
  11. from core.callback_handler.workflow_tool_callback_handler import DifyWorkflowCallbackHandler
  12. from core.file import FileType
  13. from core.file.models import FileTransferMethod
  14. from core.ops.ops_trace_manager import TraceQueueManager
  15. from core.tools.__base.tool import Tool
  16. from core.tools.entities.tool_entities import (
  17. ToolInvokeMessage,
  18. ToolInvokeMessageBinary,
  19. ToolInvokeMeta,
  20. ToolParameter,
  21. )
  22. from core.tools.errors import (
  23. ToolEngineInvokeError,
  24. ToolInvokeError,
  25. ToolNotFoundError,
  26. ToolNotSupportedError,
  27. ToolParameterValidationError,
  28. ToolProviderCredentialValidationError,
  29. ToolProviderNotFoundError,
  30. )
  31. from core.tools.utils.message_transformer import ToolFileMessageTransformer, safe_json_value
  32. from core.tools.workflow_as_tool.tool import WorkflowTool
  33. from extensions.ext_database import db
  34. from models.enums import CreatorUserRole
  35. from models.model import Message, MessageFile
  36. class ToolEngine:
  37. """
  38. Tool runtime engine take care of the tool executions.
  39. """
  40. @staticmethod
  41. def agent_invoke(
  42. tool: Tool,
  43. tool_parameters: Union[str, dict],
  44. user_id: str,
  45. tenant_id: str,
  46. message: Message,
  47. invoke_from: InvokeFrom,
  48. agent_tool_callback: DifyAgentCallbackHandler,
  49. trace_manager: Optional[TraceQueueManager] = None,
  50. conversation_id: Optional[str] = None,
  51. app_id: Optional[str] = None,
  52. message_id: Optional[str] = None,
  53. ) -> tuple[str, list[str], ToolInvokeMeta]:
  54. """
  55. Agent invokes the tool with the given arguments.
  56. """
  57. # check if arguments is a string
  58. if isinstance(tool_parameters, str):
  59. # check if this tool has only one parameter
  60. parameters = [
  61. parameter
  62. for parameter in tool.get_runtime_parameters()
  63. if parameter.form == ToolParameter.ToolParameterForm.LLM
  64. ]
  65. if parameters and len(parameters) == 1:
  66. tool_parameters = {parameters[0].name: tool_parameters}
  67. else:
  68. with contextlib.suppress(Exception):
  69. tool_parameters = json.loads(tool_parameters)
  70. if not isinstance(tool_parameters, dict):
  71. raise ValueError(f"tool_parameters should be a dict, but got a string: {tool_parameters}")
  72. try:
  73. # hit the callback handler
  74. agent_tool_callback.on_tool_start(tool_name=tool.entity.identity.name, tool_inputs=tool_parameters)
  75. messages = ToolEngine._invoke(tool, tool_parameters, user_id, conversation_id, app_id, message_id)
  76. invocation_meta_dict: dict[str, ToolInvokeMeta] = {}
  77. def message_callback(
  78. invocation_meta_dict: dict, messages: Generator[ToolInvokeMessage | ToolInvokeMeta, None, None]
  79. ):
  80. for message in messages:
  81. if isinstance(message, ToolInvokeMeta):
  82. invocation_meta_dict["meta"] = message
  83. else:
  84. yield message
  85. messages = ToolFileMessageTransformer.transform_tool_invoke_messages(
  86. messages=message_callback(invocation_meta_dict, messages),
  87. user_id=user_id,
  88. tenant_id=tenant_id,
  89. conversation_id=message.conversation_id,
  90. )
  91. message_list = list(messages)
  92. # extract binary data from tool invoke message
  93. binary_files = ToolEngine._extract_tool_response_binary_and_text(message_list)
  94. # create message file
  95. message_files = ToolEngine._create_message_files(
  96. tool_messages=binary_files, agent_message=message, invoke_from=invoke_from, user_id=user_id
  97. )
  98. plain_text = ToolEngine._convert_tool_response_to_str(message_list)
  99. meta = invocation_meta_dict["meta"]
  100. # hit the callback handler
  101. agent_tool_callback.on_tool_end(
  102. tool_name=tool.entity.identity.name,
  103. tool_inputs=tool_parameters,
  104. tool_outputs=plain_text,
  105. message_id=message.id,
  106. trace_manager=trace_manager,
  107. )
  108. # transform tool invoke message to get LLM friendly message
  109. return plain_text, message_files, meta
  110. except ToolProviderCredentialValidationError as e:
  111. error_response = "Please check your tool provider credentials"
  112. agent_tool_callback.on_tool_error(e)
  113. except (ToolNotFoundError, ToolNotSupportedError, ToolProviderNotFoundError) as e:
  114. error_response = f"there is not a tool named {tool.entity.identity.name}"
  115. agent_tool_callback.on_tool_error(e)
  116. except ToolParameterValidationError as e:
  117. error_response = f"tool parameters validation error: {e}, please check your tool parameters"
  118. agent_tool_callback.on_tool_error(e)
  119. except ToolInvokeError as e:
  120. error_response = f"tool invoke error: {e}"
  121. agent_tool_callback.on_tool_error(e)
  122. except ToolEngineInvokeError as e:
  123. meta = e.meta
  124. error_response = f"tool invoke error: {meta.error}"
  125. agent_tool_callback.on_tool_error(e)
  126. return error_response, [], meta
  127. except Exception as e:
  128. error_response = f"unknown error: {e}"
  129. agent_tool_callback.on_tool_error(e)
  130. return error_response, [], ToolInvokeMeta.error_instance(error_response)
  131. @staticmethod
  132. def generic_invoke(
  133. tool: Tool,
  134. tool_parameters: dict[str, Any],
  135. user_id: str,
  136. workflow_tool_callback: DifyWorkflowCallbackHandler,
  137. workflow_call_depth: int,
  138. thread_pool_id: Optional[str] = None,
  139. conversation_id: Optional[str] = None,
  140. app_id: Optional[str] = None,
  141. message_id: Optional[str] = None,
  142. ) -> Generator[ToolInvokeMessage, None, None]:
  143. """
  144. Workflow invokes the tool with the given arguments.
  145. """
  146. try:
  147. # hit the callback handler
  148. workflow_tool_callback.on_tool_start(tool_name=tool.entity.identity.name, tool_inputs=tool_parameters)
  149. if isinstance(tool, WorkflowTool):
  150. tool.workflow_call_depth = workflow_call_depth + 1
  151. tool.thread_pool_id = thread_pool_id
  152. if tool.runtime and tool.runtime.runtime_parameters:
  153. tool_parameters = {**tool.runtime.runtime_parameters, **tool_parameters}
  154. response = tool.invoke(
  155. user_id=user_id,
  156. tool_parameters=tool_parameters,
  157. conversation_id=conversation_id,
  158. app_id=app_id,
  159. message_id=message_id,
  160. )
  161. # hit the callback handler
  162. response = workflow_tool_callback.on_tool_execution(
  163. tool_name=tool.entity.identity.name,
  164. tool_inputs=tool_parameters,
  165. tool_outputs=response,
  166. )
  167. return response
  168. except Exception as e:
  169. workflow_tool_callback.on_tool_error(e)
  170. raise e
  171. @staticmethod
  172. def _invoke(
  173. tool: Tool,
  174. tool_parameters: dict,
  175. user_id: str,
  176. conversation_id: Optional[str] = None,
  177. app_id: Optional[str] = None,
  178. message_id: Optional[str] = None,
  179. ) -> Generator[ToolInvokeMessage | ToolInvokeMeta, None, None]:
  180. """
  181. Invoke the tool with the given arguments.
  182. """
  183. started_at = datetime.now(UTC)
  184. meta = ToolInvokeMeta(
  185. time_cost=0.0,
  186. error=None,
  187. tool_config={
  188. "tool_name": tool.entity.identity.name,
  189. "tool_provider": tool.entity.identity.provider,
  190. "tool_provider_type": tool.tool_provider_type().value,
  191. "tool_parameters": deepcopy(tool.runtime.runtime_parameters),
  192. "tool_icon": tool.entity.identity.icon,
  193. },
  194. )
  195. try:
  196. yield from tool.invoke(user_id, tool_parameters, conversation_id, app_id, message_id)
  197. except Exception as e:
  198. meta.error = str(e)
  199. raise ToolEngineInvokeError(meta)
  200. finally:
  201. ended_at = datetime.now(UTC)
  202. meta.time_cost = (ended_at - started_at).total_seconds()
  203. yield meta
  204. @staticmethod
  205. def _convert_tool_response_to_str(tool_response: list[ToolInvokeMessage]) -> str:
  206. """
  207. Handle tool response
  208. """
  209. result = ""
  210. for response in tool_response:
  211. if response.type == ToolInvokeMessage.MessageType.TEXT:
  212. result += cast(ToolInvokeMessage.TextMessage, response.message).text
  213. elif response.type == ToolInvokeMessage.MessageType.LINK:
  214. result += (
  215. f"result link: {cast(ToolInvokeMessage.TextMessage, response.message).text}."
  216. + " please tell user to check it."
  217. )
  218. elif response.type in {ToolInvokeMessage.MessageType.IMAGE_LINK, ToolInvokeMessage.MessageType.IMAGE}:
  219. result += (
  220. "image has been created and sent to user already, "
  221. + "you do not need to create it, just tell the user to check it now."
  222. )
  223. elif response.type == ToolInvokeMessage.MessageType.JSON:
  224. result += json.dumps(
  225. safe_json_value(cast(ToolInvokeMessage.JsonMessage, response.message).json_object),
  226. ensure_ascii=False,
  227. )
  228. else:
  229. result += str(response.message)
  230. return result
  231. @staticmethod
  232. def _extract_tool_response_binary_and_text(
  233. tool_response: list[ToolInvokeMessage],
  234. ) -> Generator[ToolInvokeMessageBinary, None, None]:
  235. """
  236. Extract tool response binary
  237. """
  238. for response in tool_response:
  239. if response.type in {ToolInvokeMessage.MessageType.IMAGE_LINK, ToolInvokeMessage.MessageType.IMAGE}:
  240. mimetype = None
  241. if not response.meta:
  242. raise ValueError("missing meta data")
  243. if response.meta.get("mime_type"):
  244. mimetype = response.meta.get("mime_type")
  245. else:
  246. with contextlib.suppress(Exception):
  247. url = URL(cast(ToolInvokeMessage.TextMessage, response.message).text)
  248. extension = url.suffix
  249. guess_type_result, _ = guess_type(f"a{extension}")
  250. if guess_type_result:
  251. mimetype = guess_type_result
  252. if not mimetype:
  253. mimetype = "image/jpeg"
  254. yield ToolInvokeMessageBinary(
  255. mimetype=response.meta.get("mime_type", mimetype),
  256. url=cast(ToolInvokeMessage.TextMessage, response.message).text,
  257. )
  258. elif response.type == ToolInvokeMessage.MessageType.BLOB:
  259. if not response.meta:
  260. raise ValueError("missing meta data")
  261. yield ToolInvokeMessageBinary(
  262. mimetype=response.meta.get("mime_type", "application/octet-stream"),
  263. url=cast(ToolInvokeMessage.TextMessage, response.message).text,
  264. )
  265. elif response.type == ToolInvokeMessage.MessageType.LINK:
  266. # check if there is a mime type in meta
  267. if response.meta and "mime_type" in response.meta:
  268. yield ToolInvokeMessageBinary(
  269. mimetype=response.meta.get("mime_type", "application/octet-stream")
  270. if response.meta
  271. else "application/octet-stream",
  272. url=cast(ToolInvokeMessage.TextMessage, response.message).text,
  273. )
  274. @staticmethod
  275. def _create_message_files(
  276. tool_messages: Iterable[ToolInvokeMessageBinary],
  277. agent_message: Message,
  278. invoke_from: InvokeFrom,
  279. user_id: str,
  280. ) -> list[str]:
  281. """
  282. Create message file
  283. :return: message file ids
  284. """
  285. result = []
  286. for message in tool_messages:
  287. if "image" in message.mimetype:
  288. file_type = FileType.IMAGE
  289. elif "video" in message.mimetype:
  290. file_type = FileType.VIDEO
  291. elif "audio" in message.mimetype:
  292. file_type = FileType.AUDIO
  293. elif "text" in message.mimetype or "pdf" in message.mimetype:
  294. file_type = FileType.DOCUMENT
  295. else:
  296. file_type = FileType.CUSTOM
  297. # extract tool file id from url
  298. tool_file_id = message.url.split("/")[-1].split(".")[0]
  299. message_file = MessageFile(
  300. message_id=agent_message.id,
  301. type=file_type,
  302. transfer_method=FileTransferMethod.TOOL_FILE,
  303. belongs_to="assistant",
  304. url=message.url,
  305. upload_file_id=tool_file_id,
  306. created_by_role=(
  307. CreatorUserRole.ACCOUNT
  308. if invoke_from in {InvokeFrom.EXPLORE, InvokeFrom.DEBUGGER}
  309. else CreatorUserRole.END_USER
  310. ),
  311. created_by=user_id,
  312. )
  313. db.session.add(message_file)
  314. db.session.commit()
  315. db.session.refresh(message_file)
  316. result.append(message_file.id)
  317. db.session.close()
  318. return result