|  |  |  |  |  |  | 
													
												
													
														|  |  | 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, /): |