Skip to content

Artifacts

__all__ = ['BaseArtifact', 'ErrorArtifact', 'InfoArtifact', 'TextArtifact', 'BlobArtifact', 'CsvRowArtifact', 'ListArtifact'] module-attribute

BaseArtifact

Bases: ABC

Source code in griptape/griptape/artifacts/base_artifact.py
@define
class BaseArtifact(ABC):
    id: str = field(default=Factory(lambda: uuid.uuid4().hex), kw_only=True)
    name: str = field(default=Factory(lambda self: self.id, takes_self=True), kw_only=True)
    value: any = field()
    type: str = field(default=Factory(lambda self: self.__class__.__name__, takes_self=True), kw_only=True)

    @classmethod
    def value_to_bytes(cls, value: any) -> bytes:
        if isinstance(value, bytes):
            return value
        else:
            return str(value).encode()

    @classmethod
    def value_to_dict(cls, value: any) -> dict:
        if isinstance(value, dict):
            dict_value = value
        else:
            dict_value = json.loads(value)

        return {k: v for k, v in dict_value.items()}

    @classmethod
    def from_dict(cls, artifact_dict: dict) -> BaseArtifact:
        from griptape.schemas import (
            TextArtifactSchema,
            InfoArtifactSchema,
            ErrorArtifactSchema,
            BlobArtifactSchema,
            CsvRowArtifactSchema,
            ListArtifactSchema,
        )

        class_registry.register("TextArtifact", TextArtifactSchema)
        class_registry.register("InfoArtifact", InfoArtifactSchema)
        class_registry.register("ErrorArtifact", ErrorArtifactSchema)
        class_registry.register("BlobArtifact", BlobArtifactSchema)
        class_registry.register("CsvRowArtifact", CsvRowArtifactSchema)
        class_registry.register("ListArtifact", ListArtifactSchema)

        try:
            return class_registry.get_class(artifact_dict["type"])().load(artifact_dict)
        except RegistryError:
            raise ValueError("Unsupported artifact type")

    @classmethod
    def from_json(cls, artifact_str: str) -> BaseArtifact:
        return cls.from_dict(json.loads(artifact_str))

    def __bool__(self) -> bool:
        return bool(self.value)

    def __len__(self) -> int:
        return len(self.value)

    def __str__(self):
        return json.dumps(self.to_dict())

    def to_json(self) -> str:
        return json.dumps(self.to_dict())

    @abstractmethod
    def to_text(self) -> str:
        ...

    @abstractmethod
    def to_dict(self) -> dict:
        ...

    @abstractmethod
    def __add__(self, other: BaseArtifact) -> BaseArtifact:
        ...

id: str = field(default=Factory(lambda : uuid.uuid4().hex), kw_only=True) class-attribute instance-attribute

name: str = field(default=Factory(lambda : self.id, takes_self=True), kw_only=True) class-attribute instance-attribute

type: str = field(default=Factory(lambda : self.__class__.__name__, takes_self=True), kw_only=True) class-attribute instance-attribute

value: any = field() class-attribute instance-attribute

__add__(other) abstractmethod

Source code in griptape/griptape/artifacts/base_artifact.py
@abstractmethod
def __add__(self, other: BaseArtifact) -> BaseArtifact:
    ...

__bool__()

Source code in griptape/griptape/artifacts/base_artifact.py
def __bool__(self) -> bool:
    return bool(self.value)

__len__()

Source code in griptape/griptape/artifacts/base_artifact.py
def __len__(self) -> int:
    return len(self.value)

__str__()

Source code in griptape/griptape/artifacts/base_artifact.py
def __str__(self):
    return json.dumps(self.to_dict())

from_dict(artifact_dict) classmethod

Source code in griptape/griptape/artifacts/base_artifact.py
@classmethod
def from_dict(cls, artifact_dict: dict) -> BaseArtifact:
    from griptape.schemas import (
        TextArtifactSchema,
        InfoArtifactSchema,
        ErrorArtifactSchema,
        BlobArtifactSchema,
        CsvRowArtifactSchema,
        ListArtifactSchema,
    )

    class_registry.register("TextArtifact", TextArtifactSchema)
    class_registry.register("InfoArtifact", InfoArtifactSchema)
    class_registry.register("ErrorArtifact", ErrorArtifactSchema)
    class_registry.register("BlobArtifact", BlobArtifactSchema)
    class_registry.register("CsvRowArtifact", CsvRowArtifactSchema)
    class_registry.register("ListArtifact", ListArtifactSchema)

    try:
        return class_registry.get_class(artifact_dict["type"])().load(artifact_dict)
    except RegistryError:
        raise ValueError("Unsupported artifact type")

