[Docs] use END instead of set_finish_point (#903)

This commit is contained in:
William FH
2024-07-01 21:56:10 -07:00
committed by GitHub
parent 727e63c01e
commit 320a87e1b9
31 changed files with 4189 additions and 389 deletions
+53 -44
View File
@@ -42,7 +42,16 @@
"source": [
"\"\"\"Implementation of a langgraph checkpoint saver using Postgres.\"\"\"\n",
"from contextlib import asynccontextmanager, contextmanager\n",
"from typing import Any, AsyncGenerator, AsyncIterator, Generator, Optional, Union, Tuple, List\n",
"from typing import (\n",
" Any,\n",
" AsyncGenerator,\n",
" AsyncIterator,\n",
" Generator,\n",
" Optional,\n",
" Union,\n",
" Tuple,\n",
" List,\n",
")\n",
"\n",
"import psycopg\n",
"from langchain_core.runnables import RunnableConfig\n",
@@ -136,8 +145,7 @@
" sync_connection: Optional[Union[psycopg.Connection, ConnectionPool]] = None,\n",
" async_connection: Optional[\n",
" Union[psycopg.AsyncConnection, AsyncConnectionPool]\n",
" ] = None\n",
" \n",
" ] = None,\n",
" ):\n",
" super().__init__(serde=JsonPlusSerializer())\n",
" self.sync_connection = sync_connection\n",
@@ -168,15 +176,12 @@
" );\n",
" \"\"\"\n",
"\n",
" \n",
"\n",
" @staticmethod\n",
" def create_tables(connection: Union[psycopg.Connection, ConnectionPool], /) -> None:\n",
" \"\"\"Create the schema for the checkpoint saver.\"\"\"\n",
" with _get_sync_connection(connection) as conn:\n",
" with conn.cursor() as cur:\n",
" cur.execute(PostgresSaver.CREATE_TABLES_QUERY)\n",
" \n",
"\n",
" @staticmethod\n",
" async def acreate_tables(\n",
@@ -209,7 +214,12 @@
" metadata = EXCLUDED.metadata;\n",
" \"\"\"\n",
"\n",
" def put(self, config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata) -> RunnableConfig:\n",
" def put(\n",
" self,\n",
" config: RunnableConfig,\n",
" checkpoint: Checkpoint,\n",
" metadata: CheckpointMetadata,\n",
" ) -> RunnableConfig:\n",
" \"\"\"Put the checkpoint for the given configuration.\n",
" Args:\n",
" config: The configuration for the checkpoint.\n",
@@ -244,7 +254,10 @@
" }\n",
"\n",
" async def aput(\n",
" self, config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata\n",
" self,\n",
" config: RunnableConfig,\n",
" checkpoint: Checkpoint,\n",
" metadata: CheckpointMetadata,\n",
" ) -> RunnableConfig:\n",
" \"\"\"Put the checkpoint for the given configuration.\n",
" Args:\n",
@@ -403,18 +416,18 @@
" if value:\n",
" checkpoint, metadata, thread_ts, parent_ts = value\n",
" return CheckpointTuple(\n",
" config=config,\n",
" checkpoint=self.serde.loads(checkpoint),\n",
" metadata=self.serde.loads(metadata),\n",
" parent_config={\n",
" \"configurable\": {\n",
" \"thread_id\": thread_id,\n",
" \"thread_ts\": thread_ts,\n",
" }\n",
" config=config,\n",
" checkpoint=self.serde.loads(checkpoint),\n",
" metadata=self.serde.loads(metadata),\n",
" parent_config={\n",
" \"configurable\": {\n",
" \"thread_id\": thread_id,\n",
" \"thread_ts\": thread_ts,\n",
" }\n",
" if thread_ts\n",
" else None,\n",
" )\n",
" }\n",
" if thread_ts\n",
" else None,\n",
" )\n",
" else:\n",
" cur.execute(\n",
" self.GET_CHECKPOINT_QUERY,\n",
@@ -473,18 +486,18 @@
" if value:\n",
" checkpoint, metadata, thread_ts, parent_ts = value\n",
" return CheckpointTuple(\n",
" config=config,\n",
" checkpoint=self.serde.loads(checkpoint),\n",
" metadata=self.serde.loads(metadata),\n",
" parent_config={\n",
" \"configurable\": {\n",
" \"thread_id\": thread_id,\n",
" \"thread_ts\": thread_ts,\n",
" }\n",
" config=config,\n",
" checkpoint=self.serde.loads(checkpoint),\n",
" metadata=self.serde.loads(metadata),\n",
" parent_config={\n",
" \"configurable\": {\n",
" \"thread_id\": thread_id,\n",
" \"thread_ts\": thread_ts,\n",
" }\n",
" if thread_ts\n",
" else None,\n",
" )\n",
" }\n",
" if thread_ts\n",
" else None,\n",
" )\n",
" else:\n",
" await cur.execute(\n",
" self.GET_CHECKPOINT_QUERY,\n",
@@ -663,9 +676,7 @@
" max_size=20,\n",
")\n",
"\n",
"checkpointer = PostgresSaver(\n",
" sync_connection=pool\n",
")\n",
"checkpointer = PostgresSaver(sync_connection=pool)\n",
"checkpointer.create_tables(pool)"
]
},
@@ -761,9 +772,7 @@
"from psycopg import Connection\n",
"\n",
"with Connection.connect(DB_URI) as conn:\n",
" checkpointer = PostgresSaver(\n",
" sync_connection=conn\n",
" )\n",
" checkpointer = PostgresSaver(sync_connection=conn)\n",
"\n",
" graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)\n",
" config = {\"configurable\": {\"thread_id\": \"2\"}}\n",
@@ -833,9 +842,7 @@
" max_size=20,\n",
")\n",
"\n",
"checkpointer = PostgresSaver(\n",
" async_connection=pool\n",
")\n",
"checkpointer = PostgresSaver(async_connection=pool)\n",
"await checkpointer.acreate_tables(pool)"
]
},
@@ -848,7 +855,9 @@
"source": [
"graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)\n",
"config = {\"configurable\": {\"thread_id\": \"3\"}}\n",
"res = await graph.ainvoke({\"messages\": [(\"human\", \"what's the weather in nyc\")]}, config)"
"res = await graph.ainvoke(\n",
" {\"messages\": [(\"human\", \"what's the weather in nyc\")]}, config\n",
")"
]
},
{
@@ -900,12 +909,12 @@
"from psycopg import AsyncConnection\n",
"\n",
"async with await AsyncConnection.connect(DB_URI) as conn:\n",
" checkpointer = PostgresSaver(\n",
" async_connection=conn\n",
" )\n",
" checkpointer = PostgresSaver(async_connection=conn)\n",
" graph = create_react_agent(model, tools=tools, checkpointer=checkpointer)\n",
" config = {\"configurable\": {\"thread_id\": \"4\"}}\n",
" res = await graph.ainvoke({\"messages\": [(\"human\", \"what's the weather in nyc\")]}, config)\n",
" res = await graph.ainvoke(\n",
" {\"messages\": [(\"human\", \"what's the weather in nyc\")]}, config\n",
" )\n",
" checkpoint_tuples = [c async for c in checkpointer.alist(config)]"
]
},