Skip to content

Text artifact

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) -> list[float] | None:
        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: list[float] | None 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))