Skip to content

message

T = TypeVar('T', bound=BaseMessageContent) module-attribute

Message

Bases: BaseMessage

Source code in griptape/common/prompt_stack/messages/message.py
@define
class Message(BaseMessage):
    def __init__(self, content: str | list[BaseMessageContent], **kwargs: Any) -> None:
        if isinstance(content, str):
            content = [TextMessageContent(TextArtifact(value=content))]
        self.__attrs_init__(content, **kwargs)  # pyright: ignore[reportAttributeAccessIssue]

    content: list[BaseMessageContent] = field(metadata={"serializable": True})

    @property
    def value(self) -> Any:
        return self.to_artifact().value

    def __str__(self) -> str:
        return self.to_text()

    def has_all_content_type(self, content_type: type[T]) -> bool:
        return all(isinstance(content, content_type) for content in self.content)

    def has_any_content_type(self, content_type: type[T]) -> bool:
        return any(isinstance(content, content_type) for content in self.content)

    def get_content_type(self, content_type: type[T]) -> list[T]:
        return [content for content in self.content if isinstance(content, content_type)]

    def is_text(self) -> bool:
        return all(isinstance(content, TextMessageContent) for content in self.content)

    def to_text(self) -> str:
        return "".join(
            [content.artifact.to_text() for content in self.content if isinstance(content, TextMessageContent)],
        )

    def to_artifact(self) -> BaseArtifact:
        if len(self.content) == 1:
            return self.content[0].artifact
        else:
            return ListArtifact([content.artifact for content in self.content])

content: list[BaseMessageContent] = field(metadata={'serializable': True}) class-attribute instance-attribute

value: Any property

__init__(content, **kwargs)

Source code in griptape/common/prompt_stack/messages/message.py
def __init__(self, content: str | list[BaseMessageContent], **kwargs: Any) -> None:
    if isinstance(content, str):
        content = [TextMessageContent(TextArtifact(value=content))]
    self.__attrs_init__(content, **kwargs)  # pyright: ignore[reportAttributeAccessIssue]

__str__()

Source code in griptape/common/prompt_stack/messages/message.py
def __str__(self) -> str:
    return self.to_text()

get_content_type(content_type)

Source code in griptape/common/prompt_stack/messages/message.py
def get_content_type(self, content_type: type[T]) -> list[T]:
    return [content for content in self.content if isinstance(content, content_type)]

has_all_content_type(content_type)

Source code in griptape/common/prompt_stack/messages/message.py
def has_all_content_type(self, content_type: type[T]) -> bool:
    return all(isinstance(content, content_type) for content in self.content)

has_any_content_type(content_type)

Source code in griptape/common/prompt_stack/messages/message.py
def has_any_content_type(self, content_type: type[T]) -> bool:
    return any(isinstance(content, content_type) for content in self.content)

is_text()

Source code in griptape/common/prompt_stack/messages/message.py
def is_text(self) -> bool:
    return all(isinstance(content, TextMessageContent) for content in self.content)

to_artifact()

Source code in griptape/common/prompt_stack/messages/message.py
def to_artifact(self) -> BaseArtifact:
    if len(self.content) == 1:
        return self.content[0].artifact
    else:
        return ListArtifact([content.artifact for content in self.content])

to_text()

Source code in griptape/common/prompt_stack/messages/message.py
def to_text(self) -> str:
    return "".join(
        [content.artifact.to_text() for content in self.content if isinstance(content, TextMessageContent)],
    )