Skip to content

Commit 81db124

Browse files
tk1475eyurtsev
andauthored
docs: replace deprecated initialize_agent with create_react_agent and non-deprecated functions (#31361)
**Description:** This PR updates approximately 4' occurences of the deprecated `initialize_agent` function in LangChain documentation and examples, replacing it with the recommended `create_react_agent` and pattern. It also refactors related examples to align with current best practices. **Issue:** Partially Fixes #29277 **Dependencies:** None **X handle:** @tk1475 --------- Co-authored-by: Eugene Yurtsev <[email protected]>
1 parent 4ec46ae commit 81db124

File tree

4 files changed

+49
-30
lines changed

4 files changed

+49
-30
lines changed

docs/docs/integrations/providers/google_serper.mdx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ You can use it as part of a Self Ask chain:
2222

2323
```python
2424
from langchain_community.utilities import GoogleSerperAPIWrapper
25-
from langchain_openai import OpenAI
26-
from langchain.agents import initialize_agent, Tool
27-
from langchain.agents import AgentType
25+
from langchain_core.tools import Tool
26+
from langchain_openai import ChatOpenAI
27+
from langgraph.prebuilt import create_react_agent
2828

2929
import os
3030

3131
os.environ["SERPER_API_KEY"] = ""
3232
os.environ['OPENAI_API_KEY'] = ""
3333

34-
llm = OpenAI(temperature=0)
34+
llm = ChatOpenAI(temperature=0)
3535
search = GoogleSerperAPIWrapper()
3636
tools = [
3737
Tool(
@@ -41,8 +41,13 @@ tools = [
4141
)
4242
]
4343

44-
self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)
45-
self_ask_with_search.run("What is the hometown of the reigning men's U.S. Open champion?")
44+
agent = create_react_agent(llm, tools)
45+
46+
result = agent.invoke({
47+
"messages": [("human", "What is the hometown of the reigning men's U.S. Open champion?")]
48+
})
49+
50+
print(result)
4651
```
4752

4853
#### Output

docs/docs/integrations/providers/searchapi.mdx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ You can use it as part of a Self Ask chain:
2222
```python
2323
from langchain_community.utilities import SearchApiAPIWrapper
2424
from langchain_openai import OpenAI
25-
from langchain.agents import initialize_agent, Tool
26-
from langchain.agents import AgentType
25+
from langchain.agents import Tool, create_openai_functions_agent, AgentExecutor
2726

2827
import os
2928

30-
os.environ["SEARCHAPI_API_KEY"] = ""
31-
os.environ['OPENAI_API_KEY'] = ""
29+
os.environ["SEARCHAPI_API_KEY"] = "<your-searchapi-key>"
30+
os.environ['OPENAI_API_KEY'] = "<your-openai-key>"
3231

3332
llm = OpenAI(temperature=0)
3433
search = SearchApiAPIWrapper()
@@ -40,8 +39,13 @@ tools = [
4039
)
4140
]
4241

43-
self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)
44-
self_ask_with_search.run("Who lived longer: Plato, Socrates, or Aristotle?")
42+
# Create agent and executor
43+
agent = create_openai_functions_agent(llm=llm, tools=tools)
44+
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
45+
46+
# Run the agent
47+
response = agent_executor.invoke({"input": "Who lived longer: Plato, Socrates, or Aristotle?"})
48+
print(response["output"])
4549
```
4650

4751
#### Output

docs/docs/integrations/tools/chatgpt_plugins.ipynb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@
5252
},
5353
{
5454
"cell_type": "code",
55-
"execution_count": 1,
55+
"execution_count": null,
5656
"id": "d41405b5",
5757
"metadata": {},
5858
"outputs": [],
5959
"source": [
60-
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
60+
"from langchain.agents import AgentExecutor, create_react_agent, load_tools\n",
6161
"from langchain_openai import ChatOpenAI"
6262
]
6363
},
6464
{
6565
"cell_type": "code",
66-
"execution_count": 2,
66+
"execution_count": null,
6767
"id": "d9e61df5",
6868
"metadata": {},
6969
"outputs": [],
@@ -73,7 +73,7 @@
7373
},
7474
{
7575
"cell_type": "code",
76-
"execution_count": 3,
76+
"execution_count": null,
7777
"id": "edc0ea0e",
7878
"metadata": {},
7979
"outputs": [
@@ -113,13 +113,15 @@
113113
],
114114
"source": [
115115
"llm = ChatOpenAI(temperature=0)\n",
116+
"\n",
116117
"tools = load_tools([\"requests_all\"])\n",
117118
"tools += [tool]\n",
118119
"\n",
119-
"agent_chain = initialize_agent(\n",
120-
" tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
121-
")\n",
122-
"agent_chain.run(\"what t shirts are available in klarna?\")"
120+
"agent = create_react_agent(llm=llm, tools=tools)\n",
121+
"\n",
122+
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)\n",
123+
"\n",
124+
"agent_executor.invoke({\"input\": \"what t shirts are available in klarna?\"})"
123125
]
124126
},
125127
{
@@ -133,7 +135,7 @@
133135
],
134136
"metadata": {
135137
"kernelspec": {
136-
"display_name": "Python 3 (ipykernel)",
138+
"display_name": "AI_env",
137139
"language": "python",
138140
"name": "python3"
139141
},
@@ -147,7 +149,7 @@
147149
"name": "python",
148150
"nbconvert_exporter": "python",
149151
"pygments_lexer": "ipython3",
150-
"version": "3.10.12"
152+
"version": "3.12.2"
151153
}
152154
},
153155
"nbformat": 4,

docs/docs/integrations/tools/nasa.ipynb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,29 @@
3535
{
3636
"cell_type": "code",
3737
"execution_count": null,
38-
"id": "648a2cb2-308e-4b2e-9b73-37109be4e258",
38+
"id": "cf0cb2e5",
3939
"metadata": {},
4040
"outputs": [],
4141
"source": [
42-
"from langchain.agents import AgentType, initialize_agent\n",
4342
"from langchain_community.agent_toolkits.nasa.toolkit import NasaToolkit\n",
4443
"from langchain_community.utilities.nasa import NasaAPIWrapper\n",
4544
"from langchain_openai import OpenAI\n",
46-
"\n",
45+
"from langgraph.prebuilt import create_react_agent"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": null,
51+
"id": "648a2cb2-308e-4b2e-9b73-37109be4e258",
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
4755
"llm = OpenAI(temperature=0, openai_api_key=\"\")\n",
4856
"nasa = NasaAPIWrapper()\n",
4957
"toolkit = NasaToolkit.from_nasa_api_wrapper(nasa)\n",
50-
"agent = initialize_agent(\n",
51-
" toolkit.get_tools(), llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
52-
")"
58+
"tools = toolkit.get_tools()\n",
59+
"\n",
60+
"agent = create_react_agent(llm, tools)"
5361
]
5462
},
5563
{
@@ -96,7 +104,7 @@
96104
],
97105
"metadata": {
98106
"kernelspec": {
99-
"display_name": "Python 3 (ipykernel)",
107+
"display_name": "AI_env",
100108
"language": "python",
101109
"name": "python3"
102110
},
@@ -110,7 +118,7 @@
110118
"name": "python",
111119
"nbconvert_exporter": "python",
112120
"pygments_lexer": "ipython3",
113-
"version": "3.11.5"
121+
"version": "3.12.2"
114122
}
115123
},
116124
"nbformat": 4,

0 commit comments

Comments
 (0)