Scripts2026年3月29日·1 分钟阅读

Smolagents — Lightweight AI Agent Framework

Hugging Face minimalist agent framework. Build AI agents in ~1000 lines of code with code-based actions, tool calling, and multi-step reasoning.

TO
TokRepo精选 · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

pip install smolagents

介绍

Smolagents is Hugging Face's lightweight agent framework — the core library is ~1000 lines of code. Agents write and execute Python code as their actions, making them more flexible and debuggable than JSON-based tool calling.

Best for: Quick agent prototyping, research, code-executing agents Works with: OpenAI, Anthropic, HuggingFace models, local models


Key Concept: Code Agents

Instead of outputting JSON tool calls, smolagents agents write Python code:

from smolagents import CodeAgent, tool, HfApiModel

@tool
def get_weather(city: str) -> str:
    """Get current weather for a city."""
    return f"Sunny, 22C in {city}"

agent = CodeAgent(tools=[get_weather], model=HfApiModel())
agent.run("What's the weather in Tokyo?")

The agent writes: result = get_weather("Tokyo") and executes it.


Why Code Agents?

  • More expressive — loops, conditionals, variables
  • Composable — chain tool calls naturally
  • Debuggable — read the generated code
  • Efficient — fewer LLM calls than ReAct

🙏

来源与感谢

Created by Hugging Face. Licensed under Apache 2.0. huggingface/smolagents

相关资产