OnurKerimoglu commited on
Commit
0282f48
·
1 Parent(s): 11d3f6f

chatbot_agentic: introduced memory

Browse files
Files changed (1) hide show
  1. notebooks/chatbot_agentic.ipynb +16 -5
notebooks/chatbot_agentic.ipynb CHANGED
@@ -19,6 +19,7 @@
19
  "import json\n",
20
  "from typing import Annotated, List\n",
21
  "from typing_extensions import TypedDict\n",
 
22
  "from langchain_core.messages import ToolMessage\n",
23
  "from langgraph.graph import StateGraph, START, END\n",
24
  "from langgraph.graph.message import add_messages\n",
@@ -187,7 +188,8 @@
187
  "# Any time a tool is called, we return to the chatbot to decide the next step\n",
188
  "graph_builder.add_edge(\"tools\", \"chatbot\")\n",
189
  "\n",
190
- "graph = graph_builder.compile()"
 
191
  ]
192
  },
193
  {
@@ -211,10 +213,18 @@
211
  "metadata": {},
212
  "outputs": [],
213
  "source": [
 
 
214
  "def stream_graph_updates(user_input: str):\n",
215
- " for event in graph.stream({\"messages\": [(\"user\", user_input)]}):\n",
216
- " for value in event.values():\n",
217
- " print(\"Assistant:\", value[\"messages\"][-1].content)\n",
 
 
 
 
 
 
218
  "\n",
219
  "\n",
220
  "while True:\n",
@@ -223,7 +233,8 @@
223
  " if user_input.lower() in [\"quit\", \"exit\", \"q\"]:\n",
224
  " print(\"Goodbye!\")\n",
225
  " break\n",
226
- "\n",
 
227
  " stream_graph_updates(user_input)\n",
228
  " except Exception as e:\n",
229
  " # fallback if input() is not available\n",
 
19
  "import json\n",
20
  "from typing import Annotated, List\n",
21
  "from typing_extensions import TypedDict\n",
22
+ "from langgraph.checkpoint.memory import MemorySaver\n",
23
  "from langchain_core.messages import ToolMessage\n",
24
  "from langgraph.graph import StateGraph, START, END\n",
25
  "from langgraph.graph.message import add_messages\n",
 
188
  "# Any time a tool is called, we return to the chatbot to decide the next step\n",
189
  "graph_builder.add_edge(\"tools\", \"chatbot\")\n",
190
  "\n",
191
+ "memory = MemorySaver()\n",
192
+ "graph = graph_builder.compile(checkpointer=memory)"
193
  ]
194
  },
195
  {
 
213
  "metadata": {},
214
  "outputs": [],
215
  "source": [
216
+ "config = {\"configurable\": {\"thread_id\": \"1\"}}\n",
217
+ "\n",
218
  "def stream_graph_updates(user_input: str):\n",
219
+ " events= graph.stream(\n",
220
+ " {\"messages\": [(\"user\", user_input)]},\n",
221
+ " config,\n",
222
+ " stream_mode=\"values\"\n",
223
+ " )\n",
224
+ " for event in events:\n",
225
+ " event[\"messages\"][-1].pretty_print()\n",
226
+ " #for value in event.values():\n",
227
+ " # print(\"Assistant:\", value[\"messages\"][-1].content)\n",
228
  "\n",
229
  "\n",
230
  "while True:\n",
 
233
  " if user_input.lower() in [\"quit\", \"exit\", \"q\"]:\n",
234
  " print(\"Goodbye!\")\n",
235
  " break\n",
236
+ " snapshot = graph.get_state(config)\n",
237
+ " print(f'Current state: {snapshot}')\n",
238
  " stream_graph_updates(user_input)\n",
239
  " except Exception as e:\n",
240
  " # fallback if input() is not available\n",