
    h                          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 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	e      Z ed
dd       G d de             Z ed
dd       G d de             Zy)zTool for the Tavily search API.    )AnyDictListLiteralOptionalTupleTypeUnion)
deprecated)AsyncCallbackManagerForToolRunCallbackManagerForToolRun)BaseTool)	BaseModelField)TavilySearchAPIWrapperc                   .    e Zd ZU dZ ed      Zeed<   y)TavilyInputzInput for the Tavily tool.zsearch query to look up)descriptionqueryN)__name__
__module____qualname____doc__r   r   str__annotations__     j/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_community/tools/tavily_search/tool.pyr   r      s    $#<=E3=r   r   z0.3.25z1.0zlangchain_tavily.TavilySearch)sinceremovalalternative_importc                       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      Zeed<   dZed   ed<   deddf fdZ	 ddedee   dee ee!eef      ef   e!f   fdZ"	 ddedee#   dee ee!eef      ef   e!f   fdZ$ x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',
            )

    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_resultsadvancedsearch_depthinclude_domainsexclude_domainsFinclude_answerinclude_raw_contentinclude_imagesdefault_factoryapi_wrappercontent_and_artifactresponse_formatkwargsreturnNc                 P    d|v rt        |d         |d<   t        |   di | y )Ntavily_api_key)r8   r2   r   )r   super__init__)selfr5   	__class__s     r   r:   zTavilySearchResults.__init__   s5    v%$:%&67%F=! 	"6"r   r   run_managerc           
      Z   	 | j                   j                  || j                  | j                  | j                  | j
                  | j                  | j                  | j                        }| j                   j                  |d         |fS # t        $ r}t        |      i fcY d}~S d}~ww xY w)Use the tool.Nresults)r2   raw_resultsr(   r*   r+   r,   r-   r.   r/   	Exceptionreprclean_resultsr;   r   r=   rA   es        r   _runzTavilySearchResults._run   s    	**66  !!$$$$##((##	K --k).DE{RR  	7B;	s   A(B
 
	B*B%B*%B*c           
      v  K   	 | j                   j                  || j                  | j                  | j                  | j
                  | j                  | j                  | j                         d{   }| j                   j                  |d         |fS 7 $# t        $ r}t        |      i fcY d}~S d}~ww xY ww)Use the tool asynchronously.Nr@   )r2   raw_results_asyncr(   r*   r+   r,   r-   r.   r/   rB   rC   rD   rE   s        r   _arunzTavilySearchResults._arun   s     	 $ 0 0 B B  !!$$$$##((##	! 	K --k).DE{RR	  	7B;	sG   B9A+B /B0B 4 B9B 	B6B1+B6,B91B66B9N)&r   r   r   r   r%   r   r   r   r   r&   r	   r   r(   intr*   r+   r   r,   r-   boolr.   r/   r   r   r2   r4   r   r   r:   r   r   r   r
   r   rG   r   rK   __classcell__)r<   s   @r   r#   r#      s   IV -D#,	*  
 $/Ki.#K4"L#" "$OT#Y# "$OT#Y# !ND  !&% !ND  +0@V*WK'W7MOW34M# # # <@SS 78S 
uT$sCx.)3./5	6	S2 AESS <=S 
uT$sCx.)3./5	6	Sr   r#   c            	           e Zd ZU dZdZeed<   dZeed<    ee	      Z
e	ed<   eZee   ed<   	 dd
edee   deee   ef   fdZ	 dd
edee   deee   ef   fdZy	)TavilyAnswerz@Tool that queries the Tavily Search API and gets back an answer.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   r0   r2   r&   Nr   r=   r6   c                     	 | j                   j                  |ddd      d   S # t        $ r}t        |      cY d}~S d}~ww xY w)r?   r'   Tbasicr(   r-   r*   answerN)r2   rA   rB   rC   )r;   r   r=   rF   s       r   rG   zTavilyAnswer._run   sV    	##//#$	 0 
    	7N	s   !$ 	A
=AAc                    K   	 | j                   j                  |ddd       d{   }|d   S 7 	# t        $ r}t        |      cY d}~S d}~ww xY ww)rI   r'   TrT   rU   NrV   )r2   rJ   rB   rC   )r;   r   r=   resultrF   s        r   rK   zTavilyAnswer._arun   sc     		++==#$	 >  F (##  	7N	s<   A"2 02 A2 	A
AAAAArL   )r   r   r   r   r%   r   r   r   r   r   r2   r   r&   r	   r   r   r   r
   r   r   rG   r   rK   r   r   r   rQ   rQ      s     KD#	G   +0@V*WK'W#.Ki.
 <@ 78 
tDz3		& AE <= 
tDz3		r   rQ   N)r   typingr   r   r   r   r   r   r	   r
   langchain_core._apir   langchain_core.callbacksr   r   langchain_core.toolsr   pydanticr   r   +langchain_community.utilities.tavily_searchr   r   r#   rQ   r   r   r   <module>r_      s    % I I I * * % N>) > 
6
uS( uS
uSp 
6
,8 ,
,r   