
    dh                     b    S r SSKJrJrJr  SSKrSSKJr  SSKJ	r	J
r
Jr  SrSr " S S	\	5      rg)
zUtil that calls Google Books.    )DictListOptionalNget_from_dict_or_env)	BaseModel
ConfigDictmodel_validator   z+https://www.googleapis.com/books/v1/volumesc                       \ rS rSr% SrSr\\   \S'   \	r
\\S'   \" SS9r\" SS	9\S
\S\4S j5       5       rS\S\4S jrS\S\S\4S jrS\S\4S jrSrg)GoogleBooksAPIWrapper   ae  Wrapper around Google Books API.

To use, you should have a Google Books API key available.
This wrapper will use the Google Books API to conduct searches and
fetch books based on a query passed in by the agents. By default,
it will return the top-k results.

The response for each book will contain the book title, author name, summary, and
a source link.
Ngoogle_books_api_keytop_k_resultsforbid)extrabefore)modevaluesreturnc                 (    [        USS5      nX!S'   U$ )z,Validate that api key exists in environment.r   GOOGLE_BOOKS_API_KEYr   )clsr   r   s      b/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/utilities/google_books.pyvalidate_environment*GoogleBooksAPIWrapper.validate_environment    s(      4*,B 
 *>%&    queryc                 `   SU4SU R                   4SU R                  44n[        R                  " [        US9nUR                  5       nUR                  S:w  a6  UR                  nUR                  S0 5      R                  SS5      nS	U S
U 3$ U R                  XR                  S/ 5      5      $ )Nq
maxResultskey)params   errormessagezInternal failurez)Unable to retrieve books got status code : items)r   r   requestsgetGOOGLE_BOOKS_API_URLjsonstatus_code_format)selfr   r#   responser,   coder%   s          r   runGoogleBooksAPIWrapper.run+   s     %L4--.D--.
 << 4VD}} 3&''DHHWb)--i9KLE>tfBugNN ||E88GR#899r   booksc           	      D   U(       d  SU 3$ S[        U5       SU S3n/ nUR                  U5        SnU HW  nUS   nUS   nU R                  US   5      n	US	   n
US
   nU SU SU	 SU
 S3nUSU 3-  nUR                  U5        US-  nMY     SR                  U5      $ )Nz.Sorry no books could be found for your query: z	Here are z" suggestions for books related to :   
volumeInfotitleauthorsdescriptioninfoLinkz. "z" by r'   
zYou can read more at z

)lenappend_format_authorsjoin)r/   r   r4   startresultsibookinfor9   r:   summarysourcedescs                r   r.   GoogleBooksAPIWrapper._format@   s    CE7KKCJ<'I%PQRuD%DME**4	?;G=)G*%FSE7%y7)2>D+F844DNN4 FA  {{7##r   r:   c                 x    [        U5      S:X  a  US   $ SR                  SR                  US S 5      US   5      $ )Nr7   r   z	{} and {}z, )r>   formatrA   )r/   r:   s     r   r@   %GoogleBooksAPIWrapper._format_authorsY   s>    w<11:!!$))GCRL"972;GGr    )__name__
__module____qualname____firstlineno____doc__r   r   str__annotations__GOOGLE_BOOKS_MAX_ITEM_SIZEr   intr	   model_configr
   classmethodr   r   r2   r   r.   r@   __static_attributes__rO   r   r   r   r      s    	 +/(3-.3M33L (#$ 4   $: : :*$S $ $# $2Ht H Hr   r   )rT   typingr   r   r   r)   langchain_core.utilsr   pydanticr   r	   r
   rW   r+   r   rO   r   r   <module>r_      s6    # ' '  5 ; ; D OHI OHr   