
    7|hP                     R    d Z ddlmZmZmZ ddlmZ ddlmZm	Z	m
Z
  G d de      Zy)z&Util that calls Google Scholar Search.    )AnyDictOptional)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validatorc                       e Zd ZU dZdZeed<   dZeed<   dZ	eed<   dZ
ee   ed	<   dZeed
<    ed      Z ed      ededefd              ZdedefdZy)GoogleScholarAPIWrappera  Wrapper for Google Scholar API

    You can create serpapi key by signing up at: https://serpapi.com/users/sign_up.

    The wrapper uses the serpapi python package:
    https://serpapi.com/integrations/python#search-google-scholar

    To use, you should have the environment variable ``SERP_API_KEY``
    set with your API key, or pass `serp_api_key` as a named parameter
    to the constructor.

    Attributes:
        top_k_results: number of results to return from google-scholar query search.
            By default it returns top 10 results.
        hl: attribute defines the language to use for the Google Scholar search.
            It's a two-letter language code.
            (e.g., en for English, es for Spanish, or fr for French). Head to the
            Google languages page for a full list of supported Google languages:
            https://serpapi.com/google-languages

        lr: attribute defines one or multiple languages to limit the search to.
            It uses lang_{two-letter language code} to specify languages
            and | as a delimiter. (e.g., lang_fr|lang_de will only search French
            and German pages). Head to the Google lr languages for a full
            list of supported languages: https://serpapi.com/google-lr-languages

     Example:
        .. code-block:: python

        from langchain_community.utilities import GoogleScholarAPIWrapper
        google_scholar = GoogleScholarAPIWrapper()
        google_scholar.run('langchain')
    
   top_k_resultsenhllang_enlrNserp_api_keygoogle_scholar_engineforbid)extrabefore)modevaluesreturnc                     t        |dd      }||d<   	 ddlm} ||_        ||d<   |S # t        $ r t        d      w xY w)z?Validate that api key and python package exists in environment.r   SERP_API_KEYr   )GoogleScholarSearchzigoogle-search-results is not installed. Please install it with `pip install google-search-results>=2.4.2`r   )r   serpapir   ImportErrorr   )clsr   r   r   s       k/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/google_scholar.pyvalidate_environmentz,GoogleScholarAPIWrapper.validate_environment6   sb     ,FNNS!-~	3 ,8(*=&'  	 	s   ( =queryc                 8   g }d}|t        | j                  dz
  d      k  r| j                  ||| j                  t	        | j                  d      | j
                  d      j                         j                  dg       }|j                  |       |sn"|dz  }|t        | j                  dz
  d      k  r| j                  dz  dk7  rn|dkD  ri|rg| j                  ||| j                  dz  | j                  | j
                  d      j                         j                  dg       }|j                  |       |sy|D cg c]  }d|j                  d	d
       ddj                  |j                  di       j                  dg       D cg c]  }|j                  d       c}       d|j                  di       j                  dd
       d|j                  di       j                  di       j                  dd
        }}}dj                  |      S c c}w c c}}w )z6Run query through GoogleSearchScholar and parse resultr         )qstartr   numr   organic_results)r&   r'   r(   r   r   z'No good Google Scholar Result was foundzTitle: title z

Authors: ,publication_infoauthorsnamez

Summary: summaryz
Total-Citations: inline_linkscited_bytotalz

)
maxr   r   r   minr   get_dictgetextendjoin)selfr"   total_resultspageresultsresultauthordocss           r    runzGoogleScholarAPIWrapper.runK   s.   S$,,r1A66 **"!%"gg" ..  #gg
 &+    )BJD1 S$,,r1A664 #q(TBY= **"!%#11B6"gg"gg &+    )< (

 	 fjj"-. /6::N`bdCeCiCijsuwCx!y&**V"4!yz{ |

#5r:>>y"MN O  &

>2 > B B:r R V VW^`b cdf
 
 {{4   "z
s   <AHHA!HH)__name__
__module____qualname____doc__r   int__annotations__r   strr   r   r   r   r   r   model_configr	   classmethodr   r!   rA        r    r   r   	   s     D M3BNB"&L(3-&!%3%L (#$ 3   $&8! 8! 8!rL   r   N)rE   typingr   r   r   langchain_core.utilsr   pydanticr   r   r	   r   rK   rL   r    <module>rP      s%    , & & 5 ; ;z!i z!rL   