← all conversations

Using Smolagents Framework

2025-07-272 turns4,292 charsgpt-4o
smolagentshuggingfaceai-frameworks

Summary

User wants to learn how to use the Smolagents framework from Hugging Face for building AI agents.

Messages

how could I use : https://github.com/huggingface/smolagents
The Hugging Face [`smol-ai/smolagents`](https://github.com/huggingface/smolagents) repo is a lightweight framework for **building agentic systems using language models**. It makes it easy to orchestrate agents that can reason, plan, and execute tools (like search, code, file access, APIs, etc.) in a chain of thought. --- ### 🔧 What It’s For You can use it to: - **Build multi-step reasoning agents** that think before acting - **Call external tools/APIs** from language models - **Compose agents that collaborate** (chains or trees of agents) - **Use local models** (like Ollama or OpenRouter) or OpenAI/Anthropic APIs - **Build CLI or backend tools** for data scraping, code writing, API calling, etc. --- ## 🛠️ Getting Started ### 1. **Install It** ```bash pip install smolagents ``` --- ### 2. **Basic Example: A Python REPL Agent** ```python from smolagents.agent import LLMToolAgent from smolagents.tools import PythonREPLTool agent = LLMToolAgent( tools=[PythonREPLTool()], name="Python Agent", system_message="You are a helpful Python REPL agent. Think before coding.", ) print(agent.run("What is the square root of 144 plus 10?")) ``` This will output: ``` The square root of 144 is 12. 12 + 10 = 22. ``` Behind the scenes: 1. The LLM plans what it needs to do. 2. It chooses the `PythonREPLTool`. 3. Executes Python code. 4. Returns the result. --- ## 🧠 Architecture (Simple Mental Model) - **LLMToolAgent** — an agent that chooses from a list of tools - **Tool** — a callable action like `PythonREPLTool`, `SearchTool`, `FileTool`, etc. - **Planner** — the LLM generates the next action or message - **Toolcall** — when the agent invokes a tool --- ### 3. **Using Your Own Tools** Make your own tool with just a function and a docstring: ```python from smolagents.tools.base import Tool class MyTool(Tool): name = "add" description = "Adds two numbers" def call(self, a: int, b: int): return a + b ``` --- ### 4. **Using a Local LLM (like Ollama)** You can use **OpenRouter**, **Ollama**, or even **LM Studio** by customizing the LLM backend. ```python from smolagents.llms.ollama import OllamaLLM from smolagents.agent import LLMToolAgent from smolagents.tools import PythonREPLTool llm = OllamaLLM(model="phi3") agent = LLMToolAgent( tools=[PythonREPLTool()], llm=llm, name="Local Python Agent", ) agent.run("What is 5 factorial?") ``` --- ## 🧰 Tool Examples - `PythonREPLTool` — Runs Python code safely - `TerminalTool` — Executes Bash commands - `FileTool` — Reads/writes to disk - `RequestsTool` — Makes HTTP requests - `BrowserTool` — Browses and scrapes pages (headless) - `DuckDuckGoSearchTool` — Queries the web --- ### 5. **Multi-Agent Planning Example** You can chain agents or give them different roles. Here's a sketch: ```python from smolagents.agent import LLMToolAgent from smolagents.tools import PythonREPLTool, FileTool writer = LLMToolAgent(tools=[FileTool()], name="Writer") coder = LLMToolAgent(tools=[PythonREPLTool()], name="Coder") task = "Write a Python program that calculates Fibonacci numbers and saves the result to a file." writer.run(task) coder.run("Run the code written by Writer") ``` --- ## 💡 Real-World Use Cases - 🔍 Web scraping bots that plan and execute tasks - 🤖 AI assistants that write and execute code - 🗂️ Automation workflows using JSON + LLM logic - 🧵 AI project managers coordinating sub-agents (think Auto-GPT, but simpler) --- ## 🔄 Looping Agent Use `agent.loop()` to let it **keep thinking** and calling tools until it’s done: ```python agent.loop("Search for top 5 startups in AI and summarize them.") ``` --- ## 🧪 Tip: Enable Verbose Debugging ```python agent.verbose = True ``` This shows the intermediate steps like: - Thought - Tool choice - Tool input/output --- ## 📚 Resources - [GitHub Repo](https://github.com/huggingface/smolagents) - [SmolAI Discord](https://discord.gg/U9GZN7CVtY) - [Examples](https://github.com/huggingface/smolagents/tree/main/examples) --- Would you like a **custom example** (e.g. reading markdown, scraping Reddit, triggering shell scripts, interacting with a database)? I can walk you through building that with `smolagents`.
gpt-4o