Skip to main content

如何在链中使用工具

在本指南中,我们将介绍创建调用工具的链和代理的基本方法。工具可以是任何东西——API、函数、数据库等。工具使我们能够扩展模型的能力,不仅仅是输出文本/消息。使用模型与工具的关键是正确提示模型并解析其响应,以便它选择正确的工具并为其提供正确的输入。

设置

我们需要安装以下软件包以进行本指南:

%pip install --upgrade --quiet langchain

如果您想在 LangSmith 中跟踪您的运行,请取消注释并设置以下环境变量:

import getpass
import os

# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

创建一个工具

首先,我们需要创建一个要调用的工具。在这个例子中,我们将从一个函数创建一个自定义工具。有关创建自定义工具的更多信息,请参见 本指南

<!--IMPORTS:[{"imported": "tool", "source": "langchain_core.tools", "docs": "https://python.langchain.com/api_reference/core/tools/langchain_core.tools.convert.tool.html", "title": "How to use tools in a chain"}]-->
from langchain_core.tools import tool


@tool
def multiply(first_int: int, second_int: int) -> int:
"""Multiply two integers together."""
return first_int * second_int
print(multiply.name)
print(multiply.description)
print(multiply.args)
multiply
multiply(first_int: int, second_int: int) -> int - Multiply two integers together.
{'first_int': {'title': 'First Int', 'type': 'integer'}, 'second_int': {'title': 'Second Int', 'type': 'integer'}}
multiply.invoke({"first_int": 4, "second_int": 5})
20

如果我们知道只需要固定次数地使用一个工具,我们可以为此创建一个链。让我们创建一个简单的链,仅仅是乘以用户指定的数字。

chain

工具/函数调用

与大型语言模型一起使用工具的最可靠方法之一是使用工具调用 API(有时也称为函数调用)。这仅适用于明确支持工具调用的模型。您可以在 这里 查看哪些模型支持工具调用,并在 本指南 中了解更多关于如何使用工具调用的信息。

首先我们将定义我们的模型和工具。我们将从一个单一的工具 multiply 开始。

pip install -qU langchain-openai
import getpass
import os

os.environ["OPENAI_API_KEY"] = getpass.getpass()

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

我们将使用 bind_tools 将工具的定义作为每次调用模型的一部分传递,以便模型在适当的时候可以调用该工具:

llm_with_tools = llm.bind_tools([multiply])

当模型调用工具时,这将显示在输出的 AIMessage.tool_calls 属性中:

msg = llm_with_tools.invoke("whats 5 times forty two")
msg.tool_calls
[{'name': 'multiply',
'args': {'first_int': 5, 'second_int': 42},
'id': 'call_cCP9oA3tRz7HDrjFn1FdmDaG'}]

查看 LangSmith 跟踪这里

调用工具

太好了!我们能够生成工具调用。但是如果我们想实际调用工具呢?为此,我们需要将生成的工具参数传递给我们的工具。作为一个简单的例子,我们将提取第一个工具调用的参数:

from operator import itemgetter

chain = llm_with_tools | (lambda x: x.tool_calls[0]["args"]) | multiply
chain.invoke("What's four times 23")
92

查看 LangSmith 跟踪这里

代理

当我们知道任何用户输入所需的工具使用特定顺序时,链是很好的选择。但对于某些用例,我们使用工具的次数取决于输入。在这些情况下,我们希望让模型自己决定使用工具的次数和顺序。代理 让我们可以做到这一点。

LangChain 提供了许多内置代理,针对不同的用例进行了优化。阅读所有 代理类型这里

我们将使用 工具调用代理,这通常是最可靠的类型,也是大多数用例推荐使用的。

agent

<!--IMPORTS:[{"imported": "AgentExecutor", "source": "langchain.agents", "docs": "https://python.langchain.com/api_reference/langchain/agents/langchain.agents.agent.AgentExecutor.html", "title": "How to use tools in a chain"}, {"imported": "create_tool_calling_agent", "source": "langchain.agents", "docs": "https://python.langchain.com/api_reference/langchain/agents/langchain.agents.tool_calling_agent.base.create_tool_calling_agent.html", "title": "How to use tools in a chain"}]-->
from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
# Get the prompt to use - can be replaced with any prompt that includes variables "agent_scratchpad" and "input"!
prompt = hub.pull("hwchase17/openai-tools-agent")
prompt.pretty_print()
================================ System Message ================================

You are a helpful assistant

============================= Messages Placeholder =============================

{chat_history}

================================ Human Message =================================

{input}

============================= Messages Placeholder =============================

{agent_scratchpad}

代理也很棒,因为它们使得使用多个工具变得简单。

@tool
def add(first_int: int, second_int: int) -> int:
"Add two integers."
return first_int + second_int


@tool
def exponentiate(base: int, exponent: int) -> int:
"Exponentiate the base to the exponent power."
return base**exponent


tools = [multiply, add, exponentiate]
# Construct the tool calling agent
agent = create_tool_calling_agent(llm, tools, prompt)
# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

有了代理,我们可以提出需要任意多次使用工具的问题:

agent_executor.invoke(
{
"input": "Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result"
}
)


> Entering new AgentExecutor chain...

Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`


243
Invoking: `add` with `{'first_int': 12, 'second_int': 3}`


15
Invoking: `multiply` with `{'first_int': 243, 'second_int': 15}`


3645
Invoking: `exponentiate` with `{'base': 405, 'exponent': 2}`


13286025The result of taking 3 to the fifth power is 243.

The sum of twelve and three is 15.

Multiplying 243 by 15 gives 3645.

Finally, squaring 3645 gives 13286025.

> Finished chain.
{'input': 'Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result',
'output': 'The result of taking 3 to the fifth power is 243. \n\nThe sum of twelve and three is 15. \n\nMultiplying 243 by 15 gives 3645. \n\nFinally, squaring 3645 gives 13286025.'}

请查看LangSmith追踪这里


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书