Exploring Smol Agents: Building an Intelligent Shopping List Assistant
Introduction
The world of AI development is experiencing a fascinating shift toward more lightweight, specialized tools that pack powerful capabilities into smaller packages. Enter smolagents – a nimble framework from Hugging Face that's changing how developers build AI applications. By combining the reasoning power of Large Language Models (LLMs) with a streamlined architecture, smol agents offer a fresh approach to creating AI assistants that can tackle complex tasks while remaining surprisingly simple to implement. In this article, we'll explore the practical applications of smol agents by building a shopping list assistant, demonstrating how these compact but capable agents can transform everyday tasks into intelligent, automated solutions.
What is a Smol Agent?
A smol agent is a minimalist AI agent framework developed by Hugging Face. It's designed to simplify the development of AI agents that can interact with the real world, make decisions, and execute tasks based on the outputs of Large Language Models (LLMs). With its core functionality encapsulated in just about 1,000 lines of code, smolagents is a perfect choice for developers who want to build powerful agents without the overhead of more complex frameworks.
Key Features of Smol Agents
What Can Smol Agents Do?
Smol agents are versatile and can be used for a wide range of tasks, from automated programming to web search and data retrieval. They excel in scenarios where agents need to perform tasks step-by-step, think, act, and observe the results.
Why I Chose to Build a Shopping List Assistant
When I first decided to explore the capabilities of smol agents, I knew I wanted a project that would not only test the framework's limits but also resonate with a wide audience. After all, the best way to understand the potential of a new tool is to apply it to a real-world problem that people can relate to. That’s when the idea of building a Shopping List Assistant came to mind.
Here’s why this example stood out:
So, let’s dive into how this assistant was built and see how it handles everything from simple item additions to complex budget optimisations!
Example: Building a Shopping List Assistant
To illustrate the capabilities of smol agents, let's consider a practical example: a Shopping List Assistant. This assistant helps users manage their shopping lists, add items, remove items, and even optimize their lists based on budget constraints.
Step 1: Setting Up the Environment
First, we install the necessary libraries:
pip install smolagents
Step 2: Defining the Tools
We define tools that our agent will use to interact with the shopping list:
from smolagents import Tool
class GetShoppingListTool(Tool):
# Retrieves the shopping list for a given customer.
# Implementation details... :
....
class UpdateListTool(Tool):
# Updates items in the shopping list with options to add, remove, or modify quantities.
# Implementation details...
....
class GetItemDetailsTool(Tool):
# Retrieves detailed information about a specific item.
# Implementation details...
....
Step 3: Creating the Agent
We create a CodeAgent that uses these tools and a specific LLM (in this case, DeepSeek Coder V3):
from smolagents import CodeAgent, LiteLLMModel
get_shopping_list_tool = GetShoppingListTool()
get_item_details_tool = GetItemDetailsTool()
update_list_tool = UpdateListTool()
model=model = LiteLLMModel(model_id="deepseek/deepseek-coder")
agent = CodeAgent(
tools=[get_shopping_list_tool, get_item_details_tool, update_list_tool],
model=model,
system_prompt=CUSTOM_PROMPT
)
Simple Queries
To demonstrate the basics, here are some simple interactions with the Shopping List Assistant.
User Query:
add 5 apples to my shopping list
AI Response:
5 apples have been added to your shopping list.
This is a relatively simple task for the agent to performs
User Query:
show my shopping list
AI Response:
Your shopping list:
- Apples: 5
- Bread: 2
- Milk: 1
Another simple task for the agent to perform:
Handling Complex Queries
One of the standout features of smol agents is their ability to handle complex, multi-step queries efficiently. Let's see how our Shopping List Assistant performs with some intricate tasks.
Example 1: Adding Items with Budget Constraints
add $25 of bread, 3 tomatoes and $15 of pasta and twice as many eggs as tomatoes
AI Response:
Added $25 of bread, 3 tomatoes, $15 of pasta, and 6 eggs to the shopping list.
Analysis:
Example 2: Optimising the Shopping List
User Query:
I can only spend a max of $10 per item total. Fix my list
AI Response:
Your shopping list has been updated to meet the $10 per item budget. Here's your updated list:
- Bread: $10
- Tomatoes: $7.5
- Pasta: $7.5
See the full agent transcript in the Appendix.
What makes this implementation notable is its autonomous operation. Rather than simply responding to commands, the assistant:
This demonstrates how smol agents can be used to create AI assistants that go beyond simple task execution to provide comprehensive, intelligent assistance.
Lessons Learned
While building the Shopping List Assistant proved to be a successful demonstration of smol agents' capabilities, the development process revealed several critical insights that could benefit others undertaking similar projects:
Conclusion
Smolagents from Hugging Face offer a powerful yet simple way to build intelligent agents that can handle complex tasks with ease. By leveraging the capabilities of models like DeepSeek Coder V3, developers can create applications that are not only efficient but also highly intelligent. Whether you're automating programming tasks, managing shopping lists, or exploring new use cases, smol agents provide the tools you need to build the future of AI applications.
So, what are you waiting for? Dive into the world of smol agents and start building your own intelligent applications today!
Getting Started with Smol Agents
If you're interested in building your own AI agents, check out
领英推荐
Appendix - Annotated Examples
1. Initial Setup and Empty List
User Query:
Get my shopping list
AI Response:
Your shopping list is currently empty. Would you like to add some items?
The assistant handles straightforward commands with ease, confirming actions clearly and maintaining accurate item counts.
2. Basic Item Management
User Query:
add 2 tomatoes
AI Response:
2 tomatoes have been successfully added to the shopping list.
The assistant handles straightforward commands with ease, confirming actions clearly.
User Query:
add $10 of milk
AI Response:
Added 4 units of milk to the shopping list, totaling approximately $11.2.
Analysis:
The assistant accurately interprets monetary values and converts them into appropriate quantities. It provides a clear confirmation, detailing both the quantity added and the estimated total cost. By ensuring mathematical precision in unit price calculations, it enhances reliability. Additionally, it demonstrates flexibility in processing both cost-based and quantity-based item additions, making the shopping experience more intuitive.
User Query:
what does my list look like now
AI Response:
{'shopping_list': [{'item': 'Tomatoes', 'quantity': 2, 'unit_price': 2.5}, {'item': 'Milk', 'quantity': 4, 'unit_price': 2.8}], 'message': 'Your current shopping list contains 2 items.'}
Analysis:
3. Complex Item Addition
User Query:
add $25 of bread, 3 tomatoes, $15 of pasta and twice as many eggs as tomatoes
AI Response:
Added $25 of bread, 3 tomatoes, $15 of pasta, and 6 eggs to the shopping list.
Why It’s Impressive:
The assistant effectively handles multiple item types and complex instructions within a single command. It accurately processes monetary values and relative quantities while demonstrating strong mathematical reasoning (e.g., determining egg quantities based on tomatoes). It does all this by writing and running python code
User Query:
show my invoice
AI Response:
INVOICE
Tomatoes 5 x $2.5 = $12.5
Milk 4 x $2.8 = $11.2
Bread 5 x $5.0 = $25.0
Pasta 5 x $3.0 = $15.0
Eggs 6 x $4.2 = $25.2
Total Cost: $88.9
The assistant efficiently queries the shopping list, retrieves prices, and accurately calculates totals. It presents the data in a well-structured, tabular format that enhances readability. By maintaining consistent decimal precision and currency formatting, it ensures clarity and accuracy.
Additionally, it effectively breaks down individual item costs while seamlessly aggregating them into a final total, showcasing strong data organization and presentation skills.
5. Budget Constraints
User Query:
I can only spend a max of $10 per item. Fix my list
AI Response:
Your shopping list has been updated to stay within $10 per item:
Tomatoes: 4 units ($10.0)
Milk: 3 units ($8.40)
Bread: 2 units ($10.0)
Pasta: 3 units ($9.0)
Eggs: 2 units ($8.40)
Why It’s Impressive:
The assistant uses code to enforce budget constraints intelligently, adjusting quantities to meet the user’s financial goals while preserving the list’s integrity. This demonstrates its ability to write and execute code that can optimize and problem-solve.
6. Bill Optimisation
User Query:
think of ways that I could have all the current items on my list and for the total to be exactly $60.00 (or just under).
AI Response:
To reach exactly $60.00 or just under while keeping all the current items on your list, here are two possible solutions:
Option 1: Increase Quantities of Cheaper Items
Increase tomatoes by 5: This would bring the total to $58.30 (just under $60.00).
Tomatoes: 4 → 9
Total: $45.80 → $58.30
Option 2: Balanced Adjustment
Increase tomatoes by 2, milk by 1, pasta by 1, and eggs by 1: This would bring the total to $60.80 (slightly over $60.00). To stay under, you could:
Increase tomatoes by 2, milk by 1, and pasta by 1: Total = $56.60.
Alternatively, increase tomatoes by 3, milk by 1, and pasta by 1: Total = $59.10.
Recommendation
The most practical solution is Option 1, where you increase the quantity of tomatoes by 5, bringing the total to $58.30. This keeps the total just under $60.00 without requiring large adjustments to other items.
Let me know if you'd like further adjustments or alternative suggestions!
Why It’s Impressive:
This is where the assistant truly excels. It uses code to solve a complex optimisation problem, providing multiple solutions with detailed explanations. It even recommends the most practical option, showcasing its decision-making and mathematical prowess.
7. Recipe Suggestions
User Query:
Show recipe suggestions based on my shopping list
AI Response:
[
'Tomato and Egg Scramble: A quick breakfast using Tomatoes and Eggs.',
'Pasta with Tomato Sauce: A classic dish using Pasta and Tomatoes.',
'Bread and Milk Pudding: A simple dessert using Bread and Milk.',
'Egg and Bread French Toast: A delicious breakfast using Eggs and Bread.',
'Pasta Carbonara: A creamy pasta dish using Pasta, Eggs, and Milk.'
]
Why It’s Impressive::
The assistant efficiently queries the shopping list and generates relevant recipe suggestions based on the available items. It showcases contextual understanding and creativity by proposing thoughtful meal ideas. With clear and concise recipe titles, it ensures easy comprehension for the user. Additionally, it maximises the value of existing data by transforming a simple shopping list into actionable meal options.
User Query:
To make Pasta Carbonara, you are missing the following ingredients: Pancetta (or Bacon), Parmesan Cheese, Black Pepper. Please add them to your shopping list.
AI Response:
To make Pasta Carbonara, you are missing the following ingredients: Pancetta (or Bacon), Parmesan Cheese, Black Pepper. Please add them to your shopping list.
Analysis:
Summary
The assistant dynamically writes and executes code to interact with backend APIs, enabling it to read and update data in an external shopping list application rather than maintaining the list internally. This capability allows for real-time integration, ensuring accuracy and seamless data management.
Innovative Technology Leader | Automation, AI & Cloud Evangelist | Collaborative Leadership and Team Building
1 个月BTW: All the code for this demo was created using Cline with DeepSeek V3