Skip to content

Commit 09a756c

Browse files
addressed comments
1 parent 4a8665a commit 09a756c

File tree

3 files changed

+68
-48
lines changed

3 files changed

+68
-48
lines changed

README.md

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -68,58 +68,72 @@ with CouchbaseSaver.from_conn_info(
6868
bucket_name=os.getenv("CB_BUCKET") or "test",
6969
scope_name=os.getenv("CB_SCOPE") or "langgraph",
7070
) as checkpointer:
71-
# Create the agent with checkpointing
72-
graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)
73-
74-
# Configure with a unique thread ID
75-
config = {"configurable": {"thread_id": "1"}}
76-
77-
# Run the agent
78-
res = graph.invoke({"messages": [("human", "what's the weather in sf")]}, config)
79-
80-
# Retrieve checkpoints
81-
latest_checkpoint = checkpointer.get(config)
82-
latest_checkpoint_tuple = checkpointer.get_tuple(config)
83-
checkpoint_tuples = list(checkpointer.list(config))
84-
85-
print(latest_checkpoint)
86-
print(latest_checkpoint_tuple)
87-
print(checkpoint_tuples)
71+
# Create the agent with checkpointing
72+
graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)
73+
74+
# Configure with a unique thread ID
75+
config = {"configurable": {"thread_id": "1"}}
76+
77+
# Run the agent
78+
res = graph.invoke({"messages": [("human", "what's the weather in sf")]}, config)
79+
80+
# Retrieve checkpoints
81+
latest_checkpoint = checkpointer.get(config)
82+
latest_checkpoint_tuple = checkpointer.get_tuple(config)
83+
checkpoint_tuples = list(checkpointer.list(config))
84+
85+
print(latest_checkpoint)
86+
print(latest_checkpoint_tuple)
87+
print(checkpoint_tuples)
8888
```
8989

9090
### Asynchronous Usage
9191

9292
```python
9393
import os
94+
from acouchbase.cluster import Cluster as ACluster
95+
from couchbase.auth import PasswordAuthenticator
96+
from couchbase.options import ClusterOptions
9497
from langgraph_checkpointer_couchbase import AsyncCouchbaseSaver
9598
from langgraph.graph import create_react_agent
9699

