Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docarray/documents/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down
4 changes: 4 additions & 0 deletions tests/units/document/test_docs_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions tests/units/document/test_text_document.py
Original file line number Diff line number Diff line change
@@ -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