from_json(artifact_str) classmethod

Source code in griptape/griptape/artifacts/base_artifact.py
@classmethod
def from_json(cls, artifact_str: str) -> BaseArtifact:
    return cls.from_dict(json.loads(artifact_str))

to_dict() abstractmethod

Source code in griptape/griptape/artifacts/base_artifact.py
@abstractmethod
def to_dict(self) -> dict:
    ...

to_json()

Source code in griptape/griptape/artifacts/base_artifact.py
def to_json(self) -> str:
    return json.dumps(self.to_dict())

to_text() abstractmethod

Source code in griptape/griptape/artifacts/base_artifact.py
@abstractmethod
def to_text(self) -> str:
    ...

value_to_bytes(value) classmethod

Source code in griptape/griptape/artifacts/base_artifact.py
@classmethod
def value_to_bytes(cls, value: any) -> bytes:
    if isinstance(value, bytes):
        return value
    else:
        return str(value).encode()

value_to_dict(value) classmethod

Source code in griptape/griptape/artifacts/base_artifact.py
@classmethod
def value_to_dict(cls, value: any) -> dict:
    if isinstance(value, dict):
        dict_value = value
    else:
        dict_value = json.loads(value)

    return {k: v for k, v in dict_value.items()}

BlobArtifact

Bases: BaseArtifact

Source code in griptape/griptape/artifacts/blob_artifact.py
@define(frozen=True)
class BlobArtifact(BaseArtifact):
    value: bytes = field(converter=BaseArtifact.value_to_bytes)
    dir_name: Optional[str] = field(default=None, kw_only=True)
    encoding: str = field(default="utf-8", kw_only=True)
    encoding_error_handler: str = field(default="strict", kw_only=True)

    def __add__(self, other: BlobArtifact) -> BlobArtifact:
        return BlobArtifact(self.value + other.value, name=self.name)

    @property
    def full_path(self) -> str:
        return os.path.join(self.dir_name, self.name) if self.dir_name else self.name

    def to_text(self) -> str:
        return self.value.decode(encoding=self.encoding, errors=self.encoding_error_handler)

    def to_dict(self) -> dict:
        from griptape.schemas import BlobArtifactSchema

        return dict(BlobArtifactSchema().dump(self))

dir_name: Optional[str] = field(default=None, kw_only=True) class-attribute instance-attribute

encoding: str = field(default='utf-8', kw_only=True) class-attribute instance-attribute

encoding_error_handler: str = field(default='strict', kw_only=True) class-attribute instance-attribute

full_path: str property

value: bytes = field(converter=BaseArtifact.value_to_bytes) class-attribute instance-attribute

__add__(other)

Source code in griptape/griptape/artifacts/blob_artifact.py
def __add__(self, other: BlobArtifact) -> BlobArtifact:
    return BlobArtifact(self.value + other.value, name=self.name)

to_dict()

Source code in griptape/griptape/artifacts/blob_artifact.py
def to_dict(self) -> dict:
    from griptape.schemas import BlobArtifactSchema

    return dict(BlobArtifactSchema().dump(self))

to_text()

Source code in griptape/griptape/artifacts/blob_artifact.py
def to_text(self) -> str:
    return self.value.decode(encoding=self.encoding, errors=self.encoding_error_handler)

CsvRowArtifact

Bases: TextArtifact

Source code in griptape/griptape/artifacts/csv_row_artifact.py
@define(frozen=True)
class CsvRowArtifact(TextArtifact):
    value: dict[str, str] = field(converter=BaseArtifact.value_to_dict)
    delimiter: str = field(default=",", kw_only=True)

    def __add__(self, other: CsvRowArtifact) -> CsvRowArtifact:
        return CsvRowArtifact(self.value | other.value)

    def to_text(self) -> str:
        with io.StringIO() as csvfile:
            writer = csv.DictWriter(
                csvfile, fieldnames=self.value.keys(), quoting=csv.QUOTE_MINIMAL, delimiter=self.delimiter
            )

            writer.writerow(self.value)

            return csvfile.getvalue().strip()

    def to_dict(self) -> dict:
        from griptape.schemas import CsvRowArtifactSchema

        return dict(CsvRowArtifactSchema().dump(self))

    def __bool__(self) -> bool:
        return len(self) > 0

delimiter: str = field(default=',', kw_only=True) class-attribute instance-attribute

value: dict[str, str] = field(converter=BaseArtifact.value_to_dict) class-attribute instance-attribute

__add__(other)

