Skip to content

Commit c0b6980

Browse files
docs: replace initialize_agent with create_react_agent in openweathermap.ipynb (#31115)
Thank you for contributing to LangChain! - [x] **PR title**: "package: description" - Where "package" is whichever of langchain, core, etc. is being modified. Use "docs: ..." for purely docs changes, "infra: ..." for CI changes. - Example: "core: add foobar LLM" - [x] **PR message**: ***Delete this entire checklist*** and replace with - **Description:** a description of the change - **Issue:** the issue # it fixes, if applicable - **Dependencies:** any dependencies required for this change - **Twitter handle:** if your PR gets announced, and you'd like a mention, we'll gladly shout you out! - [x] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [x] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. If no one reviews your PR within a few days, please @-mention one of baskaryan, eyurtsev, ccurme, vbarda, hwchase17. --------- Co-authored-by: Chester Curme <chester.curme@gmail.com>
1 parent fce8cac commit c0b6980

File tree

1 file changed

+77
-53
lines changed

1 file changed

+77
-53
lines changed

docs/docs/integrations/tools/openweathermap.ipynb

Lines changed: 77 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,26 @@
2222
},
2323
{
2424
"cell_type": "code",
25-
"execution_count": 9,
26-
"id": "34bb5968",
25+
"execution_count": 1,
26+
"id": "8b81a74e-db10-4e8d-9f90-83219df30ab3",
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"name": "stdout",
31+
"output_type": "stream",
32+
"text": [
33+
"Note: you may need to restart the kernel to use updated packages.\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"%pip install --upgrade --quiet pyowm"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 2,
44+
"id": "78ab9fcd-bb7b-434b-9a38-0a9249e35768",
2745
"metadata": {},
2846
"outputs": [],
2947
"source": [
@@ -38,26 +56,26 @@
3856
},
3957
{
4058
"cell_type": "code",
41-
"execution_count": 10,
42-
"id": "ac4910f8",
59+
"execution_count": 3,
60+
"id": "0a8aa4b0-6aea-4172-9546-361e127a4a02",
4361
"metadata": {},
4462
"outputs": [
4563
{
4664
"name": "stdout",
4765
"output_type": "stream",
4866
"text": [
4967
"In London,GB, the current weather is as follows:\n",
50-
"Detailed status: broken clouds\n",
51-
"Wind speed: 2.57 m/s, direction: 240°\n",
52-
"Humidity: 55%\n",
68+
"Detailed status: overcast clouds\n",
69+
"Wind speed: 4.12 m/s, direction: 10°\n",
70+
"Humidity: 51%\n",
5371
"Temperature: \n",
54-
" - Current: 20.12°C\n",
55-
" - High: 21.75°C\n",
56-
" - Low: 18.68°C\n",
57-
" - Feels like: 19.62°C\n",
72+
" - Current: 12.82°C\n",
73+
" - High: 13.98°C\n",
74+
" - Low: 12.01°C\n",
75+
" - Feels like: 11.49°C\n",
5876
"Rain: {}\n",
5977
"Heat index: None\n",
60-
"Cloud cover: 75%\n"
78+
"Cloud cover: 100%\n"
6179
]
6280
}
6381
],
@@ -76,76 +94,82 @@
7694
},
7795
{
7896
"cell_type": "code",
79-
"execution_count": 11,
80-
"id": "b3367417",
97+
"execution_count": 4,
98+
"id": "402c832c-87c7-4088-b80f-ec1924a43796",
8199
"metadata": {},
82100
"outputs": [],
83101
"source": [
84102
"import os\n",
85103
"\n",
86-
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
87-
"from langchain_openai import OpenAI\n",
104+
"from langgraph.prebuilt import create_react_agent\n",
88105
"\n",
89106
"os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
90107
"os.environ[\"OPENWEATHERMAP_API_KEY\"] = \"\"\n",
91108
"\n",
92-
"llm = OpenAI(temperature=0)\n",
93-
"\n",
94-
"tools = load_tools([\"openweathermap-api\"], llm)\n",
95-
"\n",
96-
"agent_chain = initialize_agent(\n",
97-
" tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
98-
")"
109+
"tools = [weather.run]\n",
110+
"agent = create_react_agent(\"openai:gpt-4.1-mini\", tools)"
99111
]
100112
},
101113
{
102114
"cell_type": "code",
103-
"execution_count": 12,
104-
"id": "bf4f6854",
115+
"execution_count": 5,
116+
"id": "9b423a92-1568-4ee2-9c7d-3b9acf7756a1",
105117
"metadata": {},
106118
"outputs": [
107119
{
108120
"name": "stdout",
109121
"output_type": "stream",
110122
"text": [
123+
"================================\u001b[1m Human Message \u001b[0m=================================\n",
111124
"\n",
125+
"What's the weather like in London?\n",
126+
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
127+
"Tool Calls:\n",
128+
" run (call_6vPq9neyy7oOnht29ExidE2g)\n",
129+
" Call ID: call_6vPq9neyy7oOnht29ExidE2g\n",
130+
" Args:\n",
131+
" location: London\n",
132+
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
133+
"Name: run\n",
112134
"\n",
113-
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
114-
"\u001b[32;1m\u001b[1;3m I need to find out the current weather in London.\n",
115-
"Action: OpenWeatherMap\n",
116-
"Action Input: London,GB\u001b[0m\n",
117-
"Observation: \u001b[36;1m\u001b[1;3mIn London,GB, the current weather is as follows:\n",
118-
"Detailed status: broken clouds\n",
119-
"Wind speed: 2.57 m/s, direction: 240°\n",
120-
"Humidity: 56%\n",
135+
"In London, the current weather is as follows:\n",
136+
"Detailed status: overcast clouds\n",
137+
"Wind speed: 4.12 m/s, direction: 10°\n",
138+
"Humidity: 51%\n",
121139
"Temperature: \n",
122-
" - Current: 20.11°C\n",
123-
" - High: 21.75°C\n",
124-
" - Low: 18.68°C\n",
125-
" - Feels like: 19.64°C\n",
140+
" - Current: 12.82°C\n",
141+
" - High: 13.98°C\n",
142+
" - Low: 12.01°C\n",
143+
" - Feels like: 11.49°C\n",
126144
"Rain: {}\n",
127145
"Heat index: None\n",
128-
"Cloud cover: 75%\u001b[0m\n",
129-
"Thought:\u001b[32;1m\u001b[1;3m I now know the current weather in London.\n",
130-
"Final Answer: The current weather in London is broken clouds, with a wind speed of 2.57 m/s, direction 240°, humidity of 56%, temperature of 20.11°C, high of 21.75°C, low of 18.68°C, and a heat index of None.\u001b[0m\n",
146+
"Cloud cover: 100%\n",
147+
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
131148
"\n",
132-
"\u001b[1m> Finished chain.\u001b[0m\n"
149+
"The weather in London is currently overcast with 100% cloud cover. The temperature is around 12.82°C, feeling like 11.49°C. The wind is blowing at 4.12 m/s from the direction of 10°. Humidity is at 51%. The high for the day is 13.98°C, and the low is 12.01°C.\n"
133150
]
134-
},
135-
{
136-
"data": {
137-
"text/plain": [
138-
"'The current weather in London is broken clouds, with a wind speed of 2.57 m/s, direction 240°, humidity of 56%, temperature of 20.11°C, high of 21.75°C, low of 18.68°C, and a heat index of None.'"
139-
]
140-
},
141-
"execution_count": 12,
142-
"metadata": {},
143-
"output_type": "execute_result"
144151
}
145152
],
146153
"source": [
147-
"agent_chain.run(\"What's the weather like in London?\")"
154+
"input_message = {\n",
155+
" \"role\": \"user\",\n",
156+
" \"content\": (\"What's the weather like in London?\"),\n",
157+
"}\n",
158+
"\n",
159+
"for step in agent.stream(\n",
160+
" {\"messages\": [input_message]},\n",
161+
" stream_mode=\"values\",\n",
162+
"):\n",
163+
" step[\"messages\"][-1].pretty_print()"
148164
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": null,
169+
"id": "f2af226a-9cca-468d-b07f-0a928ea61f48",
170+
"metadata": {},
171+
"outputs": [],
172+
"source": []
149173
}
150174
],
151175
"metadata": {
@@ -164,7 +188,7 @@
164188
"name": "python",
165189
"nbconvert_exporter": "python",
166190
"pygments_lexer": "ipython3",
167-
"version": "3.10.12"
191+
"version": "3.13.3"
168192
}
169193
},
170194
"nbformat": 4,

0 commit comments

Comments
 (0)