워크플로를 사용하면 여러 단계를 함께 연결할 수 있습니다. 각 단계는 데이터를 처리하고 다음 단계로 전달합니다.
워크플로 단계 정의(실행기):
using Microsoft.Agents.AI.Workflows;
// Step 1: Convert text to uppercase
class UpperCase : Executor
{
[Handler]
public async Task ToUpperCase(string text, WorkflowContext<string> ctx)
{
await ctx.SendMessageAsync(text.ToUpper());
}
}
// Step 2: Reverse the string and yield output
[Executor(Id = "reverse_text")]
static async Task ReverseText(string text, WorkflowContext<Never, string> ctx)
{
var reversed = new string(text.Reverse().ToArray());
await ctx.YieldOutputAsync(reversed);
}
워크플로를 빌드하고 실행합니다.
var upper = new UpperCase();
var workflow = new AgentWorkflowBuilder(startExecutor: upper)
.AddEdge(upper, ReverseText)
.Build();
var result = await workflow.RunAsync("hello world");
Console.WriteLine($"Output: {string.Join(", ", result.GetOutputs())}");
// Output: DLROW OLLEH
팁 (조언)
전체 실행 가능한 파일에 대한 전체 샘플을 참조하세요.
워크플로 단계(실행기)를 정의하고 에지와 연결합니다.
# Step 1: A class-based executor that converts text to uppercase
class UpperCase(Executor):
def __init__(self, id: str):
super().__init__(id=id)
@handler
async def to_upper_case(self, text: str, ctx: WorkflowContext[str]) -> None:
"""Convert input to uppercase and forward to the next node."""
await ctx.send_message(text.upper())
# Step 2: A function-based executor that reverses the string and yields output
@executor(id="reverse_text")
async def reverse_text(text: str, ctx: WorkflowContext[Never, str]) -> None:
"""Reverse the string and yield the final workflow output."""
await ctx.yield_output(text[::-1])
def create_workflow():
"""Build the workflow: UpperCase → reverse_text."""
upper = UpperCase(id="upper_case")
return (
WorkflowBuilder(start_executor=upper)
.add_edge(upper, reverse_text)
.build()
)
워크플로를 빌드하고 실행합니다.
workflow = create_workflow()
events = await workflow.run("hello world")
print(f"Output: {events.get_outputs()}")
print(f"Final state: {events.get_final_state()}")
팁 (조언)
전체 실행 가능한 파일에 대한 전체 샘플을 참조하세요.
다음 단계:
더 자세히 살펴보기:
- 워크플로 개요 - 워크플로 아키텍처 이해
- 순차 워크플로 - 선형 단계별 패턴
- 워크플로의 에이전트 - 에이전트를 워크플로 단계로 사용