
    7|h                     b    d Z ddlmZmZmZ ddlZddlmZ ddlm	Z	m
Z
mZ dZdZ G d d	e	      Zy)
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                       e Zd ZU dZdZee   ed<   eZ	e
ed<    ed      Z ed      ed	ed
efd              Zded
efdZdeded
efdZded
efdZy)GoogleBooksAPIWrappera  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                 *    t        |dd      }||d<   |S )z,Validate that api key exists in environment.r   GOOGLE_BOOKS_API_KEYr   )clsr   r   s      i/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/google_books.pyvalidate_environmentz*GoogleBooksAPIWrapper.validate_environment    s*      4*,B 
 *>%&    queryc                 d   d|fd| j                   fd| j                  ff}t        j                  t        |      }|j                         }|j                  dk7  r6|j                  }|j                  di       j                  dd      }d	| d
| S | j                  ||j                  dg             S )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   runzGoogleBooksAPIWrapper.run+   s     %L4--.D--.
 << 4VD}} 3&''DHHWb)--i9KLE>tfBugNN ||E488GR#899r   booksc           	      6   |sd| S dt        |       d| d}g }|j                  |       d}|D ]V  }|d   }|d   }| j                  |d         }	|d	   }
|d
   }| d| d|	 d|
 d}|d| z  }|j                  |       |dz  }X dj                  |      S )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   r1   startresultsibookinfor6   r7   summarysourcedescs                r   r,   z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   r7   c                 v    t        |      dk(  r|d   S dj                  dj                  |d d       |d         S )Nr4   r   z	{} and {}z, )r;   formatr>   )r-   r7   s     r   r=   z%GoogleBooksAPIWrapper._format_authorsY   s>    w<11:!!$))GCRL"972;GGr   )__name__
__module____qualname____doc__r   r   str__annotations__GOOGLE_BOOKS_MAX_ITEM_SIZEr   intr	   model_configr
   classmethodr   r   r0   r   r,   r=    r   r   r   r      s    	 +/(3-.3M33L (#$ 4   $: : :*$S $ $# $2Ht H Hr   r   )rM   typingr   r   r   r'   langchain_core.utilsr   pydanticr   r	   r
   rP   r)   r   rT   r   r   <module>rX      s6    # ' '  5 ; ; D OHI OHr   