
    7|h                     f    d Z ddlmZmZmZmZ ddlZddlmZm	Z	 ddl
mZmZmZmZ  G d de      Zy)z#Util that calls Google Lens Search.    )AnyDictOptionalcastNconvert_to_secret_strget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrmodel_validatorc                       e Zd ZU dZdZeed<   dZee	   ed<    e
d      Z ed      ed	ed
efd              Zded
efdZy)GoogleLensAPIWrapperaV  Wrapper for SerpApi's Google Lens 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 GoogleLensAPIWrapper
        google_lens = GoogleLensAPIWrapper()
        google_lens.run('langchain')
    Nserp_search_engineserp_api_keyforbid)extrabefore)modevaluesreturnc                 8    t        t        |dd            |d<   |S )z?Validate that api key and python package exists in environment.r   SERPAPI_API_KEYr   )clsr   s     h/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/utilities/google_lens.pyvalidate_environmentz)GoogleLensAPIWrapper.validate_environment%   s'     "7 9JK"
~     queryc                 ^   t        t        | j                        }d|j                         |d}d|d    d|d    d|d    }t	        j
                  |      }|j                  d	k7  ry
|j                         }|d   d   dk7  ry
d}d|v r7t        |d         dkD  r&|d   d   }|d|d    d|d    dz  }|d|d    dz  }|dz  }|d   D ],  }	|d|	d    dz  }|d|	d    d|	d    dz  }|d |	d!    dz  }. d"|v r|d#d$|d"   d    dz   z  }t        |       |g}
dj                  |
      S )%z,Run query through Google Trends with Serpapigoogle_lens)engineapi_keyurlz"https://serpapi.com/search?engine=r!   z	&api_key=r"   z&url=r#      zGoogle Lens search failedsearch_metadatastatusSuccess knowledge_graphr   zSubject:title(subtitlez)
zLink to subject:linkz

zRelated Images:

visual_matcheszTitle: 
zSource(sourcez): zImage: 	thumbnailreverse_image_searchzReverse Image SearchzLink: )r   r   r   get_secret_valuerequestsgetstatus_codejsonlenprintjoin)selfr   serpapi_api_keyparamsqueryURLresponseresponseValuexssubjectimagedocss              r   runzGoogleLensAPIWrapper.run/   s   y$*;*;< $&779

 8x8H7ISYZcSdReejkqrwkxjyz<<)3&. *+H5B..M"3459#$56q9GHWW-.a
0C/DCHHB$WV_$5T::B
##"#34 	5EGE'N+2..BGE(O,CfbAABGE+./t44B	5 "]2&=)?@HILMB 	b	t{{4  r   )__name__
__module____qualname____doc__r   r   __annotations__r   r   r   r   model_configr   classmethodr   r   strrE    r   r   r   r   
   sr    & #"(,L(9%,L (#$ 3   $*! *! *!r   r   )rI   typingr   r   r   r   r4   langchain_core.utilsr   r	   pydanticr
   r   r   r   r   rN   r   r   <module>rR      s(    ) , ,  L F FO!9 O!r   