
    dh%                         S r SSKrSSKrSSKrSSKJrJrJrJrJ	r	  SSK
Jr  SSKJrJr  \R                  " \5      r " S S\5      rg)zUtil that calls Arxiv.    N)AnyDictIteratorListOptional)Document)	BaseModelmodel_validatorc                   4   \ rS rSr% Sr\\S'   \\S'   Sr\\S'   Sr	\\S'   S	r
\\S
'   Sr\\S'   S	r\\S'   Sr\\   \S'   S\S\4S jr\" SS9\S\S\4S j5       5       rS\S\4S jrS\S\\   4S jrS\S\4S jrS\S\\   4S jrS\S\\   4S jrSrg)ArxivAPIWrapper   a  Wrapper around ArxivAPI.

To use, you should have the ``arxiv`` python package installed.
https://lukasschwab.me/arxiv.py/index.html
This wrapper will use the Arxiv API to conduct searches and
fetch document summaries. By default, it will return the document summaries
of the top-k results.
If the query is in the form of arxiv identifier
(see https://info.arxiv.org/help/find/index.html), it will return the paper
corresponding to the arxiv identifier.
It limits the Document content by doc_content_chars_max.
Set doc_content_chars_max=None if you don't want to limit the content size.

Attributes:
    top_k_results: number of the top-scored document used for the arxiv tool
    ARXIV_MAX_QUERY_LENGTH: the cut limit on the query used for the arxiv tool.
    continue_on_failure (bool): If True, continue loading other URLs on failure.
    load_max_docs: a limit to the number of loaded documents
    load_all_available_meta:
        if True: the `metadata` of the loaded Documents contains all available
        meta info (see https://lukasschwab.me/arxiv.py/index.html#Result),
        if False: the `metadata` contains only the published date, title,
        authors and summary.
    doc_content_chars_max: an optional cut limit for the length of a document's
        content

Example:
    .. code-block:: python

        from langchain_community.utilities.arxiv import ArxivAPIWrapper
        arxiv = ArxivAPIWrapper(
            top_k_results = 3,
            ARXIV_MAX_QUERY_LENGTH = 300,
            load_max_docs = 3,
            load_all_available_meta = False,
            doc_content_chars_max = 40000
        )
        arxiv.run("tree of thought llm")
arxiv_searcharxiv_exceptions   top_k_resultsi,  ARXIV_MAX_QUERY_LENGTHFcontinue_on_failured   load_max_docsload_all_available_metai  doc_content_chars_maxqueryreturnc                     SnUSU R                    R                  5        H>  n[        R                  " X#5      nU(       d    gUc   eUR	                  S5      U:X  a  M>    g   g)z(Check if a query is an arxiv identifier.z,\d{2}(0[1-9]|1[0-2])\.\d{4,5}(v\d+|)|\d{7}.*NFr   T)r   splitrematchgroup)selfr   arxiv_identifier_pattern
query_itemmatch_results        [/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/arxiv.pyis_arxiv_identifier#ArxivAPIWrapper.is_arxiv_identifier@   sf    #R  =$"="=>DDFJ88$<IL+++%%a(J6 G     before)modevaluesc                      SSK nUR                  US'   UR                  UR                  UR                  4US'   UR
                  US'   U$ ! [         a    [        S5      ef = f)z7Validate that the python package exists in environment.r   Nr   r   arxiv_resultzRCould not import arxiv python package. Please install it with `pip install arxiv`.)arxivSearch
ArxivErrorUnexpectedEmptyPageError	HTTPErrorResultImportError)clsr)   r,   s      r#   validate_environment$ArxivAPIWrapper.validate_environmentL   sx    	%*\\F>"  ..*F%&
 &+\\F>"   	> 	s   AA A"c                    U R                  U5      (       a6  U R                  UR                  5       U R                  S9R	                  5       $ U R                  USU R
                   U R                  S9R	                  5       $ )z6Helper function to fetch arxiv results based on query.)id_listmax_resultsN)r8   )r$   r   r   r   resultsr   r   r   s     r#   _fetch_resultsArxivAPIWrapper._fetch_resultsa   s}    ##E**$$43E3E % gi   /D//0d>P>P ! 

')	r&   c                     U R                  U5      nU Vs/ sHh  n[	        UR
                  UR                  UR                  R                  5       UR                  SR                  S UR                   5       5      S.S9PMj     nnU$ ! U R                   a/  n[        R                  SU 35        [	        SU 3S9/s SnA$ SnAff = fs  snf )a  
Performs an arxiv search and returns list of
documents, with summaries as the content.

If an error occurs or no documents found, error text
is returned instead. Wrapper for
https://lukasschwab.me/arxiv.py/index.html#Search

Args:
    query: a plaintext search query
Arxiv exception: )page_contentN, c              3   6   #    U H  oR                   v   M     g 7fNname.0as     r#   	<genexpr>8ArxivAPIWrapper.get_summaries_as_docs.<locals>.<genexpr>   s     (HA   )zEntry ID	PublishedTitleAuthorsr?   metadata)r;   r   loggererrorr   summaryentry_idupdateddatetitlejoinauthorsr   r   r9   exresultdocss         r#   get_summaries_as_docs%ArxivAPIWrapper.get_summaries_as_docsk   s    	E))G  "
 " #^^ &!'!4!4!6#\\#yy(H(HH	 " 	 
  $$ 	ELL,RD12,=bT*BCDD	E
