Skip to content

Storage

__all__ = ['BaseArtifactStorage', 'TextArtifactStorage', 'BlobArtifactStorage'] module-attribute

BaseArtifactStorage

Bases: ABC

Source code in griptape/memory/task/storage/base_artifact_storage.py
@define
class BaseArtifactStorage(ABC):
    @abstractmethod
    def store_artifact(self, namespace: str, artifact: BaseArtifact) -> None: ...

    @abstractmethod
    def load_artifacts(self, namespace: str) -> ListArtifact: ...

    @abstractmethod
    def can_store(self, artifact: BaseArtifact) -> bool: ...

    @abstractmethod
    def summarize(self, namespace: str) -> TextArtifact | InfoArtifact: ...

    @abstractmethod
    def query(self, namespace: str, query: str, metadata: Any = None) -> TextArtifact | InfoArtifact: ...

can_store(artifact) abstractmethod

Source code in griptape/memory/task/storage/base_artifact_storage.py
@abstractmethod
def can_store(self, artifact: BaseArtifact) -> bool: ...

load_artifacts(namespace) abstractmethod

Source code in griptape/memory/task/storage/base_artifact_storage.py
@abstractmethod
def load_artifacts(self, namespace: str) -> ListArtifact: ...

query(namespace, query, metadata=None) abstractmethod

Source code in griptape/memory/task/storage/base_artifact_storage.py
@abstractmethod
def query(self, namespace: str, query: str, metadata: Any = None) -> TextArtifact | InfoArtifact: ...

store_artifact(namespace, artifact) abstractmethod

Source code in griptape/memory/task/storage/base_artifact_storage.py
@abstractmethod
def store_artifact(self, namespace: str, artifact: BaseArtifact) -> None: ...

summarize(namespace) abstractmethod

Source code in griptape/memory/task/storage/base_artifact_storage.py
@abstractmethod
def summarize(self, namespace: str) -> TextArtifact | InfoArtifact: ...

BlobArtifactStorage

Bases: BaseArtifactStorage

Source code in griptape/memory/task/storage/blob_artifact_storage.py
@define
class BlobArtifactStorage(BaseArtifactStorage):
    blobs: dict[str, list[BlobArtifact]] = field(factory=dict, kw_only=True)

    def can_store(self, artifact: BaseArtifact) -> bool:
        return isinstance(artifact, BlobArtifact)

    def store_artifact(self, namespace: str, artifact: BaseArtifact) -> None:
        if isinstance(artifact, BlobArtifact):
            if namespace not in self.blobs:
                self.blobs[namespace] = []

            self.blobs[namespace].append(artifact)
        else:
            raise ValueError("Artifact must be of instance BlobArtifact")

    def load_artifacts(self, namespace: str) -> ListArtifact:
        return ListArtifact(next((blobs for key, blobs in self.blobs.items() if key == namespace), []))

    def summarize(self, namespace: str) -> InfoArtifact:
        return InfoArtifact("can't summarize artifacts")

    def query(self, namespace: str, query: str, metadata: Any = None) -> InfoArtifact:
        return InfoArtifact("can't query artifacts")

blobs: dict[str, list[BlobArtifact]] = field(factory=dict, kw_only=True) class-attribute instance-attribute

can_store(artifact)

Source code in griptape/memory/task/storage/blob_artifact_storage.py
def can_store(self, artifact: BaseArtifact) -> bool:
    return isinstance(artifact, BlobArtifact)

load_artifacts(namespace)

Source code in griptape/memory/task/storage/blob_artifact_storage.py
def load_artifacts(self, namespace: str) -> ListArtifact:
    return ListArtifact(next((blobs for key, blobs in self.blobs.items() if key == namespace), []))

query(namespace, query, metadata=None)

Source code in griptape/memory/task/storage/blob_artifact_storage.py
def query(self, namespace: str, query: str, metadata: Any = None) -> InfoArtifact:
    return InfoArtifact("can't query artifacts")

store_artifact(namespace, artifact)

Source code in griptape/memory/task/storage/blob_artifact_storage.py
def store_artifact(self, namespace: str, artifact: BaseArtifact) -> None:
    if isinstance(artifact, BlobArtifact):
        if namespace not in self.blobs:
            self.blobs[namespace] = []

        self.blobs[namespace].append(artifact)
    else:
        raise ValueError("Artifact must be of instance BlobArtifact")

summarize(namespace)

Source code in griptape/memory/task/storage/blob_artifact_storage.py
def summarize(self, namespace: str) -> InfoArtifact:
    return InfoArtifact("can't summarize artifacts")

TextArtifactStorage

Bases: BaseArtifactStorage

Source code in griptape/memory/task/storage/text_artifact_storage.py
@define
class TextArtifactStorage(BaseArtifactStorage):
    query_engine: VectorQueryEngine = field(kw_only=True)
    summary_engine: Optional[BaseSummaryEngine] = field(kw_only=True, default=None)
    csv_extraction_engine: Optional[CsvExtractionEngine] = field(kw_only=True, default=None)
    json_extraction_engine: Optional[JsonExtractionEngine] = field(kw_only=True, default=None)

    def can_store(self, artifact: BaseArtifact) -> bool:
        return isinstance(artifact, TextArtifact)

    def store_artifact(self, namespace: str, artifact: BaseArtifact) -> None:
        if isinstance(artifact, TextArtifact):
            self.query_engine.upsert_text_artifact(artifact, namespace)
        else:
            raise ValueError("Artifact must be of instance TextArtifact")

    def load_artifacts(self, namespace: str) -> ListArtifact:
        return self.query_engine.load_artifacts(namespace)

    def summarize(self, namespace: str) -> TextArtifact:
        if self.summary_engine is None:
            raise ValueError("Summary engine is not set.")
        return self.summary_engine.summarize_artifacts(self.load_artifacts(namespace))

    def query(self, namespace: str, query: str, metadata: Any = None) -> TextArtifact:
        return self.query_engine.query(namespace=namespace, query=query, metadata=str(metadata) if metadata else None)

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

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

query_engine: VectorQueryEngine = field(kw_only=True) class-attribute instance-attribute

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

can_store(artifact)

Source code in griptape/memory/task/storage/text_artifact_storage.py
def can_store(self, artifact: BaseArtifact) -> bool:
    return isinstance(artifact, TextArtifact)

load_artifacts(namespace)

Source code in griptape/memory/task/storage/text_artifact_storage.py
def load_artifacts(self, namespace: str) -> ListArtifact:
    return self.query_engine.load_artifacts(namespace)

query(namespace, query, metadata=None)

Source code in griptape/memory/task/storage/text_artifact_storage.py
def query(self, namespace: str, query: str, metadata: Any = None) -> TextArtifact:
    return self.query_engine.query(namespace=namespace, query=query, metadata=str(metadata) if metadata else None)

store_artifact(namespace, artifact)

Source code in griptape/memory/task/storage/text_artifact_storage.py
def store_artifact(self, namespace: str, artifact: BaseArtifact) -> None:
    if isinstance(artifact, TextArtifact):
        self.query_engine.upsert_text_artifact(artifact, namespace)
    else:
        raise ValueError("Artifact must be of instance TextArtifact")

summarize(namespace)

Source code in griptape/memory/task/storage/text_artifact_storage.py
def summarize(self, namespace: str) -> TextArtifact:
    if self.summary_engine is None:
        raise ValueError("Summary engine is not set.")
    return self.summary_engine.summarize_artifacts(self.load_artifacts(namespace))