|
22 | 22 | },
|
23 | 23 | {
|
24 | 24 | "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", |
27 | 45 | "metadata": {},
|
28 | 46 | "outputs": [],
|
29 | 47 | "source": [
|
|
38 | 56 | },
|
39 | 57 | {
|
40 | 58 | "cell_type": "code",
|
41 |
| - "execution_count": 10, |
42 |
| - "id": "ac4910f8", |
| 59 | + "execution_count": 3, |
| 60 | + "id": "0a8aa4b0-6aea-4172-9546-361e127a4a02", |
43 | 61 | "metadata": {},
|
44 | 62 | "outputs": [
|
45 | 63 | {
|
46 | 64 | "name": "stdout",
|
47 | 65 | "output_type": "stream",
|
48 | 66 | "text": [
|
49 | 67 | "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", |
53 | 71 | "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", |
58 | 76 | "Rain: {}\n",
|
59 | 77 | "Heat index: None\n",
|
60 |
| - "Cloud cover: 75%\n" |
| 78 | + "Cloud cover: 100%\n" |
61 | 79 | ]
|
62 | 80 | }
|
63 | 81 | ],
|
|
76 | 94 | },
|
77 | 95 | {
|
78 | 96 | "cell_type": "code",
|
79 |
| - "execution_count": 11, |
80 |
| - "id": "b3367417", |
| 97 | + "execution_count": 4, |
| 98 | + "id": "402c832c-87c7-4088-b80f-ec1924a43796", |
81 | 99 | "metadata": {},
|
82 | 100 | "outputs": [],
|
83 | 101 | "source": [
|
84 | 102 | "import os\n",
|
85 | 103 | "\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", |
88 | 105 | "\n",
|
89 | 106 | "os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
|
90 | 107 | "os.environ[\"OPENWEATHERMAP_API_KEY\"] = \"\"\n",
|
91 | 108 | "\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)" |
99 | 111 | ]
|
100 | 112 | },
|
101 | 113 | {
|
102 | 114 | "cell_type": "code",
|
103 |
| - "execution_count": 12, |
104 |
| - "id": "bf4f6854", |
| 115 | + "execution_count": 5, |
| 116 | + "id": "9b423a92-1568-4ee2-9c7d-3b9acf7756a1", |
105 | 117 | "metadata": {},
|
106 | 118 | "outputs": [
|
107 | 119 | {
|
108 | 120 | "name": "stdout",
|
109 | 121 | "output_type": "stream",
|
110 | 122 | "text": [
|
| 123 | + "================================\u001b[1m Human Message \u001b[0m=================================\n", |
111 | 124 | "\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", |
112 | 134 | "\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", |
121 | 139 | "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", |
126 | 144 | "Rain: {}\n",
|
127 | 145 | "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", |
131 | 148 | "\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" |
133 | 150 | ]
|
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" |
144 | 151 | }
|
145 | 152 | ],
|
146 | 153 | "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()" |
148 | 164 | ]
|
| 165 | + }, |
| 166 | + { |
| 167 | + "cell_type": "code", |
| 168 | + "execution_count": null, |
| 169 | + "id": "f2af226a-9cca-468d-b07f-0a928ea61f48", |
| 170 | + "metadata": {}, |
| 171 | + "outputs": [], |
| 172 | + "source": [] |
149 | 173 | }
|
150 | 174 | ],
|
151 | 175 | "metadata": {
|
|
164 | 188 | "name": "python",
|
165 | 189 | "nbconvert_exporter": "python",
|
166 | 190 | "pygments_lexer": "ipython3",
|
167 |
| - "version": "3.10.12" |
| 191 | + "version": "3.13.3" |
168 | 192 | }
|
169 | 193 | },
|
170 | 194 | "nbformat": 4,
|
|
0 commit comments