检查点¶
类
名称 | 描述 |
---|---|
CheckpointMetadata |
与检查点相关的元数据。 |
Checkpoint |
在给定时间点的状态快照。 |
BaseCheckpointSaver |
用于创建图检查点的基类。 |
函数
名称 | 描述 |
---|---|
create_checkpoint |
为给定通道创建检查点。 |
CheckpointMetadata ¶
基类:TypedDict
与检查点相关的元数据。
属性
名称 | 类型 | 描述 |
---|---|---|
source |
Literal['input', 'loop', 'update', 'fork']
|
检查点的来源。 |
step |
int
|
检查点的步骤编号。 |
writes |
dict[str, Any]
|
在上一个检查点和此检查点之间进行的写入。 |
parents |
dict[str, str]
|
父检查点的 ID。 |
Checkpoint ¶
基类:TypedDict
在给定时间点的状态快照。
属性
名称 | 类型 | 描述 |
---|---|---|
v |
int
|
检查点格式的版本。当前为 1。 |
id |
str
|
检查点的 ID。这既是唯一的,也是单调递增的 |
ts |
str
|
检查点的 ISO 8601 格式时间戳。 |
channel_values |
dict[str, Any]
|
检查点时通道的值。 |
channel_versions |
ChannelVersions
|
检查点时通道的版本。 |
versions_seen |
dict[str, ChannelVersions]
|
从节点 ID 到从通道名称到已见版本的映射。 |
pending_sends |
List[SendProtocol]
|
已推送到节点但尚未处理的输入列表。 |
BaseCheckpointSaver ¶
基类:Generic[V]
用于创建图检查点的基类。
检查点器允许 LangGraph Agent 在多次交互中以及跨多次交互持久化其状态。
属性
名称 | 类型 | 描述 |
---|---|---|
serde |
SerializerProtocol
|
用于编码/解码检查点的序列化器。 |
注意
创建自定义检查点保存器时,考虑实现异步版本以避免阻塞主线程。
方法
名称 | 描述 |
---|---|
get |
使用给定配置获取检查点。 |
get_tuple |
使用给定配置获取检查点元组。 |
list |
列出符合给定条件的检查点。 |
put |
存储检查点及其配置和元数据。 |
put_writes |
存储与检查点关联的中间写入。 |
delete_thread |
删除与特定线程 ID 关联的所有检查点和写入。 |
aget |
使用给定配置异步获取检查点。 |
aget_tuple |
使用给定配置异步获取检查点元组。 |
alist |
异步列出符合给定条件的检查点。 |
aput |
异步存储检查点及其配置和元数据。 |
aput_writes |
异步存储与检查点关联的中间写入。 |
adelete_thread |
删除与特定线程 ID 关联的所有检查点和写入。 |
get_next_version |
生成通道的下一个版本 ID。 |
config_specs property
¶
config_specs: list[ConfigurableFieldSpec]
get ¶
get(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
get_tuple ¶
get_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
使用给定配置获取检查点元组。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:请求的检查点元组,如果未找到则为 None。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
list ¶
list(
config: Optional[RunnableConfig],
*,
filter: Optional[Dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None
) -> Iterator[CheckpointTuple]
列出符合给定条件的检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
Optional[RunnableConfig]
|
用于过滤检查点的基本配置。 |
必需 |
filter
|
Optional[Dict[str, Any]]
|
附加过滤条件。 |
None
|
before
|
Optional[RunnableConfig]
|
列出在此配置之前创建的检查点。 |
None
|
limit
|
Optional[int]
|
返回的最大检查点数量。 |
None
|
返回
类型 | 描述 |
---|---|
Iterator[CheckpointTuple]
|
Iterator[CheckpointTuple]:匹配检查点元组的迭代器。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
put ¶
put(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
存储检查点及其配置和元数据。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
检查点的配置。 |
必需 |
checkpoint
|
Checkpoint
|
要存储的检查点。 |
必需 |
metadata
|
CheckpointMetadata
|
检查点的附加元数据。 |
必需 |
new_versions
|
ChannelVersions
|
本次写入产生的新通道版本。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
RunnableConfig |
RunnableConfig
|
存储检查点后更新的配置。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
put_writes ¶
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
|
str
|
创建写入的任务的路径。 |
''
|
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
aget async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置异步获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
aget_tuple async
¶
aget_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
使用给定配置异步获取检查点元组。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:请求的检查点元组,如果未找到则为 None。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
alist async
¶
alist(
config: Optional[RunnableConfig],
*,
filter: Optional[Dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None
) -> AsyncIterator[CheckpointTuple]
异步列出符合给定条件的检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
Optional[RunnableConfig]
|
用于过滤检查点的基本配置。 |
必需 |
filter
|
Optional[Dict[str, Any]]
|
元数据的附加过滤条件。 |
None
|
before
|
Optional[RunnableConfig]
|
列出在此配置之前创建的检查点。 |
None
|
limit
|
Optional[int]
|
返回的最大检查点数量。 |
None
|
返回
类型 | 描述 |
---|---|
AsyncIterator[CheckpointTuple]
|
AsyncIterator[CheckpointTuple]:匹配检查点元组的异步迭代器。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
aput async
¶
aput(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
异步存储检查点及其配置和元数据。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
检查点的配置。 |
必需 |
checkpoint
|
Checkpoint
|
要存储的检查点。 |
必需 |
metadata
|
CheckpointMetadata
|
检查点的附加元数据。 |
必需 |
new_versions
|
ChannelVersions
|
本次写入产生的新通道版本。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
RunnableConfig |
RunnableConfig
|
存储检查点后更新的配置。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
aput_writes async
¶
aput_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
|
str
|
创建写入的任务的路径。 |
''
|
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
create_checkpoint ¶
create_checkpoint(
checkpoint: Checkpoint,
channels: Optional[Mapping[str, ChannelProtocol]],
step: int,
*,
id: Optional[str] = None
) -> Checkpoint
为给定通道创建检查点。
类
名称 | 描述 |
---|---|
SerializerProtocol |
用于对象序列化和反序列化的协议。 |
类
名称 | 描述 |
---|---|
InMemorySaver |
一个内存中的检查点保存器。 |
PersistentDict |
具有与 shelve 和 anydbm 兼容的 API 的持久化字典。 |
InMemorySaver ¶
基类:BaseCheckpointSaver[str]
, AbstractContextManager
, AbstractAsyncContextManager
一个内存中的检查点保存器。
此检查点保存器使用 defaultdict 将检查点存储在内存中。
注意
仅将 InMemorySaver
用于调试或测试目的。对于生产用例,我们建议安装 langgraph-checkpoint-postgres 并使用 PostgresSaver
/ AsyncPostgresSaver
。
如果您正在使用 LangGraph Platform,则无需指定检查点器。将自动使用正确的托管检查点器。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
serde
|
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
方法
名称 | 描述 |
---|---|
get_tuple |
从内存存储中获取检查点元组。 |
list |
列出内存存储中的检查点。 |
put |
将检查点保存到内存存储中。 |
put_writes |
将写入列表保存到内存存储中。 |
delete_thread |
删除与线程 ID 关联的所有检查点和写入。 |
aget_tuple |
get_tuple 的异步版本。 |
alist |
list 的异步版本。 |
aput |
put 的异步版本。 |
aput_writes |
put_writes 的异步版本。 |
adelete_thread |
删除与线程 ID 关联的所有检查点和写入。 |
get |
使用给定配置获取检查点。 |
aget |
使用给定配置异步获取检查点。 |
属性
名称 | 类型 | 描述 |
---|---|---|
config_specs |
list[ConfigurableFieldSpec]
|
定义检查点保存器的配置选项。 |
config_specs property
¶
config_specs: list[ConfigurableFieldSpec]
get_tuple ¶
get_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
从内存存储中获取检查点元组。
此方法根据提供的配置从内存存储中检索检查点元组。如果配置包含 "checkpoint_id" 键,则检索与线程 ID 和时间戳匹配的检查点。否则,检索给定线程 ID 的最新检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
用于检索检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:检索到的检查点元组,如果未找到匹配的检查点,则为 None。 |
list ¶
list(
config: Optional[RunnableConfig],
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None
) -> Iterator[CheckpointTuple]
列出内存存储中的检查点。
此方法根据提供的条件从内存存储中检索检查点元组列表。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
Optional[RunnableConfig]
|
用于过滤检查点的基本配置。 |
必需 |
filter
|
Optional[dict[str, Any]]
|
元数据的附加过滤条件。 |
None
|
before
|
Optional[RunnableConfig]
|
列出在此配置之前创建的检查点。 |
None
|
limit
|
Optional[int]
|
返回的最大检查点数量。 |
None
|
生成
类型 | 描述 |
---|---|
CheckpointTuple
|
Iterator[CheckpointTuple]:匹配检查点元组的迭代器。 |
put ¶
put(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
将检查点保存到内存存储中。
此方法将检查点保存到内存存储中。检查点与提供的配置关联。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
与检查点关联的配置。 |
必需 |
checkpoint
|
Checkpoint
|
要保存的检查点。 |
必需 |
metadata
|
CheckpointMetadata
|
与检查点一起保存的附加元数据。 |
必需 |
new_versions
|
ChannelVersions
|
本次写入产生的新版本 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
RunnableConfig |
RunnableConfig
|
包含已保存检查点时间戳的更新配置。 |
put_writes ¶
aget_tuple async
¶
aget_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
get_tuple 的异步版本。
此方法是 get_tuple 的异步包装器,它使用 asyncio 在单独的线程中运行同步方法。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
用于检索检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:检索到的检查点元组,如果未找到匹配的检查点,则为 None。 |
alist async
¶
alist(
config: Optional[RunnableConfig],
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None
) -> AsyncIterator[CheckpointTuple]
list 的异步版本。
此方法是 list 的异步包装器,它使用 asyncio 在单独的线程中运行同步方法。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
Optional[RunnableConfig]
|
用于列出检查点的配置。 |
必需 |
生成
类型 | 描述 |
---|---|
AsyncIterator[CheckpointTuple]
|
AsyncIterator[CheckpointTuple]:检查点元组的异步迭代器。 |
aput async
¶
aput(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
put 的异步版本。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
与检查点关联的配置。 |
必需 |
checkpoint
|
Checkpoint
|
要保存的检查点。 |
必需 |
metadata
|
CheckpointMetadata
|
与检查点一起保存的附加元数据。 |
必需 |
new_versions
|
ChannelVersions
|
本次写入产生的新版本 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
RunnableConfig |
RunnableConfig
|
包含已保存检查点时间戳的更新配置。 |
aput_writes async
¶
get ¶
get(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
aget async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置异步获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
PersistentDict ¶
基础:defaultdict
具有与 shelve 和 anydbm 兼容的 API 的持久化字典。
字典保存在内存中,因此字典操作的运行速度与常规字典一样快。
写入磁盘会延迟到关闭或同步(类似于 gdbm 的 fast 模式)。
输入文件格式自动发现。输出文件格式可在 pickle、json 和 csv 之间选择。所有三种序列化格式都由快速 C 实现提供支持。
改编自 https://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/
方法
名称 | 描述 |
---|---|
sync |
将字典写入磁盘 |
模块
名称 | 描述 |
---|---|
aio |
|
utils |
|
类
名称 | 描述 |
---|---|
SqliteSaver |
将检查点存储在 SQLite 数据库中的检查点保存器。 |
SqliteSaver ¶
将检查点存储在 SQLite 数据库中的检查点保存器。
注意
此类适用于轻量级、同步使用场景(演示和小型项目),不适用于多线程。对于具有 async
支持的类似 sqlite 保存器,请考虑使用 AsyncSqliteSaver。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
conn
|
连接
|
SQLite 数据库连接。 |
必需 |
serde
|
Optional[SerializerProtocol]
|
用于序列化和反序列化检查点的序列化器。默认为 JsonPlusSerializerCompat。 |
None
|
示例
>>> 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")
>>> # Create a new SqliteSaver instance
>>> # Note: check_same_thread=False is OK as the implementation uses a lock
>>> # to ensure thread safety.
>>> conn = sqlite3.connect("checkpoints.sqlite", check_same_thread=False)
>>> 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)
方法
名称 | 描述 |
---|---|
from_conn_string |
从连接字符串创建一个新的 SqliteSaver 实例。 |
setup |
设置检查点数据库。 |
cursor |
获取 SQLite 数据库的游标。 |
get_tuple |
从数据库中获取检查点元组。 |
list |
列出数据库中的检查点。 |
put |
将检查点保存到数据库。 |
put_writes |
存储与检查点关联的中间写入。 |
delete_thread |
删除与线程 ID 关联的所有检查点和写入。 |
aget_tuple |
从数据库异步获取检查点元组。 |
alist |
异步列出数据库中的检查点。 |
aput |
异步将检查点保存到数据库。 |
get_next_version |
生成通道的下一个版本 ID。 |
get |
使用给定配置获取检查点。 |
aget |
使用给定配置异步获取检查点。 |
aput_writes |
异步存储与检查点关联的中间写入。 |
adelete_thread |
删除与特定线程 ID 关联的所有检查点和写入。 |
属性
名称 | 类型 | 描述 |
---|---|---|
config_specs |
list[ConfigurableFieldSpec]
|
定义检查点保存器的配置选项。 |
config_specs 属性
¶
config_specs: list[ConfigurableFieldSpec]
from_conn_string 类方法
¶
from_conn_string(conn_string: str) -> Iterator[SqliteSaver]
从连接字符串创建一个新的 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:
...
cursor ¶
get_tuple ¶
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 ¶
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
|
Optional[Dict[str, Any]]
|
元数据的附加过滤条件。默认为 None。 |
None
|
before
|
Optional[RunnableConfig]
|
如果提供,仅返回指定检查点 ID 之前的检查点。默认为 None。 |
None
|
limit
|
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 ¶
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 ¶
aget_tuple async
¶
aget_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
从数据库异步获取检查点元组。
注意
SqliteSaver 类不支持此异步方法。请改用 get_tuple(),或考虑使用 AsyncSqliteSaver。
alist async
¶
alist(
config: Optional[RunnableConfig],
*,
filter: Optional[Dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None
) -> AsyncIterator[CheckpointTuple]
异步列出数据库中的检查点。
注意
SqliteSaver 类不支持此异步方法。请改用 list(),或考虑使用 AsyncSqliteSaver。
aput async
¶
aput(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
异步将检查点保存到数据库。
注意
SqliteSaver 类不支持此异步方法。请改用 put(),或考虑使用 AsyncSqliteSaver。
get_next_version ¶
get ¶
get(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
aget async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置异步获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
aput_writes async
¶
aput_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
|
str
|
创建写入的任务的路径。 |
''
|
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
类
名称 | 描述 |
---|---|
AsyncSqliteSaver |
将检查点存储在 SQLite 数据库中的异步检查点保存器。 |
AsyncSqliteSaver ¶
将检查点存储在 SQLite 数据库中的异步检查点保存器。
此类提供了使用 SQLite 数据库保存和检索检查点的异步接口。它专为异步环境设计,与同步替代方案相比,为 I/O 密集型操作提供更好的性能。
属性
名称 | 类型 | 描述 |
---|---|---|
conn |
连接
|
异步 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
>>>
>>> async def main():
>>> 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(await asyncio.gather(coro))
>>>
>>> asyncio.run(main())
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_ns": ""}}
... checkpoint = {"ts": "2023-05-03T10:00:00Z", "data": {"key": "value"}, "id": "0c62ca34-ac19-445d-bbb0-5b4984975b2a"}
... saved_config = await saver.aput(config, checkpoint, {}, {})
... print(saved_config)
>>> asyncio.run(main())
{'configurable': {'thread_id': '1', 'checkpoint_ns': '', 'checkpoint_id': '0c62ca34-ac19-445d-bbb0-5b4984975b2a'}}
方法
名称 | 描述 |
---|---|
from_conn_string |
从连接字符串创建一个新的 AsyncSqliteSaver 实例。 |
get_tuple |
从数据库中获取检查点元组。 |
list |
异步列出数据库中的检查点。 |
put |
将检查点保存到数据库。 |
delete_thread |
删除与线程 ID 关联的所有检查点和写入。 |
setup |
异步设置检查点数据库。 |
aget_tuple |
从数据库异步获取检查点元组。 |
alist |
异步列出数据库中的检查点。 |
aput |
异步将检查点保存到数据库。 |
aput_writes |
异步存储链接到检查点的中间写入。 |
adelete_thread |
删除与线程 ID 关联的所有检查点和写入。 |
get_next_version |
生成通道的下一个版本 ID。 |
get |
使用给定配置获取检查点。 |
aget |
使用给定配置异步获取检查点。 |
config_specs 属性
¶
config_specs: list[ConfigurableFieldSpec]
from_conn_string async
类方法
¶
from_conn_string(
conn_string: str,
) -> AsyncIterator[AsyncSqliteSaver]
从连接字符串创建一个新的 AsyncSqliteSaver 实例。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
conn_string
|
str
|
SQLite 连接字符串。 |
必需 |
生成
名称 | 类型 | 描述 |
---|---|---|
AsyncSqliteSaver |
AsyncIterator[AsyncSqliteSaver]
|
一个新的 AsyncSqliteSaver 实例。 |
get_tuple ¶
get_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
从数据库中获取检查点元组。
此方法根据提供的配置从 SQLite 数据库中检索检查点元组。如果配置包含 "checkpoint_id" 键,则检索具有匹配线程 ID 和检查点 ID 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
用于检索检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:检索到的检查点元组,如果未找到匹配的检查点,则为 None。 |
list ¶
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
|
Optional[dict[str, Any]]
|
元数据的附加过滤条件。 |
None
|
before
|
Optional[RunnableConfig]
|
如果提供,仅返回指定检查点 ID 之前的检查点。默认为 None。 |
None
|
limit
|
Optional[int]
|
返回的最大检查点数量。 |
None
|
生成
类型 | 描述 |
---|---|
CheckpointTuple
|
Iterator[CheckpointTuple]:匹配检查点元组的迭代器。 |
put ¶
put(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
将检查点保存到数据库。
此方法将检查点保存到 SQLite 数据库。检查点与提供的配置及其父配置(如果存在)相关联。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
与检查点关联的配置。 |
必需 |
checkpoint
|
Checkpoint
|
要保存的检查点。 |
必需 |
metadata
|
CheckpointMetadata
|
与检查点一起保存的附加元数据。 |
必需 |
new_versions
|
ChannelVersions
|
本次写入产生的新通道版本。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
RunnableConfig |
RunnableConfig
|
存储检查点后更新的配置。 |
aget_tuple async
¶
aget_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
从数据库异步获取检查点元组。
此方法根据提供的配置从 SQLite 数据库中检索检查点元组。如果配置包含 "checkpoint_id" 键,则检索具有匹配线程 ID 和检查点 ID 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
用于检索检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:检索到的检查点元组,如果未找到匹配的检查点,则为 None。 |
alist async
¶
alist(
config: Optional[RunnableConfig],
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None
) -> AsyncIterator[CheckpointTuple]
异步列出数据库中的检查点。
此方法根据提供的配置从 SQLite 数据库中检索检查点元组列表。检查点按检查点 ID 降序(最新优先)排序。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
Optional[RunnableConfig]
|
用于过滤检查点的基本配置。 |
必需 |
filter
|
Optional[dict[str, Any]]
|
元数据的附加过滤条件。 |
None
|
before
|
Optional[RunnableConfig]
|
如果提供,仅返回指定检查点 ID 之前的检查点。默认为 None。 |
None
|
limit
|
Optional[int]
|
返回的最大检查点数量。 |
None
|
生成
类型 | 描述 |
---|---|
AsyncIterator[CheckpointTuple]
|
AsyncIterator[CheckpointTuple]:匹配检查点元组的异步迭代器。 |
aput async
¶
aput(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
异步将检查点保存到数据库。
此方法将检查点保存到 SQLite 数据库。检查点与提供的配置及其父配置(如果存在)相关联。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
与检查点关联的配置。 |
必需 |
checkpoint
|
Checkpoint
|
要保存的检查点。 |
必需 |
metadata
|
CheckpointMetadata
|
与检查点一起保存的附加元数据。 |
必需 |
new_versions
|
ChannelVersions
|
本次写入产生的新通道版本。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
RunnableConfig |
RunnableConfig
|
存储检查点后更新的配置。 |
aput_writes async
¶
get_next_version ¶
get ¶
get(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
aget async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置异步获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
类
名称 | 描述 |
---|---|
PostgresSaver |
将检查点存储在 Postgres 数据库中的检查点器。 |
PostgresSaver ¶
基础:BasePostgresSaver
将检查点存储在 Postgres 数据库中的检查点器。
方法
名称 | 描述 |
---|---|
from_conn_string |
从连接字符串创建一个新的 PostgresSaver 实例。 |
setup |
异步设置检查点数据库。 |
list |
列出数据库中的检查点。 |
get_tuple |
从数据库中获取检查点元组。 |
put |
将检查点保存到数据库。 |
put_writes |
存储与检查点关联的中间写入。 |
delete_thread |
删除与线程 ID 关联的所有检查点和写入。 |
get |
使用给定配置获取检查点。 |
aget |
使用给定配置异步获取检查点。 |
aget_tuple |
使用给定配置异步获取检查点元组。 |
alist |
异步列出符合给定条件的检查点。 |
aput |
异步存储检查点及其配置和元数据。 |
aput_writes |
异步存储与检查点关联的中间写入。 |
adelete_thread |
删除与特定线程 ID 关联的所有检查点和写入。 |
属性
名称 | 类型 | 描述 |
---|---|---|
config_specs |
list[ConfigurableFieldSpec]
|
定义检查点保存器的配置选项。 |
config_specs 属性
¶
config_specs: list[ConfigurableFieldSpec]
from_conn_string 类方法
¶
from_conn_string(
conn_string: str, *, pipeline: bool = False
) -> Iterator[PostgresSaver]
从连接字符串创建一个新的 PostgresSaver 实例。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
conn_string
|
str
|
Postgres 连接信息字符串。 |
必需 |
pipeline
|
bool
|
是否使用 Pipeline |
False
|
返回
名称 | 类型 | 描述 |
---|---|---|
PostgresSaver |
Iterator[PostgresSaver]
|
一个新的 PostgresSaver 实例。 |
setup ¶
异步设置检查点数据库。
如果 Postgres 数据库中尚不存在必要的表,此方法会创建它们并运行数据库迁移。首次使用检查点器时,用户必须直接调用此方法。
list ¶
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
|
Optional[dict[str, Any]]
|
元数据的附加过滤条件。默认为 None。 |
None
|
before
|
Optional[RunnableConfig]
|
如果提供,仅返回指定检查点 ID 之前的检查点。默认为 None。 |
None
|
limit
|
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 ¶
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 ¶
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 ¶
get ¶
get(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
aget async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置异步获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
aget_tuple async
¶
aget_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
使用给定配置异步获取检查点元组。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:请求的检查点元组,如果未找到则为 None。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
alist async
¶
alist(
config: Optional[RunnableConfig],
*,
filter: Optional[Dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None
) -> AsyncIterator[CheckpointTuple]
异步列出符合给定条件的检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
Optional[RunnableConfig]
|
用于过滤检查点的基本配置。 |
必需 |
filter
|
Optional[Dict[str, Any]]
|
元数据的附加过滤条件。 |
None
|
before
|
Optional[RunnableConfig]
|
列出在此配置之前创建的检查点。 |
None
|
limit
|
Optional[int]
|
返回的最大检查点数量。 |
None
|
返回
类型 | 描述 |
---|---|
AsyncIterator[CheckpointTuple]
|
AsyncIterator[CheckpointTuple]:匹配检查点元组的异步迭代器。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
aput async
¶
aput(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
异步存储检查点及其配置和元数据。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
检查点的配置。 |
必需 |
checkpoint
|
Checkpoint
|
要存储的检查点。 |
必需 |
metadata
|
CheckpointMetadata
|
检查点的附加元数据。 |
必需 |
new_versions
|
ChannelVersions
|
本次写入产生的新通道版本。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
RunnableConfig |
RunnableConfig
|
存储检查点后更新的配置。 |
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
aput_writes async
¶
aput_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
|
str
|
创建写入的任务的路径。 |
''
|
抛出
类型 | 描述 |
---|---|
NotImplementedError
|
在您的自定义检查点保存器中实现此方法。 |
类
名称 | 描述 |
---|---|
AsyncPostgresSaver |
将检查点存储在 Postgres 数据库中的异步检查点器。 |
AsyncPostgresSaver ¶
基础:BasePostgresSaver
将检查点存储在 Postgres 数据库中的异步检查点器。
方法
名称 | 描述 |
---|---|
from_conn_string |
从连接字符串创建一个新的 AsyncPostgresSaver 实例。 |
setup |
异步设置检查点数据库。 |
alist |
异步列出数据库中的检查点。 |
aget_tuple |
从数据库异步获取检查点元组。 |
aput |
异步将检查点保存到数据库。 |
aput_writes |
异步存储链接到检查点的中间写入。 |
adelete_thread |
删除与线程 ID 关联的所有检查点和写入。 |
list |
列出数据库中的检查点。 |
get_tuple |
从数据库中获取检查点元组。 |
put |
将检查点保存到数据库。 |
put_writes |
存储与检查点关联的中间写入。 |
delete_thread |
删除与线程 ID 关联的所有检查点和写入。 |
get |
使用给定配置获取检查点。 |
aget |
使用给定配置异步获取检查点。 |
属性
名称 | 类型 | 描述 |
---|---|---|
config_specs |
list[ConfigurableFieldSpec]
|
定义检查点保存器的配置选项。 |
config_specs 属性
¶
config_specs: list[ConfigurableFieldSpec]
from_conn_string async
类方法
¶
from_conn_string(
conn_string: str,
*,
pipeline: bool = False,
serde: Optional[SerializerProtocol] = None
) -> AsyncIterator[AsyncPostgresSaver]
从连接字符串创建一个新的 AsyncPostgresSaver 实例。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
conn_string
|
str
|
Postgres 连接信息字符串。 |
必需 |
pipeline
|
bool
|
是否使用 AsyncPipeline |
False
|
返回
名称 | 类型 | 描述 |
---|---|---|
AsyncPostgresSaver |
AsyncIterator[AsyncPostgresSaver]
|
一个新的 AsyncPostgresSaver 实例。 |
setup async
¶
异步设置检查点数据库。
如果 Postgres 数据库中尚不存在必要的表,此方法会创建它们并运行数据库迁移。首次使用检查点器时,用户必须直接调用此方法。
alist async
¶
alist(
config: Optional[RunnableConfig],
*,
filter: Optional[dict[str, Any]] = None,
before: Optional[RunnableConfig] = None,
limit: Optional[int] = None
) -> AsyncIterator[CheckpointTuple]
异步列出数据库中的检查点。
此方法根据提供的配置从 Postgres 数据库中检索检查点元组列表。检查点按检查点 ID 降序(最新优先)排序。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
Optional[RunnableConfig]
|
用于过滤检查点的基本配置。 |
必需 |
filter
|
Optional[dict[str, Any]]
|
元数据的附加过滤条件。 |
None
|
before
|
Optional[RunnableConfig]
|
如果提供,仅返回指定检查点 ID 之前的检查点。默认为 None。 |
None
|
limit
|
Optional[int]
|
返回的最大检查点数量。 |
None
|
生成
类型 | 描述 |
---|---|
AsyncIterator[CheckpointTuple]
|
AsyncIterator[CheckpointTuple]:匹配检查点元组的异步迭代器。 |
aget_tuple async
¶
aget_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
从数据库异步获取检查点元组。
此方法根据提供的配置从 Postgres 数据库中检索检查点元组。如果配置包含 "checkpoint_id" 键,则检索具有匹配线程 ID 和 "checkpoint_id" 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
用于检索检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:检索到的检查点元组,如果未找到匹配的检查点,则为 None。 |
aput async
¶
aput(
config: RunnableConfig,
checkpoint: Checkpoint,
metadata: CheckpointMetadata,
new_versions: ChannelVersions,
) -> RunnableConfig
异步将检查点保存到数据库。
此方法将检查点保存到 Postgres 数据库。检查点与提供的配置及其父配置(如果存在)相关联。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
与检查点关联的配置。 |
必需 |
checkpoint
|
Checkpoint
|
要保存的检查点。 |
必需 |
metadata
|
CheckpointMetadata
|
与检查点一起保存的附加元数据。 |
必需 |
new_versions
|
ChannelVersions
|
本次写入产生的新通道版本。 |
必需 |
返回
名称 | 类型 | 描述 |
---|---|---|
RunnableConfig |
RunnableConfig
|
存储检查点后更新的配置。 |
aput_writes async
¶
list ¶
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
|
Optional[dict[str, Any]]
|
元数据的附加过滤条件。 |
None
|
before
|
Optional[RunnableConfig]
|
如果提供,仅返回指定检查点 ID 之前的检查点。默认为 None。 |
None
|
limit
|
Optional[int]
|
返回的最大检查点数量。 |
None
|
生成
类型 | 描述 |
---|---|
CheckpointTuple
|
Iterator[CheckpointTuple]:匹配检查点元组的迭代器。 |
get_tuple ¶
get_tuple(
config: RunnableConfig,
) -> Optional[CheckpointTuple]
从数据库中获取检查点元组。
此方法根据提供的配置从 Postgres 数据库中检索检查点元组。如果配置包含 "checkpoint_id" 键,则检索具有匹配线程 ID 和 "checkpoint_id" 的检查点。否则,检索给定线程 ID 的最新检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
用于检索检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[CheckpointTuple]
|
Optional[CheckpointTuple]:检索到的检查点元组,如果未找到匹配的检查点,则为 None。 |
put ¶
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 ¶
get ¶
get(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |
aget async
¶
aget(config: RunnableConfig) -> Optional[Checkpoint]
使用给定配置异步获取检查点。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
config
|
RunnableConfig
|
指定要检索哪个检查点的配置。 |
必需 |
返回
类型 | 描述 |
---|---|
Optional[Checkpoint]
|
Optional[Checkpoint]:请求的检查点,如果未找到则为 None。 |