Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

feat: add qrcode tool for QR code generation (#2699)

Bowen Liang1cf5f510ed74
diff --git a/api/core/tools/provider/_position.yaml b/api/core/tools/provider/_position.yaml
index e796e58e13c..2f332b1c31c 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 00000000000..d44bb0bca91
--- /dev/null
+++ b/api/core/tools/provider/builtin/qrcode/_assets/icon.svg
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="utf-8"?>
+ <!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->
+<svg width="800px" height="800px" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
+ <g>
+ <path fill="none" d="M0 0h24v24H0z"/>
+ <path d="M16 17v-1h-3v-3h3v2h2v2h-1v2h-2v2h-2v-3h2v-1h1zm5 4h-4v-2h2v-2h2v4zM3 3h8v8H3V3zm2 2v4h4V5H5zm8-2h8v8h-8V3zm2 2v4h4V5h-4zM3 13h8v8H3v-8zm2 2v4h4v-4H5zm13-2h3v2h-3v-2zM6 6h2v2H6V6zm0 10h2v2H6v-2zM16 6h2v2h-2V6z"/>
+ </g>
+</svg>
\ 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 00000000000..9fa7d012657
--- /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 00000000000..c117c3de74c
--- /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 00000000000..a86f17a999b
--- /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 00000000000..ca562ac0947
--- /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 7f25fa14859..8dace7c4fa4 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