Article
ai-agentsllmpythonhuggingfacetool-calling
Smolagents - Lightweight Tool-Calling AI Agent Framework
Build lightweight AI agents with Hugging Face's Smolagents framework. This Action Pack guides you to define custom tools, integrate them with an LLM, and create an agent that performs real-world tasks, simplifying complex automation.
beginner15 min4 steps
The play
- Define a Custom ToolCreate a Python class for your tool, inheriting from a base `Tool` class. Implement the `__init__` method to define its `name`, `description`, and `parameters`. Crucially, implement an `execute` method that contains the tool's specific logic, like making an API call or performing a calculation.
- Implement a Mock LLM for Tool CallingDevelop a `MockLLM` class that holds a list of your defined tools. Its `generate_response` method will simulate an LLM's decision-making process, parsing the input prompt to determine if a tool should be called and, if so, which one and with what arguments. It then returns the tool's output.
- Assemble Your AgentInstantiate your custom tools (e.g., `SearchTool`) and then create an instance of your `MockLLM`, passing the list of available tools to it. Finally, create your `Agent` instance, providing it with the configured `MockLLM`.
- Chat with Your AgentUse the `agent.chat()` method to interact with your newly created agent. Provide different prompts to observe how it processes your input, potentially invokes the defined `SearchTool`, and returns a response based on the tool's output or its own general knowledge.
Starter code
from typing import List, Dict, Any
# Base Tool class defines the interface for all tools
class Tool:
def __init__(self, name: str, description: str, parameters: Dict[str, Any]):
self.name = name
self.description = description
self.parameters = parameters
def execute(self, **kwargs) -> str:
raise NotImplementedError("Tool must implement an execute method.")
# Specific implementation of a Search Tool
class SearchTool(Tool):
def __init__(self):
super().__init__(
name="search",
description="Searches the web for information.",
parameters={"query": {"type": "string", "description": "The search query."}}
)
def execute(self, query: str) -> str:
# In a real scenario, this would call a search API (e.g., Google Search)
# For this example, we'll return a mock result
if "Hugging Face Smolagents" in query:
return "Hugging Face Smolagents is a lightweight framework for building tool-calling AI agents."
elif "current weather" in query:
return "The weather in your location is sunny with 25 degrees Celsius."
else:
return f"Mock search result for '{query}': Information about {query} found."
# Simple Mock LLM for demonstration
class MockLLM:
def __init__(self, tools: List[Tool]):
self.tools = {tool.name: tool for tool in tools}
def generate_response(self, prompt: str) -> str:
# Simple rule-based "tool calling" for demonstration
if "search for" in prompt.lower():
query = prompt.lower().split("search for", 1)[1].strip().replace("?", "")
if query:
print(f"LLM decided to use tool: search with query='{query}'")
result = self.tools["search"].execute(query=query)
return f"I found this: {result}"
return f"LLM's response to '{prompt}': I don't have a specific tool for that, but I understand."
# Agent combines LLM and tools
class Agent:
def __init__(self, llm: MockLLM):
self.llm = llm
def chat(self, user_input: str) -> str:
return self.llm.generate_response(user_input)
# --- Usage ---
# 1. Define Tools
search_tool = SearchTool()
# 2. Create a Mock LLM with the tool
mock_llm = MockLLM(tools=[search_tool])
# 3. Create the Agent
agent = Agent(llm=mock_llm)
# 4. Interact with the Agent
print("Agent: Hello! How can I help you today?")
print(f"User: What is Hugging Face Smolagents?")
print(f"Agent: {agent.chat('Search for Hugging Face Smolagents?')}\n")
print(f"User: What is the current weather?")
print(f"Agent: {agent.chat('Search for current weather?')}\n")
print(f"User: Tell me a joke.")
print(f"Agent: {agent.chat('Tell me a joke.')}\n")