这是用户在 2025-7-28 10:33 为 https://langchain-ai.github.io/langgraph/concepts/tools/ 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
Skip to content

Tools  工具

Many AI applications interact with users via natural language. However, some use cases require models to interface directly with external systems—such as APIs, databases, or file systems—using structured input. In these scenarios, tool calling enables models to generate requests that conform to a specified input schema.
许多 AI 应用程序通过自然语言与用户交互。但是,有些用例需要模型使用结构化输入直接与外部系统(如 API、数据库或文件系统)接口。在这些场景中, 工具调用使模型能够生成符合指定输入模式的请求。

Tools encapsulate a callable function and its input schema. These can be passed to compatible chat models, allowing the model to decide whether to invoke a tool and with what arguments.
工具封装了一个可调用函数及其输入模式。这些可以传递给兼容的聊天模型 ,允许模型决定是否调用工具以及使用什么参数。

Tool calling
工具调用

Diagram of a tool call by a model

Tool calling is typically conditional. Based on the user input and available tools, the model may choose to issue a tool call request. This request is returned in an AIMessage object, which includes a tool_calls field that specifies the tool name and input arguments:
工具调用通常是有条件的。基于用户输入和可用工具,模型可以选择发出工具调用请求。此请求在 AIMessage 对象中返回,该对象包括指定工具名称和输入参数的 tool_calls 字段:

llm_with_tools.invoke("What is 2 multiplied by 3?")
# -> AIMessage(tool_calls=[{'name': 'multiply', 'args': {'a': 2, 'b': 3}, ...}])

If the input is unrelated to any tool, the model returns only a natural language message:
如果输入与任何工具无关,则模型仅返回自然语言消息:

llm_with_tools.invoke("Hello world!")  # -> AIMessage(content="Hello!")

Importantly, the model does not execute the tool—it only generates a request. A separate executor (such as a runtime or agent) is responsible for handling the tool call and returning the result.
重要的是,该模型不会执行该工具--它只生成一个请求。一个单独的执行器(比如运行时或代理)负责处理工具调用并返回结果。

See the tool calling guide for more details.
有关详细信息,请参阅工具调用指南

Prebuilt tools
预建工具

LangChain provides prebuilt tool integrations for common external systems including APIs, databases, file systems, and web data.
LangChain 为常见的外部系统提供预构建的工具集成,包括 API,数据库,文件系统和 Web 数据。

Browse the integrations directory for available tools.
浏览集成目录以获取可用的工具。

Common categories:  常见类别:

  • Search: Bing, SerpAPI, Tavily
    搜索 :Bing,SerpAPI,Tavily
  • Code execution: Python REPL, Node.js REPL
    代码执行 :Python REPL,Node.js REPL
  • Databases: SQL, MongoDB, Redis
    数据库 :SQL,MongoDB,Redis
  • Web data: Scraping and browsing
    Web 数据 :抓取和浏览
  • APIs: OpenWeatherMap, NewsAPI, etc.
    API:OpenWeatherMap、NewsAPI 等

Custom tools
自定义工具

You can define custom tools using the @tool decorator or plain Python functions. For example:
您可以使用 @tool 装饰器或普通 Python 函数定义自定义工具。举例来说:

API Reference: tool  API 参考:工具

from langchain_core.tools import tool

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

See the tool calling guide for more details.
有关详细信息,请参阅工具调用指南

Tool execution
工具执行

While the model determines when to call a tool, execution of the tool call must be handled by a runtime component.
虽然模型决定何时调用工具,但工具调用的执行必须由运行时组件处理。

LangGraph provides prebuilt components for this:
LangGraph 为此提供了预构建的组件:

Copied to clipboard