
    NgM                       U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZmZmZmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ eZded<    ej        e          Zd/dZ d0dZ! G d dee          Z" eddd           G d de"                      Z#	 	 	 	 d1d2d(Z$ eddd           G d) d*e"                      Z% eddd           G d+ d,e"                      Z& eddd           G d- d.e"                      Z'dS )3z,Loader that uses unstructured to load files.    )annotationsN)ABCabstractmethod)Path)IOAnyCallableIteratorListOptionalSequenceUnion)
deprecated)Document)	TypeAlias)
BaseLoaderr   Elementmin_versionstrreturnboolc                    ddl m} t          d |                     d          D                       }|                    d          d         }t          d |                    d          D                       }||k    S )zjCheck if the installed `Unstructured` version exceeds the minimum version
    for the feature in question.r   )__version__c                ,    g | ]}t          |          S  int.0xs     m/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/document_loaders/unstructured.py
<listcomp>z6satisfies_min_unstructured_version.<locals>.<listcomp>   s    FFF!s1vvFFF    .-c                ,    g | ]}t          |          S r   r   r   s     r!   r"   z6satisfies_min_unstructured_version.<locals>.<listcomp>!   s    :::AQ:::r#   )unstructured.__version__r   tuplesplit)r   __unstructured_version__min_version_tuple_unstructured_versionunstructured_version_tuples        r!   "satisfies_min_unstructured_versionr.      s     QPPPPPFF{/@/@/E/EFFFGG 5::3??B!&::.44S99:::" " &):::r#   min_unstructured_versionNonec                J    t          |           st          d|  d          dS )zWRaise an error if the `Unstructured` version does not exceed the
    specified minimum.zunstructured>=z is required in this loader.N)r.   
ValueErrorr/   s    r!   validate_unstructured_versionr4   '   s@     ..FGG 
S5SSS
 
 	

 
r#   c                  h    e Zd ZdZ	 	 ddd
Zedd            Zedd            ZddZddZ	ddZ
dS )UnstructuredBaseLoaderz%Base Loader that uses `Unstructured`.singleNmoder   post_processors$Optional[List[Callable[[str], str]]]unstructured_kwargsr   c                2   	 ddl }n# t          $ r t          d          w xY wh d}||vrt          d| d| d          t          d          sd	|v r|                    d	           |                     ||           || _        || _        |pg | _        dS )
Initialize with file path.r   NzQunstructured package not found, please install it with `pip install unstructured`>   pagedr7   elementszGot z# for `mode`, but should be one of ``z0.5.4strategy)	unstructuredImportErrorr2   r.   pop5_check_if_both_mode_and_chunking_strategy_are_by_pager8   r;   r9   )selfr8   r9   r;   rB   _valid_modess         r!   __init__zUnstructuredBaseLoader.__init__3   s    	 	 	 	-  	 766|##OtOOOOO   2':: 	4000#''
333BB%	
 	
 	
 	#6 .4"s    !r   List[Element]c                    dS )zGet elements.Nr   rF   s    r!   _get_elementsz$UnstructuredBaseLoader._get_elementsV         r#   dict[str, Any]c                    dS )z$Get file_path metadata if available.Nr   rK   s    r!   _get_metadataz$UnstructuredBaseLoader._get_metadataZ   rM   r#   r?   c                N    |D ]!}| j         D ]}|                    |           "|S zApply post processing functions to extracted unstructured elements.

        Post processing functions are str -> str callables passed
        in using the post_processors kwarg when the loader is instantiated.
        r9   applyrF   r?   elementpost_processors       r!   _post_process_elementsz-UnstructuredBaseLoader._post_process_elements^   D       	. 	.G"&"6 . .n----.r#   Iterator[Document]c              #  V  K   |                                  }|                     |           | j        dk    r|D ]}|                                 }t	          |d          r,|                    |j                                                   t	          |d          r
