检查点器¶
CheckpointMetadata
¶
基类:
与检查点关联的元数据。
source: Literal['input', 'loop', 'update', 'fork']
instance-attribute
¶
检查点的来源。
- "input": 检查点是从调用/流式传输/批处理的输入创建的。
- "loop": 检查点是从 pregel 循环内部创建的。
- "update": 检查点是从手动状态更新创建的。
- "fork": 检查点是作为另一个检查点的副本创建的。
step: int
instance-attribute
¶
检查点的步骤编号。
-1 代表第一个 "input" 检查点。 0 代表第一个 "loop" 检查点。 ... 代表之后的第 n 个检查点。
writes: dict[str, Any]
instance-attribute
¶
在前一个检查点和当前检查点之间进行的写入操作。
从节点名称到该节点发出的写入操作的映射。
parents: dict[str, str]
instance-attribute
¶
父检查点的 ID。
从检查点命名空间到检查点 ID 的映射。
Checkpoint
¶
基类:
在给定时间点的状态快照。
v: int
instance-attribute
¶
检查点格式的版本。当前为 1。
id: str
instance-attribute
¶
检查点的 ID。它是唯一且单调递增的,因此可用于按从先到后的顺序对检查点进行排序。
ts: str
instance-attribute
¶
ISO 8601 格式的检查点时间戳。
channel_values: dict[str, Any]
instance-attribute
¶
检查点时间通道的值。从通道名称到反序列化通道快照值的映射。
channel_versions: ChannelVersions
instance-attribute
¶
检查点时间通道的版本。键是通道名称,值是每个通道单调递增的版本字符串。
versions_seen: dict[str, ChannelVersions]
instance-attribute
¶
从节点 ID 到从通道名称到已查看版本的映射。 这会跟踪每个节点已查看的通道版本。 用于确定接下来要执行的节点。
pending_sends: List[SendProtocol]
instance-attribute
¶
推送到节点但尚未处理的输入列表。在下一个检查点清除。
BaseCheckpointSaver
¶
基类:
用于创建图检查点器的基类。
检查点器允许 LangGraph 代理在多次交互中及其之间持久化其状态。
属性
-
(serde
) –SerializerProtocol 用于编码/解码检查点的序列化器。
注意
在创建自定义检查点保存器时,请考虑实现异步版本以避免阻塞主线程。
config_specs: list[ConfigurableFieldSpec]
property
¶
定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
使用给定的配置获取检查点元组。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 请求的检查点元组,如果未找到则为 None。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
list(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
列出符合给定条件的检查点。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –额外的过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –列出在此配置之前创建的检查点。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
返回
-
–Iterator [CheckpointTuple ]Iterator[CheckpointTuple]: 匹配的检查点元组的迭代器。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
存储带有其配置和元数据的检查点。
参数
-
config
(
) –RunnableConfig 检查点的配置。
-
checkpoint
(
) –Checkpoint 要存储的检查点。
-
metadata
(
) –CheckpointMetadata 检查点的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
put_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶
存储链接到检查点的中间写入操作。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
使用给定的配置异步获取检查点元组。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 请求的检查点元组,如果未找到则为 None。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
alist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
异步列出符合给定条件的检查点。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –列出在此配置之前创建的检查点。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
返回
-
–AsyncIterator [CheckpointTuple ]AsyncIterator[CheckpointTuple]: 匹配的检查点元组的异步迭代器。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
异步存储带有其配置和元数据的检查点。
参数
-
config
(
) –RunnableConfig 检查点的配置。
-
checkpoint
(
) –Checkpoint 要存储的检查点。
-
metadata
(
) –CheckpointMetadata 检查点的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
异步存储链接到检查点的中间写入操作。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
get_next_version(current: Optional[V], channel: ChannelProtocol) -> V
¶
为通道生成下一个版本 ID。
默认使用整数版本,每次递增 1。如果需要自定义,可以使用字符串/整数/浮点数版本,只要它们是单调递增的。
参数
-
current
(
) –Optional [V ]当前版本标识符(整数、浮点数或字符串)。
-
channel
(
) –BaseChannel 正在进行版本控制的通道。
返回
-
V
(
) –V 下一个版本标识符,必须是递增的。
create_checkpoint(checkpoint: Checkpoint, channels: Optional[Mapping[str, ChannelProtocol]], step: int, *, id: Optional[str] = None) -> Checkpoint
¶
为给定通道创建检查点。
SerializerProtocol
¶
基类:
用于对象序列化和反序列化的协议。
dumps
: 将对象序列化为字节。dumps_typed
: 将对象序列化为元组 (类型, 字节)。loads
: 从字节反序列化对象。loads_typed
: 从元组 (类型, 字节) 反序列化对象。
有效的实现包括 pickle、json 和 orjson 模块。
JsonPlusSerializer
¶
基类:
InMemorySaver
¶
基类:
,
,
一个内存中的检查点保存器。
此检查点保存器使用 defaultdict 在内存中存储检查点。
注意
仅将 InMemorySaver 用于调试或测试目的。 对于生产用例,我们建议安装 langgraph-checkpoint-postgres 并使用 PostgresSaver / AsyncPostgresSaver。
参数
-
serde
(
, default:Optional [SerializerProtocol ]None
) –用于序列化和反序列化检查点的序列化器。默认使用 None。
示例
import asyncio
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
memory = InMemorySaver()
graph = builder.compile(checkpointer=memory)
coro = graph.ainvoke(1, {"configurable": {"thread_id": "thread-1"}})
asyncio.run(coro) # Output: 2
config_specs: list[ConfigurableFieldSpec]
property
¶
定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
从内存存储中获取检查点元组。
此方法基于提供的配置从内存存储中检索检查点元组。 如果配置包含“checkpoint_id”键,则检索具有匹配线程 ID 和时间戳的检查点。 否则,检索给定线程 ID 的最新检查点。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
从内存存储中列出检查点。
此方法基于提供的条件从内存存储中检索检查点元组列表。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –列出在此配置之前创建的检查点。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
产生
-
–CheckpointTuple Iterator[CheckpointTuple]: 匹配的检查点元组的迭代器。
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
将检查点保存到内存存储中。
此方法将检查点保存到内存存储中。 检查点与提供的配置相关联。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –dict 截至本次写入的新版本
返回
-
RunnableConfig
(
) –RunnableConfig 包含已保存检查点时间戳的更新配置。
put_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶
将写入操作列表保存到内存存储中。
此方法将写入操作列表保存到内存存储中。 写入操作与提供的配置相关联。
参数
-
config
(
) –RunnableConfig 要与写入操作关联的配置。
-
writes
(
) –list [tuple [str ,Any ]]要保存的写入操作。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
返回
-
RunnableConfig
(None
) –包含已保存写入操作时间戳的更新配置。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
get_tuple 的异步版本。
此方法是 get_tuple 的异步包装器,它使用 asyncio 在单独的线程中运行同步方法。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
alist(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
list 的异步版本。
此方法是 list 的异步包装器,它使用 asyncio 在单独的线程中运行同步方法。
参数
-
config
(
) –RunnableConfig 用于列出检查点的配置。
产生
-
–AsyncIterator [CheckpointTuple ]AsyncIterator[CheckpointTuple]: 检查点元组的异步迭代器。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
put 的异步版本。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –dict 截至本次写入的新版本
返回
-
RunnableConfig
(
) –RunnableConfig 包含已保存检查点时间戳的更新配置。
aput_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
put_writes 的异步版本。
此方法是 put_writes 的异步包装器,它使用 asyncio 在单独的线程中运行同步方法。
参数
-
config
(
) –RunnableConfig 要与写入操作关联的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要保存的写入内容,每个写入内容都是一个(通道,值)对。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
返回
-
None
–无
PersistentDict
¶
基类:
持久字典,其 API 与 shelve 和 anydbm 兼容。
字典保存在内存中,因此字典操作的运行速度与常规字典一样快。
写入磁盘的操作会延迟到 close 或 sync 时执行(类似于 gdbm 的快速模式)。
输入文件格式会自动发现。输出文件格式可在 pickle、json 和 csv 之间选择。所有三种序列化格式均由快速 C 实现提供支持。
改编自 https://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/
sync() -> None
¶
将字典写入磁盘
SqliteSaver
¶
基类:
一个检查点保存器,用于将检查点存储在 SQLite 数据库中。
注意
此类适用于轻量级、同步用例(演示和小项目),并且无法扩展到多个线程。对于具有 async
支持的类似 sqlite 保存器,请考虑使用 AsyncSqliteSaver。
参数
-
conn
(
) –Connection SQLite 数据库连接。
-
serde
(
, default:Optional [SerializerProtocol ]None
) –用于序列化和反序列化检查点的序列化器。默认为 JsonPlusSerializerCompat。
示例
>>> import sqlite3
>>> from langgraph.checkpoint.sqlite import SqliteSaver
>>> from langgraph.graph import StateGraph
>>>
>>> builder = StateGraph(int)
>>> builder.add_node("add_one", lambda x: x + 1)
>>> builder.set_entry_point("add_one")
>>> builder.set_finish_point("add_one")
>>> conn = sqlite3.connect("checkpoints.sqlite")
>>> memory = SqliteSaver(conn)
>>> graph = builder.compile(checkpointer=memory)
>>> config = {"configurable": {"thread_id": "1"}}
>>> graph.get_state(config)
>>> result = graph.invoke(3, config)
>>> graph.get_state(config)
StateSnapshot(values=4, next=(), config={'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '0c62ca34-ac19-445d-bbb0-5b4984975b2a'}}, parent_config=None)
config_specs: list[ConfigurableFieldSpec]
property
¶
定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
异步存储链接到检查点的中间写入操作。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
from_conn_string(conn_string: str) -> Iterator[SqliteSaver]
classmethod
¶
从连接字符串创建新的 SqliteSaver 实例。
参数
-
conn_string
(
) –str SQLite 连接字符串。
产生
-
SqliteSaver
(
) –SqliteSaver 新的 SqliteSaver 实例。
示例
In memory:
with SqliteSaver.from_conn_string(":memory:") as memory:
...
To disk:
with SqliteSaver.from_conn_string("checkpoints.sqlite") as memory:
...
setup() -> None
¶
设置检查点数据库。
此方法在 SQLite 数据库中创建必要的表(如果它们尚不存在)。它会在需要时自动调用,不应由用户直接调用。
cursor(transaction: bool = True) -> Iterator[sqlite3.Cursor]
¶
获取 SQLite 数据库的游标。
此方法返回 SQLite 数据库的游标。它在 SqliteSaver 内部使用,不应由用户直接调用。
参数
-
transaction
(
, 默认值:bool True
) –是否在游标关闭时提交事务。默认为 True。
产生
-
–Cursor sqlite3.Cursor:SQLite 数据库的游标。
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
从数据库获取检查点元组。
此方法根据提供的配置从 SQLite 数据库检索检查点元组。如果配置包含“checkpoint_id”键,则检索具有匹配线程 ID 和检查点 ID 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
示例
Basic:
>>> config = {"configurable": {"thread_id": "1"}}
>>> checkpoint_tuple = memory.get_tuple(config)
>>> print(checkpoint_tuple)
CheckpointTuple(...)
With checkpoint ID:
>>> config = {
... "configurable": {
... "thread_id": "1",
... "checkpoint_ns": "",
... "checkpoint_id": "1ef4f797-8335-6428-8001-8a1503f9b875",
... }
... }
>>> checkpoint_tuple = memory.get_tuple(config)
>>> print(checkpoint_tuple)
CheckpointTuple(...)
list(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
从数据库列出检查点。
此方法根据提供的配置从 SQLite 数据库检索检查点元组列表。检查点按检查点 ID 降序排序(最新的排在最前面)。
参数
-
config
(
) –RunnableConfig 用于列出检查点的配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他筛选条件。默认为 None。
-
before
(
, default:Optional [RunnableConfig ]None
) –如果提供,则仅返回指定检查点 ID 之前的检查点。默认为 None。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。默认为 None。
产生
-
–CheckpointTuple Iterator[CheckpointTuple]:检查点元组的迭代器。
示例
>>> from langgraph.checkpoint.sqlite import SqliteSaver
>>> with SqliteSaver.from_conn_string(":memory:") as memory:
... # Run a graph, then list the checkpoints
>>> config = {"configurable": {"thread_id": "1"}}
>>> checkpoints = list(memory.list(config, limit=2))
>>> print(checkpoints)
[CheckpointTuple(...), CheckpointTuple(...)]
>>> config = {"configurable": {"thread_id": "1"}}
>>> before = {"configurable": {"checkpoint_id": "1ef4f797-8335-6428-8001-8a1503f9b875"}}
>>> with SqliteSaver.from_conn_string(":memory:") as memory:
... # Run a graph, then list the checkpoints
>>> checkpoints = list(memory.list(config, before=before))
>>> print(checkpoints)
[CheckpointTuple(...), ...]
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
将检查点保存到数据库。
此方法将检查点保存到 SQLite 数据库。检查点与提供的配置及其父配置(如果有)相关联。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
示例
>>> from langgraph.checkpoint.sqlite import SqliteSaver
>>> with SqliteSaver.from_conn_string(":memory:") as memory:
>>> config = {"configurable": {"thread_id": "1", "checkpoint_ns": ""}}
>>> checkpoint = {"ts": "2024-05-04T06:32:42.235444+00:00", "id": "1ef4f797-8335-6428-8001-8a1503f9b875", "channel_values": {"key": "value"}}
>>> saved_config = memory.put(config, checkpoint, {"source": "input", "step": 1, "writes": {"key": "value"}}, {})
>>> print(saved_config)
{'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1ef4f797-8335-6428-8001-8a1503f9b875'}}
put_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶
存储链接到检查点的中间写入操作。
此方法将与检查点关联的中间写入保存到 SQLite 数据库。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –Sequence [Tuple [str ,Any ]]要存储的写入列表,每个写入都是(通道,值)对。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
从数据库异步获取检查点元组。
注意
SqliteSaver 类不支持此异步方法。请改用 get_tuple(),或考虑使用 AsyncSqliteSaver。
alist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
从数据库异步列出检查点。
注意
SqliteSaver 类不支持此异步方法。请改用 list(),或考虑使用 AsyncSqliteSaver。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
异步将检查点保存到数据库。
注意
SqliteSaver 类不支持此异步方法。请改用 put(),或考虑使用 AsyncSqliteSaver。
get_next_version(current: Optional[str], channel: ChannelProtocol) -> str
¶
为通道生成下一个版本 ID。
此方法基于通道的当前版本为通道创建新的版本标识符。
参数
-
current
(
) –Optional [str ]通道的当前版本标识符。
-
channel
(
) –BaseChannel 正在进行版本控制的通道。
返回
-
str
(
) –str 下一个版本标识符,保证单调递增。
AsyncSqliteSaver
¶
基类:
一个异步检查点保存器,用于将检查点存储在 SQLite 数据库中。
此类提供了一个异步接口,用于使用 SQLite 数据库保存和检索检查点。它专为在异步环境中使用而设计,与同步替代方案相比,为 I/O 绑定操作提供了更好的性能。
属性
-
(conn
) –Connection 异步 SQLite 数据库连接。
-
(serde
) –SerializerProtocol 用于编码/解码检查点的序列化器。
提示
需要 aiosqlite 软件包。使用 pip install aiosqlite
安装它。
警告
虽然此类支持异步检查点,但不建议将其用于生产工作负载,因为 SQLite 的写入性能存在限制。对于生产用途,请考虑使用更强大的数据库,如 PostgreSQL。
提示
请记住在执行代码后关闭数据库连接,否则,您可能会看到图在执行后“挂起”(因为程序在连接关闭之前不会退出)。
最简单的方法是使用 async with
语句,如示例所示。
示例
在 StateGraph 中使用
>>> import asyncio
>>>
>>> from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
>>> from langgraph.graph import StateGraph
>>>
>>> builder = StateGraph(int)
>>> builder.add_node("add_one", lambda x: x + 1)
>>> builder.set_entry_point("add_one")
>>> builder.set_finish_point("add_one")
>>> async with AsyncSqliteSaver.from_conn_string("checkpoints.db") as memory:
>>> graph = builder.compile(checkpointer=memory)
>>> coro = graph.ainvoke(1, {"configurable": {"thread_id": "thread-1"}})
>>> print(asyncio.run(coro))
Output: 2
>>> import asyncio
>>> import aiosqlite
>>> from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
>>>
>>> async def main():
>>> async with aiosqlite.connect("checkpoints.db") as conn:
... saver = AsyncSqliteSaver(conn)
... config = {"configurable": {"thread_id": "1"}}
... checkpoint = {"ts": "2023-05-03T10:00:00Z", "data": {"key": "value"}}
... saved_config = await saver.aput(config, checkpoint, {}, {})
... print(saved_config)
>>> asyncio.run(main())
{"configurable": {"thread_id": "1", "checkpoint_id": "0c62ca34-ac19-445d-bbb0-5b4984975b2a"}}
config_specs: list[ConfigurableFieldSpec]
property
¶
定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
from_conn_string(conn_string: str) -> AsyncIterator[AsyncSqliteSaver]
async
classmethod
¶
从连接字符串创建新的 AsyncSqliteSaver 实例。
参数
-
conn_string
(
) –str SQLite 连接字符串。
产生
-
AsyncSqliteSaver
(
) –AsyncIterator [AsyncSqliteSaver ]新的 AsyncSqliteSaver 实例。
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
从数据库获取检查点元组。
此方法根据提供的配置从 SQLite 数据库检索检查点元组。如果配置包含“checkpoint_id”键,则检索具有匹配线程 ID 和检查点 ID 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
从数据库异步列出检查点。
此方法根据提供的配置从 SQLite 数据库检索检查点元组列表。检查点按检查点 ID 降序排序(最新的排在最前面)。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –如果提供,则仅返回指定检查点 ID 之前的检查点。默认为 None。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
产生
-
–CheckpointTuple Iterator[CheckpointTuple]: 匹配的检查点元组的迭代器。
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
将检查点保存到数据库。
此方法将检查点保存到 SQLite 数据库。检查点与提供的配置及其父配置(如果有)相关联。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
setup() -> None
async
¶
异步设置检查点数据库。
此方法在 SQLite 数据库中创建必要的表(如果它们尚不存在)。它会在需要时自动调用,不应由用户直接调用。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
从数据库异步获取检查点元组。
此方法根据提供的配置从 SQLite 数据库检索检查点元组。如果配置包含“checkpoint_id”键,则检索具有匹配线程 ID 和检查点 ID 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
alist(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
从数据库异步列出检查点。
此方法根据提供的配置从 SQLite 数据库检索检查点元组列表。检查点按检查点 ID 降序排序(最新的排在最前面)。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –如果提供,则仅返回指定检查点 ID 之前的检查点。默认为 None。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
产生
-
–AsyncIterator [CheckpointTuple ]AsyncIterator[CheckpointTuple]:匹配检查点元组的异步迭代器。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
异步将检查点保存到数据库。
此方法将检查点保存到 SQLite 数据库。检查点与提供的配置及其父配置(如果有)相关联。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
aput_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
异步存储链接到检查点的中间写入。
此方法将与检查点关联的中间写入保存到数据库。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –Sequence [Tuple [str ,Any ]]要存储的写入列表,每个写入都是(通道,值)对。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
get_next_version(current: Optional[str], channel: ChannelProtocol) -> str
¶
为通道生成下一个版本 ID。
此方法基于通道的当前版本为通道创建新的版本标识符。
参数
-
current
(
) –Optional [str ]通道的当前版本标识符。
-
channel
(
) –BaseChannel 正在进行版本控制的通道。
返回
-
str
(
) –str 下一个版本标识符,保证单调递增。
BasePostgresSaver
¶
基类:
config_specs: list[ConfigurableFieldSpec]
property
¶
定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
使用给定的配置获取检查点元组。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 请求的检查点元组,如果未找到则为 None。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
list(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
列出符合给定条件的检查点。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –额外的过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –列出在此配置之前创建的检查点。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
返回
-
–Iterator [CheckpointTuple ]Iterator[CheckpointTuple]: 匹配的检查点元组的迭代器。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
存储带有其配置和元数据的检查点。
参数
-
config
(
) –RunnableConfig 检查点的配置。
-
checkpoint
(
) –Checkpoint 要存储的检查点。
-
metadata
(
) –CheckpointMetadata 检查点的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
put_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶
存储链接到检查点的中间写入操作。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
使用给定的配置异步获取检查点元组。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 请求的检查点元组,如果未找到则为 None。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
alist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
异步列出符合给定条件的检查点。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –列出在此配置之前创建的检查点。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
返回
-
–AsyncIterator [CheckpointTuple ]AsyncIterator[CheckpointTuple]: 匹配的检查点元组的异步迭代器。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
异步存储带有其配置和元数据的检查点。
参数
-
config
(
) –RunnableConfig 检查点的配置。
-
checkpoint
(
) –Checkpoint 要存储的检查点。
-
metadata
(
) –CheckpointMetadata 检查点的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
异步存储链接到检查点的中间写入操作。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
ShallowPostgresSaver
¶
基类:
一个检查点保存器,它使用 Postgres 存储检查点。
此检查点程序仅存储最新的检查点,并且不保留任何历史记录。它旨在作为 PostgresSaver 的轻量级直接替代品,支持大多数 LangGraph 持久性功能,但时间旅行除外。
config_specs: list[ConfigurableFieldSpec]
property
¶
定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
使用给定的配置异步获取检查点元组。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 请求的检查点元组,如果未找到则为 None。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
alist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
alist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶异步列出符合给定条件的检查点。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –列出在此配置之前创建的检查点。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
返回
-
–AsyncIterator [CheckpointTuple ]AsyncIterator[CheckpointTuple]: 匹配的检查点元组的异步迭代器。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶异步存储带有其配置和元数据的检查点。
参数
-
config
(
) –RunnableConfig 检查点的配置。
-
checkpoint
(
) –Checkpoint 要存储的检查点。
-
metadata
(
) –CheckpointMetadata 检查点的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
aput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶异步存储链接到检查点的中间写入操作。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
from_conn_string(conn_string: str, *, pipeline: bool = False) -> Iterator[ShallowPostgresSaver]
classmethod
¶
from_conn_string(conn_string: str, *, pipeline: bool = False) -> Iterator[ShallowPostgresSaver]
classmethod
¶从连接字符串创建一个新的 ShallowPostgresSaver 实例。
参数
-
conn_string
(
) –str Postgres 连接信息字符串。
-
pipeline
(
, 默认:bool False
) –是否使用 Pipeline
返回
-
ShallowPostgresSaver
(
) –Iterator [ShallowPostgresSaver ]一个新的 ShallowPostgresSaver 实例。
setup() -> None
¶
setup() -> None
¶异步设置检查点数据库。
此方法在 Postgres 数据库中创建必要的表(如果它们尚不存在),并运行数据库迁移。首次使用检查点程序时,**必须**由用户直接调用此方法。
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶从数据库列出检查点。
此方法根据提供的配置从 Postgres 数据库检索检查点元组列表。对于 ShallowPostgresSaver,此方法返回一个列表,其中**仅**包含最新的检查点。
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶从数据库获取检查点元组。
此方法根据提供的配置(匹配配置中的线程 ID)从 Postgres 数据库检索检查点元组。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
示例
Basic:
>>> config = {"configurable": {"thread_id": "1"}}
>>> checkpoint_tuple = memory.get_tuple(config)
>>> print(checkpoint_tuple)
CheckpointTuple(...)
With timestamp:
>>> config = {
... "configurable": {
... "thread_id": "1",
... "checkpoint_ns": "",
... "checkpoint_id": "1ef4f797-8335-6428-8001-8a1503f9b875",
... }
... }
>>> checkpoint_tuple = memory.get_tuple(config)
>>> print(checkpoint_tuple)
CheckpointTuple(...)
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶将检查点保存到数据库。
此方法将检查点保存到 Postgres 数据库。检查点与提供的配置关联。对于 ShallowPostgresSaver,此方法**仅**保存最新的检查点,并覆盖之前的检查点(如果存在)。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
示例
>>> from langgraph.checkpoint.postgres import ShallowPostgresSaver
>>> DB_URI = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
>>> with ShallowPostgresSaver.from_conn_string(DB_URI) as memory:
>>> config = {"configurable": {"thread_id": "1", "checkpoint_ns": ""}}
>>> checkpoint = {"ts": "2024-05-04T06:32:42.235444+00:00", "id": "1ef4f797-8335-6428-8001-8a1503f9b875", "channel_values": {"key": "value"}}
>>> saved_config = memory.put(config, checkpoint, {"source": "input", "step": 1, "writes": {"key": "value"}}, {})
>>> print(saved_config)
{'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1ef4f797-8335-6428-8001-8a1503f9b875'}}
put_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶
put_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶存储链接到检查点的中间写入操作。
此方法将与检查点关联的中间写入保存到 Postgres 数据库。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
PostgresSaver
¶
PostgresSaver
¶基类:
config_specs: list[ConfigurableFieldSpec]
property
¶
config_specs: list[ConfigurableFieldSpec]
property
¶定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
get(config: RunnableConfig) -> Optional[Checkpoint]
¶使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶使用给定的配置异步获取检查点元组。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 请求的检查点元组,如果未找到则为 None。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
alist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
alist(config: Optional[RunnableConfig], *, filter: Optional[Dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶异步列出符合给定条件的检查点。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –列出在此配置之前创建的检查点。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
返回
-
–AsyncIterator [CheckpointTuple ]AsyncIterator[CheckpointTuple]: 匹配的检查点元组的异步迭代器。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶异步存储带有其配置和元数据的检查点。
参数
-
config
(
) –RunnableConfig 检查点的配置。
-
checkpoint
(
) –Checkpoint 要存储的检查点。
-
metadata
(
) –CheckpointMetadata 检查点的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
aput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
aput_writes(config: RunnableConfig, writes: Sequence[Tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶异步存储链接到检查点的中间写入操作。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
Raises
-
–NotImplementedError 在您的自定义检查点保存器中实现此方法。
from_conn_string(conn_string: str, *, pipeline: bool = False) -> Iterator[PostgresSaver]
classmethod
¶
from_conn_string(conn_string: str, *, pipeline: bool = False) -> Iterator[PostgresSaver]
classmethod
¶从连接字符串创建一个新的 PostgresSaver 实例。
参数
-
conn_string
(
) –str Postgres 连接信息字符串。
-
pipeline
(
, 默认:bool False
) –是否使用 Pipeline
返回
-
PostgresSaver
(
) –Iterator [PostgresSaver ]一个新的 PostgresSaver 实例。
setup() -> None
¶
setup() -> None
¶异步设置检查点数据库。
此方法在 Postgres 数据库中创建必要的表(如果它们尚不存在),并运行数据库迁移。首次使用检查点程序时,**必须**由用户直接调用此方法。
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶从数据库列出检查点。
此方法根据提供的配置从 Postgres 数据库检索检查点元组列表。检查点按检查点 ID 降序排列(最新的在最前面)。
参数
-
config
(
) –RunnableConfig 用于列出检查点的配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他筛选条件。默认为 None。
-
before
(
, default:Optional [RunnableConfig ]None
) –如果提供,则仅返回指定检查点 ID 之前的检查点。默认为 None。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。默认为 None。
产生
-
–CheckpointTuple Iterator[CheckpointTuple]:检查点元组的迭代器。
示例
>>> from langgraph.checkpoint.postgres import PostgresSaver
>>> DB_URI = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
>>> with PostgresSaver.from_conn_string(DB_URI) as memory:
... # Run a graph, then list the checkpoints
>>> config = {"configurable": {"thread_id": "1"}}
>>> checkpoints = list(memory.list(config, limit=2))
>>> print(checkpoints)
[CheckpointTuple(...), CheckpointTuple(...)]
>>> config = {"configurable": {"thread_id": "1"}}
>>> before = {"configurable": {"checkpoint_id": "1ef4f797-8335-6428-8001-8a1503f9b875"}}
>>> with PostgresSaver.from_conn_string(DB_URI) as memory:
... # Run a graph, then list the checkpoints
>>> checkpoints = list(memory.list(config, before=before))
>>> print(checkpoints)
[CheckpointTuple(...), ...]
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶从数据库获取检查点元组。
此方法根据提供的配置从 Postgres 数据库检索检查点元组。如果配置包含 "checkpoint_id" 键,则检索具有匹配线程 ID 和时间戳的检查点。否则,检索给定线程 ID 的最新检查点。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
示例
Basic:
>>> config = {"configurable": {"thread_id": "1"}}
>>> checkpoint_tuple = memory.get_tuple(config)
>>> print(checkpoint_tuple)
CheckpointTuple(...)
With timestamp:
>>> config = {
... "configurable": {
... "thread_id": "1",
... "checkpoint_ns": "",
... "checkpoint_id": "1ef4f797-8335-6428-8001-8a1503f9b875",
... }
... }
>>> checkpoint_tuple = memory.get_tuple(config)
>>> print(checkpoint_tuple)
CheckpointTuple(...)
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶将检查点保存到数据库。
此方法将检查点保存到 Postgres 数据库。检查点与其父配置(如果有)以及提供的配置关联。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
示例
>>> from langgraph.checkpoint.postgres import PostgresSaver
>>> DB_URI = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
>>> with PostgresSaver.from_conn_string(DB_URI) as memory:
>>> config = {"configurable": {"thread_id": "1", "checkpoint_ns": ""}}
>>> checkpoint = {"ts": "2024-05-04T06:32:42.235444+00:00", "id": "1ef4f797-8335-6428-8001-8a1503f9b875", "channel_values": {"key": "value"}}
>>> saved_config = memory.put(config, checkpoint, {"source": "input", "step": 1, "writes": {"key": "value"}}, {})
>>> print(saved_config)
{'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '1ef4f797-8335-6428-8001-8a1503f9b875'}}
put_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶
put_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶存储链接到检查点的中间写入操作。
此方法将与检查点关联的中间写入保存到 Postgres 数据库。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –List [Tuple [str ,Any ]]要存储的写入操作列表。
-
task_id
(
) –str 创建写入操作的任务的标识符。
AsyncShallowPostgresSaver
¶
AsyncShallowPostgresSaver
¶基类:
一个使用 Postgres 异步存储检查点的检查点保存器。
此检查点程序**仅**存储最新的检查点,并且**不**保留任何历史记录。它旨在作为 AsyncPostgresSaver 的轻量级直接替代品,支持 LangGraph 大部分持久性功能,但时间旅行除外。
config_specs: list[ConfigurableFieldSpec]
property
¶
config_specs: list[ConfigurableFieldSpec]
property
¶定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
get(config: RunnableConfig) -> Optional[Checkpoint]
¶使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
from_conn_string(conn_string: str, *, pipeline: bool = False, serde: Optional[SerializerProtocol] = None) -> AsyncIterator[AsyncShallowPostgresSaver]
async
classmethod
¶
from_conn_string(conn_string: str, *, pipeline: bool = False, serde: Optional[SerializerProtocol] = None) -> AsyncIterator[AsyncShallowPostgresSaver]
async
classmethod
¶从连接字符串创建一个新的 AsyncShallowPostgresSaver 实例。
参数
-
conn_string
(
) –str Postgres 连接信息字符串。
-
pipeline
(
, 默认:bool False
) –是否使用 AsyncPipeline
返回
-
AsyncShallowPostgresSaver
(
) –AsyncIterator [AsyncShallowPostgresSaver ]一个新的 AsyncShallowPostgresSaver 实例。
setup() -> None
async
¶
setup() -> None
async
¶异步设置检查点数据库。
此方法在 Postgres 数据库中创建必要的表(如果它们尚不存在),并运行数据库迁移。首次使用检查点程序时,**必须**由用户直接调用此方法。
alist(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
alist(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶从数据库异步列出检查点。
此方法根据提供的配置从 Postgres 数据库检索检查点元组列表。对于 ShallowPostgresSaver,此方法返回一个列表,其中**仅**包含最新的检查点。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶从数据库异步获取检查点元组。
此方法根据提供的配置(匹配配置中的线程 ID)从 Postgres 数据库检索检查点元组。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶异步将检查点保存到数据库。
此方法将检查点保存到 Postgres 数据库。检查点与提供的配置关联。对于 AsyncShallowPostgresSaver,此方法**仅**保存最新的检查点,并覆盖之前的检查点(如果存在)。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
aput_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
aput_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶异步存储链接到检查点的中间写入。
此方法将与检查点关联的中间写入保存到数据库。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –Sequence [Tuple [str ,Any ]]要存储的写入列表,每个写入都是(通道,值)对。
-
task_id
(
) –str 创建写入操作的任务的标识符。
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶从数据库列出检查点。
此方法根据提供的配置从 Postgres 数据库检索检查点元组列表。对于 ShallowPostgresSaver,此方法返回一个列表,其中**仅**包含最新的检查点。
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶从数据库获取检查点元组。
此方法根据提供的配置(匹配配置中的线程 ID)从 Postgres 数据库检索检查点元组。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶将检查点保存到数据库。
此方法将检查点保存到 Postgres 数据库。检查点与提供的配置关联。对于 AsyncShallowPostgresSaver,此方法**仅**保存最新的检查点,并覆盖之前的检查点(如果存在)。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
put_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶
put_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶存储链接到检查点的中间写入操作。
此方法将与检查点关联的中间写入保存到数据库。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –Sequence [Tuple [str ,Any ]]要存储的写入列表,每个写入都是(通道,值)对。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。
AsyncPostgresSaver
¶
AsyncPostgresSaver
¶基类:
config_specs: list[ConfigurableFieldSpec]
property
¶
config_specs: list[ConfigurableFieldSpec]
property
¶定义检查点保存器的配置选项。
返回
-
–list [ConfigurableFieldSpec ]list[ConfigurableFieldSpec]: 配置字段规范列表。
get(config: RunnableConfig) -> Optional[Checkpoint]
¶
get(config: RunnableConfig) -> Optional[Checkpoint]
¶使用给定的配置获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
async
¶使用给定的配置异步获取检查点。
参数
-
config
(
) –RunnableConfig 指定要检索哪个检查点的配置。
返回
-
–Optional [Checkpoint ]Optional[Checkpoint]: 请求的检查点,如果未找到则为 None。
from_conn_string(conn_string: str, *, pipeline: bool = False, serde: Optional[SerializerProtocol] = None) -> AsyncIterator[AsyncPostgresSaver]
async
classmethod
¶
from_conn_string(conn_string: str, *, pipeline: bool = False, serde: Optional[SerializerProtocol] = None) -> AsyncIterator[AsyncPostgresSaver]
async
classmethod
¶从连接字符串创建一个新的 AsyncPostgresSaver 实例。
参数
-
conn_string
(
) –str Postgres 连接信息字符串。
-
pipeline
(
, 默认:bool False
) –是否使用 AsyncPipeline
返回
-
AsyncPostgresSaver
(
) –AsyncIterator [AsyncPostgresSaver ]一个新的 AsyncPostgresSaver 实例。
setup() -> None
async
¶
setup() -> None
async
¶异步设置检查点数据库。
此方法在 Postgres 数据库中创建必要的表(如果它们尚不存在),并运行数据库迁移。首次使用检查点程序时,**必须**由用户直接调用此方法。
alist(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶
alist(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> AsyncIterator[CheckpointTuple]
async
¶从数据库异步列出检查点。
此方法根据提供的配置从 Postgres 数据库检索检查点元组列表。检查点按检查点 ID 降序排列(最新的在最前面)。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –如果提供,则仅返回指定检查点 ID 之前的检查点。默认为 None。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
产生
-
–AsyncIterator [CheckpointTuple ]AsyncIterator[CheckpointTuple]:匹配检查点元组的异步迭代器。
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶
aget_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
async
¶从数据库异步获取检查点元组。
此方法根据提供的配置从 Postgres 数据库检索检查点元组。如果配置包含 "checkpoint_id" 键,则检索具有匹配线程 ID 和 "checkpoint_id" 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶
aput(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
async
¶异步将检查点保存到数据库。
此方法将检查点保存到 Postgres 数据库。检查点与其父配置(如果有)以及提供的配置关联。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
aput_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶
aput_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
async
¶异步存储链接到检查点的中间写入。
此方法将与检查点关联的中间写入保存到数据库。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –Sequence [Tuple [str ,Any ]]要存储的写入列表,每个写入都是(通道,值)对。
-
task_id
(
) –str 创建写入操作的任务的标识符。
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶
list(config: Optional[RunnableConfig], *, filter: Optional[dict[str, Any]] = None, before: Optional[RunnableConfig] = None, limit: Optional[int] = None) -> Iterator[CheckpointTuple]
¶从数据库列出检查点。
此方法根据提供的配置从 Postgres 数据库检索检查点元组列表。检查点按检查点 ID 降序排列(最新的在最前面)。
参数
-
config
(
) –Optional [RunnableConfig ]用于过滤检查点的基本配置。
-
filter
(
, default:Optional [Dict [str ,Any ]]None
) –元数据的其他过滤条件。
-
before
(
, default:Optional [RunnableConfig ]None
) –如果提供,则仅返回指定检查点 ID 之前的检查点。默认为 None。
-
limit
(
, default:Optional [int ]None
) –要返回的最大检查点数。
产生
-
–CheckpointTuple Iterator[CheckpointTuple]: 匹配的检查点元组的迭代器。
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶
get_tuple(config: RunnableConfig) -> Optional[CheckpointTuple]
¶从数据库获取检查点元组。
此方法根据提供的配置从 Postgres 数据库检索检查点元组。如果配置包含 "checkpoint_id" 键,则检索具有匹配线程 ID 和 "checkpoint_id" 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
-
config
(
) –RunnableConfig 用于检索检查点的配置。
返回
-
–Optional [CheckpointTuple ]Optional[CheckpointTuple]: 检索到的检查点元组,如果未找到匹配的检查点,则为 None。
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶
put(config: RunnableConfig, checkpoint: Checkpoint, metadata: CheckpointMetadata, new_versions: ChannelVersions) -> RunnableConfig
¶将检查点保存到数据库。
此方法将检查点保存到 Postgres 数据库。检查点与其父配置(如果有)以及提供的配置关联。
参数
-
config
(
) –RunnableConfig 要与检查点关联的配置。
-
checkpoint
(
) –Checkpoint 要保存的检查点。
-
metadata
(
) –CheckpointMetadata 要与检查点一起保存的其他元数据。
-
new_versions
(
) –ChannelVersions 截至本次写入的新通道版本。
返回
-
RunnableConfig
(
) –RunnableConfig 存储检查点后更新的配置。
put_writes(config: RunnableConfig, writes: Sequence[tuple[str, Any]], task_id: str, task_path: str = '') -> None
¶
存储链接到检查点的中间写入操作。
此方法将与检查点关联的中间写入保存到数据库。
参数
-
config
(
) –RunnableConfig 相关检查点的配置。
-
writes
(
) –Sequence [Tuple [str ,Any ]]要存储的写入列表,每个写入都是(通道,值)对。
-
task_id
(
) –str 创建写入操作的任务的标识符。
-
task_path
(
, default:str ''
) –创建写入操作的任务的路径。