97-
async with AsyncCouchbaseSaver.from_conn_info(
98-
cb_conn_str=os.getenv("CB_CLUSTER") or "couchbase://localhost",
99-
cb_username=os.getenv("CB_USERNAME") or "Administrator",
100-
cb_password=os.getenv("CB_PASSWORD") or "password",
101-
bucket_name=os.getenv("CB_BUCKET") or "test",
102-
scope_name=os.getenv("CB_SCOPE") or "langgraph",
100+
auth = PasswordAuthenticator(
101+
os.getenv("CB_USERNAME") or "Administrator",
102+
os.getenv("CB_PASSWORD") or "password",
103+
)
104+
options = ClusterOptions(auth)
105+
cluster = await ACluster.connect(os.getenv("CB_CLUSTER") or "couchbase://localhost", options)
106+
107+
bucket_name = os.getenv("CB_BUCKET") or "test"
108+
scope_name = os.getenv("CB_SCOPE") or "langgraph"
109+
110+
async with AsyncCouchbaseSaver.from_cluster(
111+
cluster=cluster,
112+
bucket_name=bucket_name,
113+
scope_name=scope_name,
103114
) as checkpointer:
104-
# Create the agent with checkpointing
105-
graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)
106-
107-
# Configure with a unique thread ID
108-
config = {"configurable": {"thread_id": "2"}}
109-
110-
# Run the agent asynchronously
111-
res = await graph.ainvoke(
112-
{"messages": [("human", "what's the weather in nyc")]}, config
113-
)
114-
115-
# Retrieve checkpoints asynchronously
116-
latest_checkpoint = await checkpointer.aget(config)
117-
latest_checkpoint_tuple = await checkpointer.aget_tuple(config)
118-
checkpoint_tuples = [c async for c in checkpointer.alist(config)]
119-
120-
print(latest_checkpoint)
121-
print(latest_checkpoint_tuple)
122-
print(checkpoint_tuples)
115+
# Create the agent with checkpointing
116+
graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)
117+
118+
# Configure with a unique thread ID
119+
config = {"configurable": {"thread_id": "2"}}
120+
121+
# Run the agent asynchronously
122+
res = await graph.ainvoke(
123+
{"messages": [("human", "what's the weather in nyc")]}, config
124+
)
125+
126+
# Retrieve checkpoints asynchronously
127+
latest_checkpoint = await checkpointer.aget(config)
128+
latest_checkpoint_tuple = await checkpointer.aget_tuple(config)
129+
checkpoint_tuples = [c async for c in checkpointer.alist(config)]
130+
131+
print(latest_checkpoint)
132+
print(latest_checkpoint_tuple)
133+
print(checkpoint_tuples)
134+
135+
# Close the cluster when done
136+
await cluster.close()
123137
```
124138

125139
## Configuration Options

langgraph_checkpointer_couchbase/async_cb_saver.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import asynccontextmanager
22
from datetime import timedelta
33
from typing import Any, AsyncIterator, Dict, Optional, Sequence, Tuple
4+
import logging
45

56
from langchain_core.runnables import RunnableConfig
67
from acouchbase.cluster import Cluster as ACluster
@@ -19,6 +20,8 @@
1920
)
2021
from .utils import _encode_binary, _decode_binary
2122

23+
logger = logging.getLogger(__name__)
24+
2225
class AsyncCouchbaseSaver(BaseCheckpointSaver):
2326
"""A checkpoint saver that stores checkpoints in a Couchbase database."""
2427

@@ -50,7 +53,7 @@ async def create_collections(self):
5053
except CollectionAlreadyExistsException as _:
5154
pass
5255
except Exception as e:
53-
print(f"Error creating collections: {e}")
56+
logger.exception("Error creating collections")
5457
raise e
5558
finally:
5659
self.checkpoints_collection = self.bucket.scope(self.scope_name).collection(self.checkpoints_collection_name)
@@ -60,7 +63,7 @@ async def create_collections(self):
6063
except CollectionAlreadyExistsException as _:
6164
pass
6265
except Exception as e:
63-
print(f"Error creating collections: {e}")
66+
logger.exception("Error creating collections")
6467
raise e
6568
finally:
6669
self.checkpoint_writes_collection = self.bucket.scope(self.scope_name).collection(self.checkpoint_writes_collection_name)
@@ -195,7 +198,7 @@ async def aget_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
195198
async for write_doc in serialized_writes_result:
196199
checkpoint_writes = write_doc.get(self.checkpoint_writes_collection_name, {})
197200
if "task_id" not in checkpoint_writes:
198-
print("Error: 'task_id' is not present in checkpoint_writes")
201+
logger.warning("'task_id' is not present in checkpoint_writes")
199202
else:
200203
pending_writes.append(
201204
(

langgraph_checkpointer_couchbase/couchbase_saver.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import contextmanager
22
from datetime import timedelta
33
from typing import Any, Dict, Iterator, Optional, Sequence, Tuple
4+
import logging
45

56
from langchain_core.runnables import RunnableConfig
67
from couchbase.cluster import Cluster
@@ -19,6 +20,8 @@
1920
)
2021

2122
from .utils import _encode_binary, _decode_binary
23+
24+
logger = logging.getLogger(__name__)
2225
class CouchbaseSaver(BaseCheckpointSaver):
2326
"""A checkpoint saver that stores checkpoints in a Couchbase database.
2427
@@ -118,7 +121,7 @@ def create_collections(self):
118121
except CollectionAlreadyExistsException as _:
119122
pass
120123
except Exception as e:
121-
print(f"Error creating collections: {e}")
124+
logger.exception("Error creating collections")
122125
raise e
123126
finally:
124127
self.checkpoints_collection = self.bucket.scope(self.scope_name).collection(self.checkpoints_collection_name)
@@ -128,7 +131,7 @@ def create_collections(self):
128131
except CollectionAlreadyExistsException as _:
129132
pass
130133
except Exception as e:
131-
print(f"Error creating collections: {e}")
134+
logger.exception("Error creating collections")
132135
raise e
133136
finally:
134137
self.checkpoint_writes_collection = self.bucket.scope(self.scope_name).collection(self.checkpoint_writes_collection_name)
@@ -181,7 +184,7 @@ def get_tuple(self, config: RunnableConfig) -> Optional[CheckpointTuple]:
181184
for write_doc in serialized_writes_result:
182185
checkpoint_writes = write_doc.get(self.checkpoint_writes_collection_name, {})
183186
if "task_id" not in checkpoint_writes:
184-
print("Error: 'task_id' is not present in checkpoint_writes")
187+
logger.warning("'task_id' is not present in checkpoint_writes")
185188
else:
186189
# Decode and deserialize value data
187190
value_data = _decode_binary(checkpoint_writes["value"])

0 commit comments

Comments
 (0)