
    dhM                       % S r SSKJr  SSKrSSKrSSKJrJr  SSKJ	r	  SSK
JrJrJrJrJrJrJrJr  SSKJr  SSKJr  SS	KJr  SS
KJr  \rS\S'   \R:                  " \5      rSS jr SS jr! " S S\\5      r"\" SSSS9 " S S\"5      5       r#    S            S!S jjr$\" SSSS9 " S S\"5      5       r%\" SSSS9 " S S\"5      5       r&\" SSSS9 " S S\"5      5       r'g)"z,Loader that uses unstructured to load files.    )annotationsN)ABCabstractmethod)Path)IOAnyCallableIteratorListOptionalSequenceUnion)
deprecated)Document)	TypeAlias)
BaseLoaderr   Elementc                   SSK Jn  [        U R                  S5       Vs/ sH  n[	        U5      PM     sn5      nUR                  S5      S   n[        UR                  S5       Vs/ sH  n[	        U5      PM     sn5      nXS:  $ s  snf s  snf )zfCheck if the installed `Unstructured` version exceeds the minimum version
for the feature in question.r   )__version__.-)unstructured.__version__r   tuplesplitint)min_version__unstructured_version__xmin_version_tuple_unstructured_versionunstructured_version_tuples         i/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/document_loaders/unstructured.py"satisfies_min_unstructured_versionr#      s     Q{/@/@/EF/E!s1v/EFG 5::3?B!&.44S9:9AQ9:" &:: G 	;s   B&B
c                B    [        U 5      (       d  [        SU  S35      eg)zSRaise an error if the `Unstructured` version does not exceed the
specified minimum.zunstructured>=z is required in this loader.N)r#   
ValueErrormin_unstructured_versions    r"   validate_unstructured_versionr(   '   s2     ..FGG566RS
 	
 H    c                      \ rS rSrSr  S     SS jjr\SS j5       r\SS j5       rSS jr	SS jr
      SS	 jrS
rg)UnstructuredBaseLoader0   z%Base Loader that uses `Unstructured`.Nc                    SSK n1 SknX;  a  [        SU SU S35      e[        S5      (       d  S	U;   a  UR	                  S	5        U R                  X5        Xl        X0l        U=(       d    / U l        g! [         a    [        S5      ef = f)
Initialize with file path.r   NzQunstructured package not found, please install it with `pip install unstructured`>   pagedsingleelementszGot z# for `mode`, but should be one of ``z0.5.4strategy)	unstructuredImportErrorr%   r#   pop5_check_if_both_mode_and_chunking_strategy_are_by_pagemodeunstructured_kwargspost_processors)selfr8   r:   r9   r4   _valid_modess         r"   __init__UnstructuredBaseLoader.__init__3   s    	 7#tf?~QO  2'::00#''
3BB	
 	#6 .4"1  	- 	s   A6 6Bc                    g)zGet elements.N r;   s    r"   _get_elements$UnstructuredBaseLoader._get_elementsV       r)   c                    g)z$Get file_path metadata if available.Nr@   rA   s    r"   _get_metadata$UnstructuredBaseLoader._get_metadataZ   rD   r)   c                `    U H'  nU R                    H  nUR                  U5        M     M)     U$ 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.
r:   applyr;   r1   elementpost_processors       r"   _post_process_elements-UnstructuredBaseLoader._post_process_elements^   1      G"&"6"6n- #7   r)   c              #    #    U R                  5       nU R                  U5        U R                  S:X  a  U H  nU R                  5       n[	        US5      (       a)  UR                  UR                  R                  5       5        [	        US5      (       a  UR                  US'   UR                  5       R                  S5      (       a"  UR                  5       R                  S5      US'   [        [        U5      US9v   M     gU R                  S:X  a  [        R                  S5        0 n0 nU H  nU R                  5       n[	        US5      (       a)  UR                  UR                  R                  5       5        UR                  SS	5      nXd;  a  [        U5      S
-   XF'   X5U'   Mz  XF==   [        U5      S
-   -  ss'   XV   R                  U5        M     UR                  5        H  n[        XG   XW   S9v   M     gU R                  S:X  aF  U R                  5       nS
R                  U Vs/ sH  n[        U5      PM     sn5      n	[        XS9v   g[!        SU R                   S35      es  snf 7f)z
Load file.r1   metadatacategory
element_id)page_contentrS   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

r0   zmode of z not supported.N)rB   rO   r8   rF   hasattrupdaterS   to_dictrT   getr   strloggerwarningkeysjoinr%   )
r;   r1   rM   rS   	text_dict	meta_dictrW   keyeltexts
             r"   	lazy_load UnstructuredBaseLoader.lazy_loadi   s    %%'##H-99
"#--/ 7J//OOG$4$4$<$<$>?7J//+2+;+;HZ(??$((66-4__->-B-B<-PH\*CL8LL $ YY'!NNX
 )+I35I#--/7J//OOG$4$4$<$<$>?&ll=!< /-0\F-BI*-5k* *c'lV.CC**11(; $" !~~'INY^TT (YY("))+H;;(;(BB(;<D@@x		{/BCC  <s   H,I3.I.1I3c                R    US:X  a!  UR                  S5      S:X  a  [        S5      eg g )Nr/   chunking_strategyby_pageziOnly one of `chunking_strategy='by_page'` or `mode='paged'` may be set. `chunking_strategy` is preferred.)r\   r%   )r;   r8   r9   s      r"   r7   LUnstructuredBaseLoader._check_if_both_mode_and_chunking_strategy_are_by_page   s<     GO#''(;<	I:  J r)   )r8   r:   r9   )r0   N)r8   r]   r:   z$Optional[List[Callable[[str], str]]]r9   r   returnList[Element]rn   dict[str, Any]r1   ro   rn   ro   )rn   zIterator[Document])r8   r]   r9   rq   rn   None)__name__
__module____qualname____firstlineno____doc__r=   r   rB   rF   rO   rg   r7   __static_attributes__r@   r)   r"   r+   r+   0   s    / @D!5!5 >!5  #	!5F   3 3	1Df

.<
	
r)   r+   z0.2.8z1.0z)langchain_unstructured.UnstructuredLoader)sinceremovalalternative_importc                  V   ^  \ rS rSrSrSS.     S	U 4S jjjrS
S jrSS jrSrU =r	$ )UnstructuredFileLoader   a  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
r0   r8   c               6   > Xl         [        TU ]  " SSU0UD6  gr.   r8   Nr@   )	file_pathsuperr=   )r;   r   r8   r9   	__class__s       r"   r=   UnstructuredFileLoader.__init__   s      #:d:&9:r)   c                   SSK Jn  [        U R                  [        5      (       aZ  / nU R                   HF  n[        U[
        5      (       a  [        U5      nUR                  U" SSU0U R                  D65        MH     U$ [        U R                  [
        5      (       a  [        U R                  5      U l        U" SSU R                  0U R                  D6$ )Nr   	partitionfilenamer@   )	unstructured.partition.autor   
isinstancer   listr   r]   extendr9   )r;   r   r1   files       r"   rB   $UnstructuredFileLoader._get_elements   s    9dnnd++&(HdD))t9D	 T4 T4;S;S TU ' O$..$//!$T^^!4QdnnQ8P8PQQr)   c                    SU R                   0$ Nsourcer   rA   s    r"   rF   $UnstructuredFileLoader._get_metadata       $..))r)   r   )r   z'Union[str, List[str], Path, List[Path]]r8   r]   r9   r   rm   rp   )
rt   ru   rv   rw   rx   r=   rB   rF   ry   __classcell__r   s   @r"   r~   r~      sD    @ 	
;:
; 	
;
  #
