Browse Source

feat: update file manager and file factory implementations (#22704)

Signed-off-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: Claude <noreply@anthropic.com>
tags/1.7.0
-LAN- 3 months ago
parent
commit
62b29b3d76
No account linked to committer's email address
2 changed files with 38 additions and 20 deletions
  1. 35
    11
      api/core/file/file_manager.py
  2. 3
    9
      api/factories/file_factory.py

+ 35
- 11
api/core/file/file_manager.py View File

AudioPromptMessageContent, AudioPromptMessageContent,
DocumentPromptMessageContent, DocumentPromptMessageContent,
ImagePromptMessageContent, ImagePromptMessageContent,
TextPromptMessageContent,
VideoPromptMessageContent, VideoPromptMessageContent,
) )
from core.model_runtime.entities.message_entities import PromptMessageContentUnionTypes from core.model_runtime.entities.message_entities import PromptMessageContentUnionTypes
*, *,
image_detail_config: ImagePromptMessageContent.DETAIL | None = None, image_detail_config: ImagePromptMessageContent.DETAIL | None = None,
) -> PromptMessageContentUnionTypes: ) -> PromptMessageContentUnionTypes:
"""
Convert a file to prompt message content.

This function converts files to their appropriate prompt message content types.
For supported file types (IMAGE, AUDIO, VIDEO, DOCUMENT), it creates the
corresponding message content with proper encoding/URL.

For unsupported file types, instead of raising an error, it returns a
TextPromptMessageContent with a descriptive message about the file.

Args:
f: The file to convert
image_detail_config: Optional detail configuration for image files

Returns:
PromptMessageContentUnionTypes: The appropriate message content type

Raises:
ValueError: If file extension or mime_type is missing
"""
if f.extension is None: if f.extension is None:
raise ValueError("Missing file extension") raise ValueError("Missing file extension")
if f.mime_type is None: if f.mime_type is None:
raise ValueError("Missing file mime_type") raise ValueError("Missing file mime_type")


prompt_class_map: Mapping[FileType, type[PromptMessageContentUnionTypes]] = {
FileType.IMAGE: ImagePromptMessageContent,
FileType.AUDIO: AudioPromptMessageContent,
FileType.VIDEO: VideoPromptMessageContent,
FileType.DOCUMENT: DocumentPromptMessageContent,
}

# Check if file type is supported
if f.type not in prompt_class_map:
# For unsupported file types, return a text description
return TextPromptMessageContent(data=f"[Unsupported file type: {f.filename} ({f.type.value})]")

# Process supported file types
params = { params = {
"base64_data": _get_encoded_string(f) if dify_config.MULTIMODAL_SEND_FORMAT == "base64" else "", "base64_data": _get_encoded_string(f) if dify_config.MULTIMODAL_SEND_FORMAT == "base64" else "",
"url": _to_url(f) if dify_config.MULTIMODAL_SEND_FORMAT == "url" else "", "url": _to_url(f) if dify_config.MULTIMODAL_SEND_FORMAT == "url" else "",
if f.type == FileType.IMAGE: if f.type == FileType.IMAGE:
params["detail"] = image_detail_config or ImagePromptMessageContent.DETAIL.LOW params["detail"] = image_detail_config or ImagePromptMessageContent.DETAIL.LOW


prompt_class_map: Mapping[FileType, type[PromptMessageContentUnionTypes]] = {
FileType.IMAGE: ImagePromptMessageContent,
FileType.AUDIO: AudioPromptMessageContent,
FileType.VIDEO: VideoPromptMessageContent,
FileType.DOCUMENT: DocumentPromptMessageContent,
}

try:
return prompt_class_map[f.type].model_validate(params)
except KeyError:
raise ValueError(f"file type {f.type} is not supported")
return prompt_class_map[f.type].model_validate(params)




def download(f: File, /): def download(f: File, /):

+ 3
- 9
api/factories/file_factory.py View File

if strict_type_validation and detected_file_type.value != specified_type: if strict_type_validation and detected_file_type.value != specified_type:
raise ValueError("Detected file type does not match the specified type. Please verify the file.") raise ValueError("Detected file type does not match the specified type. Please verify the file.")


file_type = (
FileType(specified_type) if specified_type and specified_type != FileType.CUSTOM.value else detected_file_type
)
file_type = FileType(specified_type) if specified_type and specified_type != FileType.CUSTOM else detected_file_type


return File( return File(
id=mapping.get("id"), id=mapping.get("id"),
raise ValueError("Detected file type does not match the specified type. Please verify the file.") raise ValueError("Detected file type does not match the specified type. Please verify the file.")


file_type = ( file_type = (
FileType(specified_type)
if specified_type and specified_type != FileType.CUSTOM.value
else detected_file_type
FileType(specified_type) if specified_type and specified_type != FileType.CUSTOM else detected_file_type
) )


return File( return File(
if strict_type_validation and specified_type and detected_file_type.value != specified_type: if strict_type_validation and specified_type and detected_file_type.value != specified_type:
raise ValueError("Detected file type does not match the specified type. Please verify the file.") raise ValueError("Detected file type does not match the specified type. Please verify the file.")


file_type = (
FileType(specified_type) if specified_type and specified_type != FileType.CUSTOM.value else detected_file_type
)
file_type = FileType(specified_type) if specified_type and specified_type != FileType.CUSTOM else detected_file_type


return File( return File(
id=mapping.get("id"), id=mapping.get("id"),

Loading…
Cancel
Save