Converts the PIL image URLs in the messages to base64 encoded data URIs.
This function iterates over a list of message dictionaries. For each message,
if it contains a ‘content’ key with a list of items, it looks for items
with an ‘image_url’ key. The function then converts the PIL image URL
(pointed to by ‘image_url’) to a base64 encoded data URI.
Parameters:
messages (List[Dict]): A list of message dictionaries. Each dictionary
may contain a ‘content’ key with a list of items,
some of which might be image URLs.
Returns:
List[Dict]: A new list of message dictionaries with PIL image URLs in the
‘image_url’ key converted to base64 encoded data URIs.
Example Input:
```python
[
{‘content’: [{‘type’: ‘text’, ‘text’: ‘You are a helpful AI assistant.’}], ‘role’: ‘system’},
{‘content’: [
{‘type’: ‘text’, ‘text’: “What’s the breed of this dog here?”},
{‘type’: ‘image_url’, ‘image_url’: {‘url’: a PIL.Image.Image}},
{‘type’: ‘text’, ‘text’: ’.’}],
‘role’: ‘user’}
]
Parameters:
Name | Description |
---|
messages | Type: list[dict[str, typing.Any]] |
Returns:
Type | Description |
---|
list[dict[str, typing.Any]] | List[Dict]: A new list of message dictionaries with PIL image URLs in the ‘image_url’ key converted to base64 encoded data URIs. Example Input: python [ \{'content': [\{'type': 'text', 'text': 'You are a helpful AI assistant.'}], 'role': 'system'}, \{'content': [ \{'type': 'text', 'text': "What's the breed of this dog here?"}, \{'type': 'image_url', 'image_url': \{'url': a PIL.Image.Image}}, \{'type': 'text', 'text': '.'}], 'role': 'user'} ] Example Output: python [ \{'content': [\{'type': 'text', 'text': 'You are a helpful AI assistant.'}], 'role': 'system'}, \{'content': [ \{'type': 'text', 'text': "What's the breed of this dog here?"}, \{'type': 'image_url', 'image_url': \{'url': a B64 Image}}, \{'type': 'text', 'text': '.'}], 'role': 'user'} ] |