From 5415dbafa9a3eacb89c0ff6880ad887282ba052f Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Wed, 25 Jun 2025 15:03:28 +0800 Subject: [PATCH] feat: enhance plugin functionality with dynamic parameter options and trigger interface - Added dynamic parameter fetching capabilities to the Plugin class. - Introduced TriggerProvider and Trigger classes for managing trigger functionalities. - Updated Tool interface to enforce implementation of credential validation and OAuth methods. - Enhanced error messages for NotImplementedError in ToolProvider and Trigger classes. - Removed deprecated tools and their configurations from the codebase. --- .../dify_plugin/interfaces/tool/__init__.py | 12 ++- .../interfaces/trigger/__init__.py | 99 +++++++++++++++++++ python/dify_plugin/plugin.py | 7 ++ python/examples/code_based_workflow/.env | 2 +- .../examples/code_based_workflow/.installed | 0 .../provider/code_based_workflow.yaml | 9 -- .../examples/code_based_workflow/tools/llm.py | 9 ++ .../code_based_workflow/tools/llm.yaml | 21 +++- .../code_based_workflow/tools/moderation.py | 22 ----- .../code_based_workflow/tools/moderation.yaml | 30 ------ .../tools/parameter_extractor.py | 28 ------ .../tools/parameter_extractor.yaml | 30 ------ .../tools/question_classifier.py | 9 -- .../tools/question_classifier.yaml | 30 ------ .../code_based_workflow/tools/rerank.py | 25 ----- .../code_based_workflow/tools/rerank.yaml | 17 ---- .../code_based_workflow/tools/summary.py | 19 ---- .../code_based_workflow/tools/summary.yaml | 17 ---- .../code_based_workflow/tools/tool.py | 9 -- .../code_based_workflow/tools/tool.yaml | 16 --- .../tools/transform_image.py | 19 ---- .../tools/transform_image.yaml | 30 ------ .../examples/code_based_workflow/tools/tts.py | 21 ---- .../code_based_workflow/tools/tts.yaml | 17 ---- .../code_based_workflow/tools/upload_file.py | 10 -- .../tools/upload_file.yaml | 16 --- 26 files changed, 144 insertions(+), 380 deletions(-) create mode 100644 python/dify_plugin/interfaces/trigger/__init__.py delete mode 100644 python/examples/code_based_workflow/.installed delete mode 100644 python/examples/code_based_workflow/tools/moderation.py delete mode 100644 python/examples/code_based_workflow/tools/moderation.yaml delete mode 100644 python/examples/code_based_workflow/tools/parameter_extractor.py delete mode 100644 python/examples/code_based_workflow/tools/parameter_extractor.yaml delete mode 100644 python/examples/code_based_workflow/tools/question_classifier.py delete mode 100644 python/examples/code_based_workflow/tools/question_classifier.yaml delete mode 100644 python/examples/code_based_workflow/tools/rerank.py delete mode 100644 python/examples/code_based_workflow/tools/rerank.yaml delete mode 100644 python/examples/code_based_workflow/tools/summary.py delete mode 100644 python/examples/code_based_workflow/tools/summary.yaml delete mode 100644 python/examples/code_based_workflow/tools/tool.py delete mode 100644 python/examples/code_based_workflow/tools/tool.yaml delete mode 100644 python/examples/code_based_workflow/tools/transform_image.py delete mode 100644 python/examples/code_based_workflow/tools/transform_image.yaml delete mode 100644 python/examples/code_based_workflow/tools/tts.py delete mode 100644 python/examples/code_based_workflow/tools/tts.yaml delete mode 100644 python/examples/code_based_workflow/tools/upload_file.py delete mode 100644 python/examples/code_based_workflow/tools/upload_file.yaml diff --git a/python/dify_plugin/interfaces/tool/__init__.py b/python/dify_plugin/interfaces/tool/__init__.py index 18a9f49c..b6f69c75 100644 --- a/python/dify_plugin/interfaces/tool/__init__.py +++ b/python/dify_plugin/interfaces/tool/__init__.py @@ -232,19 +232,21 @@ class ToolProvider: return self._validate_credentials(credentials) def _validate_credentials(self, credentials: dict): - raise NotImplementedError("This method should be implemented by a subclass") + raise NotImplementedError( + "This plugin should implement `_validate_credentials` method to enable credentials validation" + ) def oauth_get_authorization_url(self, system_credentials: Mapping[str, Any]) -> str: return self._oauth_get_authorization_url(system_credentials) def _oauth_get_authorization_url(self, system_credentials: Mapping[str, Any]) -> str: - raise NotImplementedError("This method should be implemented by a subclass") + raise NotImplementedError("This plugin should implement `_oauth_get_authorization_url` method to enable oauth") def oauth_get_credentials(self, system_credentials: Mapping[str, Any], request: Request) -> Mapping[str, Any]: return self._oauth_get_credentials(system_credentials, request) def _oauth_get_credentials(self, system_credentials: Mapping[str, Any], request: Request) -> Mapping[str, Any]: - raise NotImplementedError("This method should be implemented by a subclass") + raise NotImplementedError("This plugin should implement `_oauth_get_credentials` method to enable oauth") class Tool(ToolLike[ToolInvokeMessage]): @@ -294,7 +296,9 @@ class Tool(ToolLike[ToolInvokeMessage]): Also, it's optional to implement, that's why it's not an abstract method. """ - raise NotImplementedError("This method should be implemented by a subclass") + raise NotImplementedError( + "This plugin should implement `_fetch_parameter_options` method to enable dynamic select parameter" + ) ############################################################ # For executor use only # diff --git a/python/dify_plugin/interfaces/trigger/__init__.py b/python/dify_plugin/interfaces/trigger/__init__.py new file mode 100644 index 00000000..83e87b03 --- /dev/null +++ b/python/dify_plugin/interfaces/trigger/__init__.py @@ -0,0 +1,99 @@ +from abc import ABC, abstractmethod +from collections.abc import Mapping +from typing import Any, final + +from werkzeug import Request + +from dify_plugin.core.runtime import Session +from dify_plugin.entities import ParameterOption +from dify_plugin.entities.trigger import TriggerResponse, TriggerRuntime + + +class TriggerProvider: + """ + The provider of a trigger + """ + + def validate_credentials(self, credentials: dict): + return self._validate_credentials(credentials) + + def _validate_credentials(self, credentials: dict): + raise NotImplementedError( + "This plugin should implement `_validate_credentials` method to enable credentials validation" + ) + + def oauth_get_authorization_url(self, system_credentials: Mapping[str, Any]) -> str: + return self._oauth_get_authorization_url(system_credentials) + + def _oauth_get_authorization_url(self, system_credentials: Mapping[str, Any]) -> str: + raise NotImplementedError("This plugin should implement `_oauth_get_authorization_url` method to enable oauth") + + def oauth_get_credentials(self, system_credentials: Mapping[str, Any], request: Request) -> Mapping[str, Any]: + return self._oauth_get_credentials(system_credentials, request) + + def _oauth_get_credentials(self, system_credentials: Mapping[str, Any], request: Request) -> Mapping[str, Any]: + raise NotImplementedError("This plugin should implement `_oauth_get_credentials` method to enable oauth") + + +class Trigger(ABC): + """ + The trigger interface + """ + + runtime: TriggerRuntime + session: Session + + @final + def __init__( + self, + runtime: TriggerRuntime, + session: Session, + ): + """ + Initialize the trigger + + NOTE: + - This method has been marked as final, DO NOT OVERRIDE IT. + """ + self.runtime = runtime + self.session = session + + ############################################################ + # Methods that can be implemented by plugin # + ############################################################ + + @abstractmethod + def _trigger(self, request: Request, values: Mapping, parameters: Mapping) -> TriggerResponse: + """ + Trigger the trigger with the given request. + + To be implemented by subclasses. + """ + + def _fetch_parameter_options(self, parameter: str) -> list[ParameterOption]: + """ + Fetch the parameter options of the trigger. + + To be implemented by subclasses. + + Also, it's optional to implement, that's why it's not an abstract method. + """ + raise NotImplementedError( + "This plugin should implement `_fetch_parameter_options` method to enable dynamic select parameter" + ) + + ############################################################ + # For executor use only # + ############################################################ + + def trigger(self, request: Request, values: Mapping, parameters: Mapping) -> TriggerResponse: + """ + Trigger the trigger with the given request. + """ + return self._trigger(request, values, parameters) + + def fetch_parameter_options(self, parameter: str) -> list[ParameterOption]: + """ + Fetch the parameter options of the trigger. + """ + return self._fetch_parameter_options(parameter) diff --git a/python/dify_plugin/plugin.py b/python/dify_plugin/plugin.py index aae0daf9..6328a7c6 100644 --- a/python/dify_plugin/plugin.py +++ b/python/dify_plugin/plugin.py @@ -12,6 +12,7 @@ from dify_plugin.config.logger_format import plugin_logger_handler from dify_plugin.core.entities.message import InitializeMessage from dify_plugin.core.entities.plugin.request import ( AgentActions, + DynamicParameterActions, EndpointActions, ModelActions, PluginInvokeType, @@ -302,6 +303,12 @@ class Plugin(IOServer, Router): and data.get("action") == ModelActions.GetAIModelSchemas.value, ) + self.register_route( + self.plugin_executer.fetch_parameter_options, + lambda data: data.get("type") == PluginInvokeType.DynamicParameter.value + and data.get("action") == DynamicParameterActions.FetchParameterOptions.value, + ) + def _execute_request( self, session_id: str, diff --git a/python/examples/code_based_workflow/.env b/python/examples/code_based_workflow/.env index aee0ed69..52be5689 100644 --- a/python/examples/code_based_workflow/.env +++ b/python/examples/code_based_workflow/.env @@ -1,4 +1,4 @@ INSTALL_METHOD=remote REMOTE_INSTALL_HOST=localhost REMOTE_INSTALL_PORT=5003 -REMOTE_INSTALL_KEY=23620d39-d006-4613-85be-844874ed5d89 \ No newline at end of file +REMOTE_INSTALL_KEY=1fbe8953-7bf7-4d77-8916-ee217f7068df \ No newline at end of file diff --git a/python/examples/code_based_workflow/.installed b/python/examples/code_based_workflow/.installed deleted file mode 100644 index e69de29b..00000000 diff --git a/python/examples/code_based_workflow/provider/code_based_workflow.yaml b/python/examples/code_based_workflow/provider/code_based_workflow.yaml index ac03a24e..4f00c83d 100644 --- a/python/examples/code_based_workflow/provider/code_based_workflow.yaml +++ b/python/examples/code_based_workflow/provider/code_based_workflow.yaml @@ -13,16 +13,7 @@ identity: tags: - search tools: - - tools/parameter_extractor.yaml - - tools/question_classifier.yaml - tools/llm.yaml - - tools/tool.yaml - - tools/moderation.yaml - - tools/rerank.yaml - - tools/tts.yaml - - tools/upload_file.yaml - - tools/summary.yaml - - tools/transform_image.yaml extra: python: source: provider/code_based_workflow.py diff --git a/python/examples/code_based_workflow/tools/llm.py b/python/examples/code_based_workflow/tools/llm.py index 2df6794b..d356545e 100644 --- a/python/examples/code_based_workflow/tools/llm.py +++ b/python/examples/code_based_workflow/tools/llm.py @@ -2,12 +2,21 @@ from collections.abc import Generator from typing import Any from dify_plugin import Tool +from dify_plugin.entities import I18nObject, ParameterOption from dify_plugin.entities.model.llm import LLMModelConfig from dify_plugin.entities.model.message import SystemPromptMessage, UserPromptMessage from dify_plugin.entities.tool import ToolInvokeMessage class LLMTool(Tool): + def _fetch_parameter_options(self, parameter: str) -> list[ParameterOption]: + return [ + ParameterOption( + value="hello world", + label=I18nObject(en_US="Prompt string"), + ) + ] + def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessage]: response = self.session.model.llm.invoke( model_config=LLMModelConfig( diff --git a/python/examples/code_based_workflow/tools/llm.yaml b/python/examples/code_based_workflow/tools/llm.yaml index 47ece198..ef8b58d2 100644 --- a/python/examples/code_based_workflow/tools/llm.yaml +++ b/python/examples/code_based_workflow/tools/llm.yaml @@ -13,7 +13,7 @@ description: llm: A tool for invoking a large language model parameters: - name: prompt - type: string + type: dynamic-select required: true label: en_US: Prompt string @@ -25,6 +25,25 @@ parameters: pt_BR: used for searching llm_description: key words for searching form: llm + - name: prompt1 + type: select + required: true + options: + - value: 1 + label: + en_US: Prompt string + zh_Hans: 提示字符串 + pt_BR: Prompt string + label: + en_US: Prompt string + zh_Hans: 提示字符串 + pt_BR: Prompt string + human_description: + en_US: used for searching + zh_Hans: 用于搜索网页内容 + pt_BR: used for searching + llm_description: key words for searching + form: llm extra: python: source: tools/llm.py diff --git a/python/examples/code_based_workflow/tools/moderation.py b/python/examples/code_based_workflow/tools/moderation.py deleted file mode 100644 index 79a6af6f..00000000 --- a/python/examples/code_based_workflow/tools/moderation.py +++ /dev/null @@ -1,22 +0,0 @@ -from collections.abc import Generator - -from dify_plugin.entities.model.moderation import ModerationModelConfig -from dify_plugin.entities.tool import ToolInvokeMessage -from dify_plugin.interfaces.tool import Tool - - -class Moderation(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - response = self.session.model.moderation.invoke( - model_config=ModerationModelConfig( - provider="openai", - model="text-moderation-stable", - ), - text=tool_parameters["text"], - ) - - yield self.create_json_message( - { - "data": response, - } - ) diff --git a/python/examples/code_based_workflow/tools/moderation.yaml b/python/examples/code_based_workflow/tools/moderation.yaml deleted file mode 100644 index e466ba87..00000000 --- a/python/examples/code_based_workflow/tools/moderation.yaml +++ /dev/null @@ -1,30 +0,0 @@ -identity: - name: moderation - author: Dify - label: - en_US: Moderation - zh_Hans: 内容审核 - pt_BR: Moderation -description: - human: - en_US: A tool for moderation - zh_Hans: 用于内容审核的工具 - pt_BR: A tool for moderation - llm: A tool for moderation -parameters: - - name: text - type: string - required: true - label: - en_US: Text to moderate - zh_Hans: 要审核的文本 - pt_BR: Text to moderate - human_description: - en_US: Text to moderate - zh_Hans: 要审核的文本 - pt_BR: Text to moderate - llm_description: Text to moderate - form: llm -extra: - python: - source: tools/moderation.py diff --git a/python/examples/code_based_workflow/tools/parameter_extractor.py b/python/examples/code_based_workflow/tools/parameter_extractor.py deleted file mode 100644 index 17ee8a6d..00000000 --- a/python/examples/code_based_workflow/tools/parameter_extractor.py +++ /dev/null @@ -1,28 +0,0 @@ -from collections.abc import Generator - -from dify_plugin import Tool -from dify_plugin.entities.tool import ToolInvokeMessage -from dify_plugin.entities.workflow_node import ModelConfig, ParameterConfig - - -class ParameterExtractorTool(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - response = self.session.workflow_node.parameter_extractor.invoke( - parameters=[ - ParameterConfig( - name="name", - description="name of the person", - required=True, - type="string", - ) - ], - model=ModelConfig( - provider="openai", - name="gpt-4o-mini", - completion_params={}, - ), - query="My name is John Doe", - instruction="Extract the name of the person", - ) - - yield self.create_json_message(response.outputs) diff --git a/python/examples/code_based_workflow/tools/parameter_extractor.yaml b/python/examples/code_based_workflow/tools/parameter_extractor.yaml deleted file mode 100644 index 5ca92ef0..00000000 --- a/python/examples/code_based_workflow/tools/parameter_extractor.yaml +++ /dev/null @@ -1,30 +0,0 @@ -identity: - name: parameter_extractor - author: Dify - label: - en_US: Parameter Extractor - zh_Hans: 参数提取器 - pt_BR: Parameter Extractor -description: - human: - en_US: A tool for extracting parameters from a given string - zh_Hans: 从给定字符串中提取参数的工具 - pt_BR: A tool for extracting parameters from a given string - llm: A tool for extracting parameters from a given string -parameters: - - name: prompt - type: string - required: true - label: - en_US: Prompt string - zh_Hans: 提示字符串 - pt_BR: Prompt string - human_description: - en_US: used for extracting parameters - zh_Hans: 用于提取参数 - pt_BR: used for extracting parameters - llm_description: key words for extracting parameters - form: llm -extra: - python: - source: tools/parameter_extractor.py diff --git a/python/examples/code_based_workflow/tools/question_classifier.py b/python/examples/code_based_workflow/tools/question_classifier.py deleted file mode 100644 index 9499b39a..00000000 --- a/python/examples/code_based_workflow/tools/question_classifier.py +++ /dev/null @@ -1,9 +0,0 @@ -from collections.abc import Generator - -from dify_plugin import Tool -from dify_plugin.entities.tool import ToolInvokeMessage - - -class QuestionClassifierTool(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - return super()._invoke(tool_parameters) diff --git a/python/examples/code_based_workflow/tools/question_classifier.yaml b/python/examples/code_based_workflow/tools/question_classifier.yaml deleted file mode 100644 index 6b6474c6..00000000 --- a/python/examples/code_based_workflow/tools/question_classifier.yaml +++ /dev/null @@ -1,30 +0,0 @@ -identity: - name: question_classifier - author: Dify - label: - en_US: Question Classifier - zh_Hans: 问题分类器 - pt_BR: Question Classifier -description: - human: - en_US: A tool for classifying questions - zh_Hans: 用于分类问题的工具 - pt_BR: A tool for classifying questions - llm: A tool for classifying questions -parameters: - - name: prompt - type: string - required: true - label: - en_US: Prompt string - zh_Hans: 提示字符串 - pt_BR: Prompt string - human_description: - en_US: used for classifying questions - zh_Hans: 用于分类问题 - pt_BR: used for classifying questions - llm_description: key words for classifying questions - form: llm -extra: - python: - source: tools/question_classifier.py diff --git a/python/examples/code_based_workflow/tools/rerank.py b/python/examples/code_based_workflow/tools/rerank.py deleted file mode 100644 index 66ac02ca..00000000 --- a/python/examples/code_based_workflow/tools/rerank.py +++ /dev/null @@ -1,25 +0,0 @@ -from collections.abc import Generator - -from dify_plugin.entities.model.rerank import RerankModelConfig -from dify_plugin.entities.tool import ToolInvokeMessage -from dify_plugin.interfaces.tool import Tool - - -class Rerank(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - response = self.session.model.rerank.invoke( - model_config=RerankModelConfig( - provider="jina", - model="jina-embeddings-v2-base-v1.0", - score_threshold=0.5, - top_n=10, - ), - docs=["Kasumi", "Utae", "Arisa"], - query="Utae", - ) - - yield self.create_json_message( - { - "data": response.docs, - } - ) diff --git a/python/examples/code_based_workflow/tools/rerank.yaml b/python/examples/code_based_workflow/tools/rerank.yaml deleted file mode 100644 index 855fbe6e..00000000 --- a/python/examples/code_based_workflow/tools/rerank.yaml +++ /dev/null @@ -1,17 +0,0 @@ -identity: - name: rerank - author: Dify - label: - en_US: Rerank - zh_Hans: 重新排序 - pt_BR: Rerank -description: - human: - en_US: A tool for reranking questions - zh_Hans: 用于重新排序问题的工具 - pt_BR: A tool for reranking questions - llm: A tool for reranking questions -parameters: -extra: - python: - source: tools/rerank.py diff --git a/python/examples/code_based_workflow/tools/summary.py b/python/examples/code_based_workflow/tools/summary.py deleted file mode 100644 index 55e8e9e9..00000000 --- a/python/examples/code_based_workflow/tools/summary.py +++ /dev/null @@ -1,19 +0,0 @@ -from collections.abc import Generator - -from dify_plugin.entities.tool import ToolInvokeMessage -from dify_plugin.interfaces.tool import Tool - - -class Summary(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - response = self.session.model.summary.invoke( - text="Hello, world!", - instruction="Summarize the text", - min_summarize_length=1, - ) - - yield self.create_json_message( - { - "data": response, - } - ) diff --git a/python/examples/code_based_workflow/tools/summary.yaml b/python/examples/code_based_workflow/tools/summary.yaml deleted file mode 100644 index 235a26b9..00000000 --- a/python/examples/code_based_workflow/tools/summary.yaml +++ /dev/null @@ -1,17 +0,0 @@ -identity: - name: summary - author: Dify - label: - en_US: Summary - zh_Hans: 总结 - pt_BR: Summary -description: - human: - en_US: A tool for summarizing questions - zh_Hans: 用于总结问题的工具 - pt_BR: A tool for summarizing questions - llm: A tool for reranking questions -parameters: -extra: - python: - source: tools/summary.py diff --git a/python/examples/code_based_workflow/tools/tool.py b/python/examples/code_based_workflow/tools/tool.py deleted file mode 100644 index c84bd7f7..00000000 --- a/python/examples/code_based_workflow/tools/tool.py +++ /dev/null @@ -1,9 +0,0 @@ -from collections.abc import Generator - -from dify_plugin import Tool -from dify_plugin.entities.tool import ToolInvokeMessage - - -class ToolTool(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - yield self.create_image_message("https://assets.dify.ai/images/dify_logo_dark_s.png") diff --git a/python/examples/code_based_workflow/tools/tool.yaml b/python/examples/code_based_workflow/tools/tool.yaml deleted file mode 100644 index d7064b0b..00000000 --- a/python/examples/code_based_workflow/tools/tool.yaml +++ /dev/null @@ -1,16 +0,0 @@ -identity: - name: tool - author: Dify - label: - en_US: Tool - zh_Hans: 工具 - pt_BR: Tool -description: - human: - en_US: invoke other tools - zh_Hans: 调用其他工具 - pt_BR: invoke other tools - llm: invoke other tools -extra: - python: - source: tools/tool.py diff --git a/python/examples/code_based_workflow/tools/transform_image.py b/python/examples/code_based_workflow/tools/transform_image.py deleted file mode 100644 index 3b9a5eb2..00000000 --- a/python/examples/code_based_workflow/tools/transform_image.py +++ /dev/null @@ -1,19 +0,0 @@ -from collections.abc import Generator - -from dify_plugin import Tool -from dify_plugin.entities.tool import ToolInvokeMessage -from dify_plugin.file.file import File - - -class TransformImageTool(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - for image in tool_parameters["image"]: - assert isinstance(image, File) - yield self.create_json_message( - { - "mime_type": image.mime_type, - "type": image.type, - "length": len(image.blob), - } - ) - yield self.create_blob_message(image.blob, {"mime_type": image.mime_type}) diff --git a/python/examples/code_based_workflow/tools/transform_image.yaml b/python/examples/code_based_workflow/tools/transform_image.yaml deleted file mode 100644 index 0d25f7e1..00000000 --- a/python/examples/code_based_workflow/tools/transform_image.yaml +++ /dev/null @@ -1,30 +0,0 @@ -identity: - name: transform_image - author: Dify - label: - en_US: Transform Image - zh_Hans: 转换图片 - pt_BR: Transformar Imagem -description: - human: - en_US: Transform image to another format - zh_Hans: 将图片转换为另一种格式 - pt_BR: Transformar imagem para outro formato - llm: Transform image to another format -parameters: - - name: image - type: files - required: true - label: - en_US: Images - zh_Hans: 图片 - pt_BR: Imagens - human_description: - en_US: Images to transform - zh_Hans: 需要转换的图片 - pt_BR: Imagens para transformar - llm_description: Images to transform - form: llm -extra: - python: - source: tools/transform_image.py diff --git a/python/examples/code_based_workflow/tools/tts.py b/python/examples/code_based_workflow/tools/tts.py deleted file mode 100644 index 316568d2..00000000 --- a/python/examples/code_based_workflow/tools/tts.py +++ /dev/null @@ -1,21 +0,0 @@ -import binascii -from collections.abc import Generator - -from dify_plugin.entities.model.tts import TTSModelConfig -from dify_plugin.entities.tool import ToolInvokeMessage -from dify_plugin.interfaces.tool import Tool - - -class TTS(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - response = self.session.model.tts.invoke( - model_config=TTSModelConfig( - provider="openai", - model="tts-1", - voice="alloy", - ), - content_text="Hello, world!", - ) - - for chunk in response: - yield self.create_text_message(binascii.hexlify(chunk).decode()) diff --git a/python/examples/code_based_workflow/tools/tts.yaml b/python/examples/code_based_workflow/tools/tts.yaml deleted file mode 100644 index 75bb1a01..00000000 --- a/python/examples/code_based_workflow/tools/tts.yaml +++ /dev/null @@ -1,17 +0,0 @@ -identity: - name: tts - author: Dify - label: - en_US: TTS - zh_Hans: 文本转语音 - pt_BR: TTS -description: - human: - en_US: A tool for text to speech - zh_Hans: 用于文本转语音的工具 - pt_BR: A tool for text to speech - llm: A tool for text to speech -parameters: -extra: - python: - source: tools/tts.py diff --git a/python/examples/code_based_workflow/tools/upload_file.py b/python/examples/code_based_workflow/tools/upload_file.py deleted file mode 100644 index 341670f6..00000000 --- a/python/examples/code_based_workflow/tools/upload_file.py +++ /dev/null @@ -1,10 +0,0 @@ -from collections.abc import Generator - -from dify_plugin import Tool -from dify_plugin.entities.tool import ToolInvokeMessage - - -class UploadFileTool(Tool): - def _invoke(self, tool_parameters: dict) -> Generator[ToolInvokeMessage, None, None]: - response = self.session.file.upload("1.txt", b"", "text/plain") - yield self.create_text_message(f"file id: {response.id}") diff --git a/python/examples/code_based_workflow/tools/upload_file.yaml b/python/examples/code_based_workflow/tools/upload_file.yaml deleted file mode 100644 index 0ecff204..00000000 --- a/python/examples/code_based_workflow/tools/upload_file.yaml +++ /dev/null @@ -1,16 +0,0 @@ -identity: - name: upload_file - author: Dify - label: - en_US: Upload File - zh_Hans: 上传文件 - pt_BR: Upload File -description: - human: - en_US: upload file - zh_Hans: 上传文件 - pt_BR: upload file - llm: upload file -extra: - python: - source: tools/upload_file.py