固定顺序调用
from openprogram import Agent
from openprogram.runtime.agent import llm
class ExampleAgent(Agent):
method_options = {
'survey_pipeline': {'tool': True, 'input': {'task': {'description': 'Research topic.'}}},
}
def survey_pipeline(self, task: str) -> dict:
"""Run the full research pipeline: survey, find gaps, generate ideas."""
survey = survey_topic(topic=task)
gaps = identify_gaps(survey=survey)
ideas = generate_ideas(gaps=gaps)
return {"survey": survey, "gaps": gaps, "ideas": ideas}
# Or summarise with one model call instead:
# return llm([{"type": "text", "text": f"Survey:\n{survey}\n\nGaps:\n{gaps}\n\nIdeas:\n{ideas}"}])
_example_agent = ExampleAgent()
survey_pipeline = _example_agent.survey_pipeline
调研、找缺口、出想法。也适用于:起草、评审、修改;采集、清洗、分析。
| 要点 | 规则 |
|---|---|
llm() | 可选,一个函数里可调用多次,并可混用任意多个 Agent 方法调用 |
| 数据 | 普通 Python 变量:一步的返回值就是下一步的参数 |
| Runtime | 子方法运行在调用者的 Runtime 上,步骤之间不传 runtime |
步骤之间插入 Python
survey = survey_topic(topic=task)
# plain Python processing in between
key_points = extract_key_points(survey)
filtered = [p for p in key_points if p["relevance"] > 0.5]
gaps = identify_gaps(survey="\n".join(filtered))
会话 DAG
| 调用 | 记录方式 |
|---|---|
| 启动这次执行的方法 | 一个节点 |
每次 llm() 或模型请求 | 一个子节点,caller 指向该方法 |
| 同一 Agent 上的帮助函数或方法 | 没有自己的节点,共用当前调用 |
| 另一个 Agent | 自己的 DAG;父级保存结果和 child_session_id 引用 |
错误处理
try:
survey = survey_topic(topic=task)
except Exception as e:
return {"error": f"Survey failed: {e}"}
抛出的异常会到达编排函数;被记录的调用,其节点以 status='error' 结束。
子函数返回错误字符串时
survey = survey_topic(topic=task)
if not survey or "error" in survey.lower():
return {"error": "Survey failed", "survey": survey}
与由 LLM 选择的调用对比
| 固定顺序 | 工具调用或决策 | |
|---|---|---|
| 顺序由谁决定 | Python | LLM |
| 运行多少子函数 | 全部 | 工具循环:多轮、多个。决策菜单:一个。 |
| 灵活性 | 固定流水线 | 随任务变化 |
Last updated · 2026-11-25