Skip to main content

作为 OpenAI 函数的工具

本笔记本介绍了如何将 LangChain 工具作为 OpenAI 函数使用。

from langchain.chat_models import ChatOpenAI  
from langchain.schema import HumanMessage

API 参考:

model = ChatOpenAI(model="gpt-3.5-turbo-0613")  
from langchain.tools import MoveFileTool, format_tool_to_openai_function  

API 参考:

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}'}