Skip to content

Commit

Permalink
refactor(models): enhance file model and tool processing
Browse files Browse the repository at this point in the history
- Add description to file extension field in File model for clarity.
- Default meta field to an empty dictionary in ToolInvokeMessage to prevent NoneType errors.
- Extend VariableKey enum in Tool with additional categories (DOCUMENT, VIDEO, AUDIO, CUSTOM).
- Refactor ToolNode to handle different response types more robustly by refining file ID extraction and managing default extensions.
  • Loading branch information
laipz8200 committed Oct 12, 2024
1 parent 8b76c15 commit 772b588
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api/core/file/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class File(BaseModel):
remote_url: Optional[str] = None # remote url
related_id: Optional[str] = None
filename: Optional[str] = None
extension: Optional[str] = None
extension: Optional[str] = Field(default=None, description="File extension, should contains dot")
mime_type: Optional[str] = None
size: int = -1
_extra_config: FileExtraConfig | None = None
Expand Down
3 changes: 2 additions & 1 deletion api/core/tools/entities/tool_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class MessageType(Enum):
plain text, image url or link url
"""
message: str | bytes | dict | None = None
meta: dict[str, Any] | None = None
# TODO: Use a BaseModel for meta
meta: dict[str, Any] = Field(default_factory=dict)
save_as: str = ""


Expand Down
4 changes: 4 additions & 0 deletions api/core/tools/tool/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def __init__(self, **data: Any):

class VariableKey(str, Enum):
IMAGE = "image"
DOCUMENT = "document"
VIDEO = "video"
AUDIO = "audio"
CUSTOM = "custom"

def fork_tool_runtime(self, runtime: dict[str, Any]) -> "Tool":
"""
Expand Down
30 changes: 24 additions & 6 deletions api/core/workflow/nodes/tool/tool_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,32 +165,31 @@ def _extract_tool_response_binary(self, tool_response: list[ToolInvokeMessage])
Extract tool response binary
"""
result = []

for response in tool_response:
if response.type in {ToolInvokeMessage.MessageType.IMAGE_LINK, ToolInvokeMessage.MessageType.IMAGE}:
url = response.message
ext = path.splitext(url)[1]
mimetype = response.meta.get("mime_type", "image/jpeg")
filename = response.save_as or url.split("/")[-1]
tool_file_id = response.save_as or url.split("/")[-1]
transfer_method = response.meta.get("transfer_method", FileTransferMethod.TOOL_FILE)

# get tool file id
tool_file_id = url.split("/")[-1].split(".")[0]
tool_file_id = str(url).split("/")[-1].split(".")[0]
result.append(
File(
tenant_id=self.tenant_id,
type=FileType.IMAGE,
transfer_method=transfer_method,
remote_url=url,
related_id=tool_file_id,
filename=filename,
filename=tool_file_id,
extension=ext,
mime_type=mimetype,
)
)
elif response.type == ToolInvokeMessage.MessageType.BLOB:
# get tool file id
tool_file_id = response.message.split("/")[-1].split(".")[0]
tool_file_id = str(response.message).split("/")[-1].split(".")[0]
result.append(
File(
tenant_id=self.tenant_id,
Expand All @@ -203,7 +202,26 @@ def _extract_tool_response_binary(self, tool_response: list[ToolInvokeMessage])
)
)
elif response.type == ToolInvokeMessage.MessageType.LINK:
pass # TODO:
url = str(response.message)
transfer_method = FileTransferMethod.TOOL_FILE
mimetype = response.meta.get("mime_type", "application/octet-stream")
tool_file_id = url.split("/")[-1].split(".")[0]
if "." in url:
extension = "." + url.split("/")[-1].split(".")[1]
else:
extension = ".bin"
file = File(
tenant_id=self.tenant_id,
type=FileType(response.save_as),
transfer_method=transfer_method,
remote_url=url,
filename=tool_file_id,
related_id=tool_file_id,
extension=extension,
mime_type=mimetype,
)
result.append(file)

elif response.type == ToolInvokeMessage.MessageType.FILE:
assert response.meta is not None
result.append(response.meta["file"])
Expand Down

0 comments on commit 772b588

Please sign in to comment.