
    j
i              
           d Z ddlZddlmZmZ ddlmZ ddlmZ dede	f         dede
ee	ef                  f         fd	Zd
edee	         f         dedee
ee	ef                           f         fdZdededefdZdS )z.Custom tool decorator for OpenAI custom tools.    N)	AwaitableCallable)Any)toolfunc.returnc                 j     dt           dt          t          t           t          f                  f fd}|S )Nxr   c                 "    d |           dgS Ncustom_tool_call_output)typeoutput )r
   r   s    C:\Users\Dell Inspiron 16\Desktop\tws\AgrotaPowerBi\back-agrota-powerbi\mcp-client-agrota\venv\Lib\site-packages\langchain_openai/tools/custom_tool.pywrappedz#_make_wrapped_func.<locals>.wrapped   s    2dd1ggFFGG    )strlistdictr   )r   r   s   ` r   _make_wrapped_funcr   
   sK    H3 H4S#X/ H H H H H H Nr   	coroutinec           	      x     dt           dt           dt          t          t          t           f                  f fd}|S )Nargskwargsr   c                  0   K    | i | d {V }d|dgS r   r   )r   r   resultr   s      r   r   z(_make_wrapped_coroutine.<locals>.wrapped   s>       y$1&111111112fEEFFr   )r   r   r   r   )r   r   s   ` r   _make_wrapped_coroutiner      sS    GS GC GDc3h4H G G G G G G Nr   r   r   c                      dt           dt          f         dt          ffd}| r(t          | d                   rs || d                   S |S )a  Decorator to create an OpenAI custom tool.

    Custom tools allow for tools with (potentially long) freeform string inputs.

    See below for an example using LangGraph:

    ```python
    @custom_tool
    def execute_code(code: str) -> str:
        """Execute python code."""
        return "27"


    model = ChatOpenAI(model="gpt-5", output_version="responses/v1")

    agent = create_react_agent(model, [execute_code])

    input_message = {"role": "user", "content": "Use the tool to calculate 3^3."}
    for step in agent.stream(
        {"messages": [input_message]},
        stream_mode="values",
    ):
        step["messages"][-1].pretty_print()
    ```

    You can also specify a format for a corresponding context-free grammar using the
    `format` kwarg:

    ```python
    from langchain_openai import ChatOpenAI, custom_tool
    from langgraph.prebuilt import create_react_agent

    grammar = """
    start: expr
    expr: term (SP ADD SP term)* -> add
    | term
    term: factor (SP MUL SP factor)* -> mul
    | factor
    factor: INT
    SP: " "
    ADD: "+"
    MUL: "*"
    %import common.INT
    """

    format = {"type": "grammar", "syntax": "lark", "definition": grammar}

    # highlight-next-line
    @custom_tool(format=format)
    def do_math(input_string: str) -> str:
        """Do a mathematical operation."""
        return "27"


    model = ChatOpenAI(model="gpt-5", output_version="responses/v1")

    agent = create_react_agent(model, [do_math])

    input_message = {"role": "user", "content": "Use the tool to calculate 3^3."}
    for step in agent.stream(
        {"messages": [input_message]},
        stream_mode="values",
    ):
        step["messages"][-1].pretty_print()
    ```
    r   .r   c                    ddi}dv r                     d          |d<    t          dddi|           }||_        | j        |_        t          j        |           rt          |           |_        nt          |           |_
        |S )Nr   custom_toolformatinfer_schemaFr   )popr   metadata__doc__descriptioninspectiscoroutinefunctionr   r   r   r   )r   r%   tool_objr   s      r   	decoratorzcustom_tool.<locals>.decorator_   s    M*v!'H!5!5HX5455U5f55d;;$#|&t,, 	5!8!>!>H.t44HMr   r   )r   r   callable)r   r   r+   s    ` r   r!   r!      sw    Hc* s        "a!! "& "ya!!!r   )r&   r(   collections.abcr   r   typingr   langchain_core.toolsr   r   r   r   r   r   r!   r   r   r   <module>r0      s   4 4  / / / / / / / /       % % % % % %Xc3h/ HS$tCQTH~BV=V4W    Ys^+,c9T$sCx.1223   Ts Tc Tc T T T T T Tr   