; 
;R* *r)   r~   c                J   [        U [        5      =n(       a  U  Vs/ sH  n[        U5      PM     n n[        U[        5      (       d  U(       a1  SSKJn  U" SU UUUS.UD6n/ n	U H  n
U	R                  U
5        M     U	$ SSKJn  U" SU b  [        U 5      OSUUUS.UD6$ s  snf )z8Retrieve a list of elements from the `Unstructured API`.r   )partition_multiple_via_api)	filenamesfilesapi_keyapi_url)partition_via_apiN)r   r   r   r   r@   )r   r   r]   r   unstructured.partition.apir   r   r   )r   r   r   r   r9   is_listpathr   _doc_elementsr1   	_elementsr   s               r"   get_elements_from_apir      s     Y--w-+4594SY9	5$!!WI2 
	

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

 "
 	
% 6s   B c                  l   ^  \ rS rSrSrSSSS.         SU 4S jjjrSS jrSS	 jrSS
 jrSr	U =r
$ )UnstructuredAPIFileLoaderi  a%  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
r0   1https://api.unstructuredapp.io/general/v0/general r8   urlr   c                  > [        SS9  Xl        X0l        [        R                  " S5      =(       d    UU l        [        TU ]  " SSU0UD6  g)r.   z0.10.15r&   UNSTRUCTURED_API_KEYr8   Nr@   )r(   r   r   osgetenvr   r   r=   )r;   r   r8   r   r   r9   r   s         r"   r=   "UnstructuredAPIFileLoader.__init__7  sC     	&yI"yy!78CG:d:&9:r)   c                    SU R                   0$ r   r   rA   s    r"   rF   'UnstructuredAPIFileLoader._get_metadataI  r   r)   c                p    [        SU R                  U R                  U R                  S.U R                  D6$ )N)r   r   r   r@   )r   r   r   r   r9   rA   s    r"   rB   'UnstructuredAPIFileLoader._get_elementsL  s9    $ 
nnLLHH
 &&	
 	
r)   c                `    U H'  nU R                    H  nUR                  U5        M     M)     U$ rI   rJ   rL   s       r"   rO   0UnstructuredAPIFileLoader._post_process_elementsT  rQ   r)   )r   r   r   )
r   zUnion[str, List[str]]r8   r]   r   r]   r   r]   r9   r   rp   rm   rr   )rt   ru   rv   rw   rx   r=   rF   rB   rO   ry   r   r   s   @r"   r   r     sb    !N F;(; 	;
 ; ;  #; ;$*
	 	r)   r   c                  `   ^  \ rS rSrSrSS.     S
U 4S jjjrSS jrSS jrSS jrS	r	U =r
$ )UnstructuredFileIOLoaderi`  a  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
r0   r   c               6   > Xl         [        TU ]  " SSU0UD6  gr   )r   r   r=   )r;   r   r8   r9   r   s       r"   r=   !UnstructuredFileIOLoader.__init__  s      	:d:&9:r)   c                J    SSK Jn  U" SSU R                  0U R                  D6$ )Nr   r   r   r@   )r   r   r   r9   )r;   r   s     r"   rB   &UnstructuredFileIOLoader._get_elements  s"    9DdiiD4+C+CDDr)   c                    0 $ Nr@   rA   s    r"   rF   &UnstructuredFileIOLoader._get_metadata      	r)   c                `    U H'  nU R                    H  nUR                  U5        M     M)     U$ rI   rJ   rL   s       r"   rO   /UnstructuredFileIOLoader._post_process_elements  rQ   r)   )r   )r   z	IO[bytes]r8   r]   r9   r   rm   rp   rr   rt   ru   rv   rw   rx   r=   rB   rF   rO   ry   r   r   s   @r"   r   r   `  sI    D 		;	; 		;
  #	; 	;E
	 	r)   r   c                  l   ^  \ rS rSrSrSSSS.         SU 4S jjjrSS jrSS	 jrSS
 jrSr	U =r
$ )UnstructuredAPIFileIOLoaderi  ay  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
r0   r   r   r   c                  > [        U[        5      (       a	  [        SS9  [        SS9  Xl        X0l        [
        R                  " S5      =(       d    UU l        [        TU ]$  " SSU0UD6  g)r.   z0.6.3r&   z0.6.2r   r8   Nr@   )
r   r   r(   r   r   r   r   r   r   r=   )r;   r   r8   r   r   r9   r   s         r"   r=   $UnstructuredAPIFileIOLoader.__init__  sX     dH%%)7K%wG	yy!78CG:d:&9:r)   c                    U R                   R                  S5      (       aQ  [        SU R                  U R                   R	                  S5      U R
                  U R                  S.U R                   D6$ [        S5      e)Nmetadata_filename)r   r   r   r   zLIf partitioning a file via api, metadata_filename must be specified as well.r@   )r9   r\   r   r   r6   r   r   r%   rA   s    r"   rB   )UnstructuredAPIFileIOLoader._get_elements  s{    ##''(;<<( YY22667JK	
 **  @ r)   c                    0 $ r   r@   rA   s    r"   rF   )UnstructuredAPIFileIOLoader._get_metadata  r   r)   c                `    U H'  nU R                    H  nUR                  U5        M     M)     U$ rI   rJ   rL   s       r"   rO   2UnstructuredAPIFileIOLoader._post_process_elements  rQ   r)   )r   r   r   )
r   z%Union[IO[bytes], Sequence[IO[bytes]]]r8   r]   r   r]   r   r]   r9   r   rm   rp   rr   r   r   s   @r"   r   r     sb    "P F;3; 	;
 ; ;  #; ;*	 	r)   r   )r   r]   rn   bool)r'   r]   rn   rs   )NNr   r   )r   z-Union[str, List[str], Path, List[Path], None]r   z+Union[IO[bytes], Sequence[IO[bytes]], None]r   r]   r   r]   r9   r   rn   ro   )(rx   
__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__r^   r#   r(   r+   r~   r   r   r   r   r@   r)   r"   <module>r      s`   2 "  	 #  O O O 6 - ' @ 			8	$;"
vZ vr 
B
9*3 9*
9*z @D8<F	!
<!

5!
 !
 	!

 !
 !
H 
B
J 6 J
JZ 
B
;5 ;
;| 
B
U"8 U
Ur)   