百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

smolagents

> 编程语言
开源

smolagents:一个光骨库,供在代码中思考的特工使用.

28.6K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

smolagents:一个光骨库,供在代码中思考的特工使用.

`smolagents` is a library that enables you to run powerful agents in a few lines of code. It offers: ✨ **Simplicity**: the logic for agents fits in ~1,000 lines of code (see [agents.py](https://github.com/huggingface/smolagents/blob/main/src/smolagents/agents.py)). We kept abstractions to their minimal shape above raw code! 🧑‍💻 **First-class support for Code Agents**. Our [`CodeAgent`](https://huggingface.co/docs/smolagents/reference/agents#smolagents.CodeAgent) writes its actions in code (as opposed to "agents being used to write code"). To make it secure, we support executing in sandboxed environments via [Blaxel](https://blaxel.ai), [E2B](https://e2b.dev/), [Modal](https://modal.com/), or Docker. 🤗 **Hub integrations**: you can [share/pull tools or agents to/from the Hub](https://huggingface.co/docs/smolagents/reference/tools#smolagents.Tool.from_hub) for instant sharing of the most efficient agents! 🌐 **Model-agnostic**: smolagents supports any LLM. It can be a local `transformers` or `ollama` model, one of [many providers on the Hub](https://huggingface.co/blog/inference-providers), or any model from OpenAI, Anthropic and many others via our [LiteLLM](https://www.litellm.ai/) integration. 👁️ **Modality-agnostic**: Agents support text, vision, video, even audio inputs! Cf [this tutorial](https://huggingface.co/docs/smolagents/examples/web_browser) for vision. 🛠️ **Tool-agnostic**: you can use tools from any [MCP server](https://huggingface.co/docs/smolagents/reference/tools#smolagents.ToolCollection.from_mcp), from [LangChain](https://huggingface.co/docs/smolagents/reference/tools#smolagents.Tool.from_langchain), you can even use a [Hub Space](https://huggingface.co/docs/smolagents/reference/tools#smolagents.Tool.from_space) as a tool. Full documentation can be found [here](https://huggingface.co/docs/smolagents/index). > [!NOTE] > Check out our [launch blog post](https://huggingface.co/blog/smolagents) to learn more about `smolagents`! ## Quick demo First install the package with a default set of tools: ```bash pip install "smolagents[toolkit]" ``` Then define your agent, give it the tools it needs and run it! ```py from smolagents import CodeAgent, WebSearchTool, InferenceClientModel model = InferenceClientModel() agent = CodeAgent(tools=[WebSearchTool()], model=model, stream_outputs=True) agent.run("How many seconds would it take for a leopard at full speed to run through Pont des Arts?") ``` https://github.com/user-attachments/assets/84b149b4-246c-40c9-a48d-ba013b08e600 You can even share your agent to the Hub, as a Space repository: ```py agent.push_to_hub("m-ric/my_agent") # agent.from_hub("m-ric/my_agent") to load an agent from Hub ``` Our library is LLM-agnostic: you could switch the example above to any inference provider. InferenceClientModel, gateway for all inference providers supported on HF ```py from smolagents import InferenceClientModel model = InferenceClientModel( model_id="deepseek-ai/DeepSeek-R1", provider="together", ) ``` LiteLLM to access 100+ LLMs ```py from smolagents import LiteLLMModel model = LiteLLMModel( model_id="anthropic/claude-4-sonnet-latest", temperature=0.2, api_key=os.environ["ANTHROPIC_API_KEY"] ) ``` OpenAI-compatible servers: Together AI ```py import os from smolagents import OpenAIModel model = OpenAIModel( model_id="deepseek-ai/DeepSeek-R1", api_base="https://api.together.xyz/v1/", # Leave this blank to query OpenAI servers. api_key=os.environ["TOGETHER_API_KEY"], # Switch to the API key for the server you're targeting. ) ``` OpenAI-compatible servers: OpenRouter ```py import os from smolagents import OpenAIModel model = OpenAIModel( model_id="openai/gpt-4o", api_base="https://openrouter.ai/api/v1", # Leave this blank to query OpenAI servers. api_key=os.environ["OPENROUTER_API_KEY"], # Switch to the API key for the server you're targeting. ) ``` Local `transformers` model ```py from smolagents import TransformersModel model = TransformersModel( model_id="Qwen/Qwen3-Next-80B-A3B-Thinking", max_new_tokens=4096, device_map="auto" ) ``` Azure models ```py import os from smolagents import AzureOpenAIModel model = AzureOpenAIModel( model_id = os.environ.get("AZURE_OPENAI_MODEL"), azure_endpoint=os.environ.get("AZURE_OPENAI_ENDPOINT"), api_key=os.environ.get("AZURE_OPENAI_API_KEY"), api_version=os.environ.get("OPENAI_API_VERSION") ) ``` Amazon Bedrock models ```py import os from smolagents import AmazonBedrockModel model = AmazonBedrockModel( model_id = os.environ.get("AMAZON_BEDROCK_MODEL_ID") ) ``` ## CLI You can run agents from CLI using two commands: `smolagent` and `webagent`. `smolagent` is a generalist command to run a multi-step `CodeAgent` that can be equipped with various tools. ```bash # Run with direct prompt and options smolagent "Plan a trip to Tokyo, Kyoto and Osaka between Mar 28 and Apr 7." --model-type "InferenceClientModel" --model-id "Qwen/Qwen3-Next-80B-A3B-Thinking" --imports pandas numpy --tools web_search # Run in interactive mode (launches setup wizard when no prompt provided) smolagent ``` Interactive mode guides you through: - Agent type selection (CodeAgent vs ToolCallingAgent) - Tool selection from available toolbox - Model configuration (type, ID, API settings) - Advanced options like additional imports - Task prompt input Meanwhile `webagent` is a specific web-browsing agent using [helium](https://github.com/mherrmann/helium) (read more [here](https://github.com/huggingface/smolagents/blob/main/src/smolagents/vision_web_browser.py)). For instance: ```bash webagent "go to xyz.com/men, get to sale section, click the first clothing item you see. Get the product details, and the price, return them. note that I'm shopping from France" --model-type "LiteLLMModel" --model-id "gpt-5" ``` ## How do Code agents work? Our [`CodeAgent`](https://huggingface.co/docs/smolagents/reference/agents#smolagents.CodeAgent) works mostly like classical ReAct agents - the exception being that the LLM engine writes its actions as Python code snippets. ``` … ``` Actions are now Python code snippets. Hence, tool calls will be performed as Python function calls. For instance, here is how the agent can perform web search over several websites in one single action: ```py requests_to_search = ["gulf of mexico america", "greenland denmark", "tariffs"] for request in requests_to_search: print(f"Here are the search results for {request}:", web_search(request)) ``` Writing actions as code snippets is demonstrated to work better than the current industry practice of letting the LLM output a dictionary of the tools it wants to call: [uses 30% fewer steps](https://huggingface.co/papers/2402.01030) (thus 30% fewer LLM calls) and [reaches higher performance on difficult benchmarks](https://huggingface.co/papers/2411.01747). Head to [our high-level intro to agents](https://huggingface.co/docs/smolagents/conceptual_guides/intro_agents) to learn more on that. Since code execution can be a serious security concern (arbitrary code execution!), **you should run agent code in a sandbox**. We support several options: - [E2B](https://e2b.dev/), [Blaxel](https://blaxel.ai), [Modal](https://modal.com/) — managed cloud sandboxes, simplest to set up - [Docker](https://www.docker.com/) — self-hosted container isolation The built-in `LocalPythonExecutor` is **not a security sandbox**. It applies some restrictions but can be bypassed and must not be used as a security boundary. Alongside [`CodeAgent`](https://huggingface.co/docs/smolagents/reference/agents#smolagents.CodeAgent), we also provide the standard [`ToolCallingAgent`](https://huggingface.co/docs/smolagents/reference/agents#smolagents.ToolCallingAgent) which writes actions as JSON/text blobs. You can pick whichever style best suits your use case. ## How smol is this library? We strived to keep abstractions to a strict minimum: the main code in `agents.py` has <1,000 lines of code. Still, we implement several types of agents: `CodeAgent` writes its actions as Python code snippets, and the more classic `ToolCallingAgent` leverages built-in tool calling methods. We also have multi-agent hierarchies, import from tool collections, remote code execution, vision models... By the way, why use a framework at all? Well, because a big part of this stuff is non-trivial. For instance, the code agent has to keep a consistent format for code throughout its system prompt, its parser, the execution. So our framework handles this complexity for you. But of course we still encourage you to hack into the source code and use only the bits that you need, to the exclusion of everything else! ## How strong are open models for agentic workflows? We've created [`CodeAgent`](https://huggingface.co/docs/smolagents/reference/agents#smolagents.CodeAgent) instances with some leading models, and compared them on [this benchmark](https://huggingface.co/datasets/m-ric/agents_medium_benchmark_2) that gathers questions from a few different benchmarks to propose a varied blend of challenges. [Find the benchmarking code here](https://github.com/huggingface/smolagents/blob/main/examples/smolagents_benchmark/run.py) for more detail on the agentic setup used, and see a comparison of using LLMs code agents compared to vanilla (spoilers: code agents works better).

This comparison shows that open-source models can now take on the best closed models! ## Security Security is a critical consideration when working with code-executing agents. Ensure you are using one of the sandboxed execution options that provide isolation from untrusted code. **Warning:** `LocalPythonExecutor` provides best-effort mitigations only and is **not a security boundary**. Do not use it to run untrusted code. For security policies, vulnerability reporting, and more information on secure agent execution, please see our [Security Policy](SECURITY.md). ## Contribute Everyone is welcome to contribute, get started with our [contribution guide](https://github.com/huggingface/smolagents/blob/main/CONTRIBUTING.md). ## Cite smolagents If you use `smolagents` in your publication, please cite it by using the following BibTeX entry. ```bibtex @Misc{smolagents, title = {`smolagents`: a smol library to build great agentic systems.}, author = {Aymeric Roucher and Albert Villanova del Moral and Thomas Wolf and Leandro von Werra and Erik Kaunismäki}, howpublished = {\url{https://github.com/huggingface/smolagents}}, year = {2025} } ```

Issues· 814 开放

查看全部 Issues在 GitHub 打开
  • #1368

    _generate_planning_step 应支持图像输入

    enhancement更新于 2026年9月17日
  • #2800

    DOC: zh/examples/multiagents.md 导入已删除的 `ManagedAgent` 符号

    documentation更新于 2026年9月17日
  • #2071

    特性:代理工具执行的加密收据 (AAR)

    更新于 2026年9月16日
  • #2654

    ENH: [Feature] (英语). 用于工具/代码执行的治理中间软件钩(PII屏蔽、成本上限、工具授权)

    enhancement更新于 2026年9月16日
  • #2799

    BUG: Model.to_dict() 删除自定义角色转换和客户端参数设置 (api_base/organization/project/azure_endpoint),因此保存的代理会重新加载到错误的端点和角色映射

    更新于 2026年9月16日
  • #2746

    LocalPythonExecutor: for/while ... else 会默默地丢弃 else 子句; list += <非列表可迭代对象> 会被错误拒绝

    更新于 2026年9月15日
  • #2782

    本地 Python 执行器忽略循环 else 子句 (for...else / while...else)

    更新于 2026年9月15日
  • #2725

    ActionStep.dict() 为 observations_images 返回原始的非 JSON 序列化的字节

    更新于 2026年9月15日
  • #2656

    [BUG] 从张量构建的 AgentImage 会反转像素值 (255 - 数组 * 255)

    更新于 2026年9月14日
  • #566

    [BUG] GradioUI 的 uploadfile 函数错误修改文件扩展名

    bug更新于 2026年9月12日

> 标签

Python

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言