diff --git a/api/core/tools/provider/_position.yaml b/api/core/tools/provider/_position.yaml index e796e58e13..2f332b1c31 100644 --- a/api/core/tools/provider/_position.yaml +++ b/api/core/tools/provider/_position.yaml @@ -18,3 +18,4 @@ - vectorizer - gaode - wecom +- qrcode diff --git a/api/core/tools/provider/builtin/qrcode/_assets/icon.svg b/api/core/tools/provider/builtin/qrcode/_assets/icon.svg new file mode 100644 index 0000000000..d44bb0bca9 --- /dev/null +++ b/api/core/tools/provider/builtin/qrcode/_assets/icon.svg @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/api/core/tools/provider/builtin/qrcode/qrcode.py b/api/core/tools/provider/builtin/qrcode/qrcode.py new file mode 100644 index 0000000000..9fa7d01265 --- /dev/null +++ b/api/core/tools/provider/builtin/qrcode/qrcode.py @@ -0,0 +1,16 @@ +from typing import Any + +from core.tools.errors import ToolProviderCredentialValidationError +from core.tools.provider.builtin.qrcode.tools.qrcode_generator import QRCodeGeneratorTool +from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController + + +class QRCodeProvider(BuiltinToolProviderController): + def _validate_credentials(self, credentials: dict[str, Any]) -> None: + try: + QRCodeGeneratorTool().invoke(user_id='', + tool_parameters={ + 'content': 'Dify 123 😊' + }) + except Exception as e: + raise ToolProviderCredentialValidationError(str(e)) diff --git a/api/core/tools/provider/builtin/qrcode/qrcode.yaml b/api/core/tools/provider/builtin/qrcode/qrcode.yaml new file mode 100644 index 0000000000..c117c3de74 --- /dev/null +++ b/api/core/tools/provider/builtin/qrcode/qrcode.yaml @@ -0,0 +1,12 @@ +identity: + author: Bowen Liang + name: qrcode + label: + en_US: QRCode + zh_Hans: 二维码工具 + pt_BR: QRCode + description: + en_US: A tool for generating QR code (quick-response code) image. + zh_Hans: 一个二维码工具 + pt_BR: A tool for generating QR code (quick-response code) image. + icon: icon.svg diff --git a/api/core/tools/provider/builtin/qrcode/tools/qrcode_generator.py b/api/core/tools/provider/builtin/qrcode/tools/qrcode_generator.py new file mode 100644 index 0000000000..a86f17a999 --- /dev/null +++ b/api/core/tools/provider/builtin/qrcode/tools/qrcode_generator.py @@ -0,0 +1,35 @@ +import io +import logging +from typing import Any, Union + +import qrcode +from qrcode.image.pure import PyPNGImage + +from core.tools.entities.tool_entities import ToolInvokeMessage +from core.tools.tool.builtin_tool import BuiltinTool + + +class QRCodeGeneratorTool(BuiltinTool): + def _invoke(self, + user_id: str, + tool_parameters: dict[str, Any], + ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: + """ + invoke tools + """ + # get expression + content = tool_parameters.get('content', '') + if not content: + return self.create_text_message('Invalid parameter content') + + try: + img = qrcode.make(data=content, image_factory=PyPNGImage) + byte_stream = io.BytesIO() + img.save(byte_stream) + byte_array = byte_stream.getvalue() + return self.create_blob_message(blob=byte_array, + meta={'mime_type': 'image/png'}, + save_as=self.VARIABLE_KEY.IMAGE.value) + except Exception: + logging.exception(f'Failed to generate QR code for content: {content}') + return self.create_text_message('Failed to generate QR code') diff --git a/api/core/tools/provider/builtin/qrcode/tools/qrcode_generator.yaml b/api/core/tools/provider/builtin/qrcode/tools/qrcode_generator.yaml new file mode 100644 index 0000000000..ca562ac094 --- /dev/null +++ b/api/core/tools/provider/builtin/qrcode/tools/qrcode_generator.yaml @@ -0,0 +1,26 @@ +identity: + name: qrcode_generator + author: Bowen Liang + label: + en_US: QR Code Generator + zh_Hans: 二维码生成器 + pt_BR: QR Code Generator +description: + human: + en_US: A tool for generating QR code image + zh_Hans: 一个用于生成二维码的工具 + pt_BR: A tool for generating QR code image + llm: A tool for generating QR code image +parameters: + - name: content + type: string + required: true + label: + en_US: content text for QR code + zh_Hans: 二维码文本内容 + pt_BR: content text for QR code + human_description: + en_US: content text for QR code + zh_Hans: 二维码文本内容 + pt_BR: 二维码文本内容 + form: llm diff --git a/api/requirements.txt b/api/requirements.txt index 7f25fa1485..8dace7c4fa 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -69,4 +69,5 @@ gmpy2~=2.1.5 numexpr~=2.9.0 duckduckgo-search==4.4.3 arxiv==2.1.0 -yarl~=1.9.4 \ No newline at end of file +yarl~=1.9.4 +qrcode~=7.4.2