Prepare image content for API request. Args: image_source: Either a path to local image or a URL. detail: Image detail level ('auto', 'low', or 'high'). Returns: Formatted image content for API request.
(
self, image_source: Union[str, Dict[str, Any]], detail: str = "auto"
)
| 558 | raise ValueError(f"Error encoding image {image_path}: {str(e)}") |
| 559 | |
| 560 | def _prepare_image_content( |
| 561 | self, image_source: Union[str, Dict[str, Any]], detail: str = "auto" |
| 562 | ) -> Dict[str, Any]: |
| 563 | """Prepare image content for API request. |
| 564 | |
| 565 | Args: |
| 566 | image_source: Either a path to local image or a URL. |
| 567 | detail: Image detail level ('auto', 'low', or 'high'). |
| 568 | |
| 569 | Returns: |
| 570 | Formatted image content for API request. |
| 571 | """ |
| 572 | if isinstance(image_source, str): |
| 573 | if image_source.startswith(("http://", "https://")): |
| 574 | return { |
| 575 | "type": "image_url", |
| 576 | "image_url": {"url": image_source, "detail": detail}, |
| 577 | } |
| 578 | else: |
| 579 | base64_image = self._encode_image(image_source) |
| 580 | return { |
| 581 | "type": "image_url", |
| 582 | "image_url": { |
| 583 | "url": f"data:image/jpeg;base64,{base64_image}", |
| 584 | "detail": detail, |
| 585 | }, |
| 586 | } |
| 587 | return image_source |
| 588 | |
| 589 | |
| 590 | # Example usage: |
no test coverage detected