1. The tool abstraction in LangChain associates a Python function with a schema that defines the function's namedescription and expected arguments.
  2. Tools can be passed to chat models that support tool calling allowing the model to request the execution of a specific function with specific inputs.

Key concepts

Tool interface

The key attributes that correspond to the tool's schema:

The key methods to execute the function associated with the tool:

Create tools using the @tool decorator

  1. Way to create tools is using the @tool decorator.

    from langchain_core.tools import tool
    
    @tool
    def multiply(a: int, b: int) -> int:
       """Multiply two numbers."""
       return a * b
    
  2. Once you have defined a tool, you can use it directly by calling the function.

    multiply.invoke({"a": 2, "b": 3})
    
  3. You can also inspect the tool's schema and other properties:

    print(multiply.name) # multiply
    print(multiply.description) # Multiply two numbers.
    print(multiply.args) 
    # {
    # 'type': 'object', 
    # 'properties': {'a': {'type': 'integer'}, 'b': {'type': 'integer'}}, 
    # 'required': ['a', 'b']
    # }
    

Configuring the schema