Skip to main content

工具调用

¥Tool calling

概述

¥Overview

许多 AI 应用直接与人类交互。在这些情况下,模型以自然语言进行响应是合适的。但是,如果我们希望模型也能直接与系统(例如数据库或 API)交互,该怎么办呢?这些系统通常具有特定的输入模式;例如,API 通常具有必需的有效负载结构。这种需求催生了工具调用的概念。你可以使用 工具调用 请求与特定模式匹配的模型响应。

¥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 interact directly 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 of tool calling. You can use tool calling to request model responses that match a particular schema.

info

你有时会听到 function calling 这个术语。我们通常将此术语与 tool calling 互换使用。

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

Conceptual overview of tool calling

关键概念

¥Key concepts

(1)工具创建:使用 tool 函数创建 tool。工具是函数与其模式之间的关联。(2)工具绑定:该工具需要连接到支持工具调用的模型。这让模型能够感知工具以及工具所需的相关输入模式。(3)工具调用:在适当的情况下,模型可以决定调用某个工具并确保其响应符合工具的输入模式。(4)工具执行:可以使用模型提供的参数执行该工具。

¥(1) Tool Creation: Use the tool function 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.

Conceptual parts of tool calling

¥Recommended usage

此伪代码演示了使用工具调用的推荐工作流程。创建的工具将以列表形式传递给 .bindTools() 方法。此模型可以照常调用。如果进行了工具调用,模型的响应将包含工具调用参数。工具调用参数可以直接传递给工具。

¥This pseudo-code illustrates the recommended workflow for using tool calling. Created tools are passed to .bindTools() 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
const tools = [myTool];
// Tool binding
const modelWithTools = model.bindTools(tools);
// Tool calling
const response = await modelWithTools.invoke(userInput);

工具创建

¥Tool creation

创建工具的推荐方法是使用 tool 函数。

¥The recommended way to create a tool is using the tool function.

import { tool } from "@langchain/core/tools";

const multiply = tool(
({ a, b }: { a: number; b: number }): number => {
/**

* Multiply a and b.
*/
return a * b;
},
{
name: "multiply",
description: "Multiply two numbers",
schema: z.object({
a: z.number(),
b: z.number(),
}),
}
);
[Further reading]
  • 请参阅我们关于 tools 的概念指南,了解更多详情。

    ¥See our conceptual guide on tools for more details.

  • 请参阅我们支持工具调用的 模型集成

    ¥See our model integrations that support tool calling.

  • 观看我们的 操作指南,了解如何使用工具调用。

    ¥See our how-to guide on tool calling.

对于不需要执行函数的工具调用,你也可以仅定义工具架构:

¥For tool calling that does not require a function to execute, you can also define just the tool schema:

const multiplyTool = {
name: "multiply",
description: "Multiply two numbers",
schema: z.object({
a: z.number(),
b: z.number(),
}),
};

工具绑定

¥Tool binding

许多 模型提供程序 支持工具调用。

¥Many model providers support tool calling.

tip

观看我们的 模型集成页面,了解支持工具调用的提供商列表。

¥See our model integration page for a list of providers that support tool calling.

需要理解的核心概念是,LangChain 提供了一个标准化接口,用于将工具连接到模型。.bindTools() 方法可用于指定模型可调用的工具。

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

const modelWithTools = model.bindTools([toolsList]);

作为一个具体示例,我们以函数 multiply 为例,并将其作为工具绑定到支持工具调用的模型。

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

const multiply = tool(
({ a, b }: { a: number; b: number }): number => {
/**

* Multiply a and b.

* * @param a - first number

* @param b - second number

* @returns The product of a and b
*/
return a * b;
},
{
name: "multiply",
description: "Multiply two numbers",
schema: z.object({
a: z.number(),
b: z.number(),
}),
}
);

const llmWithTools = toolCallingModel.bindTools([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:

const result = await llmWithTools.invoke("Hello world!");

结果将是一个包含模型自然语言响应的 AIMessage(例如 "你好!")。但是,如果我们传递与工具相关的输入,模型应该选择调用它:

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

const result = await llmWithTools.invoke("What is 2 multiplied by 3?");

和以前一样,输出 result 将是 AIMessage。但是,如果调用该工具,result 将具有 tool_calls 属性。此属性包含执行该工具所需的所有内容,包括工具名称和输入参数:

¥As before, the output result will be an AIMessage. But, if the tool was called, result will have a tool_calls attribute. 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 our how-to guides!

工具执行

¥Tool execution

工具 实现了 Runnable 接口,这意味着可以直接调用它们(例如 tool.invoke(args))。

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

LangGraph 提供预构建的组件(例如 ToolNode),这些组件通常会代表用户调用该工具。

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

[Further reading]

最佳实践

¥Best practices

在设计模型使用的 tools 时,请务必牢记以下几点:

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

  • 具有显式 工具调用 API 的模型在工具调用方面会比未微调的模型表现更好。

    ¥Models that have explicit tool-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.