|j        |d<   |                                	                    d          r*|                                	                    d          |d<   t          t          |          |          V  dS | j        dk    rt                              d           i }i }|D ]}|                                 }t	          |d          r,|                    |j                                                   |	                    dd	          }||vrt          |          d
z   ||<   |||<   ||xx         t          |          d
z   z  cc<   ||                             |           |                                D ]!}t          ||         ||                   V  "dS | j        dk    rH|                                 }d
                    d |D                       }t          ||          V  dS t!          d| j         d          )z
Load file.r?   metadatacategory
element_id)page_contentr\   r>   z`mode='paged'` is deprecated in favor of the 'by_page' chunking strategy. Learn more about chunking here: https://docs.unstructured.io/open-source/core-functionality/chunkingpage_number   z

r7   c                ,    g | ]}t          |          S r   r   )r   els     r!   r"   z4UnstructuredBaseLoader.lazy_load.<locals>.<listcomp>   s    ;;;BB;;;r#   zmode of z not supported.N)rL   rX   r8   rP   hasattrupdater\   to_dictr]   getr   r   loggerwarningkeysjoinr2   )	rF   r?   rV   r\   	text_dict	meta_dictr`   keytexts	            r!   	lazy_loadz UnstructuredBaseLoader.lazy_loadi   s     %%''##H---9
""# 
M 
M--// 7J// @OOG$4$<$<$>$>???7J// <+2+;HZ(??$$((66 Q-4__->->-B-B<-P-PH\*CLL8LLLLLLL
M 
M Y'!!NNX  
 )+I35I# < <--//7J// @OOG$4$<$<$>$>???&ll=!<< i//-0\\F-BIk*-5Ik** k***c'llV.CC***k*11(;;;; !~~'' U UIcNYs^TTTTTTTU UY(""))++H;;;;(;;;<<Dx@@@@@@@@B	BBBCCCr#   r0   c                f    |dk    r(|                     d          dk    rt          d          d S d S )Nr>   chunking_strategyby_pageziOnly one of `chunking_strategy='by_page'` or `mode='paged'` may be set. `chunking_strategy` is preferred.)rh   r2   )rF   r8   r;   s      r!   rE   zLUnstructuredBaseLoader._check_if_both_mode_and_chunking_strategy_are_by_page   sK     GOO#''(;<<	II:   OIIr#   )r7   N)r8   r   r9   r:   r;   r   r   rI   r   rN   r?   rI   r   rI   )r   rZ   )r8   r   r;   rN   r   r0   )__name__
__module____qualname____doc__rH   r   rL   rP   rX   rq   rE   r   r#   r!   r6   r6   0   s        // @D!5 !5 !5 !5 !5F    ^ 3 3 3 ^3	 	 	 	1D 1D 1D 1Df
 
 
 
 
 
r#   r6   z0.2.8z1.0z)langchain_unstructured.UnstructuredLoader)sinceremovalalternative_importc                  :     e Zd ZdZddd fd
ZddZddZ xZS )UnstructuredFileLoadera  Load files using `Unstructured`.

    The file loader uses the unstructured partition function and will automatically
    detect the file type. You can run the loader in different modes: "single",
    "elements", and "paged". The default "single" mode will return a single langchain
    Document object. If you use "elements" mode, the unstructured library will split
    the document into elements such as Title and NarrativeText and return those as
    individual langchain Document objects. In addition to these post-processing modes
    (which are specific to the LangChain Loaders), Unstructured has its own "chunking"
    parameters for post-processing elements into more useful chunks for uses cases such
    as Retrieval Augmented Generation (RAG). You can pass in additional unstructured
    kwargs to configure different unstructured settings.

    Examples
    --------
    from langchain_community.document_loaders import UnstructuredFileLoader

    loader = UnstructuredFileLoader(
        "example.pdf", mode="elements", strategy="fast",
    )
    docs = loader.load()

    References
    ----------
    https://docs.unstructured.io/open-source/core-functionality/partitioning
    https://docs.unstructured.io/open-source/core-functionality/chunking
    r7   r8   	file_path'Union[str, List[str], Path, List[Path]]r8   r   r;   r   c               L    || _          t                      j        dd|i| dS r=   r8   Nr   )r   superrH   )rF   r   r8   r;   	__class__s       r!   rH   zUnstructuredFileLoader.__init__   s5     #::d:&9:::::r#   r   rI   c                x   ddl m} t          | j        t                    rTg }| j        D ]H}t          |t
                    rt          |          }|                     |dd|i| j                   I|S t          | j        t
                    rt          | j                  | _         |dd| j        i| j        S )Nr   	partitionfilenamer   )	unstructured.partition.autor   