s#   B	 A.C	C$C=CCc                     U R                  U5      nU Vs/ sH]  nSUR                  R                  5        SUR                   SSR                  S UR                   5       5       SUR                   3PM_     nnU(       a  S	R                  U5      SU R                   $ g
! U R                   a'  n[        R                  SU 35        SU 3s SnA$ SnAff = fs  snf )aA  
Performs an arxiv search and A single string
with the publish date, title, authors, and summary
for each article separated by two newlines.

If an error occurs or no documents found, error text
is returned instead. Wrapper for
https://lukasschwab.me/arxiv.py/index.html#Search

Args:
    query: a plaintext search query
r>   NzPublished: z
Title: z

Authors: r@   c              3   6   #    U H  oR                   v   M     g 7frB   rC   rE   s     r#   rH   &ArxivAPIWrapper.run.<locals>.<genexpr>   s     !A.Q&&.rJ   z

Summary: z

zNo good Arxiv Result was found)r;   r   rP   rQ   rT   rU   rV   rW   rX   rR   r   rY   s         r#   runArxivAPIWrapper.run   s    	,))G "

 "	 &..--/0 1ll^ $		!A&..!AAB C') " 	 
 ;;t$%At'A'ABB3 $$ 	,LL,RD12&rd++	,
s#   B" A#C"C2CCCc                 6    [        U R                  U5      5      $ )aq  
Run Arxiv search and get the article texts plus the article meta information.
See https://lukasschwab.me/arxiv.py/index.html#Search

Returns: a list of documents with the document.page_content in text format

Performs an arxiv search, downloads the top k results as PDFs, loads
them as Documents, and returns them in a List.

Args:
    query: a plaintext search query
)list	lazy_loadr:   s     r#   loadArxivAPIWrapper.load   s     DNN5)**r&   c              #   8  #     SSK n UR                  SS5      R                  SS5      nU R                  U5      nU GHs  n UR                  5       nUR                  U5       nSR                  S U 5       5      nSSS5        U R                  (       a  UR                   [#        UR$                  R'                  5       5      UR(                  UR*                  UR,                  UR.                  UR0                  UR2                   Vs/ sH  oR4                  PM     snS	.nO0 n[#        UR6                  R'                  5       5      UR8                  S
R                  S UR:                   5       5      UR<                  S.UEn[?        WSU R@                   US9v   [B        RD                  " U5        GMv     g! [         a    [        S5      ef = f! U R                   a   n[
        R                  SU5         SnAgSnAff = f! , (       d  f       GN= f! [        UR                   R                  4 a!  n	[
        R                  U	5         Sn	A	GM  Sn	A	f[         a4  n
U R                  (       a  [
        R                  U
5         Sn
A
GMM  U
eSn
A
ff = fs  snf 7f)a]  
Run Arxiv search and get the article texts plus the article meta information.
See https://lukasschwab.me/arxiv.py/index.html#Search

Returns: documents with the document.page_content in text format

Performs an arxiv search, downloads the top k results as PDFs, loads
them as Documents, and returns them.

Args:
    query: a plaintext search query
r   NzGPyMuPDF package not found, please install it with `pip install pymupdf`: -zError on arxiv: %sc              3   >   #    U H  oR                  5       v   M     g 7frB   )get_text)rF   pages     r#   rH   ,ArxivAPIWrapper.lazy_load.<locals>.<genexpr>   s     'MHDHs   )rS   published_first_timecommentjournal_refdoiprimary_category
categorieslinksr@   c              3   6   #    U H  oR                   v   M     g 7frB   rC   rE   s     r#   rH   rp      s     $D^VV^rJ   )rK   rL   rM   SummaryrN   )#fitzr2   replacer;   r   rP   debugdownload_pdfopenrW   FileNotFoundErrorFileDataError	Exceptionr   rQ   r   rS   str	publishedrU   rr   rs   rt   ru   rv   rw   hrefrT   rV   rX   rR   r   r   osremove)r   r   rz   r9   rZ   r[   doc_file_namedoc_filetextf_exelinkextra_metadatarO   s                 r#   rf   ArxivAPIWrapper.lazy_load   s'    		MM#r*223;E))G F%+%8%8%:YY}- "'MH'M MD . ++ &,/0@0@0E0E0G,H%~~#)#5#5!::(.(?(?"("3"34:LLALDiiLA	" "$ !4!4!6799$DV^^$DD!>>	
 !H !">D$>$>?(  IIm$K !  	( 	 $$ 	LL-r2	 .-%tyy'>'>? T" ++LLOG Bs   JF7 3G J!H$H=HBJJBJ7GJH  G;6J;H  J
H	HJH J5I
JJ&JJJJJ N)__name__
__module____qualname____firstlineno____doc__r   __annotations__r   intr   r   boolr   r   r   r   r   r$   r
   classmethodr   r4   r;   r   r   r]   rb   rg   r   rf   __static_attributes__r   r&   r#   r   r      s   &P M3"%C% %%M3$)T)+/8C=/
 
 
 (#$ 3   $&C C 3 4> B4 4 4@+# +$x. +D%s D%x'9 D%r&   r   )r   loggingr   r   typingr   r   r   r   r   langchain_core.documentsr   pydanticr	   r
   	getLoggerr   rP   r   r   r&   r#   <module>r      s;      	 	 6 6 - /			8	$q%i q%r&   