plugin_invoke_service.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from collections.abc import Generator
  2. from typing import Any, Union
  3. from core.app.entities.app_invoke_entities import InvokeFrom
  4. from core.callback_handler.plugin_tool_callback_handler import DifyPluginCallbackHandler
  5. from core.model_runtime.entities.model_entities import ModelType
  6. from core.tools.entities.tool_entities import ToolInvokeMessage, ToolProviderType
  7. from core.tools.tool_engine import ToolEngine
  8. from core.tools.tool_manager import ToolManager
  9. from core.tools.utils.message_transformer import ToolFileMessageTransformer
  10. from core.workflow.entities.node_entities import NodeType
  11. from models.account import Tenant
  12. from services.tools.tools_transform_service import ToolTransformService
  13. class PluginInvokeService:
  14. @classmethod
  15. def invoke_tool(cls, user_id: str, invoke_from: InvokeFrom, tenant: Tenant,
  16. tool_provider_type: ToolProviderType, tool_provider: str, tool_name: str,
  17. tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]:
  18. """
  19. Invokes a tool with the given user ID and tool parameters.
  20. """
  21. tool_runtime = ToolManager.get_tool_runtime(tool_provider_type, provider_id=tool_provider,
  22. tool_name=tool_name, tenant_id=tenant.id,
  23. invoke_from=invoke_from)
  24. response = ToolEngine.plugin_invoke(tool_runtime,
  25. tool_parameters,
  26. user_id,
  27. callback=DifyPluginCallbackHandler())
  28. response = ToolFileMessageTransformer.transform_tool_invoke_messages(response)
  29. return ToolTransformService.transform_messages_to_dict(response)
  30. @classmethod
  31. def invoke_model(cls, user_id: str, tenant: Tenant,
  32. model_provider: str, model_name: str, model_type: ModelType,
  33. model_parameters: dict[str, Any]) -> Union[dict, Generator[ToolInvokeMessage]]:
  34. """
  35. Invokes a model with the given user ID and model parameters.
  36. """
  37. @classmethod
  38. def invoke_workflow_node(cls, user_id: str, tenant: Tenant,
  39. node_type: NodeType, node_data: dict[str, Any],
  40. inputs: dict[str, Any]) -> Generator[ToolInvokeMessage]:
  41. """
  42. Invokes a workflow node with the given user ID and node parameters.
  43. """