Prepare message format for API interface. Args: inputs: Input message list. Returns: tuple: (formatted_messages, text_content).
(self, inputs)
| 198 | return None |
| 199 | |
| 200 | def prepare_inputs(self, inputs): |
| 201 | """Prepare message format for API interface. |
| 202 | |
| 203 | Args: |
| 204 | inputs: Input message list. |
| 205 | |
| 206 | Returns: |
| 207 | tuple: (formatted_messages, text_content). |
| 208 | """ |
| 209 | input_msgs = [] |
| 210 | |
| 211 | # Extract system prompt if present |
| 212 | system_items = [x for x in inputs if x["type"] == "system"] |
| 213 | inputs = [x for x in inputs if x["type"] != "system"] |
| 214 | for sys_item in system_items: |
| 215 | if self.openai_format: |
| 216 | input_msgs.append( |
| 217 | { |
| 218 | "role": "system", |
| 219 | "content": [{"type": "text", "text": sys_item["value"]}], |
| 220 | } |
| 221 | ) |
| 222 | else: |
| 223 | input_msgs.append( |
| 224 | { |
| 225 | "role": "system", |
| 226 | "content": [{"type": "text", "value": sys_item["value"]}], |
| 227 | } |
| 228 | ) |
| 229 | |
| 230 | # Check if input contains images |
| 231 | has_images = np.sum( |
| 232 | [x["type"] == "image" or x["type"] == "image_url" for x in inputs] |
| 233 | ) |
| 234 | if has_images: |
| 235 | # Process messages containing images |
| 236 | text = "" |
| 237 | img_list = [] |
| 238 | |
| 239 | # Collect all text and images |
| 240 | for msg in inputs: |
| 241 | if msg["type"] == "text": |
| 242 | text += ( |
| 243 | msg["value"].replace("<image>\n", " ").replace("<image>", " ") |
| 244 | ) |
| 245 | elif msg["type"] == "image" or msg["type"] == "image_url": |
| 246 | # Handle list of images |
| 247 | if isinstance(msg["value"], list): |
| 248 | for img_item in msg["value"]: |
| 249 | if img_item is None: |
| 250 | if self.logger: |
| 251 | self.logger.warning("Skipping None image in list") |
| 252 | continue |
| 253 | encoded_img = self.encode_image_directly(img_item) |
| 254 | img_list.append([msg["type"], encoded_img]) |
| 255 | |
| 256 | # Handle single image |
| 257 | elif msg["value"] is not None: |