isinstancer   listr   r   extendr;   )rF   r   r?   files       r!   rL   z$UnstructuredFileLoader._get_elements   s    999999dnd++ 
	R&(H V VdD)) %t99D		 T T4 T4;S T TUUUUO$.$// 5!$T^!4!49QQdnQ8PQQQr#   rN   c                    d| j         iS Nsourcer   rK   s    r!   rP   z$UnstructuredFileLoader._get_metadata       $.))r#   )r   r   r8   r   r;   r   ru   rv   )rx   ry   rz   r{   rH   rL   rP   __classcell__r   s   @r!   r   r      s         @ 	
; 
; 
; 
; 
; 
; 
; 
;R R R R* * * * * * * *r#   r   1https://api.unstructuredapp.io/general/v0/general r   -Union[str, List[str], Path, List[Path], None]r   +Union[IO[bytes], Sequence[IO[bytes]], None]api_urlapi_keyr;   r   rI   c                    t          | t                    x}rd | D             } t          |t                    s|r1ddlm}  |d| |||d|}g }|D ]}	|                    |	           |S ddlm}
  |
d| t          |           nd|||d|S )	z8Retrieve a list of elements from the `Unstructured API`.c                ,    g | ]}t          |          S r   rc   )r   paths     r!   r"   z)get_elements_from_api.<locals>.<listcomp>   s    5554SYY555r#   r   )partition_multiple_via_api)	filenamesfilesr   r   )partition_via_apiN)r   r   r   r   r   )r   r   r   unstructured.partition.apir   r   r   r   )r   r   r   r   r;   is_listr   _doc_elementsr?   	_elementsr   s              r!   get_elements_from_apir      s    Y---w 6559555	$!! 
W 
IIIIII22 
	
 

 "
 
 & 	' 	'IOOI&&&&@@@@@@   
'0'<S^^^$	
 

 "
 
 	
