o
    {qi%                  
   @   s   d Z ddlZddlmZ ddlmZmZ ddlmZ dede	f dede
ee	ef  f fd	d
Zdedee	 f dedee
ee	ef   f fddZdededefddZdS )z.Custom tool decorator for OpenAI custom tools.    N)	Awaitable)AnyCallable)toolfunc.returnc                    s&   dt dttt tf  f fdd}|S )Nxr   c                    s   d | dgS NZcustom_tool_call_output)typeoutput )r   r   r   i/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/langchain_openai/tools/custom_tool.pywrapped   s   z#_make_wrapped_func.<locals>.wrapped)strlistdictr   )r   r   r   r   r   _make_wrapped_func
   s   "r   	coroutinec              	      s*   dt dt dtttt f  f fdd}|S )Nargskwargsr   c                     s"    | i |I d H }d|dgS r	   r   )r   r   resultr   r   r   r      s   z(_make_wrapped_coroutine.<locals>.wrapped)r   r   r   r   )r   r   r   r   r   _make_wrapped_coroutine   s   &r   r   r   c                     sB   dt dtf dtf fdd}| rt| d r s|| d S |S )aj  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:

    .. code-block:: python

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


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

        agent = create_react_agent(llm, [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:

    .. code-block:: 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"


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

        agent = create_react_agent(llm, [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                    sh   ddi}d v r  d|d< tdddi | }||_| j|_t| r-t| |_|S t	| |_
|S )Nr
   custom_toolformatZinfer_schemaFr   )popr   metadata__doc__descriptioninspectiscoroutinefunctionr   r   r   r   )r   r   Ztool_objr   r   r   	decorator_   s   


zcustom_tool.<locals>.decoratorr   )r   r   callable)r   r   r#   r   r"   r   r      s   Dr   )r   r    collections.abcr   typingr   r   Zlangchain_core.toolsr   r   r   r   r   r   r   r   r   r   r   <module>   s    .

