Skip to content

Commit

Permalink
feat: support Vectorizer can be used in workflow (langgenius#9932)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjlarry authored Oct 28, 2024
1 parent 0ebd985 commit ddb960d
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 43 deletions.
12 changes: 12 additions & 0 deletions api/core/agent/base_agent_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ def _convert_tool_to_prompt_message_tool(self, tool: AgentToolEntity) -> tuple[P
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]
Expand Down Expand Up @@ -250,6 +256,12 @@ def update_prompt_message_tool(self, tool: Tool, prompt_tool: PromptMessageTool)
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]
Expand Down
12 changes: 10 additions & 2 deletions api/core/file/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, /):
Expand Down

This file was deleted.

53 changes: 33 additions & 20 deletions api/core/tools/provider/builtin/vectorizer/tools/vectorizer.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -16,30 +17,30 @@ def _invoke(
"""
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,
)
Expand All @@ -59,11 +60,23 @@ def get_runtime_parameters(self) -> list[ToolParameter]:
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,
),
]
15 changes: 9 additions & 6 deletions api/core/tools/provider/builtin/vectorizer/tools/vectorizer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
10 changes: 9 additions & 1 deletion api/core/tools/provider/builtin/vectorizer/vectorizer.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
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


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={
"credentials": credentials,
}
).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))
8 changes: 0 additions & 8 deletions api/core/tools/provider/builtin/vectorizer/vectorizer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,28 +18,22 @@ 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
required: true
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.
14 changes: 9 additions & 5 deletions api/core/tools/tool_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,15 @@ def get_agent_tool_runtime(
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:
Expand Down

0 comments on commit ddb960d

Please sign in to comment.