把大任务拆成子任务有两种活法:固定链式(步骤预先写死)和动态自适应(模型运行时决定子任务)。同一节课跑两遍,看清「按工作性质选模式」。
本演示为非官方原创练习内容,依据公开考纲编写,非真实考题。
# 模式一:固定链式 (steps hard-coded ahead of time)def fixed_chain(raw_invoice: str) -> str: # Step 1 — 抽取字段(确定性,结构永远一样) fields = one_shot( "Extract sender, amount, due_date as JSON:", raw_invoice, ) # Step 2 — 校验金额(纯规则,不需要模型自由发挥) checked = one_shot("Validate the amount field:", fields) # Step 3 — 渲染为标准模板 return one_shot("Render this into the invoice template:", checked) def one_shot(instruction: str, payload: str) -> str: resp = client.messages.create( model="claude-sonnet-4-6", messages=[{"role": "user", "content": f"{instruction}\n{payload}"}], ) return resp.content[0].text # 模式二:动态自适应分解 (model picks sub-tasks at runtime)def dynamic_decompose(goal: str) -> str: messages = [{"role": "user", "content": goal}] while True: resp = client.messages.create( model="claude-sonnet-4-6", tools=tools, # plan_steps / web_search / draft_section messages=messages, ) messages.append({"role": "assistant", "content": resp.content}) if resp.stop_reason == "end_turn": break results = [] for block in resp.content: if block.type == "tool_use": out = run_tool(block.name, block.input) results.append({ "type": "tool_result", "tool_use_id": block.id, "content": out, }) messages.append({"role": "user", "content": results}) return messages[-1]["content"][0].text把这张发票转成标准模板(左:确定性工作)。
Step 1 抽取(链上写死的一步):sender=Acme Co. · amount=1280.00 · due_date=2026-07-15。
本步未发生工具调用。
左边的工作是确定性的:发票字段永远是发件人、金额、到期日。所以三步直接写进代码——抽取、校验、渲染。模型不需要「想」流程,每一步只做一件被指定好的事。能写死就别让模型即兴发挥。
固定链式 · 写死的步骤 · 3 tools