a
    bg                     @   s\   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	 d	e	ZdS )
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                   @   s   e Zd ZU dZdZee ed< eZ	e
ed< eddZeddeeed	d
dZeedddZeeedddZeedddZdS )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_resultsZforbid)extrabefore)mode)valuesreturnc                 C   s   t |dd}||d< |S )z,Validate that api key exists in environment.r   ZGOOGLE_BOOKS_API_KEYr   )clsr   r    r   x/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/google_books.pyvalidate_environment    s
    z*GoogleBooksAPIWrapper.validate_environment)queryr   c                 C   sx   d|fd| j fd| jff}tjt|d}| }|jdkrd|j}|di dd}d	| d
| S | ||dg S )NqZ
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   r   r   r   run+   s    
zGoogleBooksAPIWrapper.run)r   booksr   c              	   C   s   |sd| S dt | d| d}g }|| d}|D ]n}|d }|d }| |d }	|d	 }
|d
 }| d| d|	 d|
 d}|d| 7 }|| |d7 }q:d|S )Nz.Sorry no books could be found for your query: z	Here are z" suggestions for books related to :   Z
volumeInfotitleauthorsdescriptionZinfoLinkz. "z" by r   
zYou can read more at z

)lenappend_format_authorsjoin)r&   r   r*   startresultsibookinfor-   r.   summarysourcedescr   r   r   r%   @   s"    



zGoogleBooksAPIWrapper._format)r.   r   c                 C   s2   t |dkr|d S dd|d d |d S )Nr,   r   z	{} and {}z, )r1   formatr4   )r&   r.   r   r   r   r3   Y   s    z%GoogleBooksAPIWrapper._format_authors)__name__
__module____qualname____doc__r   r   str__annotations__GOOGLE_BOOKS_MAX_ITEM_SIZEr   intr   Zmodel_configr	   classmethodr   r   r)   r   r%   r3   r   r   r   r   r      s   
	r   )rB   typingr   r   r   r    Zlangchain_core.utilsr   Zpydanticr   r   r	   rE   r"   r   r   r   r   r   <module>   s   