Source code in griptape/griptape/artifacts/csv_row_artifact.py
def __add__(self, other: CsvRowArtifact) -> CsvRowArtifact:
    return CsvRowArtifact(self.value | other.value)

__bool__()

Source code in griptape/griptape/artifacts/csv_row_artifact.py
def __bool__(self) -> bool:
    return len(self) > 0

to_dict()

Source code in griptape/griptape/artifacts/csv_row_artifact.py
def to_dict(self) -> dict:
    from griptape.schemas import CsvRowArtifactSchema

    return dict(CsvRowArtifactSchema().dump(self))

to_text()

Source code in griptape/griptape/artifacts/csv_row_artifact.py
def to_text(self) -> str:
    with io.StringIO() as csvfile:
        writer = csv.DictWriter(
            csvfile, fieldnames=self.value.keys(), quoting=csv.QUOTE_MINIMAL, delimiter=self.delimiter
        )

        writer.writerow(self.value)

        return csvfile.getvalue().strip()

ErrorArtifact

Bases: BaseArtifact

Source code in griptape/griptape/artifacts/error_artifact.py
@define(frozen=True)
class ErrorArtifact(BaseArtifact):
    value: str = field(converter=str)

    def __add__(self, other: ErrorArtifact) -> ErrorArtifact:
        return ErrorArtifact(self.value + other.value)

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

    def to_dict(self) -> dict:
        from griptape.schemas import ErrorArtifactSchema

        return dict(ErrorArtifactSchema().dump(self))

value: str = field(converter=str) class-attribute instance-attribute

__add__(other)

Source code in griptape/griptape/artifacts/error_artifact.py
def __add__(self, other: ErrorArtifact) -> ErrorArtifact:
    return ErrorArtifact(self.value + other.value)

to_dict()

Source code in griptape/griptape/artifacts/error_artifact.py
def to_dict(self) -> dict:
    from griptape.schemas import ErrorArtifactSchema

    return dict(ErrorArtifactSchema().dump(self))

to_text()

Source code in griptape/griptape/artifacts/error_artifact.py
def to_text(self) -> str:
    return self.value

InfoArtifact

Bases: BaseArtifact

Source code in griptape/griptape/artifacts/info_artifact.py
@define(frozen=True)
class InfoArtifact(BaseArtifact):
    value: str = field(converter=str)

    def __add__(self, other: InfoArtifact) -> InfoArtifact:
        return InfoArtifact(self.value + other.value)

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

    def to_dict(self) -> dict:
        from griptape.schemas import InfoArtifactSchema

        return dict(InfoArtifactSchema().dump(self))

value: str = field(converter=str) class-attribute instance-attribute

__add__(other)

Source code in griptape/griptape/artifacts/info_artifact.py
def __add__(self, other: InfoArtifact) -> InfoArtifact:
    return InfoArtifact(self.value + other.value)

to_dict()

Source code in griptape/griptape/artifacts/info_artifact.py
def to_dict(self) -> dict:
    from griptape.schemas import InfoArtifactSchema

    return dict(InfoArtifactSchema().dump(self))

to_text()

Source code in griptape/griptape/artifacts/info_artifact.py
def to_text(self) -> str:
    return self.value

ListArtifact

Bases: BaseArtifact

Source code in griptape/griptape/artifacts/list_artifact.py
@define
class ListArtifact(BaseArtifact):
    value: list[BaseArtifact] = field(factory=list)
    item_separator: str = field(default="\n\n", kw_only=True)

    @value.validator
    def validate_value(self, _, value: list[BaseArtifact]) -> None:
        if len(value) > 0:
            first_type = type(value[0])

            if not all(isinstance(v, first_type) for v in value):
                raise ValueError(f"list elements in 'value' are not the same type")

    @property
    def child_type(self) -> Optional[type]:
        if self.value:
            return type(self.value[0])
        else:
            return None

    def __bool__(self) -> bool:
        return len(self) > 0

    def to_text(self) -> str:
        return self.item_separator.join([v.to_text() for v in self.value])

    def to_dict(self) -> dict:
        from griptape.schemas import ListArtifactSchema

        return dict(ListArtifactSchema().dump(self))

    def __add__(self, other: BaseArtifact) -> BaseArtifact:
        return ListArtifact(self.value + other.value)

    def is_type(self, target_type: type) -> bool:
        if self.value:
            return isinstance(self.value[0], target_type)
        else:
            return False

    def has_items(self) -> bool:
        return len(self) > 0

child_type: Optional[type] property

item_separator: str = field(default='\n\n', kw_only=True) class-attribute instance-attribute

