3
3
from configs import dify_config
4
4
from core .file import file_repository
5
5
from core .helper import ssrf_proxy
6
- from core .model_runtime .entities import AudioPromptMessageContent , ImagePromptMessageContent , VideoPromptMessageContent
6
+ from core .model_runtime .entities import (
7
+ AudioPromptMessageContent ,
8
+ DocumentPromptMessageContent ,
9
+ ImagePromptMessageContent ,
10
+ VideoPromptMessageContent ,
11
+ )
7
12
from extensions .ext_database import db
8
13
from extensions .ext_storage import storage
9
14
@@ -29,43 +34,25 @@ def get_attr(*, file: File, attr: FileAttribute):
29
34
return file .remote_url
30
35
case FileAttribute .EXTENSION :
31
36
return file .extension
32
- case _:
33
- raise ValueError (f"Invalid file attribute: { attr } " )
34
37
35
38
36
39
def to_prompt_message_content (
37
40
f : File ,
38
41
/ ,
39
42
* ,
40
- image_detail_config : ImagePromptMessageContent .DETAIL = ImagePromptMessageContent . DETAIL . LOW ,
43
+ image_detail_config : ImagePromptMessageContent .DETAIL | None = None ,
41
44
):
42
- """
43
- Convert a File object to an ImagePromptMessageContent or AudioPromptMessageContent object.
44
-
45
- This function takes a File object and converts it to an appropriate PromptMessageContent
46
- object, which can be used as a prompt for image or audio-based AI models.
47
-
48
- Args:
49
- f (File): The File object to convert.
50
- detail (Optional[ImagePromptMessageContent.DETAIL]): The detail level for image prompts.
51
- If not provided, defaults to ImagePromptMessageContent.DETAIL.LOW.
52
-
53
- Returns:
54
- Union[ImagePromptMessageContent, AudioPromptMessageContent]: An object containing the file data and detail level
55
-
56
- Raises:
57
- ValueError: If the file type is not supported or if required data is missing.
58
- """
59
45
match f .type :
60
46
case FileType .IMAGE :
47
+ image_detail_config = image_detail_config or ImagePromptMessageContent .DETAIL .LOW
61
48
if dify_config .MULTIMODAL_SEND_IMAGE_FORMAT == "url" :
62
49
data = _to_url (f )
63
50
else :
64
51
data = _to_base64_data_string (f )
65
52
66
53
return ImagePromptMessageContent (data = data , detail = image_detail_config )
67
54
case FileType .AUDIO :
68
- encoded_string = _file_to_encoded_string (f )
55
+ encoded_string = _get_encoded_string (f )
69
56
if f .extension is None :
70
57
raise ValueError ("Missing file extension" )
71
58
return AudioPromptMessageContent (data = encoded_string , format = f .extension .lstrip ("." ))
@@ -74,9 +61,20 @@ def to_prompt_message_content(
74
61
data = _to_url (f )
75
62
else :
76
63
data = _to_base64_data_string (f )
64
+ if f .extension is None :
65
+ raise ValueError ("Missing file extension" )
77
66
return VideoPromptMessageContent (data = data , format = f .extension .lstrip ("." ))
67
+ case FileType .DOCUMENT :
68
+ data = _get_encoded_string (f )
69
+ if f .mime_type is None :
70
+ raise ValueError ("Missing file mime_type" )
71
+ return DocumentPromptMessageContent (
72
+ encode_format = "base64" ,
73
+ mime_type = f .mime_type ,
74
+ data = data ,
75
+ )
78
76
case _:
79
- raise ValueError ("file type f.type is not supported" )
77
+ raise ValueError (f "file type { f .type } is not supported" )
80
78
81
79
82
80
def download (f : File , / ):
@@ -118,40 +116,23 @@ def _get_encoded_string(f: File, /):
118
116
case FileTransferMethod .REMOTE_URL :
119
117
response = ssrf_proxy .get (f .remote_url , follow_redirects = True )
120
118
response .raise_for_status ()
121
- content = response .content
122
- encoded_string = base64 .b64encode (content ).decode ("utf-8" )
123
- return encoded_string
119
+ data = response .content
124
120
case FileTransferMethod .LOCAL_FILE :
125
121
upload_file = file_repository .get_upload_file (session = db .session (), file = f )
126
122
data = _download_file_content (upload_file .key )
127
- encoded_string = base64 .b64encode (data ).decode ("utf-8" )
128
- return encoded_string
129
123
case FileTransferMethod .TOOL_FILE :
130
124
tool_file = file_repository .get_tool_file (session = db .session (), file = f )
131
125
data = _download_file_content (tool_file .file_key )
132
- encoded_string = base64 .b64encode (data ).decode ("utf-8" )
133
- return encoded_string
134
- case _:
135
- raise ValueError (f"Unsupported transfer method: { f .transfer_method } " )
126
+
127
+ encoded_string = base64 .b64encode (data ).decode ("utf-8" )
128
+ return encoded_string
136
129
137
130
138
131
def _to_base64_data_string (f : File , / ):
139
132
encoded_string = _get_encoded_string (f )
140
133
return f"data:{ f .mime_type } ;base64,{ encoded_string } "
141
134
142
135
143
- def _file_to_encoded_string (f : File , / ):
144
- match f .type :
145
- case FileType .IMAGE :
146
- return _to_base64_data_string (f )
147
- case FileType .VIDEO :
148
- return _to_base64_data_string (f )
149
- case FileType .AUDIO :
150
- return _get_encoded_string (f )
151
- case _:
152
- raise ValueError (f"file type { f .type } is not supported" )
153
-
154
-
155
136
def _to_url (f : File , / ):
156
137
if f .transfer_method == FileTransferMethod .REMOTE_URL :
157
138
if f .remote_url is None :
0 commit comments