feat: support Vectorizer can be used in workflow (#9932)

This commit is contained in:
非法操作 2024-10-28 16:52:57 +08:00 committed by GitHub
parent 0ebd985672
commit ddb960ddfb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 82 additions and 43 deletions

View File

@ -165,6 +165,12 @@ class BaseAgentRunner(AppRunner):
continue
parameter_type = parameter.type.as_normal_type()
if parameter.type in {
ToolParameter.ToolParameterType.SYSTEM_FILES,
ToolParameter.ToolParameterType.FILE,
ToolParameter.ToolParameterType.FILES,
}:
continue
enum = []
if parameter.type == ToolParameter.ToolParameterType.SELECT:
enum = [option.value for option in parameter.options]
@ -250,6 +256,12 @@ class BaseAgentRunner(AppRunner):
continue
parameter_type = parameter.type.as_normal_type()
if parameter.type in {
ToolParameter.ToolParameterType.SYSTEM_FILES,
ToolParameter.ToolParameterType.FILE,
ToolParameter.ToolParameterType.FILES,
}:
continue
enum = []
if parameter.type == ToolParameter.ToolParameterType.SELECT:
enum = [option.value for option in parameter.options]

View File

@ -76,8 +76,16 @@ def to_prompt_message_content(f: File, /):
def download(f: File, /):
upload_file = file_repository.get_upload_file(session=db.session(), file=f)
return _download_file_content(upload_file.key)
if f.transfer_method == FileTransferMethod.TOOL_FILE:
tool_file = file_repository.get_tool_file(session=db.session(), file=f)
return _download_file_content(tool_file.file_key)
elif f.transfer_method == FileTransferMethod.LOCAL_FILE:
upload_file = file_repository.get_upload_file(session=db.session(), file=f)
return _download_file_content(upload_file.key)
# remote file
response = ssrf_proxy.get(f.remote_url, follow_redirects=True)
response.raise_for_status()
return response.content
def _download_file_content(path: str, /):

View File

@ -1 +0,0 @@
VECTORIZER_ICON_PNG = "iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAACXBIWXMAACxLAAAsSwGlPZapAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAboSURBVHgB7Z09bBxFFMffRoAvcQqbguBUxu4wCUikMCZ0TmQK4NLQJCJOlQIkokgEGhQ7NCFI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DuVCsWeJxwToHZ163m3Z8hjloDGk54vn5gFbT/5eZw8phifvZz8XPlA9qmRj8JRCumi+OkljzbbrvxM0qPMm9rIqY6FXZubVBUinMbzcP3jbuXA6Mh2kMx07KPJJLfj8Xg8Hg/4H+KfFYb2WM4MAAAAAElFTkSuQmCC" # noqa: E501

View File

@ -1,11 +1,12 @@
from base64 import b64decode
from typing import Any, Union
from httpx import post
from core.file.enums import FileType
from core.file.file_manager import download
from core.tools.entities.common_entities import I18nObject
from core.tools.entities.tool_entities import ToolInvokeMessage, ToolParameter
from core.tools.errors import ToolProviderCredentialValidationError
from core.tools.provider.builtin.vectorizer.tools.test_data import VECTORIZER_ICON_PNG
from core.tools.errors import ToolParameterValidationError
from core.tools.tool.builtin_tool import BuiltinTool
@ -16,30 +17,30 @@ class VectorizerTool(BuiltinTool):
"""
invoke tools
"""
api_key_name = self.runtime.credentials.get("api_key_name", None)
api_key_value = self.runtime.credentials.get("api_key_value", None)
api_key_name = self.runtime.credentials.get("api_key_name")
api_key_value = self.runtime.credentials.get("api_key_value")
mode = tool_parameters.get("mode", "test")
if mode == "production":
mode = "preview"
if not api_key_name or not api_key_value:
raise ToolProviderCredentialValidationError("Please input api key name and value")
# image file for workflow mode
image = tool_parameters.get("image")
if image and image.type != FileType.IMAGE:
raise ToolParameterValidationError("Not a valid image")
# image_id for agent mode
image_id = tool_parameters.get("image_id", "")
if not image_id:
return self.create_text_message("Please input image id")
if image_id.startswith("__test_"):
image_binary = b64decode(VECTORIZER_ICON_PNG)
else:
if image_id:
image_binary = self.get_variable_file(self.VariableKey.IMAGE)
if not image_binary:
return self.create_text_message("Image not found, please request user to generate image firstly.")
elif image:
image_binary = download(image)
else:
raise ToolParameterValidationError("Please provide either image or image_id")
response = post(
"https://vectorizer.ai/api/v1/vectorize",
data={"mode": mode},
files={"image": image_binary},
data={"mode": mode} if mode == "test" else {},
auth=(api_key_name, api_key_value),
timeout=30,
)
@ -59,11 +60,23 @@ class VectorizerTool(BuiltinTool):
return [
ToolParameter.get_simple_instance(
name="image_id",
llm_description=f"the image id that you want to vectorize, \
and the image id should be specified in \
llm_description=f"the image_id that you want to vectorize, \
and the image_id should be specified in \
{[i.name for i in self.list_default_image_variables()]}",
type=ToolParameter.ToolParameterType.SELECT,
required=True,
required=False,
options=[i.name for i in self.list_default_image_variables()],
)
),
ToolParameter(
name="image",
label=I18nObject(en_US="image", zh_Hans="image"),
human_description=I18nObject(
en_US="The image to be converted.",
zh_Hans="要转换的图片。",
),
type=ToolParameter.ToolParameterType.FILE,
form=ToolParameter.ToolParameterForm.LLM,
llm_description="you should not input this parameter. just input the image_id.",
required=False,
),
]

