DocArray is an innovative Python library designed for the representation, transmission, storage, and retrieval of multimodal data. It’s tailor-made for developing multimodal AI applications, seamlessly integrating into the vast Python and machine learning ecosystems. In this article, we will explore what DocArray can do, how to install it, and provide troubleshooting tips to help you make the most of this powerful library.
Installation
To install DocArray directly from the command line, execute the following command:
pip install -U docarray
If you’re using an older version (i.e., DocArray 0.21), make sure to specify it as follows:
pip install docarray==0.21
Getting Started with DocArray
Once installed, you can begin leveraging DocArray for your specific needs. Depending on your background—whether you’re coming from PyTorch, TensorFlow, FastAPI, Jina, or any other framework—DocArray offers multiple pathways for understanding and utilizing its features.
Represent Multimodal Data
DocArray empowers you to represent your data in a way that is inherently suited for machine learning. For example, consider defining a custom data model:
from docarray import BaseDoc
from docarray.typing import TorchTensor, ImageUrl
import torch
class MyDocument(BaseDoc):
description: str
image_url: ImageUrl # supports various types like VideoUrl, AudioUrl, etc.
image_tensor: TorchTensor[1704, 2272, 3] # express tensor shapes!
# Create a Document Vector with multiple instances
from docarray import DocVec
vec = DocVec[MyDocument](
[MyDocument(
description="A cat",
image_url="https://example.com/cat.jpg",
image_tensor=torch.rand(1704, 2272, 3)
) for _ in range(10)]
)
print(vec.image_tensor.shape) # should output: (10, 1704, 2272, 3)
Here’s an analogy to enhance your understanding: imagine you’re an art gallery curator. Each artwork is a MyDocument, with details like the artist’s name (description), the artwork itself (image_url), and a high-resolution file (image_tensor). Your collection (vector of documents) is like the gallery, where you’re able to review all pieces at once, perfectly organized by their dimensions and details.
Data Transmission
DocArray also facilitates the transmission of your data, ensuring compatibility with machine learning frameworks. You can serialize your data to JSON or Protobuf, making it easy to work with other tools or microservices:
from docarray.typing import ImageTorchTensor
class MyDocument(BaseDoc):
description: str
image: ImageTorchTensor[3, 224, 224]
doc = MyDocument(description="An image of a sunset", image=torch.zeros((3, 224, 224)))
# Serialize the document
json_data = doc.json()
proto_data = doc.to_protobuf()
Storage and Retrieval
After modeling your data, you can store it both locally and remotely with Document Stores. DocArray allows easy retrieval of documents from vector databases—ideal for applications needing effective similarity-based search.
from docarray.index import HnswDocumentIndex
index = HnswDocumentIndex[MyDocument](work_dir='./my_index')
index.index(vec) # Index your data for retrieval
Troubleshooting
If you encounter any issues while using DocArray, consider the following troubleshooting suggestions:
- Ensure that all dependencies are correctly installed and up to date, especially TensorFlow and PyTorch.
- Verify the compatibility of your data formats with DocArray’s requirements.
- If serialization fails, check the structure of your documents to make sure they conform to the expected types.
- For persistent issues, consult the [Documentation](https://docs.docarray.org) for additional insights and guidelines.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Final Thoughts
DocArray’s versatile capabilities allow for streamlined data management in multimodal applications. Whether you’re representing complex data types or transmitting them through various frameworks, DocArray has you covered. At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

