作为 OpenAI 函数的工具
本笔记本介绍了如何将 LangChain 工具作为 OpenAI 函数使用。
from langchain.chat_models import ChatOpenAI
from langchain.schema import HumanMessage
API 参考:
- ChatOpenAI 来自
langchain.chat_models
- HumanMessage 来自
langchain.schema
model = ChatOpenAI(model="gpt-3.5-turbo-0613")
from langchain.tools import MoveFileTool, format_tool_to_openai_function
API 参考:
- MoveFileTool 来自
langchain.tools
- format_tool_to_openai_function 来自
langchain.tools
tools = [MoveFileTool()]
functions = [format_tool_to_openai_function(t) for t in tools]
message = model.predict_messages(
[HumanMessage(content="move file foo to bar")], functions=functions
)
message
AIMessage(content='', additional_kwargs={'function_call': {'name': 'move_file', 'arguments': '{\n "source_path": "foo",\n "destination_path": "bar"\n}'}}, example=False)
message.additional_kwargs["function_call"]
{'name': 'move_file',
'arguments': '{\n "source_path": "foo",\n "destination_path": "bar"\n}'}