Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/langgraph-checkpointer/.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY="sk-proj-very-secret-key"
50 changes: 50 additions & 0 deletions examples/langgraph-checkpointer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Dapr For Agents - LangGraph Checkpointer

Supporting Dapr backed Checkpointer for LangGraph based Agents.

1. Initialize Dapr

```shell
dapr init
```

2. Install dependencies either with
```shell
uv sync
```
or
```shell
pip install -r requirements.txt
```

3. Ensure to correct the template file for `.env` with your OpenAI API Key

4. Run the agent
```shell
dapr run -f dapr.yaml
```

This will provision the redis & SQLite state stores (which we use in this example, see `./components/*-memory.yaml`) to store the state, the Checkpointer, for the LangGraph agent to have short term memory. See [State Management](https://docs.dapr.io/developing-applications/building-blocks/state-management/) for more details on State Managenent and [LangGraph - Memory](https://docs.langchain.com/oss/python/langgraph/add-memory) for details on LangGraph Agent Memory.

This example is based on the LangGraph Academy Demo repository for [agent-memory in module-1](https://github.com/langchain-ai/langchain-academy/blob/b20cf608f2bf165e09080961537d329f813cfb20/module-1/agent-memory.ipynb).

## Inspect

After running you'll find `./data.db`.

1. Connect
```shell
sqlite3 data.db
```

2. List tables
```shell
.tables
```

3. Inspect state table
```shell
select * from state;
```

In this example we ran 2 threads that each had their own seperate memory checkpointer and by using dapr it was completely transparent that one was using Redis and the other SQLite DB.
10 changes: 10 additions & 0 deletions examples/langgraph-checkpointer/components/redis-memory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: dapr-redis
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
10 changes: 10 additions & 0 deletions examples/langgraph-checkpointer/components/sqlite-memory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: dapr-sqlite
spec:
type: state.sqlite
version: v1
metadata:
- name: connectionString
value: "data.db"
11 changes: 11 additions & 0 deletions examples/langgraph-checkpointer/dapr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: 1
common:
resourcesPath: ./components
logLevel: info
appLogDestination: console
daprdLogDestination: console

apps:
- appID: langgraph_app
appDirPath: .
command: ["python", "main.py"]
86 changes: 86 additions & 0 deletions examples/langgraph-checkpointer/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import os

from dapr.ext.langgraph import DaprCheckpointer
from dotenv import load_dotenv
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
from langgraph.graph import START, MessagesState, StateGraph
from langgraph.prebuilt import ToolNode, tools_condition

load_dotenv()


def add(a: int, b: int) -> int:
"""Adds a and b.

Args:
a: first int
b: second int
"""
return a + b


def multiply(a: int, b: int) -> int:
"""Multiply a and b.

Args:
a: first int
b: second int
"""
return a * b


tools = [add, multiply]
llm = ChatOpenAI(model='gpt-4o', api_key=os.environ['OPENAI_API_KEY'])
llm_with_tools = llm.bind_tools(tools)

sys_msg = SystemMessage(
content='You are a helpful assistant tasked with performing arithmetic on a set of inputs.'
)


def assistant(state: MessagesState):
return {'messages': [llm_with_tools.invoke([sys_msg] + state['messages'])]}


builder = StateGraph(MessagesState)

builder.add_node('assistant', assistant)
builder.add_node('tools', ToolNode(tools))

builder.add_edge(START, 'assistant')
builder.add_conditional_edges(
'assistant',
tools_condition,
)
builder.add_edge('tools', 'assistant')

memory = DaprCheckpointer(store_name='dapr-redis', key_prefix='dapr')
react_graph_memory = builder.compile(checkpointer=memory)

config = {'configurable': {'thread_id': '1'}}

messages = [HumanMessage(content='Add 3 and 4.')]
messages = react_graph_memory.invoke({'messages': messages}, config)
for m in messages['messages']:
m.pretty_print()

messages = [HumanMessage(content='Multiply that by 2.')]
messages = react_graph_memory.invoke({'messages': messages}, config)
for m in messages['messages']:
m.pretty_print()

memory = DaprCheckpointer(store_name='dapr-sqlite', key_prefix='dapr')
react_graph_memory = builder.compile(checkpointer=memory)

config = {'configurable': {'thread_id': '2'}}

messages = [HumanMessage(content='Add 5 and 6.')]
messages = react_graph_memory.invoke({'messages': messages}, config)
for m in messages['messages']:
m.pretty_print()

messages = [HumanMessage(content='Multiply that by 3.')]
messages = react_graph_memory.invoke({'messages': messages}, config)
for m in messages['messages']:
m.pretty_print()
13 changes: 13 additions & 0 deletions examples/langgraph-checkpointer/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[project]
name = "langgraph-dapr"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.14"
dependencies = [
"dotenv>=0.9.9",
"langchain-core>=1.0.7",
"langchain-openai>=1.0.3",
"langgraph>=1.0.3",
"dapr-ext-langgraph>=1.16.0",
]
5 changes: 5 additions & 0 deletions examples/langgraph-checkpointer/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dotenv>=0.9.9
langchain-core>=1.0.7
langchain-openai>=1.0.3
langgraph>=1.0.3
dapr-ext-langgraph>=1.16.0
Loading