Skip to content

Commit 97e9269

Browse files
authored
Merge pull request #180 from ks6088ts-labs/feature/issue-179_checkpointer
support memory feature to store states
2 parents 80366f6 + ea2d4af commit 97e9269

File tree

6 files changed

+82
-4
lines changed

6 files changed

+82
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,5 @@ generated/
168168
*.db
169169
*.wav
170170
mlartifacts
171+
*.sqlite
172+
*.bin

docs/references.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
- [Custom UI for Deep Agents](https://github.com/langchain-ai/deep-agents-ui)
1818
- [How to deploy self-hosted standalone server](https://docs.langchain.com/langgraph-platform/deploy-standalone-server)
1919
- [「現場で活用するための AI エージェント実践入門」リポジトリ](https://github.com/masamasa59/genai-agent-advanced-book)
20+
- [Add and manage memory](https://docs.langchain.com/oss/python/langgraph/add-memory)
21+
- [Persistence](https://langchain-ai.github.io/langgraph/concepts/persistence/)
22+
- [Chatbot with message summarization & external DB memory](https://github.com/langchain-ai/langchain-academy/blob/main/module-2/chatbot-external-memory.ipynb)
23+
- [LangGraph の会話履歴を SQLite に保持しよう](https://www.creationline.com/tech-blog/chatgpt-ai/75797)
2024

2125
### LangChain
2226

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies = [
2727
"langchain-text-splitters>=0.3.9",
2828
"langfuse>=3.6.2",
2929
"langgraph>=0.6.2",
30+
"langgraph-checkpoint-sqlite>=2.0.11",
3031
"langgraph-supervisor>=0.0.29",
3132
"mlflow>=3.4.0",
3233
"openai-whisper>=20250625",

template_langgraph/agents/chat_with_tools_agent/agent.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@ def __call__(self, inputs: dict):
5050

5151

5252
class ChatWithToolsAgent:
53-
def __init__(self, tools=get_default_tools()):
53+
def __init__(
54+
self,
55+
tools=get_default_tools(),
56+
checkpointer=None,
57+
store=None,
58+
):
5459
self.llm = AzureOpenAiWrapper().chat_model
5560
self.tools = tools
61+
self.checkpointer = checkpointer
62+
self.store = store
5663

5764
def create_graph(self):
5865
"""Create the main graph for the agent."""
@@ -83,6 +90,8 @@ def create_graph(self):
8390
# Compile the graph
8491
return workflow.compile(
8592
name=ChatWithToolsAgent.__name__,
93+
checkpointer=self.checkpointer,
94+
store=self.store,
8695
)
8796

8897
def chat_with_tools(self, state: AgentState) -> AgentState:

template_langgraph/services/streamlits/pages/chat_with_tools_agent.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import os
2+
import sqlite3
23
import tempfile
4+
import uuid
35
from base64 import b64encode
46
from dataclasses import dataclass
57

@@ -9,6 +11,8 @@
911
StreamlitCallbackHandler,
1012
)
1113
from langfuse.langchain import CallbackHandler
14+
from langgraph.checkpoint.sqlite import SqliteSaver
15+
from langgraph.store.sqlite import SqliteStore
1216

1317
from template_langgraph.agents.chat_with_tools_agent.agent import (
1418
AgentState,
@@ -18,6 +22,10 @@
1822
from template_langgraph.speeches.tts import TtsWrapper
1923
from template_langgraph.tools.common import get_default_tools
2024

25+
checkpoints_conn = sqlite3.connect("checkpoints.sqlite", check_same_thread=False)
26+
store_conn = sqlite3.connect("store.sqlite", check_same_thread=False)
27+
thread_id = str(uuid.uuid4())
28+
2129

2230
def image_to_base64(image_bytes: bytes) -> str:
2331
return b64encode(image_bytes).decode("utf-8")
@@ -68,7 +76,15 @@ def ensure_agent_graph(selected_tools: list) -> None:
6876
signature = tuple(tool.name for tool in selected_tools)
6977
graph_signature = st.session_state.get("graph_tools_signature")
7078
if "graph" not in st.session_state or graph_signature != signature:
71-
st.session_state["graph"] = ChatWithToolsAgent(tools=selected_tools).create_graph()
79+
st.session_state["graph"] = ChatWithToolsAgent(
80+
tools=selected_tools,
81+
checkpointer=SqliteSaver(
82+
conn=checkpoints_conn,
83+
),
84+
store=SqliteStore(
85+
conn=store_conn,
86+
),
87+
).create_graph()
7288
st.session_state["graph_tools_signature"] = signature
7389

7490

@@ -296,12 +312,18 @@ def build_graph_messages() -> list:
296312

297313
def invoke_agent(graph_messages: list) -> AgentState:
298314
return st.session_state["graph"].invoke(
299-
{"messages": graph_messages},
315+
{
316+
"messages": graph_messages,
317+
},
300318
{
301319
"callbacks": [
302320
StreamlitCallbackHandler(st.container()),
303321
CallbackHandler(),
304-
]
322+
],
323+
"configurable": {
324+
"thread_id": thread_id,
325+
"user_id": "user_1",
326+
},
305327
},
306328
)
307329

uv.lock

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)