Skip to main content
Version: 2.1.x

PostgreSQL checkpoints

If your application already uses PostgreSQL, use it to save conversations, approvals, and long-running workflows so they can resume from a pause. If you already store checkpoints, follow the PostgreSQL migration guide first.

Configure the saver​

After initializing the schema, start the application with CREATE_NONE. Use the same DataSource for the saver, the blob store, and any parent and child graphs you need to fork together. Connect all application instances to the same schema and keep the serializer and blob namespace consistent.

import com.alibaba.cloud.ai.graph.checkpoint.savers.postgresql.CreateOption;
import com.alibaba.cloud.ai.graph.checkpoint.savers.postgresql.PostgresCheckpointBlobStore;
import com.alibaba.cloud.ai.graph.checkpoint.savers.postgresql.PostgresSaver;

var saver = PostgresSaver.builder()
.datasource(dataSource)
.createOption(CreateOption.CREATE_NONE)
.build();
var blobs = new PostgresCheckpointBlobStore(dataSource, "support-workflow");

Register saver with the graph to save and resume progress. To reduce the cost of saving large state, follow the large state guide to attach blobs and select keys. To collect recovery and collection metrics, pass the application's MeterRegistry when constructing the blob store.

Resume approvals and branch from history​

  • Use graph.getPendingInterruption(thread) to find items awaiting approval. If several parallel steps are waiting, pick the item from pendingInterruptions() and submit its feedback with RunnableConfig.Builder.addHumanFeedback(...).
  • To start a branch from history, select a snapshot with getStateHistory(thread), call forkState(snapshot.config(), edits, null), and continue with the returned configuration. Page through longer histories with getStateHistoryPage(config, null, pageSize).
  • Resupply temporary request context as input when resuming, for example graph.invoke(Map.of("requestContext", recreated), thread.withResume()). Completed tasks stay complete when the application supplies new temporary values.

Spring AI Alibaba 开源项目基于 Spring AI 构建,是阿里云通义系列模型及服务在 Java AI 应用开发领域的最佳实践,提供高层次的 AI API 抽象与云原生基础设施集成方案,帮助开发者快速构建 AI 应用。

浙ICP备2021005855号-33