Microsoft Agent Framework 工作流 - 状态隔离

在实际应用程序中,处理多个任务或请求时,正确管理状态至关重要。 如果不进行适当的隔离,不同工作流执行之间的共享状态可能会导致意外行为、数据损坏和争用情况。 本文介绍如何确保Microsoft代理框架工作流中的状态隔离,从而深入了解最佳做法和常见陷阱。

可变工作流生成器与不可变工作流

工作流由工作流生成器创建。 工作流生成器通常被视为可变的,可以在创建生成器后或在生成工作流后添加、修改启动执行程序或其他配置。 另一方面,工作流在生成工作流后是不可变的(不能修改工作流的公共 API)。

这一区别很重要,因为它会影响在不同工作流执行之间管理状态的方式。 不建议对多个任务或请求重复使用单个工作流实例,因为这可能会导致意外的状态共享。 相反,建议为每个任务或请求从生成器创建新的工作流实例,以确保适当的状态隔离和线程安全。

确保工作流生成器中的状态隔离

将执行程序实例直接传递给工作流生成器时,该执行程序实例在从生成器创建的所有工作流实例之间共享。 如果执行程序实例包含不应在多个工作流执行之间共享的状态,则可能会导致问题。 为了确保适当的状态隔离和线程安全性,建议使用工厂函数为每个工作流实例创建新的执行程序实例。

即将推出……

非线程安全示例:

executor_a = CustomExecutorA()
executor_b = CustomExecutorB()

workflow_builder = WorkflowBuilder()
# executor_a and executor_b are passed directly to the workflow builder
workflow_builder.add_edge(executor_a, executor_b)
workflow_builder.set_start_executor(executor_b)

# All workflow instances created from the builder will share the same executor instances
workflow_a = workflow_builder.build()
workflow_b = workflow_builder.build()

线程安全示例:

workflow_builder = WorkflowBuilder()
# Register executor factory functions with the workflow builder
workflow_builder.register_executor(factory_func=CustomExecutorA, name="executor_a")
workflow_builder.register_executor(factory_func=CustomExecutorB, name="executor_b")
# Add edges using registered factory function names
workflow_builder.add_edge("executor_a", "executor_b")
workflow_builder.set_start_executor("executor_b")

# Each workflow instance created from the builder will have its own executor instances
workflow_a = workflow_builder.build()
workflow_b = workflow_builder.build()

小窍门

若要确保适当的状态隔离和线程安全性,还请确保由工厂函数创建的执行程序实例不会共享可变状态。

代理状态管理

代理上下文通过代理线程进行管理。 默认情况下,工作流中的每个代理程序都将获得自己的线程,除非代理程序由自定义执行程序管理。 有关详细信息,请参阅 使用代理

代理线程在工作流运行中持久化。 这意味着,如果在工作流的第一次运行中调用代理,则代理生成的内容将在同一工作流实例的后续运行中可用。 尽管这对于在单个任务中保持连续性非常有用,但如果对不同的任务或请求重复使用同一工作流实例,也可能导致意外的状态共享。 若要确保每个任务都具有隔离的代理状态,请使用工作流生成器中的代理工厂函数为每个任务或请求创建新的工作流实例。

即将推出……

非线程安全示例:

writer_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions=(
        "You are an excellent content writer. You create new content and edit contents based on the feedback."
    ),
    name="writer_agent",
)
reviewer_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions=(
        "You are an excellent content reviewer."
        "Provide actionable feedback to the writer about the provided content."
        "Provide the feedback in the most concise manner possible."
    ),
    name="reviewer_agent",
)

builder = WorkflowBuilder()
# writer_agent and reviewer_agent are passed directly to the workflow builder
builder.add_edge(writer_agent, reviewer_agent)
builder.set_start_executor(writer_agent)

# All workflow instances created from the builder will share the same agent
# instances and agent threads
workflow = builder.build()

线程安全示例:

def create_writer_agent() -> ChatAgent:
    """Factory function to create a Writer agent."""
    return AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
        instructions=(
            "You are an excellent content writer. You create new content and edit contents based on the feedback."
        ),
        name="writer_agent",
    )

def create_reviewer_agent() -> ChatAgent:
    """Factory function to create a Reviewer agent."""
    return AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
        instructions=(
            "You are an excellent content reviewer."
            "Provide actionable feedback to the writer about the provided content."
            "Provide the feedback in the most concise manner possible."
        ),
        name="reviewer_agent",
    )

builder = WorkflowBuilder()
# Register agent factory functions with the workflow builder
builder.register_agent(factory_func=create_writer_agent, name="writer_agent")
builder.register_agent(factory_func=create_reviewer_agent, name="reviewer_agent")
# Add edges using registered factory function names
builder.add_edge("writer_agent", "reviewer_agent")
builder.set_start_executor("writer_agent")

# Each workflow instance created from the builder will have its own agent
# instances and agent threads
workflow = builder.build()

结论

Microsoft Agent Framework 工作流中的状态隔离可以通过结合使用工厂函数和工作流生成器来创建新的执行器和代理实例来有效管理。 通过为每个任务或请求创建新的工作流实例,可以保持适当的状态隔离,并避免不同工作流执行之间的意外状态共享。

后续步骤