Unverified Commit 1cf5f510 authored by Bowen Liang's avatar Bowen Liang Committed by GitHub

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

parent 526c874c
...@@ -18,3 +18,4 @@ ...@@ -18,3 +18,4 @@
- vectorizer - vectorizer
- gaode - gaode
- wecom - wecom
- qrcode
<?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
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))
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
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')
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
...@@ -70,3 +70,4 @@ numexpr~=2.9.0 ...@@ -70,3 +70,4 @@ numexpr~=2.9.0
duckduckgo-search==4.4.3 duckduckgo-search==4.4.3
arxiv==2.1.0 arxiv==2.1.0
yarl~=1.9.4 yarl~=1.9.4
qrcode~=7.4.2
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment