Skip to content

Commit ef42d9d

Browse files
authored
core[patch], community[patch], openai[patch]: consolidate openai tool… (langchain-ai#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
1 parent 148347e commit ef42d9d

File tree

25 files changed

+1471
-317
lines changed

25 files changed

+1471
-317
lines changed

docs/docs/expression_language/cookbook/prompt_size.ipynb

Lines changed: 89 additions & 86 deletions
Large diffs are not rendered by default.

docs/docs/langsmith/walkthrough.ipynb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
},
130130
{
131131
"cell_type": "code",
132-
"execution_count": 6,
132+
"execution_count": 1,
133133
"id": "a0fbfbba-3c82-4298-a312-9cec016d9d2e",
134134
"metadata": {},
135135
"outputs": [],
@@ -138,8 +138,7 @@
138138
"from langchain.agents import AgentExecutor\n",
139139
"from langchain.agents.format_scratchpad import format_to_openai_function_messages\n",
140140
"from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser\n",
141-
"from langchain.tools import DuckDuckGoSearchResults\n",
142-
"from langchain_community.tools.convert_to_openai import format_tool_to_openai_function\n",
141+
"from langchain_community.tools import DuckDuckGoSearchResults\n",
143142
"from langchain_openai import ChatOpenAI\n",
144143
"\n",
145144
"# Fetches the latest version of this prompt\n",
@@ -156,7 +155,7 @@
156155
" ), # General internet search using DuckDuckGo\n",
157156
"]\n",
158157
"\n",
159-
"llm_with_tools = llm.bind(functions=[format_tool_to_openai_function(t) for t in tools])\n",
158+
"llm_with_tools = llm.bind_functions(tools)\n",
160159
"\n",
161160
"runnable_agent = (\n",
162161
" {\n",
@@ -334,7 +333,6 @@
334333
"from langchain.agents import AgentExecutor, AgentType, initialize_agent, load_tools\n",
335334
"from langchain.agents.format_scratchpad import format_to_openai_function_messages\n",
336335
"from langchain.agents.output_parsers import OpenAIFunctionsAgentOutputParser\n",
337-
"from langchain_community.tools.convert_to_openai import format_tool_to_openai_function\n",
338336
"from langchain_openai import ChatOpenAI\n",
339337
"\n",
340338
"\n",
@@ -1345,9 +1343,9 @@
13451343
],
13461344
"metadata": {
13471345
"kernelspec": {
1348-
"display_name": "Python 3 (ipykernel)",
1346+
"display_name": "poetry-venv",
13491347
"language": "python",
1350-
"name": "python3"
1348+
"name": "poetry-venv"
13511349
},
13521350
"language_info": {
13531351
"codemirror_mode": {
@@ -1359,7 +1357,7 @@
13591357
"name": "python",
13601358
"nbconvert_exporter": "python",
13611359
"pygments_lexer": "ipython3",
1362-
"version": "3.11.2"
1360+
"version": "3.9.1"
13631361
}
13641362
},
13651363
"nbformat": 4,

docs/docs/modules/agents/how_to/agent_structured.ipynb

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"metadata": {},
4444
"outputs": [],
4545
"source": [
46-
"# pip install chromadb"
46+
"%pip install -qU chromadb langchain langchain-community langchain-openai"
4747
]
4848
},
4949
{
@@ -61,7 +61,7 @@
6161
},
6262
{
6363
"cell_type": "code",
64-
"execution_count": 16,
64+
"execution_count": 2,
6565
"id": "e3002ed7",
6666
"metadata": {},
6767
"outputs": [],
@@ -96,14 +96,12 @@
9696
},
9797
{
9898
"cell_type": "code",
99-
"execution_count": 4,
99+
"execution_count": 3,
100100
"id": "204ef7ca",
101101
"metadata": {},
102102
"outputs": [],
103103
"source": [
104-
"from langchain.agents.agent_toolkits.conversational_retrieval.tool import (\n",
105-
" create_retriever_tool,\n",
106-
")\n",
104+
"from langchain.tools.retriever import create_retriever_tool\n",
107105
"\n",
108106
"retriever_tool = create_retriever_tool(\n",
109107
" retriever,\n",
@@ -124,15 +122,14 @@
124122
},
125123
{
126124
"cell_type": "code",
127-
"execution_count": 5,
125+
"execution_count": 4,
128126
"id": "2df91723",
129127
"metadata": {},
130128
"outputs": [],
131129
"source": [
132130
"from typing import List\n",
133131
"\n",
134-
"from langchain.utils.openai_functions import convert_pydantic_to_openai_function\n",
135-
"from pydantic import BaseModel, Field\n",
132+
"from langchain_core.pydantic_v1 import BaseModel, Field\n",
136133
"\n",
137134
"\n",
138135
"class Response(BaseModel):\n",
@@ -169,7 +166,7 @@
169166
},
170167
{
171168
"cell_type": "code",
172-
"execution_count": 6,
169+
"execution_count": 5,
173170
"id": "dfb73fe3",
174171
"metadata": {},
175172
"outputs": [],
@@ -181,7 +178,7 @@
181178
},
182179
{
183180
"cell_type": "code",
184-
"execution_count": 17,
181+
"execution_count": 6,
185182
"id": "5b46cdb2",
186183
"metadata": {},
187184
"outputs": [],
@@ -224,14 +221,13 @@
224221
},
225222
{
226223
"cell_type": "code",
227-
"execution_count": null,
224+
"execution_count": 7,
228225
"id": "73c785f9",
229226
"metadata": {},
230227
"outputs": [],
231228
"source": [
232229
"from langchain.agents import AgentExecutor\n",
233230
"from langchain.agents.format_scratchpad import format_to_openai_function_messages\n",
234-
"from langchain_community.tools.convert_to_openai import format_tool_to_openai_function\n",
235231
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
236232
"from langchain_openai import ChatOpenAI"
237233
]
@@ -269,14 +265,7 @@
269265
"metadata": {},
270266
"outputs": [],
271267
"source": [
272-
"llm_with_tools = llm.bind(\n",
273-
" functions=[\n",
274-
" # The retriever tool\n",
275-
" format_tool_to_openai_function(retriever_tool),\n",
276-
" # Response schema\n",
277-
" convert_pydantic_to_openai_function(Response),\n",
278-
" ]\n",
279-
")"
268+
"llm_with_tools = llm.bind_functions([retriever_tool, Response])"
280269
]
281270
},
282271
{
@@ -302,7 +291,7 @@
302291
},
303292
{
304293
"cell_type": "code",
305-
"execution_count": 14,
294+
"execution_count": 12,
306295
"id": "2cfd783e",
307296
"metadata": {},
308297
"outputs": [],
@@ -322,7 +311,7 @@
322311
},
323312
{
324313
"cell_type": "code",
325-
"execution_count": 18,
314+
"execution_count": 20,
326315
"id": "2667c9a4",
327316
"metadata": {},
328317
"outputs": [
@@ -333,26 +322,74 @@
333322
"\n",
334323
"\n",
335324
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
336-
"\u001b[32;1m\u001b[1;3m\u001b[0m\u001b[36;1m\u001b[1;3m[Document(page_content='Tonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \\n\\nTonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \\n\\nOne of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \\n\\nAnd I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.', metadata={'page_chunk': 31, 'source': '../../state_of_the_union.txt'}), Document(page_content='One was stationed at bases and breathing in toxic smoke from “burn pits” that incinerated wastes of war—medical and hazard material, jet fuel, and more. \\n\\nWhen they came home, many of the world’s fittest and best trained warriors were never the same. \\n\\nHeadaches. Numbness. Dizziness. \\n\\nA cancer that would put them in a flag-draped coffin. \\n\\nI know. \\n\\nOne of those soldiers was my son Major Beau Biden. \\n\\nWe don’t know for sure if a burn pit was the cause of his brain cancer, or the diseases of so many of our troops. \\n\\nBut I’m committed to finding out everything we can. \\n\\nCommitted to military families like Danielle Robinson from Ohio. \\n\\nThe widow of Sergeant First Class Heath Robinson. \\n\\nHe was born a soldier. Army National Guard. Combat medic in Kosovo and Iraq. \\n\\nStationed near Baghdad, just yards from burn pits the size of football fields. \\n\\nHeath’s widow Danielle is here with us tonight. They loved going to Ohio State football games. He loved building Legos with their daughter.', metadata={'page_chunk': 37, 'source': '../../state_of_the_union.txt'}), Document(page_content='A former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since she’s been nominated, she’s received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. \\n\\nAnd if we are to advance liberty and justice, we need to secure the Border and fix the immigration system. \\n\\nWe can do both. At our border, we’ve installed new technology like cutting-edge scanners to better detect drug smuggling. \\n\\nWe’ve set up joint patrols with Mexico and Guatemala to catch more human traffickers. \\n\\nWe’re putting in place dedicated immigration judges so families fleeing persecution and violence can have their cases heard faster. \\n\\nWe’re securing commitments and supporting partners in South and Central America to host more refugees and secure their own borders.', metadata={'page_chunk': 32, 'source': '../../state_of_the_union.txt'}), Document(page_content='But cancer from prolonged exposure to burn pits ravaged Heath’s lungs and body. \\n\\nDanielle says Heath was a fighter to the very end. \\n\\nHe didn’t know how to stop fighting, and neither did she. \\n\\nThrough her pain she found purpose to demand we do better. \\n\\nTonight, Danielle—we are. \\n\\nThe VA is pioneering new ways of linking toxic exposures to diseases, already helping more veterans get benefits. \\n\\nAnd tonight, I’m announcing we’re expanding eligibility to veterans suffering from nine respiratory cancers. \\n\\nI’m also calling on Congress: pass a law to make sure veterans devastated by toxic exposures in Iraq and Afghanistan finally get the benefits and comprehensive health care they deserve. \\n\\nAnd fourth, let’s end cancer as we know it. \\n\\nThis is personal to me and Jill, to Kamala, and to so many of you. \\n\\nCancer is the #2 cause of death in America–second only to heart disease.', metadata={'page_chunk': 38, 'source': '../../state_of_the_union.txt'})]\u001b[0m\u001b[32;1m\u001b[1;3m{'name': 'Response', 'arguments': '{\\n \"answer\": \"President mentioned Ketanji Brown Jackson as a nominee for the United States Supreme Court and praised her as one of the nation\\'s top legal minds.\",\\n \"sources\": [31]\\n}'}\u001b[0m\n",
325+
"\u001b[32;1m\u001b[1;3m\u001b[0m\u001b[36;1m\u001b[1;3mTonight. I call on the Senate to: Pass the Freedom to Vote Act. Pass the John Lewis Voting Rights Act. And while you’re at it, pass the Disclose Act so Americans can know who is funding our elections. \n",
326+
"\n",
327+
"Tonight, I’d like to honor someone who has dedicated his life to serve this country: Justice Stephen Breyer—an Army veteran, Constitutional scholar, and retiring Justice of the United States Supreme Court. Justice Breyer, thank you for your service. \n",
328+
"\n",
329+
"One of the most serious constitutional responsibilities a President has is nominating someone to serve on the United States Supreme Court. \n",
330+
"\n",
331+
"And I did that 4 days ago, when I nominated Circuit Court of Appeals Judge Ketanji Brown Jackson. One of our nation’s top legal minds, who will continue Justice Breyer’s legacy of excellence.\n",
332+
"\n",
333+
"And for our LGBTQ+ Americans, let’s finally get the bipartisan Equality Act to my desk. The onslaught of state laws targeting transgender Americans and their families is wrong. \n",
334+
"\n",
335+
"As I said last year, especially to our younger transgender Americans, I will always have your back as your President, so you can be yourself and reach your God-given potential. \n",
336+
"\n",
337+
"While it often appears that we never agree, that isn’t true. I signed 80 bipartisan bills into law last year. From preventing government shutdowns to protecting Asian-Americans from still-too-common hate crimes to reforming military justice. \n",
338+
"\n",
339+
"And soon, we’ll strengthen the Violence Against Women Act that I first wrote three decades ago. It is important for us to show the nation that we can come together and do big things. \n",
340+
"\n",
341+
"So tonight I’m offering a Unity Agenda for the Nation. Four big things we can do together. \n",
342+
"\n",
343+
"First, beat the opioid epidemic.\n",
344+
"\n",
345+
"Madam Speaker, Madam Vice President, our First Lady and Second Gentleman. Members of Congress and the Cabinet. Justices of the Supreme Court. My fellow Americans. \n",
346+
"\n",
347+
"Last year COVID-19 kept us apart. This year we are finally together again. \n",
348+
"\n",
349+
"Tonight, we meet as Democrats Republicans and Independents. But most importantly as Americans. \n",
350+
"\n",
351+
"With a duty to one another to the American people to the Constitution. \n",
352+
"\n",
353+
"And with an unwavering resolve that freedom will always triumph over tyranny. \n",
354+
"\n",
355+
"Six days ago, Russia’s Vladimir Putin sought to shake the foundations of the free world thinking he could make it bend to his menacing ways. But he badly miscalculated. \n",
356+
"\n",
357+
"He thought he could roll into Ukraine and the world would roll over. Instead he met a wall of strength he never imagined. \n",
358+
"\n",
359+
"He met the Ukrainian people. \n",
360+
"\n",
361+
"From President Zelenskyy to every Ukrainian, their fearlessness, their courage, their determination, inspires the world.\n",
362+
"\n",
363+
"A former top litigator in private practice. A former federal public defender. And from a family of public school educators and police officers. A consensus builder. Since she’s been nominated, she’s received a broad range of support—from the Fraternal Order of Police to former judges appointed by Democrats and Republicans. \n",
364+
"\n",
365+
"And if we are to advance liberty and justice, we need to secure the Border and fix the immigration system. \n",
366+
"\n",
367+
"We can do both. At our border, we’ve installed new technology like cutting-edge scanners to better detect drug smuggling. \n",
368+
"\n",
369+
"We’ve set up joint patrols with Mexico and Guatemala to catch more human traffickers. \n",
370+
"\n",
371+
"We’re putting in place dedicated immigration judges so families fleeing persecution and violence can have their cases heard faster. \n",
372+
"\n",
373+
"We’re securing commitments and supporting partners in South and Central America to host more refugees and secure their own borders.\u001b[0m\u001b[32;1m\u001b[1;3m{'arguments': '{\\n\"answer\": \"President Biden nominated Ketanji Brown Jackson for the United States Supreme Court and described her as one of our nation\\'s top legal minds who will continue Justice Breyer\\'s legacy of excellence.\",\\n\"sources\": [6]\\n}', 'name': 'Response'}\u001b[0m\n",
337374
"\n",
338375
"\u001b[1m> Finished chain.\u001b[0m\n"
339376
]
340377
},
341378
{
342379
"data": {
343380
"text/plain": [
344-
"{'answer': \"President mentioned Ketanji Brown Jackson as a nominee for the United States Supreme Court and praised her as one of the nation's top legal minds.\",\n",
345-
" 'sources': [31]}"
381+
"{'answer': \"President Biden nominated Ketanji Brown Jackson for the United States Supreme Court and described her as one of our nation's top legal minds who will continue Justice Breyer's legacy of excellence.\",\n",
382+
" 'sources': [6]}"
346383
]
347384
},
348-
"execution_count": 18,
385+
"execution_count": 20,
349386
"metadata": {},
350387
"output_type": "execute_result"
351388
}
352389
],
353390
"source": [
354391
"agent_executor.invoke(\n",
355-
" {\"input\": \"what did the president say about kentaji brown jackson\"},\n",
392+
" {\"input\": \"what did the president say about ketanji brown jackson\"},\n",
356393
" return_only_outputs=True,\n",
357394
")"
358395
]
@@ -368,9 +405,9 @@
368405
],
369406
"metadata": {
370407
"kernelspec": {
371-
"display_name": "Python 3 (ipykernel)",
408+
"display_name": "poetry-venv",
372409
"language": "python",
373-
"name": "python3"
410+
"name": "poetry-venv"
374411
},
375412
"language_info": {
376413
"codemirror_mode": {
@@ -382,7 +419,7 @@
382419
"name": "python",
383420
"nbconvert_exporter": "python",
384421
"pygments_lexer": "ipython3",
385-
"version": "3.10.1"
422+
"version": "3.9.1"
386423
}
387424
},
388425
"nbformat": 4,

docs/docs/modules/agents/how_to/custom_agent.ipynb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,7 @@
152152
"metadata": {},
153153
"outputs": [],
154154
"source": [
155-
"from langchain_community.tools.convert_to_openai import format_tool_to_openai_tool\n",
156-
"\n",
157-
"llm_with_tools = llm.bind(tools=[format_tool_to_openai_tool(tool) for tool in tools])"
155+
"llm_with_tools = llm.bind_tools(tools)"
158156
]
159157
},
160158
{
@@ -229,9 +227,9 @@
229227
{
230228
"data": {
231229
"text/plain": [
232-
"[{'actions': [OpenAIToolAgentAction(tool='get_word_length', tool_input={'word': 'eudca'}, log=\"\\nInvoking: `get_word_length` with `{'word': 'eudca'}`\\n\\n\\n\", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_U9SR78eT398r9UbzID2N9LXh', 'function': {'arguments': '{\\n \"word\": \"eudca\"\\n}', 'name': 'get_word_length'}, 'type': 'function'}]})], tool_call_id='call_U9SR78eT398r9UbzID2N9LXh')],\n",
233-
" 'messages': [AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_U9SR78eT398r9UbzID2N9LXh', 'function': {'arguments': '{\\n \"word\": \"eudca\"\\n}', 'name': 'get_word_length'}, 'type': 'function'}]})]},\n",
234-
" {'steps': [AgentStep(action=OpenAIToolAgentAction(tool='get_word_length', tool_input={'word': 'eudca'}, log=\"\\nInvoking: `get_word_length` with `{'word': 'eudca'}`\\n\\n\\n\", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_U9SR78eT398r9UbzID2N9LXh', 'function': {'arguments': '{\\n \"word\": \"eudca\"\\n}', 'name': 'get_word_length'}, 'type': 'function'}]})], tool_call_id='call_U9SR78eT398r9UbzID2N9LXh'), observation=5)],\n",
230+
"[{'actions': [OpenAIToolAgentAction(tool='get_word_length', tool_input={'word': 'eudca'}, log=\"\\nInvoking: `get_word_length` with `{'word': 'eudca'}`\\n\\n\\n\", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_A07D5TuyqcNIL0DIEVRPpZkg', 'function': {'arguments': '{\\n \"word\": \"eudca\"\\n}', 'name': 'get_word_length'}, 'type': 'function'}]})], tool_call_id='call_A07D5TuyqcNIL0DIEVRPpZkg')],\n",
231+
" 'messages': [AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_A07D5TuyqcNIL0DIEVRPpZkg', 'function': {'arguments': '{\\n \"word\": \"eudca\"\\n}', 'name': 'get_word_length'}, 'type': 'function'}]})]},\n",
232+
" {'steps': [AgentStep(action=OpenAIToolAgentAction(tool='get_word_length', tool_input={'word': 'eudca'}, log=\"\\nInvoking: `get_word_length` with `{'word': 'eudca'}`\\n\\n\\n\", message_log=[AIMessageChunk(content='', additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_A07D5TuyqcNIL0DIEVRPpZkg', 'function': {'arguments': '{\\n \"word\": \"eudca\"\\n}', 'name': 'get_word_length'}, 'type': 'function'}]})], tool_call_id='call_A07D5TuyqcNIL0DIEVRPpZkg'), observation=5)],\n",
235233
" 'messages': [FunctionMessage(content='5', name='get_word_length')]},\n",
236234
" {'output': 'There are 5 letters in the word \"eudca\".',\n",
237235
" 'messages': [AIMessage(content='There are 5 letters in the word \"eudca\".')]}]"
@@ -449,7 +447,7 @@
449447
"name": "python",
450448
"nbconvert_exporter": "python",
451449
"pygments_lexer": "ipython3",
452-
"version": "3.11.4"
450+
"version": "3.9.1"
453451
},
454452
"vscode": {
455453
"interpreter": {

0 commit comments

Comments
 (0)