我烤了一个足球蛋糕,它教会了我构建 AI 代理
发布: (2025年11月30日 GMT+8 10:33)
5 分钟阅读
原文: Dev.to
介绍
我最近烤了一个足球蛋糕,这让我意识到 AI 代理的工作方式就像分层甜点。下面将说明口味、模具和糖霜是如何映射到代理设计中的。
下面的代码示例旨在将用户目标拆解为可执行的步骤,并使用自定义工具或 LLM 推理来执行这些步骤。它使用正则表达式从 LLM 的计划输出中提取编号步骤,然后通过匹配关键词(如 search 或 compute)将每一步分配给相应的工具,若没有匹配则回退到 LLM 推理。
就像定制蛋糕有口味、结构和装饰的层次,AI 代理也有自己的栈。
它使用 llama‑3 LLM 模型(通过 Ollama)和两个简单的自定义工具:
search_tool()– 模拟搜索引擎返回模拟结果。compute_tool()– 模拟计算任务返回占位结果。
基础模型(即“海绵层”)通过 LLM 处理基本推理。
Ollama LLM Wrapper
import requests
import json
class OllamaLLM:
def __init__(self, model="llama3"):
self.model = model
def __call__(self, prompt: str) -> str:
"""Send a prompt to a local Ollama instance."""
resp = requests.post(
"http://localhost:11434/api/generate",
json={"model": self.model, "prompt": prompt, "stream": False}
)
text = json.loads(resp.text).get("response", "")
return text
基础代理
class AgentCore:
def __init__(self, llm):
self.llm = llm
def reason(self, prompt):
return self.llm(prompt)
本地工具(糖霜和装饰)
def search_tool(query: str) -> dict:
return {
"tool": "search",
"query": query,
"results": [
{"title": "Top NFL QBs 2024", "eff": 98.1},
{"title": "Quarterback Rankings", "eff": 95.6},
],
}
def compute_tool(task: str) -> dict:
return {
"tool": "compute",
"task": task,
"result": 42, # placeholder result
}
步骤解析正则表达式
import re
# Detect common LLM step styles:
# 1. Do X
# 1) Do X
# Step 1: Do X
# **Step 1:** Do X
# - Step 1: Do X
# ### Step 1
# Step One:
STEP_REGEX = re.compile(
r"(?:^|\s)(?:\*\*)?(?:Step\s*)?(\d+)[\.\):\- ]+(.*)", re.IGNORECASE
)
结构化代理(提示逻辑与工具执行)
class StructuredAgent(AgentCore):
def parse_steps(self, plan: str):
"""Extract step lines starting with numbers."""
lines = plan.split("\n")
steps = []
for line in lines:
match = STEP_REGEX.search(line.strip())
if match:
cleaned = match.group(2).strip()
steps.append(cleaned)
return steps
def execute_step(self, step: str):
step_lower = step.lower()
if "search" in step_lower:
return search_tool(step)
if "calculate" in step_lower or "compute" in step_lower:
return compute_tool(step)
# fallback: let the model reason
return self.reason(step)
def run(self, goal: str):
PLAN_PROMPT = f"""You are a task decomposition engine.
Your ONLY job is to break the user's goal into a small set of concrete, functional steps.
Your outputs MUST stay within the domain of the user’s goal.
If the goal references football, metrics, or sports, remain in that domain only.
RULES:
- Only return steps directly needed to complete the user’s goal.
- Do NOT invent topics, examples, reviews, or unrelated domains.
- Do NOT expand into full explanations.
- No marketing language.
- No creative writing.
- No assumptions beyond the user's exact goal.
- No extra commentary.
FORMAT:
1. <short step>
2. <short step>
3. <short step>
User goal: "{goal}"
"""
plan = self.llm(PLAN_PROMPT)
steps = self.parse_steps(plan)
outputs = []
for step in steps:
outputs.append({
"step": step,
"output": self.execute_step(step)
})
return outputs
面向用户的代理(格式化输出层)
class FinalAgent(StructuredAgent):
def respond(self, goal: str):
results = self.run(goal)
formatted = "\n".join(
f"- **{r['step']}** → {r['output']}"
for r in results
)
return (
f"## Result for goal: *{goal}*\n\n"
f"{formatted}\n"
)
测试用例
if __name__ == "__main__":
agent = FinalAgent(llm=OllamaLLM("llama3"))
tests = [
"Compare NFL quarterback efficiency metrics and summarize insights.",
"Search for top training drills for youth football players.",
"Compute a simple metric and explain how you'd structure the process.",
]
for i, t in enumerate(tests, 1):
print("=" * 70)
print(f"TEST {i}: {t}")
print(agent.respond(t))
print()
示例输出(第一个测试)
======================================================================
Compare NFL quarterback efficiency metrics and summarize insights.
Gather data on NFL quarterback statistics → Here are some key statistics for NFL quarterbacks, gathered from various sources including Pro-Football-Reference.com, ESPN, and NFL.com:
...
Calculate averages and rankings for each quarterback → {'tool': 'compute', 'task': 'Calculate averages and rankings for each quarterback', 'result': 42}
无论是烘焙还是写代码,结构都很重要。要像分层一样思考。如果你需要一个甜美的类比来解释 AI 代理,试试蛋糕吧。有没有开发者灵感的甜点隐喻?在评论中留下你的想法,让我们一起让技术变得更美味。