value: list[BaseArtifact] = field(factory=list) class-attribute instance-attribute

__add__(other)

Source code in griptape/griptape/artifacts/list_artifact.py
def __add__(self, other: BaseArtifact) -> BaseArtifact:
    return ListArtifact(self.value + other.value)

__bool__()

Source code in griptape/griptape/artifacts/list_artifact.py
def __bool__(self) -> bool:
    return len(self) > 0

has_items()

Source code in griptape/griptape/artifacts/list_artifact.py
def has_items(self) -> bool:
    return len(self) > 0

is_type(target_type)

Source code in griptape/griptape/artifacts/list_artifact.py
def is_type(self, target_type: type) -> bool:
    if self.value:
        return isinstance(self.value[0], target_type)
    else:
        return False

to_dict()

Source code in griptape/griptape/artifacts/list_artifact.py
def to_dict(self) -> dict:
    from griptape.schemas import ListArtifactSchema

    return dict(ListArtifactSchema().dump(self))

to_text()

Source code in griptape/griptape/artifacts/list_artifact.py
def to_text(self) -> str:
    return self.item_separator.join([v.to_text() for v in self.value])

validate_value(_, value)

Source code in griptape/griptape/artifacts/list_artifact.py
@value.validator
def validate_value(self, _, value: list[BaseArtifact]) -> None:
    if len(value) > 0:
        first_type = type(value[0])

        if not all(isinstance(v, first_type) for v in value):
            raise ValueError(f"list elements in 'value' are not the same type")

TextArtifact

Bases: BaseArtifact

Source code in griptape/griptape/artifacts/text_artifact.py
@define
class TextArtifact(BaseArtifact):
    value: str = field(converter=str)
    encoding: str = field(default="utf-8", kw_only=True)
    encoding_error_handler: str = field(default="strict", kw_only=True)
    __embedding: list[float] = field(factory=list, kw_only=True)

    @property
    def embedding(self) -> Optional[list[float]]:
        return None if len(self.__embedding) == 0 else self.__embedding

    def __add__(self, other: TextArtifact) -> TextArtifact:
        return TextArtifact(self.value + other.value)

    def __bool__(self) -> bool:
        return bool(self.value.strip())

    def generate_embedding(self, driver: BaseEmbeddingDriver) -> list[float]:
        self.__embedding.clear()
        self.__embedding.extend(driver.embed_string(str(self.value)))

        return self.embedding

    def token_count(self, tokenizer: BaseTokenizer) -> int:
        return tokenizer.count_tokens(str(self.value))

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

    def to_bytes(self) -> bytes:
        return self.value.encode(encoding=self.encoding, errors=self.encoding_error_handler)

    def to_dict(self) -> dict:
        from griptape.schemas import TextArtifactSchema

        return dict(TextArtifactSchema().dump(self))

__embedding: list[float] = field(factory=list, kw_only=True) class-attribute instance-attribute

embedding: Optional[list[float]] property

encoding: str = field(default='utf-8', kw_only=True) class-attribute instance-attribute

encoding_error_handler: str = field(default='strict', kw_only=True) class-attribute instance-attribute

value: str = field(converter=str) class-attribute instance-attribute

__add__(other)

Source code in griptape/griptape/artifacts/text_artifact.py
def __add__(self, other: TextArtifact) -> TextArtifact:
    return TextArtifact(self.value + other.value)

__bool__()

Source code in griptape/griptape/artifacts/text_artifact.py
def __bool__(self) -> bool:
    return bool(self.value.strip())

generate_embedding(driver)

Source code in griptape/griptape/artifacts/text_artifact.py
def generate_embedding(self, driver: BaseEmbeddingDriver) -> list[float]:
    self.__embedding.clear()
    self.__embedding.extend(driver.embed_string(str(self.value)))

    return self.embedding

to_bytes()

Source code in griptape/griptape/artifacts/text_artifact.py
def to_bytes(self) -> bytes:
    return self.value.encode(encoding=self.encoding, errors=self.encoding_error_handler)

to_dict()

Source code in griptape/griptape/artifacts/text_artifact.py
def to_dict(self) -> dict:
    from griptape.schemas import TextArtifactSchema

    return dict(TextArtifactSchema().dump(self))

to_text()

Source code in griptape/griptape/artifacts/text_artifact.py
def to_text(self) -> str:
    return self.value

token_count(tokenizer)

Source code in griptape/griptape/artifacts/text_artifact.py
def token_count(self, tokenizer: BaseTokenizer) -> int:
    return tokenizer.count_tokens(str(self.value))