View File

@ -4,14 +4,21 @@ identity:
label:
en_US: Vectorizer.AI
zh_Hans: Vectorizer.AI
pt_BR: Vectorizer.AI
description:
human:
en_US: Convert your PNG and JPG images to SVG vectors quickly and easily. Fully automatically. Using AI.
zh_Hans: 一个将 PNG 和 JPG 图像快速轻松地转换为 SVG 矢量图的工具。
pt_BR: Convert your PNG and JPG images to SVG vectors quickly and easily. Fully automatically. Using AI.
llm: A tool for converting images to SVG vectors. you should input the image id as the input of this tool. the image id can be got from parameters.
parameters:
- name: image
type: file
label:
en_US: image
human_description:
en_US: The image to be converted.
zh_Hans: 要转换的图片。
llm_description: you should not input this parameter. just input the image_id.
form: llm
- name: mode
type: select
required: true
@ -20,19 +27,15 @@ parameters:
label:
en_US: production
zh_Hans: 生产模式
pt_BR: production
- value: test
label:
en_US: test
zh_Hans: 测试模式
pt_BR: test
default: test
label:
en_US: Mode
zh_Hans: 模式
pt_BR: Mode
human_description:
en_US: It is free to integrate with and test out the API in test mode, no subscription required.
zh_Hans: 在测试模式下可以免费测试API。
pt_BR: It is free to integrate with and test out the API in test mode, no subscription required.
form: form

View File

@ -1,5 +1,7 @@
from typing import Any
from core.file import File
from core.file.enums import FileTransferMethod, FileType
from core.tools.errors import ToolProviderCredentialValidationError
from core.tools.provider.builtin.vectorizer.tools.vectorizer import VectorizerTool
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
@ -7,6 +9,12 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
class VectorizerProvider(BuiltinToolProviderController):
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
test_img = File(
tenant_id="__test_123",
remote_url="https://cloud.dify.ai/logo/logo-site.png",
type=FileType.IMAGE,
transfer_method=FileTransferMethod.REMOTE_URL,
)
try:
VectorizerTool().fork_tool_runtime(
runtime={
@ -14,7 +22,7 @@ class VectorizerProvider(BuiltinToolProviderController):
}
).invoke(
user_id="",
tool_parameters={"mode": "test", "image_id": "__test_123"},
tool_parameters={"mode": "test", "image": test_img},
)
except Exception as e:
raise ToolProviderCredentialValidationError(str(e))

View File

@ -4,11 +4,9 @@ identity:
label:
en_US: Vectorizer.AI
zh_Hans: Vectorizer.AI
pt_BR: Vectorizer.AI
description:
en_US: Convert your PNG and JPG images to SVG vectors quickly and easily. Fully automatically. Using AI.
zh_Hans: 一个将 PNG 和 JPG 图像快速轻松地转换为 SVG 矢量图的工具。
pt_BR: Convert your PNG and JPG images to SVG vectors quickly and easily. Fully automatically. Using AI.
icon: icon.png
tags:
- productivity
@ -20,15 +18,12 @@ credentials_for_provider:
label:
en_US: Vectorizer.AI API Key name
zh_Hans: Vectorizer.AI API Key name
pt_BR: Vectorizer.AI API Key name
placeholder:
en_US: Please input your Vectorizer.AI ApiKey name
zh_Hans: 请输入你的 Vectorizer.AI ApiKey name
pt_BR: Please input your Vectorizer.AI ApiKey name
help:
en_US: Get your Vectorizer.AI API Key from Vectorizer.AI.
zh_Hans: 从 Vectorizer.AI 获取您的 Vectorizer.AI API Key。
pt_BR: Get your Vectorizer.AI API Key from Vectorizer.AI.
url: https://vectorizer.ai/api
api_key_value:
type: secret-input
@ -36,12 +31,9 @@ credentials_for_provider:
label:
en_US: Vectorizer.AI API Key
zh_Hans: Vectorizer.AI API Key
pt_BR: Vectorizer.AI API Key
placeholder:
en_US: Please input your Vectorizer.AI ApiKey
zh_Hans: 请输入你的 Vectorizer.AI ApiKey
pt_BR: Please input your Vectorizer.AI ApiKey
help:
en_US: Get your Vectorizer.AI API Key from Vectorizer.AI.
zh_Hans: 从 Vectorizer.AI 获取您的 Vectorizer.AI API Key。
pt_BR: Get your Vectorizer.AI API Key from Vectorizer.AI.

View File

@ -242,11 +242,15 @@ class ToolManager:
parameters = tool_entity.get_all_runtime_parameters()
for parameter in parameters:
# check file types
if parameter.type in {
ToolParameter.ToolParameterType.SYSTEM_FILES,
ToolParameter.ToolParameterType.FILE,
ToolParameter.ToolParameterType.FILES,
}:
if (
parameter.type
in {
ToolParameter.ToolParameterType.SYSTEM_FILES,
ToolParameter.ToolParameterType.FILE,
ToolParameter.ToolParameterType.FILES,
}
and parameter.required
):
raise ValueError(f"file type parameter {parameter.name} not supported in agent")
if parameter.form == ToolParameter.ToolParameterForm.FORM: