|
57 | 57 | "cell_type": "markdown", |
58 | 58 | "metadata": {}, |
59 | 59 | "source": [ |
60 | | - "Requires Couchbase Python SDK and langgraph package" |
| 60 | + "Requires Couchbase Python SDK, LangGraph, and langchain-openai.\n", |
| 61 | + "\n", |
| 62 | + "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.\n", |
| 63 | + "\n", |
| 64 | + "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.\n" |
61 | 65 | ] |
62 | 66 | }, |
63 | 67 | { |
|
67 | 71 | "outputs": [], |
68 | 72 | "source": [ |
69 | 73 | "%%capture --no-stderr\n", |
70 | | - "%pip install -U langgraph==0.3.22 langgraph-checkpointer-couchbase " |
| 74 | + "%pip install -U langgraph==1.1.10 langgraph-checkpointer-couchbase langchain-openai\n" |
71 | 75 | ] |
72 | 76 | }, |
73 | 77 | { |
|
88 | 92 | "\n", |
89 | 93 | "\n", |
90 | 94 | "def _set_env(var: str):\n", |
91 | | - " if not os.environ.get(var):\n", |
92 | | - " os.environ[var] = getpass.getpass(f\"{var}: \")\n", |
| 95 | + " if os.environ.get(var):\n", |
| 96 | + " return os.environ[var]\n", |
| 97 | + " value = getpass.getpass(f\"{var}: \")\n", |
| 98 | + " if not value:\n", |
| 99 | + " raise RuntimeError(f\"{var} must be set before running this notebook\")\n", |
| 100 | + " os.environ[var] = value\n", |
| 101 | + " return value\n", |
93 | 102 | "\n", |
94 | 103 | "\n", |
95 | | - "_set_env(\"OPENAI_API_KEY\")" |
| 104 | + "_set_env(\"OPENAI_API_KEY\")\n", |
| 105 | + "CB_CONN_STR = os.environ.get(\"CB_CONN_STR\", \"couchbase://localhost\")\n", |
| 106 | + "CB_USERNAME = os.environ.get(\"CB_USER\", \"Administrator\")\n", |
| 107 | + "CB_PASSWORD = os.environ.get(\"CB_PASS\", \"password\")\n", |
| 108 | + "CB_BUCKET_NAME = os.environ.get(\"CB_BUCKET_NAME\", \"test\")\n", |
| 109 | + "CB_SCOPE_NAME = os.environ.get(\"CB_SCOPE_NAME\", \"langgraph\")\n" |
96 | 110 | ] |
97 | 111 | }, |
98 | 112 | { |
|
120 | 134 | "from typing import Literal\n", |
121 | 135 | "from langchain_core.tools import tool\n", |
122 | 136 | "from langchain_openai import ChatOpenAI\n", |
123 | | - "from langgraph.prebuilt import create_react_agent\n", |
| 137 | + "from langchain.agents import create_agent\n", |
124 | 138 | "\n", |
125 | 139 | "\n", |
126 | 140 | "@tool\n", |
|
142 | 156 | "cell_type": "markdown", |
143 | 157 | "metadata": {}, |
144 | 158 | "source": [ |
145 | | - "### Couchbase Connection and intialization\n", |
146 | | - "\n", |
147 | | - "There are 2 ways to initialize a saver.\n", |
148 | | - "\n", |
149 | | - "1. `from_conn_info` - Provide details of the connection string, username, password. The package will handle connection itself.\n", |
150 | | - "2. `from_cluster` - Provide a connected Couchbase.Cluster object. \n", |
151 | | - "\n", |
152 | | - "We will be using `from_conn_info` in the sync tutorial and `from_cluster` in the async one, but any of the above can be used as per requirements\n" |
| 159 | + "### Couchbase Connection and initialization\n\nThere are 2 ways to initialize a saver.\n\n1. `from_conn_info` - Provide details of the connection string, username, password. The package will handle connection itself.\n2. `from_cluster` - Provide a connected Couchbase.Cluster object. \n\nWe will be using `from_conn_info` in the sync tutorial and `from_cluster` in the async one, but any of the above can be used as per requirements\n" |
153 | 160 | ] |
154 | 161 | }, |
155 | 162 | { |
|
163 | 170 | "cell_type": "markdown", |
164 | 171 | "metadata": {}, |
165 | 172 | "source": [ |
166 | | - "Below is usage of CouchbaseSaver (for synchronous use of graph, i.e. `.invoke()`, `.stream()`). CouchbaseSaver implements four methods that are required for any checkpointer:\n", |
167 | | - "\n", |
168 | | - "- `.put` - Store a checkpoint with its configuration and metadata.\n", |
169 | | - "- `.put_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).\n", |
170 | | - "- `.get_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).\n", |
171 | | - "- `.list` - List checkpoints that match a given configuration and filter criteria.\n", |
172 | | - "\n", |
173 | | - "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.\n", |
174 | | - "\n", |
175 | | - "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.\n", |
176 | | - "\n", |
177 | | - "LangGraph's graph is invoked with message for GPT, storing all the state in Couchbase. We use get, get_tuple and list methods to fetch the states again" |
| 173 | + "Below is usage of CouchbaseSaver (for synchronous use of graph, i.e. `.invoke()`, `.stream()`). CouchbaseSaver implements four methods that are required for any checkpointer:\n\n- `.put` - Store a checkpoint with its configuration and metadata.\n- `.put_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).\n- `.get_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).\n- `.list` - List checkpoints that match a given configuration and filter criteria.\n\nHere 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.\n\nThen a [ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) is created with GPT Model, weather tool and Couchbase checkpointer.\n\nLangGraph's graph is invoked with message for GPT, storing all the state in Couchbase. We use get, get_tuple and list methods to fetch the states again" |
178 | 174 | ] |
179 | 175 | }, |
180 | 176 | { |
|
186 | 182 | "from langgraph_checkpointer_couchbase import CouchbaseSaver\n", |
187 | 183 | "\n", |
188 | 184 | "with CouchbaseSaver.from_conn_info(\n", |
189 | | - " cb_conn_str=\"couchbase://localhost\",\n", |
190 | | - " cb_username=\"Administrator\",\n", |
191 | | - " cb_password=\"password\",\n", |
192 | | - " bucket_name=\"test\",\n", |
193 | | - " scope_name=\"langgraph\",\n", |
| 185 | + " cb_conn_str=CB_CONN_STR,\n", |
| 186 | + " cb_username=CB_USERNAME,\n", |
| 187 | + " cb_password=CB_PASSWORD,\n", |
| 188 | + " bucket_name=CB_BUCKET_NAME,\n", |
| 189 | + " scope_name=CB_SCOPE_NAME,\n", |
194 | 190 | ") as checkpointer:\n", |
195 | | - " graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)\n", |
| 191 | + " graph = create_agent(model, tools=tools, checkpointer=checkpointer)\n", |
196 | 192 | " config = {\"configurable\": {\"thread_id\": \"1\"}}\n", |
197 | 193 | " res = graph.invoke({\"messages\": [(\"human\", \"what's the weather in sf\")]}, config)\n", |
198 | 194 | " \n", |
199 | 195 | " latest_checkpoint = checkpointer.get(config)\n", |
200 | 196 | " latest_checkpoint_tuple = checkpointer.get_tuple(config)\n", |
201 | | - " checkpoint_tuples = list(checkpointer.list(config))" |
| 197 | + " checkpoint_tuples = list(checkpointer.list(config))\n" |
202 | 198 | ] |
203 | 199 | }, |
204 | 200 | { |
|
291 | 287 | "cell_type": "markdown", |
292 | 288 | "metadata": {}, |
293 | 289 | "source": [ |
294 | | - "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\n", |
295 | | - "\n", |
296 | | - "- `.aput` - Store a checkpoint with its configuration and metadata.\n", |
297 | | - "- `.aput_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).\n", |
298 | | - "- `.aget_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).\n", |
299 | | - "- `.alist` - List checkpoints that match a given configuration and filter criteria.\n", |
300 | | - "\n", |
301 | | - "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.\n", |
302 | | - "\n", |
303 | | - "LangGraph's graph is invoked with message for GPT, storing all the state in Couchbase. We use aget, aget_tuple and alist methods to fetch the states again" |
| 290 | + "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\n\n- `.aput` - Store a checkpoint with its configuration and metadata.\n- `.aput_writes` - Store intermediate writes linked to a checkpoint (i.e. pending writes).\n- `.aget_tuple` - Fetch a checkpoint tuple using a given configuration (`thread_id` and `checkpoint_id`).\n- `.alist` - List checkpoints that match a given configuration and filter criteria.\n\nThen a [ReAct Agent](https://langchain-ai.github.io/langgraph/how-tos/create-react-agent/) is created with GPT Model, weather tool and Couchbase checkpointer.\n\nLangGraph's graph is invoked with message for GPT, storing all the state in Couchbase. We use aget, aget_tuple and alist methods to fetch the states again" |
304 | 291 | ] |
305 | 292 | }, |
306 | 293 | { |
|
314 | 301 | "from couchbase.auth import PasswordAuthenticator\n", |
315 | 302 | "from couchbase.options import ClusterOptions\n", |
316 | 303 | "\n", |
317 | | - "cb_conn_str = \"couchbase://localhost\"\n", |
318 | | - "cb_username = \"Administrator\"\n", |
319 | | - "cb_password = \"password\"\n", |
320 | | - "\n", |
321 | | - "auth = PasswordAuthenticator(cb_username, cb_password)\n", |
| 304 | + "auth = PasswordAuthenticator(CB_USERNAME, CB_PASSWORD)\n", |
322 | 305 | "options = ClusterOptions(auth)\n", |
323 | | - "cb_cluster = await ACluster.connect(cb_conn_str, options)" |
| 306 | + "cb_cluster = await ACluster.connect(CB_CONN_STR, options)\n" |
324 | 307 | ] |
325 | 308 | }, |
326 | 309 | { |
|
333 | 316 | "\n", |
334 | 317 | "async with AsyncCouchbaseSaver.from_cluster(\n", |
335 | 318 | " cluster=cb_cluster,\n", |
336 | | - " bucket_name=\"test\",\n", |
337 | | - " scope_name=\"langgraph\",\n", |
| 319 | + " bucket_name=CB_BUCKET_NAME,\n", |
| 320 | + " scope_name=CB_SCOPE_NAME,\n", |
338 | 321 | ") as checkpointer:\n", |
339 | | - " graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)\n", |
| 322 | + " graph = create_agent(model, tools=tools, checkpointer=checkpointer)\n", |
340 | 323 | " config = {\"configurable\": {\"thread_id\": \"2\"}}\n", |
341 | 324 | " res = await graph.ainvoke(\n", |
342 | 325 | " {\"messages\": [(\"human\", \"what's the weather in nyc\")]}, config\n", |
343 | 326 | " )\n", |
344 | 327 | "\n", |
345 | 328 | " latest_checkpoint = await checkpointer.aget(config)\n", |
346 | 329 | " latest_checkpoint_tuple = await checkpointer.aget_tuple(config)\n", |
347 | | - " checkpoint_tuples = [c async for c in checkpointer.alist(config)]" |
| 330 | + " checkpoint_tuples = [c async for c in checkpointer.alist(config)]\n" |
348 | 331 | ] |
349 | 332 | }, |
350 | 333 | { |
|
0 commit comments