构建你的第一个 AI Agent 工作流:实用指南(无需框架)
Source: Dev.to
Everyone’s talking about AI agents. LangChain, CrewAI, AutoGen — the frameworks keep multiplying. But here’s something I’ve learned after months of building with them:
You don’t need a framework to build useful AI agent workflows.
In fact, starting with a framework before understanding the core pattern is why most people’s agents are just expensive, slow wrappers around a single API call.
Let me show you how to build a genuinely useful multi‑step AI workflow using nothing but structured prompts and basic scripting.
我们正在构建的内容
一个内容处理流水线,具备以下功能:
- 接收原始笔记/要点作为输入
- 生成结构化摘要
- 提取行动项
- 起草后续邮件
- 根据内容创建社交媒体帖子
这是一套我每周使用的真实工作流。每次可为我节省约 3 小时。
核心模式:提示链
“每个人都把‘代理’模式弄得过于复杂,实际上它很简单:”
Input → Prompt A → Output A → Prompt B → Output B → …
每一步都接收前一步的输出并对其进行转换。就是这样。这就是所有代理框架背后的模式——它们只是在此基础上添加错误处理、记忆和工具使用。
让我们来构建它。
第一步:摘要器
import openai
def summarize(raw_notes: str) -> str:
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": """You are a concise business writer.
Take raw notes and produce a structured summary with:
- Key points (bullet list)
- Decisions made
- Open questions
Keep it under 200 words."""
},
{
"role": "user",
"content": f"Summarize these notes:\n\n{raw_notes}"
}
]
)
return response.choices[0].message.content
一个聚焦的系统提示,约束输出格式。
第2步:行动项提取器
def extract_actions(summary: str) -> str:
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": """Extract action items from this summary.
Format each as:
- [ ] [ACTION] — Owner: [person] — Due: [date if mentioned]
Only include concrete, actionable tasks.
Ignore vague items like 'think about X'."""
},
{
"role": "user",
"content": summary
}
]
)
return response.choices[0].message.content
请注意系统提示对输出格式的具体要求。这一点至关重要——模糊的提示会产生模糊的结果。
第3步:邮件草稿
def draft_email(summary: str, actions: str) -> str:
response = openai.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "system",
"content": """Draft a follow‑up email based on the meeting
summary and action items. Tone: professional but not stiff.
Structure: brief recap, action items with owners, next steps.
Keep under 150 words."""
},
{
"role": "user",
"content": f"Summary:\n{summary}\n\nAction Items:\n{actions}"
}
]
)
return response.choices[0].message.content
第4步:将其串联起来
def process_notes(raw_notes: str) -> dict:
# Step 1: Summarize
summary = summarize(raw_notes)
# Step 2: Extract actions
actions = extract_actions(summary)
# Step 3: Draft follow‑up
email = draft_email(summary, actions)
return {
"summary": summary,
"actions": actions,
"email": email
}
这就是你的“agent”。四个函数,零框架,完全易懂。
为什么这比框架更好(暂时)
- 可调试 – 当出现问题时,你可以准确知道是哪一步失败,并检查中间输出。
- 低成本 – 在大多数步骤中使用
gpt-4o-mini,每次运行的费用仅为几分钱的几分之一。框架通常默认使用昂贵的模型。 - 快速 – 没有框架开销,没有向量数据库查询,没有不必要的复杂性。整个过程在几秒内完成。
- 可修改 – 想添加一步?写一个新函数。想改变邮件语气?编辑一个系统提示。无需四处寻找文档。
让它准备好投入生产
我随着时间添加的一些内容:
import json
import logging
def safe_step(func, input_data, step_name):
"""Wrapper with logging and error handling."""
try:
logging.info(f"Starting: {step_name}")
result = func(input_data)
logging.info(f"Completed: {step_name} ({len(result)} chars)")
return result
except Exception as e:
logging.error(f"Failed: {step_name} — {e}")
return None
添加重试逻辑、输出验证,你就拥有了比大多数“代理”演示更稳健的东西。
提示工程要点
这里真正的技能不是 Python,而是编写优秀的系统提示。此流水线中的每个提示:
- 拥有明确的角色(例如“你是一名简洁的商务写手”)
- 明确指定输出格式
- 设定约束(字数限制、包含/排除的内容)
- 可以单独进行测试
优秀的提示就像优秀的函数签名——它们清晰地定义了输入、输出和约束。大多数人失败是因为他们只写“总结一下”,然后惊讶于输出不一致。
扩展规模
一旦你对基本链路熟悉后,可以:
- 添加并行分支(例如,在起草电子邮件的同时生成社交媒体帖子)。
- 通过在轻量级 JSON 存储中持久化摘要来引入记忆。
- 按步骤切换模型——对摘要使用更便宜的模型,对创意写作使用更强大的模型。
- 使用
argparse或FastAPI将整个流程包装成简易的 CLI 或 Web UI。
关键在于:先从最小化实现开始,理解每个组成部分,然后迭代。你会发现,除非真的需要工具使用编排、长期记忆或多代理协作等功能,否则很少需要重量级框架。此时,几个精心设计的提示和少量函数就足够了。
- 条件分支 — 根据内容类型使用不同的提示
- 工具使用 — 让 LLM 调用函数(网页搜索、数据库查询)
- 记忆 — 在运行之间持久化上下文
这 正是框架开始有意义的时机。但如果在不了解提示链的情况下直接跳到 LangChain,就像在沙子上建房子。
完结语
AI 代理的热潮让人们误以为必须使用复杂的编排框架才能让 LLM 发挥实用价值。其实不需要。先从结构化提示开始,将它们串联起来,只有在真正需要时才增加复杂度。
最难的不是代码——而是编写能够持续产出有用结果的提示。我已经花了太多时间通过反复试验来完善自己的提示。
如果你想快速入手,我已经发布了经过测试的提示集合,分别用于业务工作流和AI 图像生成——采用我在这里描述的相同方法,涵盖了数百个真实场景,让你无需从零开始构建。
祝开发顺利。 🛠️
觉得有帮助?关注@anonimousdev_获取更多实用的 AI/自动化指南。