LangGraph 检查点保存器,使用 Postgres 实例作为后端存储。内部使用 node-postgres 包连接到 Postgres 实例。

示例

import { ChatOpenAI } from "@langchain/openai";
import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
import { createReactAgent } from "@langchain/langgraph/prebuilt";

const checkpointer = PostgresSaver.fromConnString(
"postgresql://user:password@localhost:5432/db",
// optional configuration object
{
schema: "custom_schema" // defaults to "public"
}
);

// NOTE: you need to call .setup() the first time you're using your checkpointer
await checkpointer.setup();

const graph = createReactAgent({
tools: [getWeather],
llm: new ChatOpenAI({
model: "gpt-4o-mini",
}),
checkpointSaver: checkpointer,
});
const config = { configurable: { thread_id: "1" } };

await graph.invoke({
messages: [{
role: "user",
content: "what's the weather in sf"
}],
}, config);

继承关系 (查看完整)

构造函数

属性

isSetup: boolean

方法

  • 参数

    • threadId: string
    • checkpointNs: string
    • values: Record<string, unknown>
    • versions: ChannelVersions

    返回 [string, string, string, string, string, undefined | Uint8Array][]

  • 参数

    返回 Record<string, unknown>

  • 参数

    返回 any

  • 参数

    • threadId: string
    • checkpointNs: string
    • checkpointId: string
    • taskId: string
    • writes: [string, unknown][]

    返回 [string, string, string, string, number, string, string, Uint8Array][]

  • 参数

    • blobValues: [Uint8Array, Uint8Array, Uint8Array][]

    返回 Promise<Record<string, unknown>>

  • 参数

    • checkpoint: Omit<Checkpoint<string, string>, "channel_values" | "pending_sends">
    • channelValues: [Uint8Array, Uint8Array, Uint8Array][]
    • pendingSends: [Uint8Array, Uint8Array][]

    返回 Promise<Checkpoint<string, string>>

  • 参数

    • metadata: Record<string, unknown>

    返回 Promise<any>

  • 参数

    • writes: [Uint8Array, Uint8Array, Uint8Array, Uint8Array][]

    返回 Promise<[string, string, unknown][]>

  • 返回给定 list() config, filter, cursor 的 WHERE 子句谓词。

    此方法返回一个包含字符串和值元组的元组。字符串是参数化的 WHERE 子句谓词(包含 WHERE 关键字):"WHERE column1 = $1 AND column2 IS $2"。值列表包含每个对应参数的值。

    参数

    • 可选 config: RunnableConfig<Record<string, any>>
    • 可选 filter: Record<string, unknown>
    • 可选 before: RunnableConfig<Record<string, any>>

    返回 [string, unknown[]]

  • 返回 Promise<void>

  • 参数

    • config: RunnableConfig<Record<string, any>>

    返回 Promise<undefined | Checkpoint<string, string>>

  • 生成通道的下一个版本 ID。

    默认使用整数版本,每次增加 1。如果覆盖此方法,可以使用字符串/整数/浮点数版本,只要它们是单调递增的即可。

    参数

    • current: undefined | number
    • _channel: ChannelProtocol<unknown, unknown, unknown>

    返回 number

  • 从数据库获取检查点元组。此方法根据提供的 config 从 Postgres 数据库检索检查点元组。如果 config 的 configurable 字段包含 "checkpoint_id" 键,则检索具有匹配 thread_id 和 namespace 的检查点。否则,检索给定 thread_id 的最新检查点。

    参数

    • config: RunnableConfig<Record<string, any>>

      用于检索检查点的 config。

    返回 Promise<undefined | CheckpointTuple>

    检索到的检查点元组,如果不存在则为 undefined。

  • 列出数据库中的检查点。

    此方法根据提供的 config 从 Postgres 数据库检索检查点元组列表。检查点按检查点 ID 降序排列(最新的在前)。

    参数

    返回 AsyncGenerator<CheckpointTuple, any, unknown>

  • 将检查点保存到数据库。

    此方法将检查点保存到 Postgres 数据库。该检查点与提供的 config 及其父级 config(如果存在)相关联。

    参数

    返回 Promise<RunnableConfig<Record<string, any>>>

  • 存储与检查点相关的中间写入。

    此方法将与检查点相关的中间写入保存到 Postgres 数据库。

    参数

    • config: RunnableConfig<Record<string, any>>

      相关检查点的配置。

    • writes: PendingWrite<string>[]

      要存储的写入列表。

    • taskId: string

      创建写入的任务标识符。

    返回 Promise<void>

  • 异步设置检查点数据库。

    此方法在 Postgres 数据库中创建必要的表(如果不存在)并运行数据库迁移。用户首次使用 checkpointer 时,必须直接调用此方法。

    返回 Promise<void>

  • 从连接字符串创建 PostgresSaver 的新实例。

    参数

    • connString: string

      用于连接到 Postgres 数据库的连接字符串。

    • 可选 options: Partial<PostgresSaverOptions>

      可选的配置对象。

    返回 PostgresSaver

    PostgresSaver 的新实例。

    示例

    const connString = "postgresql://user:password@localhost:5432/db";
    const checkpointer = PostgresSaver.fromConnString(connString, {
    schema: "custom_schema" // defaults to "public"
    });
    await checkpointer.setup();