流程控制与并行工作
这篇教程介绍构建更大 agent 模式前最常用的流程控制能力:
if:分支for:顺序处理parallel for:并发处理互相独立的任务loop until:有上限的重试或迭代
示例是一个小型批处理 coordinator:把 urgent 和 regular item 分开,对每个 item 独立分类,检查这批结果是否足够,然后生成总结。
完整源码:../../../tutorials/control-flow.as
1. 完整程序
创建 control-flow.as,或者直接打开仓库里的 tutorials/control-flow.as:
import llm Qwen from "ollama://localhost:11434/qwen3.6"
main agent ControlFlowExample {
model Qwen
role "Batch coordinator"
description "Classify a small batch, process independent items in parallel, and summarize the result."
main func(input {
goal: string
items: list[json]
}) {
urgent = []
regular = []
for item in input.items max 10 {
if item.urgent {
urgent.add(item)
} else {
regular.add(item)
}
}
classified = parallel for item in input.items max 5 {
classify(input.goal, item)
}
ready = false
attempts = 0
verdict = {
ready: false,
note: "not checked yet"
}
loop until ready max 2 {
attempts += 1
verdict = check(input.goal, classified, attempts)
ready = verdict.ready
}
finish(input.goal, urgent, regular, classified, verdict, attempts)
}
func classify(goal, item) {
use goal as "batch goal"
use item as "item"
generate({ input: "Classify this item for the batch goal", max_output: 300 }) -> {
label
reason
}
}
func check(goal, classified, attempts) {
use goal as "batch goal"
use classified.summary max 2k as "classified items"
use attempts as "attempt"
generate({ input: "Decide whether the batch is ready to summarize", max_output: 300 }) -> {
ready: boolean
note
}
}
func finish(goal, urgent, regular, classified, verdict, attempts) {
use goal as "batch goal"
use urgent.summary max 1k as "urgent items"
use regular.summary max 1k as "regular items"
use classified.summary max 2k as "classified items"
use verdict as "readiness verdict"
use attempts as "attempts"
generate({ input: "Summarize the batch result", max_output: 600 }) -> {
summary
urgent_count: number
regular_count: number
ready: boolean
}
}
}