|
120 | 120 | "execution_count": 3,
|
121 | 121 | "id": "7a154152-973e-4b5d-aa13-48c617744a4c",
|
122 | 122 | "metadata": {},
|
123 |
| - "outputs": [ |
124 |
| - { |
125 |
| - "name": "stdout", |
126 |
| - "output_type": "stream", |
127 |
| - "text": [ |
128 |
| - "22:31:51 redisvl.index.index INFO Index already exists, not overwriting.\n", |
129 |
| - "22:31:51 redisvl.index.index INFO Index already exists, not overwriting.\n", |
130 |
| - "22:31:51 redisvl.index.index INFO Index already exists, not overwriting.\n" |
131 |
| - ] |
132 |
| - } |
133 |
| - ], |
| 123 | + "outputs": [], |
134 | 124 | "source": [
|
135 |
| - "from typing import Literal\n", |
136 |
| - "\n", |
137 |
| - "from langchain_core.tools import tool\n", |
138 |
| - "\n", |
139 | 125 | "# First we initialize the model we want to use.\n",
|
140 | 126 | "from langchain_openai import ChatOpenAI\n",
|
141 | 127 | "\n",
|
142 |
| - "from langgraph.checkpoint.redis import RedisSaver\n", |
143 |
| - "from langgraph.prebuilt import create_react_agent\n", |
144 |
| - "\n", |
145 | 128 | "model = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n",
|
146 | 129 | "\n",
|
147 | 130 | "\n",
|
148 | 131 | "# For this tutorial we will use custom tool that returns pre-defined values for weather in two cities (NYC & SF)\n",
|
| 132 | + "\n", |
| 133 | + "from langchain_core.tools import tool\n", |
| 134 | + "\n", |
| 135 | + "\n", |
149 | 136 | "@tool\n",
|
150 |
| - "def get_weather(city: Literal[\"nyc\", \"sf\"]):\n", |
| 137 | + "def get_weather(location: str) -> str:\n", |
151 | 138 | " \"\"\"Use this to get weather information.\"\"\"\n",
|
152 |
| - " if city == \"nyc\":\n", |
| 139 | + " if any([city in location.lower() for city in [\"nyc\", \"new york city\"]]):\n", |
153 | 140 | " return \"It might be cloudy in nyc\"\n",
|
154 |
| - " elif city == \"sf\":\n", |
| 141 | + " elif any([city in location.lower() for city in [\"sf\", \"san francisco\"]]):\n", |
155 | 142 | " return \"It's always sunny in sf\"\n",
|
156 | 143 | " else:\n",
|
157 |
| - " raise AssertionError(\"Unknown city\")\n", |
| 144 | + " return f\"I am not sure what the weather is in {location}\"\n", |
158 | 145 | "\n",
|
159 | 146 | "\n",
|
160 | 147 | "tools = [get_weather]\n",
|
161 | 148 | "\n",
|
162 |
| - "# We can add \"chat memory\" to the graph with LangGraph's Redis checkpointer\n", |
| 149 | + "# We can add \"chat memory\" to the graph with LangGraph's checkpointer\n", |
163 | 150 | "# to retain the chat context between interactions\n",
|
| 151 | + "from langgraph.checkpoint.memory import MemorySaver\n", |
164 | 152 | "\n",
|
165 |
| - "\n", |
166 |
| - "REDIS_URI = \"redis://redis:6379\"\n", |
167 |
| - "memory = None\n", |
168 |
| - "with RedisSaver.from_conn_string(REDIS_URI) as cp:\n", |
169 |
| - " cp.setup()\n", |
170 |
| - " memory = cp\n", |
| 153 | + "memory = MemorySaver()\n", |
171 | 154 | "\n",
|
172 | 155 | "# Define the graph\n",
|
173 | 156 | "\n",
|
| 157 | + "from langgraph.prebuilt import create_react_agent\n", |
174 | 158 | "\n",
|
175 | 159 | "graph = create_react_agent(model, tools=tools, checkpointer=memory)"
|
176 | 160 | ]
|
|
216 | 200 | "What's the weather in NYC?\n",
|
217 | 201 | "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
218 | 202 | "Tool Calls:\n",
|
219 |
| - " get_weather (call_Edwfw0WiyIJ7vt9xzU9xvyeg)\n", |
220 |
| - " Call ID: call_Edwfw0WiyIJ7vt9xzU9xvyeg\n", |
| 203 | + " get_weather (call_LDM16pwsyYeZPQ78UlZCMs7n)\n", |
| 204 | + " Call ID: call_LDM16pwsyYeZPQ78UlZCMs7n\n", |
221 | 205 | " Args:\n",
|
222 |
| - " city: nyc\n", |
| 206 | + " location: New York City\n", |
223 | 207 | "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
|
224 | 208 | "Name: get_weather\n",
|
225 | 209 | "\n",
|
226 | 210 | "It might be cloudy in nyc\n",
|
227 | 211 | "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
228 | 212 | "\n",
|
229 |
| - "The weather in NYC might be cloudy.\n" |
| 213 | + "The weather in New York City might be cloudy.\n" |
230 | 214 | ]
|
231 | 215 | }
|
232 | 216 | ],
|
|
242 | 226 | "id": "838a043f-90ad-4e69-9d1d-6e22db2c346c",
|
243 | 227 | "metadata": {},
|
244 | 228 | "source": [
|
245 |
| - "Notice that when we pass the same the same thread ID, the chat history is preserved" |
| 229 | + "Notice that when we pass the same thread ID, the chat history is preserved." |
246 | 230 | ]
|
247 | 231 | },
|
248 | 232 | {
|
|
260 | 244 | "What's it known for?\n",
|
261 | 245 | "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
|
262 | 246 | "\n",
|
263 |
| - "Could you please specify what \"it\" refers to? Are you asking about a specific city, person, event, or something else?\n" |
| 247 | + "New York City is known for a variety of iconic landmarks, cultural institutions, and vibrant neighborhoods. Some of the most notable features include:\n", |
| 248 | + "\n", |
| 249 | + "1. **Statue of Liberty**: A symbol of freedom and democracy, located on Liberty Island.\n", |
| 250 | + "2. **Times Square**: Known for its bright lights, Broadway theaters, and bustling atmosphere.\n", |
| 251 | + "3. **Central Park**: A large public park offering a natural oasis amidst the urban environment.\n", |
| 252 | + "4. **Empire State Building**: An iconic skyscraper offering panoramic views of the city.\n", |
| 253 | + "5. **Broadway**: Famous for its world-class theater productions and musicals.\n", |
| 254 | + "6. **Wall Street**: The financial hub of the city, home to the New York Stock Exchange.\n", |
| 255 | + "7. **Museums**: Including the Metropolitan Museum of Art, Museum of Modern Art (MoMA), and the American Museum of Natural History.\n", |
| 256 | + "8. **Diverse Cuisine**: A melting pot of cultures, offering a wide range of international foods.\n", |
| 257 | + "9. **Brooklyn Bridge**: A historic bridge connecting Manhattan and Brooklyn, known for its architectural beauty.\n", |
| 258 | + "10. **Cultural Diversity**: A rich tapestry of cultures and communities, making it a global city.\n", |
| 259 | + "\n", |
| 260 | + "These are just a few highlights of what makes New York City a unique and exciting place to visit or live.\n" |
264 | 261 | ]
|
265 | 262 | }
|
266 | 263 | ],
|
267 | 264 | "source": [
|
268 | 265 | "inputs = {\"messages\": [(\"user\", \"What's it known for?\")]}\n",
|
269 | 266 | "print_stream(graph.stream(inputs, config=config, stream_mode=\"values\"))"
|
270 | 267 | ]
|
| 268 | + }, |
| 269 | + { |
| 270 | + "cell_type": "code", |
| 271 | + "execution_count": null, |
| 272 | + "id": "c461eb47-b4f9-406f-8923-c68db7c5687f", |
| 273 | + "metadata": {}, |
| 274 | + "outputs": [], |
| 275 | + "source": [] |
271 | 276 | }
|
272 | 277 | ],
|
273 | 278 | "metadata": {
|
|
292 | 297 | "nbformat": 4,
|
293 | 298 | "nbformat_minor": 5
|
294 | 299 | }
|
295 |
| - |
|
0 commit comments