Skip to content

content

Audio

Audio(data: str, format: Literal['mp3', 'wav'])

Bases: ContentPart

Represent an audio in the message.

Source code in src/appl/core/types/content.py
def __init__(self, data: str, format: Literal["mp3", "wav"]) -> None:
    """Initialize the audio with the base64 encoded data and format."""
    super().__init__(type=ContentPartType.AUDIO, data=data, format=format)

from_file classmethod

from_file(file: PathLike) -> Audio

Construct an audio prompt from an audio file.

Source code in src/appl/core/types/content.py
@classmethod
def from_file(cls, file: PathLike) -> "Audio":
    """Construct an audio prompt from an audio file."""
    ext = Path(file).suffix.lower()[1:]
    if ext not in ("mp3", "wav"):
        raise ValueError(f"Unsupported audio format: {ext}")
    with open(file, "rb") as f:
        audio_data = f.read()
        base64_encoded = base64.b64encode(audio_data).decode("utf-8")
    return cls(data=base64_encoded, format=ext)  # type: ignore

from_url classmethod

from_url(url: str, format: Literal['mp3', 'wav']) -> Audio

Construct an audio prompt from an audio URL.

Source code in src/appl/core/types/content.py
@classmethod
def from_url(cls, url: str, format: Literal["mp3", "wav"]) -> "Audio":
    """Construct an audio prompt from an audio URL."""
    audio_data = urlopen(url).read()
    base64_encoded = base64.b64encode(audio_data).decode("utf-8")
    return cls(data=base64_encoded, format=format)

get_dict

get_dict() -> Dict[str, Any]

Return a dict representation of the content part.

Source code in src/appl/core/types/content.py
def get_dict(self) -> Dict[str, Any]:
    """Return a dict representation of the content part."""
    return {"type": self.type.value, self.type.value: self._get_data()}

ContentList

ContentList(
    contents: Union[
        Iterable[ContentPart], Iterator[Dict[str, Any]]
    ]
)

Bases: BaseModel

Represent a list of contents containing text, images and audio.

Source code in src/appl/core/types/content.py
def __init__(
    self, contents: Union[Iterable[ContentPart], Iterator[Dict[str, Any]]]
) -> None:
    """Initialize the content list with a list of contents."""
    res: List[ContentPart] = []
    for c in contents:
        if isinstance(c, ContentPart):
            res.append(c)
        elif isinstance(c, dict):
            if "type" not in c:
                raise ValueError(f"Invalid content: {c}")
            t = ContentPartType(c["type"])
            cls = CONTENT_PART_CLASS_MAP[t]
            kwargs = c[t.value]
            res.append(cls(**kwargs) if isinstance(kwargs, dict) else cls(kwargs))
        else:
            raise ValueError(f"Invalid content: {c}")
    super().__init__(contents=res)

append

append(content: ContentPart) -> None

Append a content to the list.

If the last content is a string, it will be concatenated with the new content.

Source code in src/appl/core/types/content.py
def append(self, content: ContentPart) -> None:
    """Append a content to the list.

    If the last content is a string, it will be concatenated with the new content.
    """
    if is_string(content) and len(self.contents) and is_string(self.contents[-1]):
        self.contents[-1] += content  # type: ignore
    else:
        self.contents.append(content)

extend

extend(contents: list[ContentPart]) -> None

Extend the list with multiple contents.

Source code in src/appl/core/types/content.py
def extend(self, contents: list[ContentPart]) -> None:
    """Extend the list with multiple contents."""
    for content in contents:
        self.append(content)

get_contents

get_contents() -> List[Dict[str, Any]]

Return the contents as a list of dictionaries.

Source code in src/appl/core/types/content.py
def get_contents(self) -> List[Dict[str, Any]]:
    """Return the contents as a list of dictionaries."""
    return [c.get_dict() for c in self.contents]

ContentPart

ContentPart(*args: Any, **kwargs: Any)

Bases: BaseModel, ABC

Represent a part of the message content.

Source code in src/appl/core/types/content.py
def __init__(self, *args: Any, **kwargs: Any) -> None:
    """Initialize the content part."""
    super().__init__(*args, **kwargs)

get_dict

get_dict() -> Dict[str, Any]

Return a dict representation of the content part.

Source code in src/appl/core/types/content.py
def get_dict(self) -> Dict[str, Any]:
    """Return a dict representation of the content part."""
    return {"type": self.type.value, self.type.value: self._get_data()}

ContentPartType

Bases: Enum

The types of the content part.

Image

Image(url: str, detail: Optional[str] = None)

Bases: ContentPart

Represent an image in the message.

See the guide for more information about the detail level.

Source code in src/appl/core/types/content.py
def __init__(self, url: str, detail: Optional[str] = None) -> None:
    """Initialize the image with the URL and detail level.

    See [the guide](https://platform.openai.com/docs/guides/vision/low-or-high-fidelity-image-understanding)
    for more information about the detail level.
    """
    super().__init__(type=ContentPartType.IMAGE, url=url, detail=detail)

from_file classmethod

from_file(
    file: PathLike, detail: Optional[str] = None
) -> Image

Construct an image prompt from an image file.

Source code in src/appl/core/types/content.py
@classmethod
def from_file(cls, file: PathLike, detail: Optional[str] = None) -> "Image":
    """Construct an image prompt from an image file."""
    image = PIL.Image.open(Path(file))
    return cls.from_image(image, detail)  # type: ignore

from_image classmethod

from_image(
    image: ImageFile, detail: Optional[str] = None
) -> Image

Construct an image prompt from a PIL ImageFile.

Source code in src/appl/core/types/content.py
@classmethod
def from_image(cls, image: ImageFile, detail: Optional[str] = None) -> "Image":
    """Construct an image prompt from a PIL ImageFile."""
    buffered = BytesIO()
    # Save the image to the buffer in PNG format
    image.save(buffered, format="PNG")
    # Get the byte data from the buffer
    img_byte = buffered.getvalue()
    img_base64 = base64.b64encode(img_byte).decode("utf-8")
    return cls(url=f"data:image/png;base64,{img_base64}", detail=detail)

get_dict

get_dict() -> Dict[str, Any]

Return a dict representation of the content part.

Source code in src/appl/core/types/content.py
def get_dict(self) -> Dict[str, Any]:
    """Return a dict representation of the content part."""
    return {"type": self.type.value, self.type.value: self._get_data()}

TextContent

TextContent(text: String)

Bases: ContentPart

Represent a text in the message.

Source code in src/appl/core/types/content.py
def __init__(self, text: String) -> None:
    """Initialize the text content."""
    super().__init__(type=ContentPartType.TEXT, text=text)

get_dict

get_dict() -> Dict[str, Any]

Return a dict representation of the content part.

Source code in src/appl/core/types/content.py
def get_dict(self) -> Dict[str, Any]:
    """Return a dict representation of the content part."""
    return {"type": self.type.value, self.type.value: self._get_data()}