chatflow.media

This module implements utility classes for formatting/printing agent messages.

class agentopera.chatflow.media.PilImage(image: Image)[source]

Bases: Image

image class based on PIL.

this class provides functions to create, convert and operate images, and can be used to exchange image data in the message system.

Example

Loading an image from a URL:

from agentopera.media import Image
from PIL import Image as PILImage
import aiohttp
import asyncio


async def from_url(url: str) -> Image:
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            content = await response.read()
            return Image.from_pil(PILImage.open(BytesIO(content)))


image = asyncio.run(from_url("https://example.com/image"))
classmethod from_pil(pil_image: Image) Image[source]

create image object from PIL image

classmethod from_uri(uri: str) Image[source]

create image object from image uri

classmethod from_base64(base64_str: str) Image[source]

create image object from base64 encoded string

to_base64() str[source]

convert image to base64 encoded string

classmethod from_file(file_path: Path) Image[source]

create image object from file path

property data_uri: str

get data uri of image

to_openai_format(detail: Literal['auto', 'low', 'high'] = 'auto') Dict[str, Any][source]

convert image to openai api compatible format

Parameters:

detail – detail level of image, can be “auto”, “low” or “high”

Returns:

contains image url and detail level

Return type:

dict