Creating an Agent With Tools
1. Creating an Agent With Tools
Now that you understand what makes code agents special, let's build your first one using smolagents.2. Creating a Code Agent (No Tools)
Let's create a basic agent without access to tools for now. To build agents with smolagents, we first import a few classes from the smolagents package. To create code agents, we use the CodeAgent class. This is the core class in the framework that creates agents capable of writing and running Python code to solve tasks. Then, we initialize the agent with an empty tools list and use a default model provided by Hugging Face, via InferenceClientModel(). To run it, simply call the .run() method with a prompt, or task. smolagents is highly flexible and can work with almost any LLM provider, including Hugging Face, OpenAI, Anthropic, and more. That's it! With just a few lines of code, you now have a functioning agent.3. Why Use Tools with Code Agents?
The agent we just defined would be able to handle many tasks using its LLM model and standard Python. But sometimes, they need access to external information, like live data from the web. That's where tools come in!4. Adding a Web Search Tool
For example, to give the agent web search capabilities, you can import the WebSearchTool class and add it to the agent's tools list.5. Code Agent With Web Search Tool Output
Now, when you run a task that requires current information, the agent can search the web and use that data in its response.6. Built-in Tools (by Category)
The WebSearchTool is one of several built-in tools available in smolagents. These tools can extend your agent's abilities in useful ways, like: retrieving live information from different sources, interacting with the web, executing specific code, interacting with the user, processing speech, or controlling the agent's workflow. You can explore the full list of built-in tools in the official smolagents documentation.7. Tools From the Hugging Face Hub
If built-in tools don't meet your needs, you can also import specialized tools. The Hugging Face Hub hosts a growing collection of community-contributed tools for specific tasks. These tools save development time and provide basic solutions for common problems.8. Using Community Tools from Hugging Face
For example, let's say you want to find the most downloaded model for a specific task on Hugging Face. There's a tool for that! You can load the tool with the load_tool() function, passing the repository ID. Just be sure to set trust_remote_code=True to allow the tool's code to run. Once loaded, you can include it in your agent, just like any other tool. Then, you can run the agent with a prompt like: "Find the most downloaded image classification model on Hugging Face."9. Let's practice!
Now, time to build your own agents and see tools in action!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.