
Hugging Face’s SmolAgents framework provides a lightweight and efficient way to build AI agents that leverage tools like web search and code execution. In this tutorial, we demonstrate how to build an AI-powered research assistant that can autonomously search the web and summarize articles using SmolAgents. This implementation runs seamlessly, requiring minimal setup, and showcases the power of AI agents in automating real-world tasks such as research, summarization, and information retrieval.
First, we install smolagents beautifulsoup4, which enables AI agents to use tools like web search and code execution, and BeautifulSoup4, a Python library for parsing HTML and extracting text from web pages.
Now, we securely input and store the Hugging Face API token as an environment variable. It uses getpass() to prompt users to enter their token without displaying it for security. The token is then stored in os.environ[“HUGGINGFACEHUB_API_TOKEN”], allowing authenticated access to Hugging Face’s Inference API for running AI models.
# Initialize the model WITHOUT passing hf_token directly
model = HfApiModel()
# Define tools (DuckDuckGo for web search)
tools = [DuckDuckGoSearchTool()]
# Create the agent
agent = CodeAgent(tools=tools, model=model, additional_authorized_imports=[“requests”, “bs4”])
Now, we initialize an AI-powered agent using the SmolAgents framework. It sets up HfApiModel() to load a Hugging Face API-based language model, automatically detecting the stored API token for authentication. The agent is equipped with DuckDuckGoSearchTool() to perform web searches. Also, CodeAgent() is instantiated with tool access and authorized imports, such as requests for making web requests and bs4 for parsing HTML content.
query = “Summarize the main points of the Wikipedia article on Hugging Face (the company).”
# Run the agent with the query
result = agent.run(query)
print(“nAgent’s final answer:n”, result)
Finally, we send a query to the AI agent, asking it to summarize the main points of the Wikipedia article on Hugging Face. The agent.run(query) command triggers the agent to perform a web search, retrieve relevant content, and generate a summary using the language model. Finally, the print() function displays the agent’s final answer, concisely summarizing the requested topic.
Following this tutorial, we have successfully built an AI-powered research assistant using Hugging Face SmolAgents that can autonomously search the web and summarize articles. This implementation highlights the power of AI agents in automating research tasks, making it easier to retrieve and process large amounts of information efficiently. Beyond web search and summarization, SmolAgents can be extended to various real-world applications, including automated coding assistants, personal task managers, and AI-driven chatbots.
Here is the Colab Notebook for the above project. Also, don’t forget to follow us on Twitter and join our Telegram Channel and LinkedIn Group. Don’t Forget to join our 80k+ ML SubReddit.
🚨 Recommended Read- LG AI Research Releases NEXUS: An Advanced System Integrating Agent AI System and Data Compliance Standards to Address Legal Concerns in AI Datasets

Asif Razzaq is the CEO of Marktechpost Media Inc.. As a visionary entrepreneur and engineer, Asif is committed to harnessing the potential of Artificial Intelligence for social good. His most recent endeavor is the launch of an Artificial Intelligence Media Platform, Marktechpost, which stands out for its in-depth coverage of machine learning and deep learning news that is both technically sound and easily understandable by a wide audience. The platform boasts of over 2 million monthly views, illustrating its popularity among audiences.
Be the first to comment