r#   c                  F     e Zd ZdZddddd fdZddZddZddZ xZS )UnstructuredAPIFileLoadera  Load files using `Unstructured` API.

    By default, the loader makes a call to the hosted Unstructured API. If you are
    running the unstructured API locally, you can change the API rule by passing in the
    url parameter when you initialize the loader. The hosted Unstructured API requires
    an API key. See the links below to learn more about our API offerings and get an
    API key.

    You can run the loader in different modes: "single", "elements", and "paged". The
    default "single" mode will return a single langchain Document object. If you use
    "elements" mode, the unstructured library will split the document into elements such
    as Title and NarrativeText and return those as individual langchain Document
    objects. In addition to these post-processing modes (which are specific to the
    LangChain Loaders), Unstructured has its own "chunking" parameters for
    post-processing elements into more useful chunks for uses cases such as Retrieval
    Augmented Generation (RAG). You can pass in additional unstructured kwargs to
    configure different unstructured settings.

    Examples
    ```python
    from langchain_community.document_loaders import UnstructuredAPIFileLoader

    loader = UnstructuredAPIFileLoader(
        "example.pdf", mode="elements", strategy="fast", api_key="MY_API_KEY",
    )
    docs = loader.load()

    References
    ----------
    https://docs.unstructured.io/api-reference/api-services/sdk
    https://docs.unstructured.io/api-reference/api-services/overview
    https://docs.unstructured.io/open-source/core-functionality/partitioning
    https://docs.unstructured.io/open-source/core-functionality/chunking
    r7   r   r   r8   urlr   r   Union[str, List[str]]r8   r   r   r   r;   r   c                   t          d           || _        || _        t          j        d          p|| _         t                      j        dd|i| dS )r=   z0.10.15r3   UNSTRUCTURED_API_KEYr8   Nr   )r4   r   r   osgetenvr   r   rH   )rF   r   r8   r   r   r;   r   s         r!   rH   z"UnstructuredAPIFileLoader.__init__7  sd     	&yIIII"y!788CG::d:&9:::::r#   r   rN   c                    d| j         iS r   r   rK   s    r!   rP   z'UnstructuredAPIFileLoader._get_metadataI  r   r#   rI   c                J    t          d| j        | j        | j        d| j        S )N)r   r   r   r   )r   r   r   r   r;   rK   s    r!   rL   z'UnstructuredAPIFileLoader._get_elementsL  s:    $ 
nLH
 
 &	
 
 	
r#   r?   c                N    |D ]!}| j         D ]}|                    |           "|S rR   rS   rU   s       r!   rX   z0UnstructuredAPIFileLoader._post_process_elementsT  rY   r#   )
r   r   r8   r   r   r   r   r   r;   r   rv   ru   rw   )	rx   ry   rz   r{   rH   rP   rL   rX   r   r   s   @r!   r   r     s        ! !N F; ; ; ; ; ; ; ;$* * * *
 
 
 
	 	 	 	 	 	 	 	r#   r   c                  B     e Zd ZdZddd fd
ZddZddZddZ xZS )UnstructuredFileIOLoaderaF  Load file-like objects opened in read mode using `Unstructured`.

    The file loader uses the unstructured partition function and will automatically
    detect the file type. You can run the loader in different modes: "single",
    "elements", and "paged". The default "single" mode will return a single langchain
    Document object. If you use "elements" mode, the unstructured library will split
    the document into elements such as Title and NarrativeText and return those as
    individual langchain Document objects. In addition to these post-processing modes
    (which are specific to the LangChain Loaders), Unstructured has its own "chunking"
    parameters for post-processing elements into more useful chunks for uses cases
    such as Retrieval Augmented Generation (RAG). You can pass in additional
    unstructured kwargs to configure different unstructured settings.

    Examples
    --------
    from langchain_community.document_loaders import UnstructuredFileIOLoader

    with open("example.pdf", "rb") as f:
        loader = UnstructuredFileIOLoader(
            f, mode="elements", strategy="fast",
        )
        docs = loader.load()


    References
    ----------
    https://docs.unstructured.io/open-source/core-functionality/partitioning
    https://docs.unstructured.io/open-source/core-functionality/chunking
    r7   r   r   	IO[bytes]r8   r   r;   r   c               L    || _          t                      j        dd|i| dS r   )r   r   rH   )rF   r   r8   r;   r   s       r!   rH   z!UnstructuredFileIOLoader.__init__  s5     	::d:&9:::::r#   r   rI   c                6    ddl m}  |dd| j        i| j        S )Nr   r   r   r   )r   r   r   r;   )rF   r   s     r!   rL   z&UnstructuredFileIOLoader._get_elements  s4    999999yDDdiD4+CDDDr#   rN   c                    i S Nr   rK   s    r!   rP   z&UnstructuredFileIOLoader._get_metadata      	r#   r?   c                N    |D ]!}| j         D ]}|                    |           "|S rR   rS   rU   s       r!   rX   z/UnstructuredFileIOLoader._post_process_elements  rY   r#   )r   r   r8   r   r;   r   ru   rv   rw   	rx   ry   rz   r{   rH   rL   rP   rX   r   r   s   @r!   r   r   `  s         D 		; 	; 	; 	; 	; 	; 	; 	;E E E E
   	 	 	 	 	 	 	 	r#   r   c                  F     e Zd ZdZddddd fdZddZddZddZ xZS )UnstructuredAPIFileIOLoadera  Send file-like objects with `unstructured-client` sdk to the Unstructured API.

    By default, the loader makes a call to the hosted Unstructured API. If you are
    running the unstructured API locally, you can change the API rule by passing in the
    url parameter when you initialize the loader. The hosted Unstructured API requires
    an API key. See the links below to learn more about our API offerings and get an
    API key.

    You can run the loader in different modes: "single", "elements", and "paged". The
    default "single" mode will return a single langchain Document object. If you use
    "elements" mode, the unstructured library will split the document into elements
    such as Title and NarrativeText and return those as individual langchain Document
    objects. In addition to these post-processing modes (which are specific to the
    LangChain Loaders), Unstructured has its own "chunking" parameters for
    post-processing elements into more useful chunks for uses cases such as Retrieval
    Augmented Generation (RAG). You can pass in additional unstructured kwargs to
    configure different unstructured settings.

    Examples
    --------
    from langchain_community.document_loaders import UnstructuredAPIFileLoader

    with open("example.pdf", "rb") as f:
        loader = UnstructuredAPIFileIOLoader(
            f, mode="elements", strategy="fast", api_key="MY_API_KEY",
        )
        docs = loader.load()

    References
    ----------
    https://docs.unstructured.io/api-reference/api-services/sdk
    https://docs.unstructured.io/api-reference/api-services/overview
    https://docs.unstructured.io/open-source/core-functionality/partitioning
    https://docs.unstructured.io/open-source/core-functionality/chunking
    r7   r   r   r   r   %Union[IO[bytes], Sequence[IO[bytes]]]r8   r   r   r   r;   r   c                   t          |t                    rt          d           t          d           || _        || _        t          j        d          p|| _         t                      j	        dd|i| dS )r=   z0.6.3r3   z0.6.2r   r8   Nr   )
