From 82fb44b65bd9049c5610560e9e1ca5d7c80c2f35 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Tue, 27 Feb 2024 19:28:50 +0100 Subject: [PATCH] add serialization methods --- src/example_store/document_store.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/example_store/document_store.py b/src/example_store/document_store.py index 9d2b662..4014b7a 100644 --- a/src/example_store/document_store.py +++ b/src/example_store/document_store.py @@ -4,7 +4,7 @@ import logging from typing import Any, Dict, List, Optional -from haystack.dataclasses import Document +from haystack import Document, default_from_dict, default_to_dict from haystack.document_stores.errors import DuplicateDocumentError, MissingDocumentError from haystack.document_stores.protocols import DuplicatePolicy @@ -126,3 +126,22 @@ def delete_documents(self, document_ids: List[str]) -> None: for doc_id in document_ids: # FIXME msg = f"ID '{doc_id}' not found, cannot delete it." raise MissingDocumentError(msg) + + def to_dict(self) -> Dict[str, Any]: + """ + Serializes this store to a dictionary. You can customise here what goes into the + final serialized format. + """ + data = default_to_dict( + self, + example_param=self.example_param, + ) + return data + + @classmethod + def from_dict(cls, data: Dict[str, Any]) -> "ExampleDocumentStore": + """ + Deserializes the store from a dictionary, if you customised anything in `to_dict`, + you can changed it back here. + """ + return default_from_dict(cls, data)