检查点器¶
类
名称 | 描述 |
---|---|
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。此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代理在多次交互中和跨多次交互持久化其状态。
属性
名称 | 类型 | 描述 |
---|---|---|
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 |
对象序列化和反序列化的协议。 |
CipherProtocol |
数据加密和解密的协议。 |
类
名称 | 描述 |
---|---|
EncryptedSerializer |
使用加密协议加密和解密数据的序列化器。 |
EncryptedSerializer ¶
使用加密协议加密和解密数据的序列化器。
方法
名称 | 描述 |
---|---|
dumps_typed |
将对象序列化为元组 (type, bytes) 并加密字节。 |
from_pycryptodome_aes |
使用AES加密创建EncryptedSerializer。 |
from_pycryptodome_aes classmethod
¶
from_pycryptodome_aes(
serde: SerializerProtocol = JsonPlusSerializer(),
**kwargs: Any
) -> EncryptedSerializer
使用AES加密创建EncryptedSerializer。
类
名称 | 描述 |
---|---|
InMemorySaver |
一个内存中的检查点保存器。 |
PersistentDict |
具有与shelve和anydbm兼容的API的持久化字典。 |
InMemorySaver ¶
基类: BaseCheckpointSaver[str]
, AbstractContextManager
, AbstractAsyncContextManager
一个内存中的检查点保存器。
此检查点保存器使用defaultdict在内存中存储检查点。
注意
仅将InMemorySaver
用于调试或测试目的。对于生产用例,我们建议安装langgraph-checkpoint-postgres并使用PostgresSaver
/ AsyncPostgresSaver
。
如果您正在使用LangGraph平台,则无需指定检查点器。将自动使用正确的托管检查点器。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
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的快速模式)。
输入文件格式自动识别。输出文件格式可在pickle、json和csv之间选择。所有三种序列化格式都由快速C语言实现支持。
改编自 https://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/
方法
名称 | 描述 |
---|---|
sync |
将字典写入磁盘 |
模块
名称 | 描述 |
---|---|
aio |
|
utils |
|
类
名称 | 描述 |
---|---|
SqliteSaver |
一个将检查点存储在SQLite数据库中的检查点保存器。 |
SqliteSaver ¶
基类: BaseCheckpointSaver[str]
一个将检查点存储在SQLite数据库中的检查点保存器。
注意
此类别适用于轻量级、同步用例(演示和小型项目),并且不适用于多线程。对于支持async
的类似sqlite保存器,请考虑使用AsyncSqliteSaver。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
conn
|
Connection
|
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 property
¶
config_specs: list[ConfigurableFieldSpec]
from_conn_string classmethod
¶
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 ¶
基类: BaseCheckpointSaver[str]
一个将检查点存储在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
>>>
>>> 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 property
¶
config_specs: list[ConfigurableFieldSpec]
from_conn_string async
classmethod
¶
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 property
¶
config_specs: list[ConfigurableFieldSpec]
from_conn_string classmethod
¶
from_conn_string(
conn_string: str, *, pipeline: bool = False
) -> Iterator[PostgresSaver]
从连接字符串创建新的PostgresSaver实例。
参数
名称 | 类型 | 描述 | 默认 |
---|---|---|---|
conn_string
|
str
|
Postgres连接信息字符串。 |
必需 |
pipeline
|
bool
|
是否使用Pipeline |
False
|
返回
名称 | 类型 | 描述 |
---|---|---|
PostgresSaver |
Iterator[PostgresSaver]
|
一个新的PostgresSaver实例。 |
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 property
¶
config_specs: list[ConfigurableFieldSpec]
from_conn_string async
classmethod
¶
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。 |