@@ -35,6 +35,8 @@ def is_present(json, key):
3535#convert chat to prompt
3636def convert_chat (messages ):
3737 prompt = "" + args .chat_prompt .replace ("\\ n" , "\n " )
38+ image_data = []
39+ image_id = 1
3840
3941 system_n = args .system_name .replace ("\\ n" , "\n " )
4042 user_n = args .user_name .replace ("\\ n" , "\n " )
@@ -46,17 +48,35 @@ def convert_chat(messages):
4648 if (line ["role" ] == "system" ):
4749 prompt += f"{ system_n } { line ['content' ]} "
4850 if (line ["role" ] == "user" ):
49- prompt += f"{ user_n } { line ['content' ]} "
51+ # content can either be a string or an iterable with "text"
52+ # and "image_url" elements
53+ content = line ['content' ]
54+ if type (content ) == str :
55+ prompt += f"{ user_n } { line ['content' ]} "
56+ else :
57+ # add all elements from array
58+ for content_part in content :
59+ if content_part ['type' ] == "text" :
60+ prompt += f"{ user_n } { content_part ['text' ]} { stop } "
61+ elif content_part ['type' ] == "image_url" :
62+ image_data .append (
63+ {"data" : content_part ['image_url' ]['url' ].split ("," )[1 ],
64+ "id" : image_id })
65+ image_id += 1
66+
5067 if (line ["role" ] == "assistant" ):
5168 prompt += f"{ ai_n } { line ['content' ]} { stop } "
5269 prompt += ai_n .rstrip ()
5370
54- return prompt
71+ return prompt , image_data
5572
5673def make_postData (body , chat = False , stream = False ):
5774 postData = {}
5875 if (chat ):
59- postData ["prompt" ] = convert_chat (body ["messages" ])
76+ prompt , image_data = convert_chat (body ["messages" ])
77+ postData ["prompt" ] = prompt
78+ if len (image_data ) > 0 :
79+ postData ["image_data" ] = image_data
6080 else :
6181 postData ["prompt" ] = body ["prompt" ]
6282 if (is_present (body , "temperature" )): postData ["temperature" ] = body ["temperature" ]
0 commit comments