TinyAgents:一个关于代码代理和 MCP 工具的微小实验

社区文章 发布于 2025 年 5 月 16 日

最近,我被 Julien Chaumond 的一篇博客文章《“Tiny Agents: an MCP-powered agent in 50 lines of code”》所吸引,他在这篇文章中介绍了 Tiny Agents 的概念:由 MCP 工具驱动的极简 LLM 代理。核心思想令人耳目一新:小型、专注的代理通过模型上下文协议异步调用工具,没有如今“代理”基础设施中常见的笨重框架。

我忍不住问 Julien 一个后续问题:“如果不是工具调用代理,我们尝试一个代码代理呢?”

工具调用代理很直观:你给模型一个问题,它就会调用一个相关的工具(例如`get_weather_alerts("NY")`)。但是,当你要求组合推理或更复杂的工作流程时,这种方法可能会失效。这就是代码代理发挥作用的地方。

为什么选择代码代理?

顾名思义,代码代理编写并执行小段代码,而不是直接调用工具。这使得它们能够

  • 处理更复杂的逻辑(例如,条件语句、循环)
  • 将多个工具调用组合成一个代码块
  • 减少 LLM 调用次数,这可以提高性能和可靠性

简而言之:更强大的功能,更少的废话。

构建:TinyAgents

因此,我构建了一个最小化的实现:TinyAgents = LLM + 异步 MCP 工具。

这个设置非常轻量:仅仅是一个单步代理实现,将 LLM 连接起来以异步方式与 MCP 工具进行交互。

实验

我用一个简单但略带组合性的问题测试了工具调用代理和代码代理:

“纽约、加利福尼亚和阿拉斯加的天气警报是什么?”

工具调用代理

工具调用代理按字面意思解释了任务,但只执行了一次工具调用,返回了纽约的警报,然后就停止了。这通常是工具调用推理的局限性:它缺乏对更复杂工作流程的支持。在这种情况下,没有进行多次工具调用,很可能是因为处理整个任务需要多个步骤,而这些步骤尚未实现。无论如何,它仍然不如代码代理高效,代码代理在一个步骤中处理了整个任务。

输出

### Weather Alerts in New York, California, and Alaska

#### New York:
- **Special Weather Statement (Northern and Southern portions)**:
  - **Areas**: Various parts of New York including Manhattan, Brooklyn, Staten Island, parts of Queens, Nassau, and Suffolk counties.
  - **Severity**: Moderate
  - **Description**: Dense fog reducing visibility to a quarter mile or less in spots, advising motorists to slow down and use caution.
  
- **Flood Warning (Northern Tompkins County)**:
  - **Area**: Tompkins, NY
  - **Severity**: Severe
  - **Description**: Minor flooding is occurring and forecasted on Cayuga Lake in Ithaca, affecting Tompkins County.
  - **Details**: Flood stage at 383.5 feet. Lake level is expected to remain steady above flood stage until the weekend and then drop slightly.

#### California:
- As of the provided information, there are no specific weather alerts mentioned for California.

#### Alaska:
- As of the provided information, there are no specific weather alerts mentioned for Alaska.

### Summary:
- **New York**: Moderate fog warnings for many areas and a severe flood warning for Tompkins County.
- **California**: No alerts provided in the given information.
- **Alaska**: No alerts provided in the given information.

For the most up-to-date and comprehensive weather alerts, it's recommended to check the local National Weather Service (NWS) website or apps specific to California and Alaska.

代码代理

另一方面,代码代理编写了一段简洁的 Python 代码片段,一次性调用了`get_alerts("NY")`、`get_alerts("CA")`和`get_alerts("AK")`。接下来要做的是:执行代码,收集结果,并整齐地返回它们。

  • ✅ 涵盖所有州
  • ✅ 单次 LLM 调用
  • ✅ 在一个简洁的代码片段中解决

输出

要获取纽约 (NY)、加利福尼亚 (CA) 和阿拉斯加 (AK) 的天气警报,您可以使用`get_alerts`函数。以下是执行此操作的 Python 代码:
# Get weather alerts for New York
ny_alerts = get_alerts("NY")

# Get weather alerts for California
ca_alerts = get_alerts("CA")

# Get weather alerts for Alaska
ak_alerts = get_alerts("AK")

# Print the alerts
print("Weather Alerts for New York (NY):")
print(ny_alerts)
print("\nWeather Alerts for California (CA):")
print(ca_alerts)
print("\nWeather Alerts for Alaska (AK):")
print(ak_alerts)

此代码将为每个州调用`get_alerts`函数并打印天气警报。如果没有警报,该函数可能会返回一个空列表或一条消息,指示当前没有警报。

接下来是什么?

下一步显而易见:💡 构建一个微型 Python 代码执行器,用于安全高效地处理这些生成的代码块。

这将允许代理生成和运行任意 Python 代码。


如果您对轻量级、组合式和异步优先的 LLM 工作流程感兴趣,我强烈建议您探索 MCP 工具,并思考何时代码代理能让您占据上风。

您可以在 TinyAgents GitHub 仓库中找到完整的实现和实验代码。

更多实验即将推出。保持小巧 🧠

社区

注册登录 以发表评论