core[patch], community[patch], openai[patch]: consolidate openai tool… (#16485)

… converters

One way to convert anything to an OAI function:
convert_to_openai_function
One way to convert anything to an OAI tool: convert_to_openai_tool
Corresponding bind functions on OAI models: bind_functions, bind_tools
This commit is contained in:
Bagatur
2024-01-25 13:18:46 -08:00
committed by GitHub
parent 148347e858
commit ef42d9d559
25 changed files with 1480 additions and 326 deletions

View File

@@ -12,71 +12,101 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "bb220019-4012-4da4-bfee-01fb8189aa49",
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU langchain-community langchain-openai"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "d65d8a60",
"metadata": {},
"outputs": [],
"source": [
"from langchain.schema import HumanMessage\n",
"from langchain_community.tools import MoveFileTool\n",
"from langchain_core.messages import HumanMessage\n",
"from langchain_core.utils.function_calling import convert_to_openai_function\n",
"from langchain_openai import ChatOpenAI"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 20,
"id": "abd8dc72",
"metadata": {},
"outputs": [],
"source": [
"model = ChatOpenAI(model=\"gpt-3.5-turbo-0613\")"
"model = ChatOpenAI(model=\"gpt-3.5-turbo\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "dce2cdb7",
"metadata": {},
"outputs": [],
"source": [
"from langchain.tools import MoveFileTool, format_tool_to_openai_function"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 21,
"id": "3b3dc766",
"metadata": {},
"outputs": [],
"source": [
"tools = [MoveFileTool()]\n",
"functions = [format_tool_to_openai_function(t) for t in tools]"
"functions = [convert_to_openai_function(t) for t in tools]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 12,
"id": "d38c4a22-2e9e-4d15-a9e1-bf8103c6303b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'name': 'move_file',\n",
" 'description': 'Move or rename a file from one location to another',\n",
" 'parameters': {'type': 'object',\n",
" 'properties': {'source_path': {'description': 'Path of the file to move',\n",
" 'type': 'string'},\n",
" 'destination_path': {'description': 'New path for the moved file',\n",
" 'type': 'string'}},\n",
" 'required': ['source_path', 'destination_path']}}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"functions[0]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "230a7939",
"metadata": {},
"outputs": [],
"source": [
"message = model.predict_messages(\n",
"message = model.invoke(\n",
" [HumanMessage(content=\"move file foo to bar\")], functions=functions\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 16,
"id": "c118c940",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='', additional_kwargs={'function_call': {'name': 'move_file', 'arguments': '{\\n \"source_path\": \"foo\",\\n \"destination_path\": \"bar\"\\n}'}}, example=False)"
"AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"source_path\": \"foo\",\\n \"destination_path\": \"bar\"\\n}', 'name': 'move_file'}})"
]
},
"execution_count": 6,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -108,12 +138,64 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "751da79f",
"cell_type": "markdown",
"id": "77dd0d9f-2f24-4535-a658-a061f91e009a",
"metadata": {},
"outputs": [],
"source": []
"source": [
"With OpenAI chat models we can also automatically bind and convert function-like objects with `bind_functions`"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "24bb1518-8100-4ac3-acea-04acfac963d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n \"source_path\": \"foo\",\\n \"destination_path\": \"bar\"\\n}', 'name': 'move_file'}})"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model_with_functions = model.bind_functions(tools)\n",
"model_with_functions.invoke([HumanMessage(content=\"move file foo to bar\")])"
]
},
{
"cell_type": "markdown",
"id": "000ec6ff-ca67-4206-ba56-cc2a91b85ce6",
"metadata": {},
"source": [
"Or we can use the update OpenAI API that uses `tools` and `tool_choice` instead of `functions` and `function_call` by using `ChatOpenAI.bind_tools`:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "1a333e4e-df55-4e15-9d2e-4fd142d969f3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"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'}]})"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model_with_tools = model.bind_tools(tools)\n",
"model_with_tools.invoke([HumanMessage(content=\"move file foo to bar\")])"
]
}
],
"metadata": {