|
|
|
@@ -7,6 +7,7 @@ from core.model_runtime.entities import ( |
|
|
|
AudioPromptMessageContent, |
|
|
|
DocumentPromptMessageContent, |
|
|
|
ImagePromptMessageContent, |
|
|
|
TextPromptMessageContent, |
|
|
|
VideoPromptMessageContent, |
|
|
|
) |
|
|
|
from core.model_runtime.entities.message_entities import PromptMessageContentUnionTypes |
|
|
|
@@ -44,11 +45,44 @@ def to_prompt_message_content( |
|
|
|
*, |
|
|
|
image_detail_config: ImagePromptMessageContent.DETAIL | None = None, |
|
|
|
) -> 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: |
|
|
|
raise ValueError("Missing file extension") |
|
|
|
if f.mime_type is None: |
|
|
|
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 = { |
|
|
|
"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 "", |
|
|
|
@@ -58,17 +92,7 @@ def to_prompt_message_content( |
|
|
|
if f.type == FileType.IMAGE: |
|
|
|
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, /): |