Skip to content

Commit e9da6db

Browse files
committed
Generated markdown tutorials from Jupyter Notebooks
Generated from: couchbase-examples/vector-search-cookbook
1 parent 23528de commit e9da6db

1 file changed

Lines changed: 39 additions & 23 deletions

File tree

tutorial/markdown/generated/vector-search-cookbook/langgraph-couchbase_persistence_langgraph.md

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,18 @@ This package provides a seamless way to persist LangGraph agent states in Couchb
5353

5454
## Setup environment
5555

56-
Requires Couchbase Python SDK and langgraph package
56+
Requires Couchbase Python SDK, LangGraph, and langchain-openai.
57+
58+
Set `OPENAI_API_KEY` before running. For local Couchbase runs, this notebook also reads `CB_CONN_STR`, `CB_USER`, `CB_PASS`, `CB_BUCKET_NAME`, and `CB_SCOPE_NAME` from the environment.
59+
60+
For the local example below, create bucket `test`, scope `langgraph`, and the `checkpoints` / `checkpoint_writes` collections first, or provide equivalent names through the environment variables. The default `CB_*` values shown later are for local development only.
61+
5762

5863

5964
```python
6065
%%capture --no-stderr
61-
%pip install -U langgraph==0.3.22 langgraph-checkpointer-couchbase
66+
%pip install -U langgraph==1.1.10 langgraph-checkpointer-couchbase langchain-openai
67+
6268
```
6369

6470
This particular example uses OpenAI's GPT 4.1-mini as the model
@@ -70,11 +76,22 @@ import os
7076

7177

7278
def _set_env(var: str):
73-
if not os.environ.get(var):
74-
os.environ[var] = getpass.getpass(f"{var}: ")
79+
if os.environ.get(var):
80+
return os.environ[var]
81+
value = getpass.getpass(f"{var}: ")
82+
if not value:
83+
raise RuntimeError(f"{var} must be set before running this notebook")
84+
os.environ[var] = value
85+
return value
7586

7687

7788
_set_env("OPENAI_API_KEY")
89+
CB_CONN_STR = os.environ.get("CB_CONN_STR", "couchbase://localhost")
90+
CB_USERNAME = os.environ.get("CB_USER", "Administrator")
91+
CB_PASSWORD = os.environ.get("CB_PASS", "password")
92+
CB_BUCKET_NAME = os.environ.get("CB_BUCKET_NAME", "test")
93+
CB_SCOPE_NAME = os.environ.get("CB_SCOPE_NAME", "langgraph")
94+
7895
```
7996

8097
## Setup model and tools for the graph
@@ -88,7 +105,7 @@ We are using a tool `get_weather` which gives the weather information based on t
88105
from typing import Literal
89106
from langchain_core.tools import tool
90107
from langchain_openai import ChatOpenAI
91-
from langgraph.prebuilt import create_react_agent
108+
from langchain.agents import create_agent
92109

93110

94111
@tool
@@ -106,7 +123,7 @@ tools = [get_weather]
106123
model = ChatOpenAI(model_name="gpt-4.1-mini", temperature=0)
107124
```
108125

109-
### Couchbase Connection and intialization
126+
### Couchbase Connection and initialization
110127

111128
There are 2 ways to initialize a saver.
112129

