-
Notifications
You must be signed in to change notification settings - Fork 9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ComfyUI Tool realtive WS paths #13029
Open
bikevit2008
wants to merge
7
commits into
langgenius:main
Choose a base branch
from
bikevit2008:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
56c73da
Relative WS path for comfyui
85fdc5f
Merge branch 'main' of https://github.com/bikevit2008/dify
f3915b6
build from source for api in docker compose
f37c872
Linted
f96d84c
Base64 encode tool
7b1d112
Fixed mime type -> extension file by HTTP request
f88d7f6
Linted success
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
google: 1 | ||
dalle: 2 | ||
stable_diffusion: 3 | ||
serpapi: 4 | ||
wolfram: 5 | ||
url_to_base64: 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .url_to_base64 import URLToBase64Provider | ||
|
||
__all__ = ["URLToBase64Provider"] |
9 changes: 9 additions & 0 deletions
9
api/core/tools/provider/builtin/url_to_base64/_assets/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions
73
api/core/tools/provider/builtin/url_to_base64/tools/url_to_base64_converter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import base64 | ||
import logging | ||
from typing import Any | ||
|
||
import requests | ||
|
||
from core.file.file_manager import download | ||
from core.file.models import File | ||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class URLToBase64Converter(BuiltinTool): | ||
def _invoke( | ||
self, | ||
user_id: str, | ||
tool_parameters: dict[str, Any], | ||
) -> ToolInvokeMessage: | ||
""" | ||
Конвертирует файл в base64 строку. | ||
Поддерживает как внешние URL, так и локальные файлы Dify. | ||
""" | ||
logger.info(f"Received parameters: {tool_parameters}") | ||
|
||
# Получаем источник файла | ||
file_source = tool_parameters.get("file_source") | ||
if not file_source: | ||
return self.create_text_message("Please specify file source (url or local)") | ||
|
||
try: | ||
# Обработка внешнего URL | ||
if file_source == "url": | ||
url = tool_parameters.get("url") | ||
if not url: | ||
return self.create_text_message("Please provide a URL") | ||
|
||
response = requests.get(url) | ||
response.raise_for_status() | ||
content = response.content | ||
|
||
# Обработка локального файла Dify | ||
elif file_source == "local": | ||
file = tool_parameters.get("file") | ||
logger.info(f"File data: {file}") | ||
|
||
if not file: | ||
return self.create_text_message("Please provide a file") | ||
|
||
if not isinstance(file, File): | ||
logger.error(f"Invalid file type: {type(file)}") | ||
return self.create_text_message("Invalid file type. Expected Dify File object") | ||
|
||
# Загружаем содержимое файла | ||
content = download(file) | ||
if not content: | ||
logger.error("Failed to download file content") | ||
return self.create_text_message("Failed to download file content") | ||
|
||
logger.info(f"Successfully loaded file: {len(content)} bytes") | ||
|
||
else: | ||
return self.create_text_message('Invalid file source. Use "url" or "local"') | ||
|
||
# Конвертируем содержимое в base64 | ||
base64_content = base64.b64encode(content).decode("utf-8") | ||
logger.info(f"Successfully converted file to base64: {len(base64_content)} characters") | ||
return self.create_text_message(base64_content) | ||
|
||
except Exception as e: | ||
logger.exception("Error processing file") | ||
return self.create_text_message(f"Error processing file: {str(e)}") |
66 changes: 66 additions & 0 deletions
66
api/core/tools/provider/builtin/url_to_base64/tools/url_to_base64_converter.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
identity: | ||
name: url_to_base64 | ||
author: vitalijsatilov | ||
label: | ||
en_US: URL to Base64 | ||
zh_Hans: URL转Base64 | ||
ru_RU: URL в Base64 | ||
|
||
description: | ||
human: | ||
en_US: Convert URL or local file to base64 string | ||
zh_Hans: 将URL或本地文件转换为base64字符串 | ||
ru_RU: Конвертировать URL или локальный файл в строку base64 | ||
llm: Convert a file from URL or local storage to base64 string format | ||
|
||
parameters: | ||
- name: file_source | ||
type: string | ||
required: true | ||
label: | ||
en_US: File Source | ||
zh_Hans: 文件来源 | ||
ru_RU: Источник файла | ||
human_description: | ||
en_US: Source of the file (url or local) | ||
zh_Hans: 文件来源(url或本地) | ||
ru_RU: Источник файла (url или локальный) | ||
llm_description: Specify the source of the file. Use "url" for external URLs or "local" for files from Dify storage. | ||
options: | ||
- value: url | ||
label: | ||
en_US: URL | ||
zh_Hans: URL | ||
ru_RU: URL | ||
- value: local | ||
label: | ||
en_US: Local File | ||
zh_Hans: 本地文件 | ||
ru_RU: Локальный файл | ||
form: llm | ||
- name: url | ||
type: string | ||
required: false | ||
label: | ||
en_US: URL | ||
zh_Hans: URL | ||
ru_RU: URL | ||
human_description: | ||
en_US: URL of the file to convert (required if file_source is "url") | ||
zh_Hans: 要转换的文件的URL(当file_source为"url"时必填) | ||
ru_RU: URL файла для конвертации (обязательно если file_source - "url") | ||
llm_description: URL of the file to convert to base64. Only required when file_source is "url". | ||
form: llm | ||
- name: file | ||
type: file | ||
required: false | ||
label: | ||
en_US: File | ||
zh_Hans: 文件 | ||
ru_RU: Файл | ||
human_description: | ||
en_US: Local file to convert (required if file_source is "local") | ||
zh_Hans: 要转换的本地文件(当file_source为"local"时必填) | ||
ru_RU: Локальный файл для конвертации (обязательно если file_source - "local") | ||
llm_description: Local file to convert to base64. Only required when file_source is "local". | ||
form: llm |
23 changes: 23 additions & 0 deletions
23
api/core/tools/provider/builtin/url_to_base64/url_to_base64.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from core.tools.provider.builtin.url_to_base64.tools.url_to_base64_converter import URLToBase64Converter | ||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController | ||
|
||
|
||
class URLToBase64Provider(BuiltinToolProviderController): | ||
@property | ||
def need_credentials(self) -> bool: | ||
""" | ||
Whether the provider needs credentials | ||
""" | ||
return False | ||
|
||
def _tools(self) -> list: | ||
return [ | ||
URLToBase64Converter(), | ||
] | ||
|
||
def _validate_credentials(self, credentials: dict) -> bool: | ||
""" | ||
Validate the credentials. | ||
Our tool doesn't require any credentials, so we always return True. | ||
""" | ||
return True |
16 changes: 16 additions & 0 deletions
16
api/core/tools/provider/builtin/url_to_base64/url_to_base64.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
identity: | ||
author: bikevit2008 | ||
name: url_to_base64 | ||
label: | ||
en_US: URL to Base64 | ||
ru_RU: URL в Base64 | ||
zh_Hans: URL 转 Base64 | ||
description: | ||
en_US: A tool for converting files from URL to base64 string format | ||
ru_RU: Инструмент для конвертации файлов из URL в формат base64 | ||
zh_Hans: 将 URL 文件转换为 base64 字符串的工具 | ||
icon: icon.svg | ||
tags: | ||
- utilities | ||
|
||
credentials_for_provider: {} |
49 changes: 49 additions & 0 deletions
49
api/core/tools/provider/builtin/url_to_base64/url_to_base64_tool.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import base64 | ||
from typing import Any, Union | ||
|
||
import requests | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
|
||
class URLToBase64Tool(BuiltinTool): | ||
def _invoke( | ||
self, | ||
user_id: str, | ||
tool_parameters: dict[str, Any], | ||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: | ||
""" | ||
Конвертирует файл из URL в base64 строку | ||
""" | ||
url = tool_parameters.get("url", "") | ||
if not url: | ||
return self.create_text_message("Пожалуйста, укажите URL") | ||
|
||
try: | ||
# Загружаем файл по URL | ||
response = requests.get(url) | ||
response.raise_for_status() # Проверяем на ошибки | ||
|
||
# Конвертируем содержимое в base64 | ||
base64_content = base64.b64encode(response.content).decode("utf-8") | ||
|
||
# Возвращаем результат | ||
return self.create_text_message(base64_content) | ||
|
||
except Exception as e: | ||
return self.create_text_message(f"Ошибка при обработке файла: {str(e)}") | ||
|
||
def get_runtime_parameters(self) -> list[dict]: | ||
""" | ||
Определяем параметры инструмента | ||
""" | ||
return [ | ||
{ | ||
"name": "url", | ||
"type": "string", | ||
"required": True, | ||
"label": "URL файла", | ||
"description": "URL файла, который нужно конвертировать в base64", | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -393,7 +393,9 @@ x-shared-env: &shared-api-worker-env | |
services: | ||
# API service | ||
api: | ||
image: langgenius/dify-api:0.15.2 | ||
build: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please restore the changes here. |
||
dockerfile: ../api/Dockerfile | ||
context: ../api | ||
restart: always | ||
environment: | ||
# Use the shared environment variables. | ||
|
@@ -416,7 +418,9 @@ services: | |
# worker service | ||
# The Celery worker for processing the queue. | ||
worker: | ||
image: langgenius/dify-api:0.15.2 | ||
build: | ||
dockerfile: ../api/Dockerfile | ||
context: ../api | ||
restart: always | ||
environment: | ||
# Use the shared environment variables. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please write the comments in English only.