실제 애플리케이션에서는 여러 작업 또는 요청을 처리할 때 상태를 적절하게 관리하는 것이 중요합니다. 적절한 격리가 없으면 서로 다른 워크플로 실행 간의 공유 상태가 예기치 않은 동작, 데이터 손상 및 경합 상태로 이어질 수 있습니다. 이 문서에서는 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 에이전트 프레임워크 워크플로의 상태 격리는 워크플로 작성기와 함께 팩터리 함수를 사용하여 새 실행기 및 에이전트 인스턴스를 만들어 효과적으로 관리할 수 있습니다. 각 작업 또는 요청에 대한 새 워크플로 인스턴스를 만들어 적절한 상태 격리를 유지하고 여러 워크플로 실행 간에 의도하지 않은 상태 공유를 방지할 수 있습니다.