[docs]classImageReader(ReaderBase):"""A simple image reader that wraps the image into a Document object. This class is only a simple implementation to support multimodal RAG. """
[docs]asyncdef__call__(self,image_url:str|list[str])->list[Document]:"""Read an image and return the wrapped Document object. Args: image_url (`str | list[str]`): The image URL(s) or path(s). Returns: `list[Document]`: A list of Document objects containing the image data. """# Read the image data and wrap it into a Document object.ifisinstance(image_url,str):image_url=[image_url]image_blocks:list[ImageBlock]=[ImageBlock(type="image",source=URLSource(type="url",url=_,),)for_inimage_url]doc_idx=[self.get_doc_id(_)for_inimage_url]return[Document(metadata=DocMetadata(content=image_block,doc_id=doc_id,chunk_id=0,total_chunks=1,),)fordoc_id,image_blockinzip(doc_idx,image_blocks)]
[docs]defget_doc_id(self,image_path:str)->str:"""Generate a document ID based on the image path. Args: image_path (`str`): The image path or URL. Returns: `str`: The generated document ID. """returnhashlib.md5(image_path.encode("utf-8")).hexdigest()