|
| 1 | +--- |
| 2 | +type: docs |
| 3 | +title: "Agent Sessions" |
| 4 | +linkTitle: "Agent Sessions" |
| 5 | +weight: 30 |
| 6 | +description: "How to use Dapr reliably and securely manage LangGraph Agent Checkpointers" |
| 7 | +--- |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +The Dapr Python SDK provides integration with LangGraph Checkpointer using the `dapr-ext-langgraph` extension. |
| 12 | + |
| 13 | +## Getting Started |
| 14 | + |
| 15 | +Initialize Dapr locally to set up a self-hosted environment for development. This process fetches and installs the Dapr sidecar binaries, runs essential services as Docker containers, and prepares a default components folder for your application. For detailed steps, see the official [guide on initializing Dapr locally]({{% ref install-dapr-cli.md %}}). |
| 16 | + |
| 17 | +To initialize the Dapr control plane containers and create a default configuration file, run: |
| 18 | + |
| 19 | +```bash |
| 20 | +dapr init |
| 21 | +``` |
| 22 | + |
| 23 | +Verify you have container instances with `daprio/dapr`, `openzipkin/zipkin`, and `redis` images running: |
| 24 | + |
| 25 | +```bash |
| 26 | +docker ps |
| 27 | +``` |
| 28 | + |
| 29 | +### Install Python |
| 30 | + |
| 31 | +{{% alert title="Note" color="info" %}} |
| 32 | +Make sure you have Python already installed. `Python >=3.10`. For installation instructions, visit the official [Python installation guide](https://www.python.org/downloads/). |
| 33 | +{{% /alert %}} |
| 34 | + |
| 35 | +### Download Dependencies |
| 36 | + |
| 37 | +Download and install the Dapr LangGraph extension with: |
| 38 | + |
| 39 | +{{< tabpane text=true >}} |
| 40 | + |
| 41 | +{{% tab header="Stable" %}} |
| 42 | + |
| 43 | +```bash |
| 44 | +pip install dapr-ext-langgraph langchain_openai langchain_core langgraph langgraph-prebuilt |
| 45 | +``` |
| 46 | + |
| 47 | +{{% /tab %}} |
| 48 | + |
| 49 | +{{% tab header="Development" %}} |
| 50 | +{{% alert title="Note" color="warning" %}} |
| 51 | +The development package will contain features and behavior that will be compatible with the pre-release version of the Dapr runtime. Make sure to uninstall any stable versions of the Python SDK extension before installing the `dapr-dev` package. |
| 52 | +{{% /alert %}} |
| 53 | + |
| 54 | +```bash |
| 55 | +pip install dapr-ext-langgraph-dev langchain_openai langchain_core langgraph langgraph-prebuilt |
| 56 | +``` |
| 57 | + |
| 58 | +{{% /tab %}} |
| 59 | + |
| 60 | +{{< /tabpane >}} |
| 61 | + |
| 62 | +### Create a LangGraph Agent |
| 63 | + |
| 64 | +To let Dapr handle the agent memory, utilize the `DaprCheckpointer` as the checkpointer object when compiling the graph. Pass the checkpointer just like any other checkpointer provider: |
| 65 | + |
| 66 | +```python |
| 67 | +from dapr.ext.langgraph import DaprCheckpointer |
| 68 | +from langchain_openai import ChatOpenAI |
| 69 | +from langchain_core.messages import HumanMessage, SystemMessage |
| 70 | +from langgraph.graph import START, MessagesState, StateGraph |
| 71 | +from langgraph.prebuilt import ToolNode, tools_condition |
| 72 | + |
| 73 | + |
| 74 | +def add(a: int, b: int) -> int: |
| 75 | + """Adds a and b. |
| 76 | +
|
| 77 | + Args: |
| 78 | + a: first int |
| 79 | + b: second int |
| 80 | + """ |
| 81 | + return a + b |
| 82 | + |
| 83 | +tools = [add] |
| 84 | +llm = ChatOpenAI(model="gpt-4o") |
| 85 | +llm_with_tools = llm.bind_tools(tools) |
| 86 | + |
| 87 | +sys_msg = SystemMessage( |
| 88 | + content='You are a helpful assistant tasked with performing arithmetic on a set of inputs.' |
| 89 | +) |
| 90 | + |
| 91 | +def assistant(state: MessagesState): |
| 92 | + return {'messages': [llm_with_tools.invoke([sys_msg] + state['messages'])]} |
| 93 | + |
| 94 | +builder = StateGraph(MessagesState) |
| 95 | +builder.add_node('assistant', assistant) |
| 96 | +builder.add_node('tools', ToolNode(tools)) |
| 97 | +builder.add_edge(START, 'assistant') |
| 98 | +builder.add_conditional_edges( |
| 99 | + 'assistant', |
| 100 | + tools_condition, |
| 101 | +) |
| 102 | +builder.add_edge('tools', 'assistant') |
| 103 | + |
| 104 | +memory = DaprCheckpointer(store_name='statestore', key_prefix='dapr') |
| 105 | +react_graph_memory = builder.compile(checkpointer=memory) |
| 106 | + |
| 107 | +config = {'configurable': {'thread_id': '1'}} |
| 108 | + |
| 109 | +messages = [HumanMessage(content='Add 3 and 4.')] |
| 110 | +messages = react_graph_memory.invoke({'messages': messages}, config) |
| 111 | +for m in messages['messages']: |
| 112 | + m.pretty_print() |
| 113 | +``` |
| 114 | + |
| 115 | +### Set an OpenAI API key |
| 116 | + |
| 117 | +```bash |
| 118 | +export OPENAI_API_KEY=sk-... |
| 119 | +``` |
| 120 | + |
| 121 | +### Create a Python venv |
| 122 | + |
| 123 | +```bash |
| 124 | +python -m venv .venv |
| 125 | +source .venv/bin/activate # On Windows: .venv\Scripts\activate |
| 126 | +``` |
| 127 | + |
| 128 | +## Create the database component |
| 129 | + |
| 130 | +The component file is how Dapr connects to your databae. The full list of supported databases can be found [here]({{% ref supported-state-stores %}}). Create a `components` directory and this file in it: |
| 131 | + |
| 132 | +```yaml |
| 133 | +apiVersion: dapr.io/v1alpha1 |
| 134 | +kind: Component |
| 135 | +metadata: |
| 136 | + name: statestore |
| 137 | +spec: |
| 138 | + type: state.redis |
| 139 | + version: v1 |
| 140 | + metadata: |
| 141 | + - name: redisHost |
| 142 | + value: localhost:6379 |
| 143 | + - name: redisPassword |
| 144 | + value: "" |
| 145 | +``` |
| 146 | +
|
| 147 | +## Next Steps |
| 148 | +
|
| 149 | +Now that you have a LangGraph agent using Dapr to manage the agent sessions, explore more you can do with the [State API]({{% ref "state-management-overview" %}}) and how to enable [resiliency policies]({{% ref resiliency-overview %}}) for enhanced reliability. |
0 commit comments