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>
This commit is contained in:
Michael Li 2025-05-06 08:08:09 +10:00 committed by GitHub
parent fce8caca16
commit c0b69808a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -22,8 +22,26 @@
},
{
"cell_type": "code",
"execution_count": 9,
"id": "34bb5968",
"execution_count": 1,
"id": "8b81a74e-db10-4e8d-9f90-83219df30ab3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install --upgrade --quiet pyowm"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "78ab9fcd-bb7b-434b-9a38-0a9249e35768",
"metadata": {},
"outputs": [],
"source": [
@ -38,8 +56,8 @@
},
{
"cell_type": "code",
"execution_count": 10,
"id": "ac4910f8",
"execution_count": 3,
"id": "0a8aa4b0-6aea-4172-9546-361e127a4a02",
"metadata": {},
"outputs": [
{
@ -47,17 +65,17 @@
"output_type": "stream",
"text": [
"In London,GB, the current weather is as follows:\n",
"Detailed status: broken clouds\n",
"Wind speed: 2.57 m/s, direction: 240°\n",
"Humidity: 55%\n",
"Detailed status: overcast clouds\n",
"Wind speed: 4.12 m/s, direction: 10°\n",
"Humidity: 51%\n",
"Temperature: \n",
" - Current: 20.12°C\n",
" - High: 21.75°C\n",
" - Low: 18.68°C\n",
" - Feels like: 19.62°C\n",
" - Current: 12.82°C\n",
" - High: 13.98°C\n",
" - Low: 12.01°C\n",
" - Feels like: 11.49°C\n",
"Rain: {}\n",
"Heat index: None\n",
"Cloud cover: 75%\n"
"Cloud cover: 100%\n"
]
}
],
@ -76,76 +94,82 @@
},
{
"cell_type": "code",
"execution_count": 11,
"id": "b3367417",
"execution_count": 4,
"id": "402c832c-87c7-4088-b80f-ec1924a43796",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"from langchain.agents import AgentType, initialize_agent, load_tools\n",
"from langchain_openai import OpenAI\n",
"from langgraph.prebuilt import create_react_agent\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
"os.environ[\"OPENWEATHERMAP_API_KEY\"] = \"\"\n",
"\n",
"llm = OpenAI(temperature=0)\n",
"\n",
"tools = load_tools([\"openweathermap-api\"], llm)\n",
"\n",
"agent_chain = initialize_agent(\n",
" tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True\n",
")"
"tools = [weather.run]\n",
"agent = create_react_agent(\"openai:gpt-4.1-mini\", tools)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "bf4f6854",
"execution_count": 5,
"id": "9b423a92-1568-4ee2-9c7d-3b9acf7756a1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================\u001b[1m Human Message \u001b[0m=================================\n",
"\n",
"What's the weather like in London?\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"Tool Calls:\n",
" run (call_6vPq9neyy7oOnht29ExidE2g)\n",
" Call ID: call_6vPq9neyy7oOnht29ExidE2g\n",
" Args:\n",
" location: London\n",
"=================================\u001b[1m Tool Message \u001b[0m=================================\n",
"Name: run\n",
"\n",
"\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
"\u001b[32;1m\u001b[1;3m I need to find out the current weather in London.\n",
"Action: OpenWeatherMap\n",
"Action Input: London,GB\u001b[0m\n",
"Observation: \u001b[36;1m\u001b[1;3mIn London,GB, the current weather is as follows:\n",
"Detailed status: broken clouds\n",
"Wind speed: 2.57 m/s, direction: 240°\n",
"Humidity: 56%\n",
"In London, the current weather is as follows:\n",
"Detailed status: overcast clouds\n",
"Wind speed: 4.12 m/s, direction: 10°\n",
"Humidity: 51%\n",
"Temperature: \n",
" - Current: 20.11°C\n",
" - High: 21.75°C\n",
" - Low: 18.68°C\n",
" - Feels like: 19.64°C\n",
" - Current: 12.82°C\n",
" - High: 13.98°C\n",
" - Low: 12.01°C\n",
" - Feels like: 11.49°C\n",
"Rain: {}\n",
"Heat index: None\n",
"Cloud cover: 75%\u001b[0m\n",
"Thought:\u001b[32;1m\u001b[1;3m I now know the current weather in London.\n",
"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",
"Cloud cover: 100%\n",
"==================================\u001b[1m Ai Message \u001b[0m==================================\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
"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"
]
},
{
"data": {
"text/plain": [
"'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.'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"agent_chain.run(\"What's the weather like in London?\")"
"input_message = {\n",
" \"role\": \"user\",\n",
" \"content\": (\"What's the weather like in London?\"),\n",
"}\n",
"\n",
"for step in agent.stream(\n",
" {\"messages\": [input_message]},\n",
" stream_mode=\"values\",\n",
"):\n",
" step[\"messages\"][-1].pretty_print()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2af226a-9cca-468d-b07f-0a928ea61f48",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@ -164,7 +188,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
"version": "3.13.3"
}
},
"nbformat": 4,