@@ -125,7 +142,7 @@ Below is usage of CouchbaseSaver (for synchronous use of graph, i.e. `.invoke()`
125142
- `.get_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).
126143
- `.list` - List checkpoints that match a given configuration and filter criteria.
127144

128-
Here we will create a Couchbase connection. We are using local setup with bucket `test`, `langgraph` scope. You may change bucket and scope if required. We will also require `checkpoints` and `checkpoint_writes` as collections inside.
145+
Here we will create a Couchbase connection. We are using local setup with bucket `test`, `langgraph` scope. You may change bucket and scope if required. We will also require `checkpoints` and `checkpoint_writes` as collections inside the configured scope.
129146

130147
Then a [ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) is created with GPT Model, weather tool and Couchbase checkpointer.
131148

@@ -136,19 +153,20 @@ LangGraph's graph is invoked with message for GPT, storing all the state in Couc
136153
from langgraph_checkpointer_couchbase import CouchbaseSaver
137154

138155
with CouchbaseSaver.from_conn_info(
139-
cb_conn_str="couchbase://localhost",
140-
cb_username="Administrator",
141-
cb_password="password",
142-
bucket_name="test",
143-
scope_name="langgraph",
156+
cb_conn_str=CB_CONN_STR,
157+
cb_username=CB_USERNAME,
158+
cb_password=CB_PASSWORD,
159+
bucket_name=CB_BUCKET_NAME,
160+
scope_name=CB_SCOPE_NAME,
144161
) as checkpointer:
145-
graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)
162+
graph = create_agent(model, tools=tools, checkpointer=checkpointer)
146163
config = {"configurable": {"thread_id": "1"}}
147164
res = graph.invoke({"messages": [("human", "what's the weather in sf")]}, config)
148165

149166
latest_checkpoint = checkpointer.get(config)
150167
latest_checkpoint_tuple = checkpointer.get_tuple(config)
151168
checkpoint_tuples = list(checkpointer.list(config))
169+
152170
```
153171

154172

@@ -208,7 +226,7 @@ checkpoint_tuples
208226

209227
## Use async connection (AsyncCouchbaseSaver)
210228

211-
This is the asynchronous example, Here we will create a Couchbase connection. We are using local setup with bucket `test`, `langgraph` scope. We will also require `checkpoints` and `checkpoint_writes` as collections inside. These are the methods supported by the library
229+
This is the asynchronous example, Here we will create a Couchbase connection. We are using local setup with bucket `test`, `langgraph` scope. We will also require `checkpoints` and `checkpoint_writes` as collections inside the configured scope. These are the methods supported by the library
212230

213231
- `.aput` - Store a checkpoint with its configuration and metadata.
214232
- `.aput_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).
@@ -226,13 +244,10 @@ from acouchbase.cluster import Cluster as ACluster
226244
from couchbase.auth import PasswordAuthenticator
227245
from couchbase.options import ClusterOptions
228246

229-
cb_conn_str = "couchbase://localhost"
230-
cb_username = "Administrator"
231-
cb_password = "password"
232-
233-
auth = PasswordAuthenticator(cb_username, cb_password)
247+
auth = PasswordAuthenticator(CB_USERNAME, CB_PASSWORD)
234248
options = ClusterOptions(auth)
235-
cb_cluster = await ACluster.connect(cb_conn_str, options)
249+
cb_cluster = await ACluster.connect(CB_CONN_STR, options)
250+
236251
```
237252

238253

@@ -241,10 +256,10 @@ from langgraph_checkpointer_couchbase import AsyncCouchbaseSaver
241256

242257
async with AsyncCouchbaseSaver.from_cluster(
243258
cluster=cb_cluster,
244-
bucket_name="test",
245-
scope_name="langgraph",
259+
bucket_name=CB_BUCKET_NAME,
260+
scope_name=CB_SCOPE_NAME,
246261
) as checkpointer:
247-
graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)
262+
graph = create_agent(model, tools=tools, checkpointer=checkpointer)
248263
config = {"configurable": {"thread_id": "2"}}
249264
res = await graph.ainvoke(
250265
{"messages": [("human", "what's the weather in nyc")]}, config
@@ -253,6 +268,7 @@ async with AsyncCouchbaseSaver.from_cluster(
253268
latest_checkpoint = await checkpointer.aget(config)
254269
latest_checkpoint_tuple = await checkpointer.aget_tuple(config)
255270
checkpoint_tuples = [c async for c in checkpointer.alist(config)]
271+
256272
```
257273

258274

0 commit comments

Comments
 (0)