You can use tool calling to request model responses that match a particular schema.

<aside> ❗

INFO

You will sometimes hear the term function calling. We use this term interchangeably with tool calling.

</aside>

image.png

Key concepts

(1) Tool Creation: Use the @tool decorator to create a tool. A tool is an association between a function and its schema.

(2) Tool Binding: The tool needs to be connected to a model that supports tool calling. This gives the model awareness of the tool and the associated input schema required by the tool.

(3) Tool Calling: When appropriate, the model can decide to call a tool and ensure its response conforms to the tool's input schema.

(4) Tool Execution: The tool can be executed using the arguments provided by the model.

image.png

Created tools are passed to .bind_tools() method as a list.

# Tool creation
tools = [my_tool]
# Tool binding
model_with_tools = model.bind_tools(tools)
# Tool calling 
response = model_with_tools.invoke(user_input)

Tool creation

The recommended way to create a tool is using the @tool decorator.

from langchain_core.tools import tool

@tool
def multiply(a: int, b: int) -> int:
    """Multiply a and b."""
    return a * b

Tool binding

The central concept to understand is that LangChain provides a standardized interface for connecting tools to models. The .bind_tools() method can be used to specify which tools are available for a model to call.

def multiply(a: int, b: int) -> int:
    """Multiply a and b.

    Args:
        a: first int
        b: second int
    """
    return a * b

llm_with_tools = tool_calling_model.bind_tools([multiply])

Tool calling

A key principle of tool calling is that the model decides when to use a tool based on the input's relevance. The model doesn't always need to call a tool.

image.png