深入掌握工具链:多种工具协同集成指南
在本指南中,我们将介绍如何创建可以调用工具的链和代理。工具可以是任何东西,例如API、函数、数据库等。使用工具可以扩展模型的能力,不仅仅是输出文本/信息。使用工具的关键在于正确地提示模型并解析其响应,以便模型选择合适的工具并为它们提供正确的输入。
引言
本指南的目的是让你掌握如何创建和使用可以调用工具的链和代理。在编程和AI应用中,工具链的使用可以极大地增强系统的功能和灵活性,从而帮助我们解决更复杂的问题。
主要内容
安装与设置
我们需要安装以下软件包:
%pip install --upgrade --quiet langchain
如果你想追踪运行情况,可以取消注释并设置以下环境变量:
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
# 打印工具信息
print(multiply.name)
print(multiply.description)
print(multiply.args)
创建简单的链
如果我们知道只需要固定次数使用一个工具,可以创建一个简单的链来完成。下面是一个简单的链,仅用于乘法运算:
multiply.invoke({"first_int": 4, "second_int": 5}) # 输出 20
工具函数调用
最可靠的使用方式是使用工具调用API。首先定义模型和工具:
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")
llm_with_tools = llm.bind_tools([multiply])
msg = llm_with_tools.invoke("whats 5 times forty two")
msg.tool_calls
调用工具
生成工具调用后,我们需要传递参数给工具。下面是一个简单示例:
from operator import itemgetter
chain = llm_with_tools | (lambda x: x.tool_calls[0]["args"]) | multiply
chain.invoke("What's four times 23") # 输出 92
代理的使用
代理适用于我们需要根据输入使用不固定次数的工具。LangChain提供了一些内置的代理,我们可以使用工具调用代理:
from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
@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]
agent = create_tool_calling_agent(llm, tools, prompt)
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"
}
)
代码示例
from langchain_core.tools import tool
from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
# 创建工具
@tool
def multiply(first_int: int, second_int: int) -> int:
return first_int * second_int
@tool
def add(first_int: int, second_int: int) -> int:
return first_int + second_int
@tool
def exponentiate(base: int, exponent: int) -> int:
return base**exponent
tools = [multiply, add, exponentiate]
# 初始化模型和代理
llm = ChatOpenAI(model="gpt-4o-mini")
prompt = hub.pull("hwchase17/openai-tools-agent")
agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# 调用代理
result = 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"
}
)
print(result['output'])
常见问题和解决方案
- 网络限制问题:某些地区的开发者可能需要考虑使用API代理服务来提高访问的稳定性。
- 工具调用失败:确保工具被正确定义,并检查API密钥和网络环境。
- 多个工具协同问题:确保每个工具都有清晰的描述和输入输出格式,避免混淆。
总结和进一步学习资源
本文介绍了如何创建和使用可调用工具的链和代理,通过使用这些技术可以显著扩展模型的能力。以下是一些进一步学习的资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力! ---END---