a
    bg9                     @   sX   d Z ddlmZmZmZmZ ddlmZmZ ddl	m
Z
mZmZmZ G dd de
ZdS )z&Util that calls Google Finance Search.    )AnyDictOptionalcast)convert_to_secret_strget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrmodel_validatorc                   @   sf   e Zd ZU dZdZeed< dZee	 ed< e
ddZeddeeed	d
dZeedddZdS )GoogleFinanceAPIWrappera`  Wrapper for SerpApi's Google Finance API

    You can create SerpApi.com key by signing up at: https://serpapi.com/users/sign_up.
    The wrapper uses the SerpApi.com python package:
    https://serpapi.com/integrations/python
    To use, you should have the environment variable ``SERPAPI_API_KEY``
    set with your API key, or pass `serp_api_key` as a named parameter
    to the constructor.
    Example:
        .. code-block:: python
        from langchain_community.utilities import GoogleFinanceAPIWrapper
        google_Finance = GoogleFinanceAPIWrapper()
        google_Finance.run('langchain')
    Nserp_search_engineserp_api_keyZforbid)extrabefore)mode)valuesreturnc                 C   sP   t t|dd|d< zddlm} W n ty>   tdY n0 |}||d< |S )z?Validate that api key and python package exists in environment.r   ZSERPAPI_API_KEYr   )SerpApiClientzigoogle-search-results is not installed. Please install it with `pip install google-search-results>=2.4.2`r   )r   r   Zserpapir   ImportError)clsr   r   r    r   z/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/utilities/google_finance.pyvalidate_environment    s    

z,GoogleFinanceAPIWrapper.validate_environment)queryr   c                 C   sJ  t t| j}d| |d}i }| |}| }|s>d| S |di }d| d }d|v r|dg d }|d	 }	|d
 }
|d }|d }|d }|d|	 dd|
 d d| d d| d 7 }n|d7 }|D ]l}|dks|dks|dkr||7 }|d7 }|t|| d d
 7 }|d7 }||| d d d 7 }|d7 }q|S )z-Run query through Google Finance with SerpapiZgoogle_finance)ZengineZapi_keyqz"Nothing was found from the query: marketsz
Query: 
futures_chainr   stockpriceZprice_movement
percentagemovementzstock: zprice: zpercentage: z
movement: zNo summary information
usZasiaZeuropez
: price = z, movement = )r   r
   r   Zget_secret_valuer   get_dictgetstr)selfr   Zserpapi_api_keyparamstotal_resultsclientr   resr   r   r    tempr!   r"   keyr   r   r   run6   sL    





zGoogleFinanceAPIWrapper.run)__name__
__module____qualname____doc__r   r   __annotations__r   r   r
   r	   Zmodel_configr   classmethodr   r   r&   r.   r   r   r   r   r   	   s   
r   N)r2   typingr   r   r   r   Zlangchain_core.utilsr   r   Zpydanticr   r	   r
   r   r   r   r   r   r   <module>   s   