Skip to content

base_message

BaseMessage

Bases: ABC, SerializableMixin

Source code in griptape/common/prompt_stack/messages/base_message.py
@define
class BaseMessage(ABC, SerializableMixin):
    @define
    class Usage(SerializableMixin):
        input_tokens: Optional[float] = field(kw_only=True, default=None, metadata={"serializable": True})
        output_tokens: Optional[float] = field(kw_only=True, default=None, metadata={"serializable": True})

        @property
        def total_tokens(self) -> float:
            return (self.input_tokens or 0) + (self.output_tokens or 0)

        def __add__(self, other: BaseMessage.Usage) -> BaseMessage.Usage:
            return BaseMessage.Usage(
                input_tokens=(self.input_tokens or 0) + (other.input_tokens or 0),
                output_tokens=(self.output_tokens or 0) + (other.output_tokens or 0),
            )

    USER_ROLE = "user"
    ASSISTANT_ROLE = "assistant"
    SYSTEM_ROLE = "system"

    content: list[Union[BaseMessageContent, BaseDeltaMessageContent]] = field(metadata={"serializable": True})
    role: str = field(kw_only=True, metadata={"serializable": True})
    usage: Usage = field(kw_only=True, default=Factory(lambda: BaseMessage.Usage()), metadata={"serializable": True})

    def is_system(self) -> bool:
        return self.role == self.SYSTEM_ROLE

    def is_user(self) -> bool:
        return self.role == self.USER_ROLE

    def is_assistant(self) -> bool:
        return self.role == self.ASSISTANT_ROLE

ASSISTANT_ROLE = 'assistant' class-attribute instance-attribute

SYSTEM_ROLE = 'system' class-attribute instance-attribute

USER_ROLE = 'user' class-attribute instance-attribute

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

role: str = field(kw_only=True, metadata={'serializable': True}) class-attribute instance-attribute

usage: Usage = field(kw_only=True, default=Factory(lambda: BaseMessage.Usage()), metadata={'serializable': True}) class-attribute instance-attribute

Usage

Bases: SerializableMixin

Source code in griptape/common/prompt_stack/messages/base_message.py
@define
class Usage(SerializableMixin):
    input_tokens: Optional[float] = field(kw_only=True, default=None, metadata={"serializable": True})
    output_tokens: Optional[float] = field(kw_only=True, default=None, metadata={"serializable": True})

    @property
    def total_tokens(self) -> float:
        return (self.input_tokens or 0) + (self.output_tokens or 0)

    def __add__(self, other: BaseMessage.Usage) -> BaseMessage.Usage:
        return BaseMessage.Usage(
            input_tokens=(self.input_tokens or 0) + (other.input_tokens or 0),
            output_tokens=(self.output_tokens or 0) + (other.output_tokens or 0),
        )
input_tokens: Optional[float] = field(kw_only=True, default=None, metadata={'serializable': True}) class-attribute instance-attribute
output_tokens: Optional[float] = field(kw_only=True, default=None, metadata={'serializable': True}) class-attribute instance-attribute
total_tokens: float property
__add__(other)
Source code in griptape/common/prompt_stack/messages/base_message.py
def __add__(self, other: BaseMessage.Usage) -> BaseMessage.Usage:
    return BaseMessage.Usage(
        input_tokens=(self.input_tokens or 0) + (other.input_tokens or 0),
        output_tokens=(self.output_tokens or 0) + (other.output_tokens or 0),
    )

is_assistant()

Source code in griptape/common/prompt_stack/messages/base_message.py
def is_assistant(self) -> bool:
    return self.role == self.ASSISTANT_ROLE

is_system()

Source code in griptape/common/prompt_stack/messages/base_message.py
def is_system(self) -> bool:
    return self.role == self.SYSTEM_ROLE

is_user()

Source code in griptape/common/prompt_stack/messages/base_message.py
def is_user(self) -> bool:
    return self.role == self.USER_ROLE