Smolagents框架快速入门

71 阅读1分钟

一、基本介绍

官方定义:Hugging Face 开发的轻量级 Agent 框架

为什么选它

  • ✅ 简单易用,代码量少
  • ✅ 与Hugging Face生态集成好
  • ✅ 支持 Qwen3, DeepSeek 等开源模型
  • ✅ 文档齐全,社区活跃

二、安装准备

# 安装 smolagents 及其常用配套库
pip install smolagents transformers huggingface_hub
pip install smolagents[litellm]
pip install ddgs

三、快速上手

from smolagents import CodeAgent, LiteLLMModel

model = LiteLLMModel(
    model_id="deepseek/deepseek-chat",
    api_key="your_api_key"
)

agent = CodeAgent(tools=[], model=model, add_base_tools=False)
result = agent.run("What is the 118th Fibonacci number?")
print(result)

四、demo 代码简介

代理初始化

agent = CodeAgent(tools=[], model=model, add_base_tools=False)

参数详解:

  • tools=[] :传入的工具列表为空
    • 工具(tools)是代理可以调用的函数,如网络搜索、文件操作等
    • 空列表表示代理仅依靠模型自身的推理能力
  • model=model:传入之前创建的LiteLLMModel实例
  • add_base_tools=False:关键参数!
    • add_base_tools默认是True,会自动添加基础工具
    • 设置为False表示不添加任何基础工具
    • 基础工具通常包括:Python解释器、文件系统访问等

其他的问题就去问AI吧~