r   r   r4   r   r   r   r   r   r   rH   )rF   r   r8   r   r   r;   r   s         r!   rH   z$UnstructuredAPIFileIOLoader.__init__  s     dH%% 	L)7KKKK%wGGGG	y!788CG::d:&9:::::r#   r   rI   c                    | j                             d          r=t          d| j        | j                             d          | j        | j        d| j         S t          d          )Nmetadata_filename)r   r   r   r   zLIf partitioning a file via api, metadata_filename must be specified as well.r   )r;   rh   r   r   rD   r   r   r2   rK   s    r!   rL   z)UnstructuredAPIFileIOLoader._get_elements  s    #''(;<< 	( Y2667JKK	 
 *   @  r#   rN   c                    i S r   r   rK   s    r!   rP   z)UnstructuredAPIFileIOLoader._get_metadata  r   r#   r?   c                N    |D ]!}| j         D ]}|                    |           "|S rR   rS   rU   s       r!   rX   z2UnstructuredAPIFileIOLoader._post_process_elements  rY   r#   )
r   r   r8   r   r   r   r   r   r;   r   ru   rv   rw   r   r   s   @r!   r   r     s        " "P F; ; ; ; ; ; ; ;*      	 	 	 	 	 	 	 	r#   r   )r   r   r   r   )r/   r   r   r0   )NNr   r   )r   r   r   r   r   r   r   r   r;   r   r   rI   )(r{   
__future__r   loggingr   abcr   r   pathlibr   typingr   r   r	   r
   r   r   r   r   langchain_core._api.deprecationr   langchain_core.documentsr   typing_extensionsr   )langchain_community.document_loaders.baser   r   __annotations__	getLogger__file__ri   r.   r4   r6   r   r   r   r   r   r   r#   r!   <module>r      s   2 2 2 " " " " " "  				 # # # # # # # #       O O O O O O O O O O O O O O O O O O O O 6 6 6 6 6 6 - - - - - - ' ' ' ' ' ' @ @ @ @ @ @    		8	$	$; ; ; ;"
 
 
 
v v v v vZ v v vr 
B  
9* 9* 9* 9* 9*3 9* 9* 
9*z @D8<F	!
 !
 !
 !
 !
H 
B  
J J J J J 6 J J 
JZ 
B  
; ; ; ; ;5 ; ; 
;| 
B  
U U U U U"8 U U 
U U Ur#   