Source code for langchain.document_loaders.parsers.docai
"""Module contains a PDF parser based on DocAI from Google Cloud.
You need to install two libraries to use this parser:
pip install google-cloud-documentai
pip install google-cloud-documentai-toolbox
"""
import logging
import time
from dataclasses import dataclass
from typing import TYPE_CHECKING, Iterator, List, Optional, Sequence
from langchain.docstore.document import Document
from langchain.document_loaders.base import BaseBlobParser
from langchain.document_loaders.blob_loaders import Blob
from langchain.utils.iter import batch_iterate
if TYPE_CHECKING:
    from google.api_core.operation import Operation
    from google.cloud.documentai import DocumentProcessorServiceClient
logger = logging.getLogger(__name__)
[docs]@dataclass
class DocAIParsingResults:
    """A dataclass to store DocAI parsing results."""
    source_path: str
    parsed_path: str
[docs]class DocAIParser(BaseBlobParser):
[docs]    def __init__(
        self,
        *,
        client: Optional["DocumentProcessorServiceClient"] = None,
        location: Optional[str] = None,
        gcs_output_path: Optional[str] = None,
        processor_name: Optional[str] = None,
    ):
        """Initializes the parser.
        Args:
            client: a DocumentProcessorServiceClient to use
            location: a GCP location where a DOcAI parser is located
            gcs_output_path: a path on GCS to store parsing results
            processor_name: name of a processor
        You should provide either a client or location (and then a client
            would be instantiated).
        """
        if client and location:
            raise ValueError(
                "You should provide either a client or a location but not both "