From 365e718a09c59a3bbb52acf2148d59111c3f88f6 Mon Sep 17 00:00:00 2001 From: Joan Fontanals Martinez Date: Tue, 31 Jan 2023 17:31:50 +0100 Subject: [PATCH] feat: create Text directly from text Signed-off-by: Joan Fontanals Martinez --- docarray/documents/text.py | 13 +++++++++++++ tests/units/document/test_docs_operators.py | 4 ++++ tests/units/document/test_text_document.py | 15 +++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/units/document/test_text_document.py diff --git a/docarray/documents/text.py b/docarray/documents/text.py index ab128fcd6e4..2883c3c4120 100644 --- a/docarray/documents/text.py +++ b/docarray/documents/text.py @@ -27,6 +27,14 @@ class Text(BaseDocument): model = MyEmbeddingModel() txt_doc.embedding = model(txt_doc.text) + You can initialize directly from a string: + + .. code-block:: python + + from docarray.documents import Text + + txt_doc = Text('hello world') + You can extend this Document: .. code-block:: python @@ -87,6 +95,11 @@ class MultiModalDoc(BaseDocument): url: Optional[TextUrl] = None embedding: Optional[AnyEmbedding] = None + def __init__(self, text: Optional[str] = None, **kwargs): + if 'text' not in kwargs: + kwargs['text'] = text + super().__init__(**kwargs) + @classmethod def validate( cls: Type[T], diff --git a/tests/units/document/test_docs_operators.py b/tests/units/document/test_docs_operators.py index c3600c58fd8..f55660151c3 100644 --- a/tests/units/document/test_docs_operators.py +++ b/tests/units/document/test_docs_operators.py @@ -20,3 +20,7 @@ def test_text_document_operators(): t = Text(text='this is my text document') assert 'text' in t assert 'docarray' not in t + + text = Text() + assert text is not None + assert text.text is None diff --git a/tests/units/document/test_text_document.py b/tests/units/document/test_text_document.py new file mode 100644 index 00000000000..36689a96766 --- /dev/null +++ b/tests/units/document/test_text_document.py @@ -0,0 +1,15 @@ +from docarray.documents import Text + + +def test_text_document_init(): + text = Text('hello world') + assert text.text == 'hello world' + assert text == 'hello world' + + text = Text(text='hello world') + assert text.text == 'hello world' + assert text == 'hello world' + + text = Text() + assert text is not None + assert text.text is None