共用方式為


Microsoft Agent Framework 工作流程 - 檢查點

本頁提供 Microsoft Agent Framework 工作流程系統中 檢查點 的概觀。

概觀

檢查點可讓您在工作流程執行期間的特定點儲存工作流程的狀態,並在稍後從這些點繼續。 此功能對於下列案例特別有用:

  • 長時間執行的工作流程,您希望於失敗時避免遺失進度。
  • 長時間執行的工作流程,您希望在之後的時間點暫停並恢復執行。
  • 需要定期儲存狀態以進行稽核或合規目的的工作流程。
  • 需要移轉跨不同環境或執行個體的工作流程。

檢查點何時建立?

請記住,工作流程是以 超級步驟執行,如 核心概念中所述。 在每個超級步驟中的所有執行程式完成其執行之後,都會在每個超級步驟的結尾建立檢查點。 檢查點會擷取工作流程的整個狀態,包括:

  • 所有執行程式的目前狀態
  • 工作流程中下一個超級步驟的所有待處理訊息
  • 擱置中的請求和回應
  • 共用狀態

佔領檢查點

若要啟用檢查點,必須在建立工作流程執行時提供 CheckpointManager。 然後可以透過 SuperStepCompletedEvent存取檢查點。

using Microsoft.Agents.Workflows;

// Create a checkpoint manager to manage checkpoints
var checkpointManager = new CheckpointManager();
// List to store checkpoint info for later use
var checkpoints = new List<CheckpointInfo>();

// Run the workflow with checkpointing enabled
Checkpointed<StreamingRun> checkpointedRun = await InProcessExecution
    .StreamAsync(workflow, input, checkpointManager)
    .ConfigureAwait(false);
await foreach (WorkflowEvent evt in checkpointedRun.Run.WatchStreamAsync().ConfigureAwait(false))
{
    if (evt is SuperStepCompletedEvent superStepCompletedEvt)
    {
        // Access the checkpoint and store it
        CheckpointInfo? checkpoint = superStepCompletedEvt.CompletionInfo!.Checkpoint;
        if (checkpoint != null)
        {
            checkpoints.Add(checkpoint);
        }
    }
}

若要啟用檢查點,則在建立工作流程時需要提供CheckpointStorage。 然後可以通過儲存裝置存取檢查點。

from agent_framework import (
    InMemoryCheckpointStorage,
    WorkflowBuilder,
)

# Create a checkpoint storage to manage checkpoints
# There are different implementations of CheckpointStorage, such as InMemoryCheckpointStorage and FileCheckpointStorage.
checkpoint_storage = InMemoryCheckpointStorage()

# Build a workflow with checkpointing enabled
builder = WorkflowBuilder()
builder.set_start_executor(start_executor)
builder.add_edge(start_executor, executor_b)
builder.add_edge(executor_b, executor_c)
builder.add_edge(executor_b, end_executor)
workflow = builder.with_checkpointing(checkpoint_storage).build()

# Run the workflow
async for event in workflow.run_streaming(input):
    ...

# Access checkpoints from the storage
checkpoints = await checkpoint_storage.list_checkpoints()

從檢查點繼續

您可以直接在同一次執行中從特定檢查點繼續工作程序。

// Assume we want to resume from the 6th checkpoint
CheckpointInfo savedCheckpoint = checkpoints[5];
// Note that we are restoring the state directly to the same run instance.
await checkpointedRun.RestoreCheckpointAsync(savedCheckpoint, CancellationToken.None).ConfigureAwait(false);
await foreach (WorkflowEvent evt in checkpointedRun.Run.WatchStreamAsync().ConfigureAwait(false))
{
    if (evt is WorkflowOutputEvent workflowOutputEvt)
    {
        Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
    }
}

您可以在相同的工作流程執行個體上從特定檢查點接續執行該工作流程。

# Assume we want to resume from the 6th checkpoint
saved_checkpoint = checkpoints[5]
async for event in workflow.run_stream(checkpoint_id=saved_checkpoint.checkpoint_id):
    ...

從檢查站補水

或者,您可以將工作流程從檢查點重新激活到新的運行實例。

// Assume we want to resume from the 6th checkpoint
CheckpointInfo savedCheckpoint = checkpoints[5];
Checkpointed<StreamingRun> newCheckpointedRun = await InProcessExecution
    .ResumeStreamAsync(newWorkflow, savedCheckpoint, checkpointManager)
    .ConfigureAwait(false);
await foreach (WorkflowEvent evt in newCheckpointedRun.Run.WatchStreamAsync().ConfigureAwait(false))
{
    if (evt is WorkflowOutputEvent workflowOutputEvt)
    {
        Console.WriteLine($"Workflow completed with result: {workflowOutputEvt.Data}");
    }
}

或者,您可以從檢查點重新活化新的工作流程執行個體。

from agent_framework import WorkflowBuilder

builder = WorkflowBuilder()
builder.set_start_executor(start_executor)
builder.add_edge(start_executor, executor_b)
builder.add_edge(executor_b, executor_c)
builder.add_edge(executor_b, end_executor)
# This workflow instance doesn't require checkpointing enabled.
workflow = builder.build()

# Assume we want to resume from the 6th checkpoint
saved_checkpoint = checkpoints[5]
async for event in workflow.run_stream
    checkpoint_id=saved_checkpoint.checkpoint_id,
    checkpoint_storage=checkpoint_storage,
):
    ...

儲存執行程式狀態

若要確保在檢查點中擷取執行程式的狀態,執行程式必須覆寫 OnCheckpointingAsync 方法,並將其狀態儲存至工作流程內容。

using Microsoft.Agents.Workflows;
using Microsoft.Agents.Workflows.Reflection;

internal sealed class CustomExecutor() : Executor<string>("CustomExecutor")
{
    private const string StateKey = "CustomExecutorState";

    private List<string> messages = new();

    public async ValueTask HandleAsync(string message, IWorkflowContext context)
    {
        this.messages.Add(message);
        // Executor logic...
    }

    protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default)
    {
        return context.QueueStateUpdateAsync(StateKey, this.messages);
    }
}

此外,若要確保從檢查點繼續時正確還原狀態,執行程式必須覆寫 OnCheckpointRestoredAsync 方法,並從工作流程內容載入其狀態。

protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default)
{
    this.messages = await context.ReadStateAsync<List<string>>(StateKey).ConfigureAwait(false);
}

若要確保在檢查點中擷取執行程式的狀態,執行程式必須覆寫 on_checkpoint_save 方法,並將其狀態儲存至工作流程內容。

class CustomExecutor(Executor):
    def __init__(self, id: str) -> None:
        super().__init__(id=id)
        self._messages: list[str] = []

    @handler
    async def handle(self, message: str, ctx: WorkflowContext):
        self._messages.append(message)
        # Executor logic...

    async def on_checkpoint_save(self) -> dict[str, Any]:
        return {"messages": self._messages}

此外,若要確保從檢查點繼續時正確還原狀態,執行程式必須覆寫 on_checkpoint_restore 方法,並從工作流程內容載入其狀態。

async def on_checkpoint_restore(self, state: dict[str, Any]) -> None:
    self._messages = state.get("messages", [])

後續步驟