
    Ngj#                        d 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 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZmZmZ ddlmZ  G d de          Z G d de          ZdS )zRequests toolkit.    )annotations)AnyList)BaseLanguageModel)BaseToolTool)BaseToolkit)create_json_agent)JsonToolkit)DESCRIPTION)JsonSpec)RequestsDeleteToolRequestsGetToolRequestsPatchToolRequestsPostToolRequestsPutTool)TextRequestsWrapperc                  8    e Zd ZU dZded<   	 dZded<   	 dd	Zd
S )RequestsToolkita  Toolkit for making REST requests.

    *Security Note*: This toolkit contains tools to make GET, POST, PATCH, PUT,
        and DELETE requests to an API.

        Exercise care in who is allowed to use this toolkit. If exposing
        to end users, consider that users will be able to make arbitrary
        requests on behalf of the server hosting the code. For example,
        users could ask the server to make a request to a private API
        that is only accessible from the server.

        Control access to who can submit issue requests using this toolkit and
        what network access it has.

        See https://python.langchain.com/docs/security for more information.

    Setup:
        Install ``langchain-community``.

        .. code-block:: bash

            pip install -U langchain-community

    Key init args:
        requests_wrapper: langchain_community.utilities.requests.GenericRequestsWrapper
            wrapper for executing requests.
        allow_dangerous_requests: bool
            Defaults to False. Must "opt-in" to using dangerous requests by setting to True.

    Instantiate:
        .. code-block:: python

            from langchain_community.agent_toolkits.openapi.toolkit import RequestsToolkit
            from langchain_community.utilities.requests import TextRequestsWrapper

            toolkit = RequestsToolkit(
                requests_wrapper=TextRequestsWrapper(headers={}),
                allow_dangerous_requests=ALLOW_DANGEROUS_REQUEST,
            )

    Tools:
        .. code-block:: python

            tools = toolkit.get_tools()
            tools

        .. code-block:: none

            [RequestsGetTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True),
            RequestsPostTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True),
            RequestsPatchTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True),
            RequestsPutTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True),
            RequestsDeleteTool(requests_wrapper=TextRequestsWrapper(headers={}, aiosession=None, auth=None, response_content_type='text', verify=True), allow_dangerous_requests=True)]

    Use within an agent:
        .. code-block:: python

            from langchain_openai import ChatOpenAI
            from langgraph.prebuilt import create_react_agent


            api_spec = """
            openapi: 3.0.0
            info:
              title: JSONPlaceholder API
              version: 1.0.0
            servers:
              - url: https://jsonplaceholder.typicode.com
            paths:
              /posts:
                get:
                  summary: Get posts
                  parameters: &id001
                    - name: _limit
                      in: query
                      required: false
                      schema:
                        type: integer
                      example: 2
                      description: Limit the number of results
            """

            system_message = """
            You have access to an API to help answer user queries.
            Here is documentation on the API:
            {api_spec}
            """.format(api_spec=api_spec)

            llm = ChatOpenAI(model="gpt-4o-mini")
            agent_executor = create_react_agent(llm, tools, state_modifier=system_message)

            example_query = "Fetch the top two posts. What are their titles?"

            events = agent_executor.stream(
                {"messages": [("user", example_query)]},
                stream_mode="values",
            )
            for event in events:
                event["messages"][-1].pretty_print()

        .. code-block:: none

             ================================[1m Human Message [0m=================================

            Fetch the top two posts. What are their titles?
            ==================================[1m Ai Message [0m==================================
            Tool Calls:
            requests_get (call_RV2SOyzCnV5h2sm4WPgG8fND)
            Call ID: call_RV2SOyzCnV5h2sm4WPgG8fND
            Args:
                url: https://jsonplaceholder.typicode.com/posts?_limit=2
            =================================[1m Tool Message [0m=================================
            Name: requests_get

            [
            {
                "userId": 1,
                "id": 1,
                "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
                "body": "quia et suscipit..."
            },
            {
                "userId": 1,
                "id": 2,
                "title": "qui est esse",
                "body": "est rerum tempore vitae..."
            }
            ]
            ==================================[1m Ai Message [0m==================================

            The titles of the top two posts are:
            1. "sunt aut facere repellat provident occaecati excepturi optio reprehenderit"
            2. "qui est esse"
    r   requests_wrapperFboolallow_dangerous_requestsreturnList[BaseTool]c                
   t          | j        | j                  t          | j        | j                  t	          | j        | j                  t          | j        | j                  t          | j        | j                  gS )zReturn a list of tools.r   r   )r   r   r   r   r   r   r   )selfs    n/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/agent_toolkits/openapi/toolkit.py	get_toolszRequestsToolkit.get_tools   s     !%!6)-)F   !%!6)-)F   !%!6)-)F   !%!6)-)F   !%!6)-)F  #
 	
    Nr   r   )__name__
__module____qualname____doc____annotations__r   r    r    r   r   r      s]         E EN *)))%*****B
 
 
 
 
 
r    r   c                  `    e Zd ZU dZded<   	 ded<   	 dZded<   	 ddZe	 ddd            ZdS )OpenAPIToolkitaS  Toolkit for interacting with an OpenAPI API.

    *Security Note*: This toolkit contains tools that can read and modify
        the state of a service; e.g., by creating, deleting, or updating,
        reading underlying data.

        For example, this toolkit can be used to delete data exposed via
        an OpenAPI compliant API.
    r   
json_agentr   r   Fr   r   r   r   c                    t          d| j        j        t                    }t	          | j        | j                  }g |                                |S )zGet the tools in the toolkit.json_explorer)namefuncdescriptionr   )r   r*   runr   r   r   r   r   )r   json_agent_toolrequest_toolkits      r   r   zOpenAPIToolkit.get_tools   sd     $#
 
 

 *!2%)%B
 
 
 ?**,,>o>>r    llmr   	json_specr   kwargsc                V    t          |t          |          fi |} | |||          S )z,Create json agent from llm, then initialize.)spec)r*   r   r   )r
   r   )clsr3   r4   r   r   r5   r*   s          r   from_llmzOpenAPIToolkit.from_llm   sH     'sKY,G,G,GRR6RR
s!-%=
 
 
 	
r    Nr!   )F)r3   r   r4   r   r   r   r   r   r5   r   r   r)   )	r"   r#   r$   r%   r&   r   r   classmethodr9   r'   r    r   r)   r)      s           OOO))))%*****B? ? ? ?  */
 
 
 
 [
 
 
r    r)   N)r%   
__future__r   typingr   r   langchain_core.language_modelsr   langchain_core.toolsr   r   langchain_core.tools.baser	   ,langchain_community.agent_toolkits.json.baser
   /langchain_community.agent_toolkits.json.toolkitr   1langchain_community.agent_toolkits.openapi.promptr   #langchain_community.tools.json.toolr   'langchain_community.tools.requests.toolr   r   r   r   r   &langchain_community.utilities.requestsr   r   r)   r'   r    r   <module>rF      s{     " " " " " "         < < < < < < / / / / / / / / 1 1 1 1 1 1 J J J J J J G G G G G G I I I I I I 8 8 8 8 8 8              G F F F F Fd
 d
 d
 d
 d
k d
 d
 d
N.
 .
 .
 .
 .
[ .
 .
 .
 .
 .
r    