使用代理重复使用工具
当我们知道任何用户输入所需的工具使用特定顺序时,链非常有用。但是对于某些用例,我们使用工具的次数取决于输入。在这些情况下,我们希望让模型自己决定使用工具的次数和顺序。代理 让我们做到这一点。
LangChain 附带了若干针对不同用例优化的内置代理。阅读有关所有 代理类型的信息。
我们将使用 工具调用代理,它通常是最可靠的类型,也是大多数用例推荐的类型。这里的 "工具调用" 指的是特定类型的模型 API,它允许显式地将工具定义传递给模型并获得显式的工具调用。有关工具调用模型的更多信息,请参阅 本指南。
设置
我们需要安装以下包:
%pip install --upgrade --quiet langchain langchainhub如果你想使用 LangSmith,请设置下面的环境变量:
import getpass
import os
# os.environ ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ ["LANGCHAIN_API_KEY"] = getpass.getpass()创建工具
首先,我们需要创建一些要调用的工具。在本例中,我们将从函数创建自定义工具。有关创建自定义工具的更多信息,请参阅 本指南。
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
@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]创建提示
from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent# Get the prompt to use - you can modify this!
prompt = hub.pull("hwchase17/openai-tools-agent")
prompt.pretty_print()================================[1m System Message [0m================================
You are a helpful assistant
=============================[1m Messages Placeholder [0m=============================
[33;1m[1;3m{chat_history}[0m
================================[1m Human Message [0m=================================
[33;1m[1;3m{input}[0m
=============================[1m Messages Placeholder [0m=============================
[33;1m[1;3m{agent_scratchpad}[0m创建代理
我们需要使用具有工具调用能力的模型。你可以在 这里 查看哪些模型支持工具调用。
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-3-sonnet-20240229", temperature=0)# 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"
}
)
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3m
Invoking: `exponentiate` with `{'base': 3, 'exponent': 5}`
responded: [{'text': "Okay, let's break this down step-by-step:", 'type': 'text'}, {'id': 'toolu_01CjdiDhDmMtaT1F4R7hSV5D', 'input': {'base': 3, 'exponent': 5}, 'name': 'exponentiate', 'type': 'tool_use'}]
[0m[38;5;200m[1;3m243[0m[32;1m[1;3m
Invoking: `add` with `{'first_int': 12, 'second_int': 3}`
responded: [{'text': '3 to the 5th power is 243.', 'type': 'text'}, {'id': 'toolu_01EKqn4E5w3Zj7bQ8s8xmi4R', 'input': {'first_int': 12, 'second_int': 3}, 'name': 'add', 'type': 'tool_use'}]
[0m[33;1m[1;3m15[0m[32;1m[1;3m
Invoking: `multiply` with `{'first_int': 243, 'second_int': 15}`
responded: [{'text': '12 + 3 = 15', 'type': 'text'}, {'id': 'toolu_017VZJgZBYbwMo2KGD6o6hsQ', 'input': {'first_int': 243, 'second_int': 15}, 'name': 'multiply', 'type': 'tool_use'}]
[0m[36;1m[1;3m3645[0m[32;1m[1;3m
Invoking: `multiply` with `{'first_int': 3645, 'second_int': 3645}`
responded: [{'text': '243 * 15 = 3645', 'type': 'text'}, {'id': 'toolu_01RtFCcQgbVGya3NVDgTYKTa', 'input': {'first_int': 3645, 'second_int': 3645}, 'name': 'multiply', 'type': 'tool_use'}]
[0m[36;1m[1;3m13286025[0m[32;1m[1;3mSo 3645 squared is 13,286,025.
Therefore, the final result of taking 3 to the 5th power (243), multiplying by 12 + 3 (15), and then squaring the whole result is 13,286,025.[0m
[1m> Finished chain.[0m{'input': 'Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result',
'output': 'So 3645 squared is 13,286,025.\n\nTherefore, the final result of taking 3 to the 5th power (243), multiplying by 12 + 3 (15), and then squaring the whole result is 13,286,025.'}