|
58 | 58 | }, |
59 | 59 | { |
60 | 60 | "cell_type": "code", |
61 | | - "execution_count": 9, |
| 61 | + "execution_count": 1, |
62 | 62 | "id": "a213e11a-5c62-4ddb-a707-490d91add383", |
63 | 63 | "metadata": {}, |
64 | 64 | "outputs": [], |
|
69 | 69 | }, |
70 | 70 | { |
71 | 71 | "cell_type": "code", |
72 | | - "execution_count": 10, |
| 72 | + "execution_count": 2, |
73 | 73 | "id": "23a1885c-04ab-4750-aefa-105891fddf3e", |
74 | 74 | "metadata": {}, |
75 | | - "outputs": [], |
| 75 | + "outputs": [ |
| 76 | + { |
| 77 | + "name": "stdin", |
| 78 | + "output_type": "stream", |
| 79 | + "text": [ |
| 80 | + "OPENAI_API_KEY: ········\n" |
| 81 | + ] |
| 82 | + } |
| 83 | + ], |
76 | 84 | "source": [ |
77 | 85 | "import getpass\n", |
78 | 86 | "import os\n", |
|
109 | 117 | }, |
110 | 118 | { |
111 | 119 | "cell_type": "code", |
112 | | - "execution_count": 11, |
| 120 | + "execution_count": 3, |
113 | 121 | "id": "7a154152-973e-4b5d-aa13-48c617744a4c", |
114 | 122 | "metadata": {}, |
115 | | - "outputs": [], |
| 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 | + ], |
116 | 134 | "source": [ |
| 135 | + "from typing import Literal\n", |
| 136 | + "\n", |
| 137 | + "from langchain_core.tools import tool\n", |
| 138 | + "\n", |
117 | 139 | "# First we initialize the model we want to use.\n", |
118 | 140 | "from langchain_openai import ChatOpenAI\n", |
119 | 141 | "\n", |
| 142 | + "from langgraph.checkpoint.redis import RedisSaver\n", |
| 143 | + "from langgraph.prebuilt import create_react_agent\n", |
| 144 | + "\n", |
120 | 145 | "model = ChatOpenAI(model=\"gpt-4o\", temperature=0)\n", |
121 | 146 | "\n", |
122 | 147 | "\n", |
123 | 148 | "# For this tutorial we will use custom tool that returns pre-defined values for weather in two cities (NYC & SF)\n", |
124 | | - "\n", |
125 | | - "from typing import Literal\n", |
126 | | - "\n", |
127 | | - "from langchain_core.tools import tool\n", |
128 | | - "\n", |
129 | | - "\n", |
130 | 149 | "@tool\n", |
131 | 150 | "def get_weather(city: Literal[\"nyc\", \"sf\"]):\n", |
132 | 151 | " \"\"\"Use this to get weather information.\"\"\"\n", |
|
140 | 159 | "\n", |
141 | 160 | "tools = [get_weather]\n", |
142 | 161 | "\n", |
143 | | - "# We can add \"chat memory\" to the graph with LangGraph's Redi checkpointer\n", |
| 162 | + "# We can add \"chat memory\" to the graph with LangGraph's Redis checkpointer\n", |
144 | 163 | "# to retain the chat context between interactions\n", |
145 | | - "from langgraph.checkpoint.redis import RedisSaver\n", |
| 164 | + "\n", |
146 | 165 | "\n", |
147 | 166 | "REDIS_URI = \"redis://redis:6379\"\n", |
148 | 167 | "memory = None\n", |
149 | 168 | "with RedisSaver.from_conn_string(REDIS_URI) as cp:\n", |
150 | | - " cp.setup()\n", |
151 | | - " memory = cp\n", |
| 169 | + " cp.setup()\n", |
| 170 | + " memory = cp\n", |
152 | 171 | "\n", |
153 | 172 | "# Define the graph\n", |
154 | 173 | "\n", |
155 | | - "from langgraph.prebuilt import create_react_agent\n", |
156 | 174 | "\n", |
157 | 175 | "graph = create_react_agent(model, tools=tools, checkpointer=memory)" |
158 | 176 | ] |
|
169 | 187 | }, |
170 | 188 | { |
171 | 189 | "cell_type": "code", |
172 | | - "execution_count": 12, |
| 190 | + "execution_count": 4, |
173 | 191 | "id": "16636975-5f2d-4dc7-ab8e-d0bea0830a28", |
174 | 192 | "metadata": {}, |
175 | 193 | "outputs": [], |
|
185 | 203 | }, |
186 | 204 | { |
187 | 205 | "cell_type": "code", |
188 | | - "execution_count": 13, |
| 206 | + "execution_count": 5, |
189 | 207 | "id": "9ffff6c3-a4f5-47c9-b51d-97caaee85cd6", |
190 | 208 | "metadata": {}, |
191 | 209 | "outputs": [ |
|
198 | 216 | "What's the weather in NYC?\n", |
199 | 217 | "==================================\u001b[1m Ai Message \u001b[0m==================================\n", |
200 | 218 | "Tool Calls:\n", |
201 | | - " get_weather (call_RHv6T6OBCn7eKOlm5qEpLK4P)\n", |
202 | | - " Call ID: call_RHv6T6OBCn7eKOlm5qEpLK4P\n", |
| 219 | + " get_weather (call_Edwfw0WiyIJ7vt9xzU9xvyeg)\n", |
| 220 | + " Call ID: call_Edwfw0WiyIJ7vt9xzU9xvyeg\n", |
203 | 221 | " Args:\n", |
204 | 222 | " city: nyc\n", |
205 | 223 | "=================================\u001b[1m Tool Message \u001b[0m=================================\n", |
|
229 | 247 | }, |
230 | 248 | { |
231 | 249 | "cell_type": "code", |
232 | | - "execution_count": 14, |
| 250 | + "execution_count": 6, |
233 | 251 | "id": "187479f9-32fa-4611-9487-cf816ba2e147", |
234 | 252 | "metadata": {}, |
235 | 253 | "outputs": [ |
|
242 | 260 | "What's it known for?\n", |
243 | 261 | "==================================\u001b[1m Ai Message \u001b[0m==================================\n", |
244 | 262 | "\n", |
245 | | - "Could you please specify what \"it\" refers to? Are you asking about a specific place, person, event, or something else?\n" |
| 263 | + "Could you please specify what \"it\" refers to? Are you asking about a specific city, person, event, or something else?\n" |
246 | 264 | ] |
247 | 265 | } |
248 | 266 | ], |
|
274 | 292 | "nbformat": 4, |
275 | 293 | "nbformat_minor": 5 |
276 | 294 | } |
| 295 | + |
0 commit comments