Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open on GitHub

Tool calling

Prerequisites

Overview

Many AI applications interact directly with humans. In these cases, it is appropriate for models to respond in natural language.But what about cases where we want a model to also interactdirectly with systems, such as databases or an API?These systems often have a particular input schema; for example, APIs frequently have a required payload structure.This need motivates the concept oftool calling. You can usetool calling to request model responses that match a particular schema.

info

You will sometimes hear the termfunction calling. We use this term interchangeably withtool calling.

Conceptual overview of tool calling

Key concepts

  1. Tool Creation: Use the@tool decorator to create atool. 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.

Conceptual parts of tool calling

Recommended usage

This pseudocode illustrates the recommended workflow for using tool calling.Created tools are passed to.bind_tools() method as a list.This model can be called, as usual. If a tool call is made, model's response will contain the tool call arguments.The tool call arguments can be passed directly to the tool.

# 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.toolsimport tool

@tool
defmultiply(a:int, b:int)->int:
"""Multiply a and b."""
return a* b
API Reference:tool
Further reading

Tool binding

Manymodel providers support tool calling.

tip

See ourmodel integration page for a list of providers that support tool calling.

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.

model_with_tools= model.bind_tools(tools_list)

As a specific example, let's take a functionmultiply and bind it as a tool to a model that supports tool calling.

defmultiply(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

Diagram of a tool call by a model

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.For example, given an unrelated input, the model would not call the tool:

result= llm_with_tools.invoke("Hello world!")

The result would be anAIMessage containing the model's response in natural language (e.g., "Hello!").However, if we pass an inputrelevant to the tool, the model should choose to call it:

result= llm_with_tools.invoke("What is 2 multiplied by 3?")

As before, the outputresult will be anAIMessage.But, if the tool was called,result will have atool_callsattribute.This attribute includes everything needed to execute the tool, including the tool name and input arguments:

result.tool_calls
[{'name': 'multiply', 'args': {'a': 2, 'b': 3}, 'id': 'xxx', 'type': 'tool_call'}]

For more details on usage, see ourhow-to guides!

Tool execution

Tools implement theRunnable interface, which means that they can be invoked (e.g.,tool.invoke(args)) directly.

LangGraph offers pre-built components (e.g.,ToolNode) that will often invoke the tool in behalf of the user.

Further reading

Forcing tool use

By default, the model has the freedom to choose which tool to use based on the user's input. However, in certain scenarios, you might want to influence the model's decision-making process. LangChain allows you to enforce tool choice (usingtool_choice), ensuring the model uses either a particular tool orany tool from a given list. This is useful for structuring the model's behavior and guiding it towards a desired outcome.

Further reading

Best practices

When designingtools to be used by a model, it is important to keep in mind that:

  • Models that have explicittool-calling APIs will be better at tool calling than non-fine-tuned models.
  • Models will perform better if the tools have well-chosen names and descriptions.
  • Simple, narrowly scoped tools are easier for models to use than complex tools.
  • Asking the model to select from a large list of tools poses challenges for the model.

[8]ページ先頭

©2009-2025 Movatter.jp