o
    Zh$                      @   s   d Z ddlmZmZmZmZmZmZmZm	Z	 ddl
mZmZ ddlmZ ddlmZmZ ddlmZ G dd deZG d	d
 d
eZG dd deZdS )zTool for the Tavily search API.    )AnyDictListLiteralOptionalTupleTypeUnion)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseTool)	BaseModelField)TavilySearchAPIWrapperc                   @   s$   e Zd ZU dZeddZeed< dS )TavilyInputzInput for the Tavily tool.zsearch query to look up)descriptionqueryN)__name__
__module____qualname____doc__r   r   str__annotations__ r   r   c/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/tools/tavily_search/tool.pyr      s   
 r   c                       sV  e Zd ZU dZdZeed< dZeed< eZ	e
e ed< 	 dZeed< 	 d	Zeed
< 	 g Zee ed< 	 g Zee ed< 	 dZeed< 	 dZeed< 	 dZeed< 	 eedZeed< dZed ed< deddf fddZ	d dedee dee ee!eef  ef e!f fddZ"	d dedee# dee ee!eef  ef e!f fddZ$  Z%S )!TavilySearchResultsa	  Tool that queries the Tavily Search API and gets back json.

    Setup:
        Install ``langchain-openai`` and ``tavily-python``, and set environment variable ``TAVILY_API_KEY``.

        .. code-block:: bash

            pip install -U langchain-community tavily-python
            export TAVILY_API_KEY="your-api-key"

    Instantiate:

        .. code-block:: python

            from langchain_community.tools import TavilySearchResults

            tool = TavilySearchResults(
                max_results=5,
                include_answer=True,
                include_raw_content=True,
                include_images=True,
                # search_depth="advanced",
                # include_domains = []
                # exclude_domains = []
            )

    Invoke directly with args:

        .. code-block:: python

            tool.invoke({'query': 'who won the last french open'})

        .. code-block:: json

            {
                "url": "https://www.nytimes.com...",
                "content": "Novak Djokovic won the last French Open by beating Casper Ruud ..."
            }

    Invoke with tool call:

        .. code-block:: python

            tool.invoke({"args": {'query': 'who won the last french open'}, "type": "tool_call", "id": "foo", "name": "tavily"})

        .. code-block:: python

            ToolMessage(
                content='{ "url": "https://www.nytimes.com...", "content": "Novak Djokovic won the last French Open by beating Casper Ruud ..." }',
                artifact={
                    'query': 'who won the last french open',
                    'follow_up_questions': None,
                    'answer': 'Novak ...',
                    'images': [
                        'https://www.amny.com/wp-content/uploads/2023/06/AP23162622181176-1200x800.jpg',
                        ...
                        ],
                    'results': [
                        {
                            'title': 'Djokovic ...',
                            'url': 'https://www.nytimes.com...',
                            'content': "Novak...",
                            'score': 0.99505633,
                            'raw_content': 'Tennis
Novak ...'
                        },
                        ...
                    ],
                    'response_time': 2.92
                },
                tool_call_id='1',
                name='tavily_search_results_json',
            )

    Ztavily_search_results_jsonnamezA search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events. Input should be a search query.r   args_schema   max_resultsZadvancedsearch_depthinclude_domainsexclude_domainsFinclude_answerinclude_raw_contentinclude_imagesdefault_factoryapi_wrapperZcontent_and_artifactresponse_formatkwargsreturnNc                    s0   d|v rt |d d|d< t jdi | d S )Ntavily_api_key)r,   r(   r   )r   super__init__)selfr*   	__class__r   r   r.      s
   
zTavilySearchResults.__init__r   run_managerc              
   C   sr   z| j || j| j| j| j| j| j| j}W n t	y. } zt
|i fW  Y d}~S d}~ww | j |d |fS )Use the tool.Nresults)r(   raw_resultsr   r    r!   r"   r#   r$   r%   	Exceptionreprclean_resultsr/   r   r2   r5   er   r   r   _run   s    
zTavilySearchResults._runc              
      sz   z| j || j| j| j| j| j| j| jI dH }W n t	y2 } zt
|i fW  Y d}~S d}~ww | j |d |fS )Use the tool asynchronously.Nr4   )r(   raw_results_asyncr   r    r!   r"   r#   r$   r%   r6   r7   r8   r9   r   r   r   _arun   s"   
zTavilySearchResults._arunN)&r   r   r   r   r   r   r   r   r   r   r   r   r   intr    r!   r   r"   r#   boolr$   r%   r   r   r(   r)   r   r   r.   r   r   r   r	   r   r;   r
   r>   __classcell__r   r   r0   r   r      sP   
 K

r   c                	   @   s   e Zd ZU dZdZeed< dZeed< ee	dZ
e	ed< eZee ed< 		dd
edee deee ef fddZ		dd
edee deee ef fddZd	S )TavilyAnswerz@Tool that queries the Tavily Search API and gets back an answer.Ztavily_answerr   zA search engine optimized for comprehensive, accurate, and trusted results. Useful for when you need to answer questions about current events. Input should be a search query. This returns only the answer - not the original source data.r   r&   r(   r   Nr   r2   r+   c              
   C   sH   z| j j|ddddd W S  ty# } z
t|W  Y d}~S d}~ww )r3   r   Tbasicr   r#   r    answerN)r(   r5   r6   r7   )r/   r   r2   r:   r   r   r   r;      s   zTavilyAnswer._runc              
      sT   z| j j|ddddI dH }|d W S  ty) } z
t|W  Y d}~S d}~ww )r<   r   TrD   rE   NrF   )r(   r=   r6   r7   )r/   r   r2   resultr:   r   r   r   r>      s   
zTavilyAnswer._arunr?   )r   r   r   r   r   r   r   r   r   r   r(   r   r   r   r   r   r   r	   r   r   r;   r
   r>   r   r   r   r   rC      s.   
 

rC   N)r   typingr   r   r   r   r   r   r   r	   Zlangchain_core.callbacksr
   r   Zlangchain_core.toolsr   Zpydanticr   r   Z+langchain_community.utilities.tavily_searchr   r   r   rC   r   r   r   r   <module>   s    ( 9