Exploring Smol Agents: Building an Intelligent Shopping List Assistant
AI Shopping List Agent - DALL-E

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

  • Simplicity and Minimalism: Easy to understand, modify, and extend.
  • Support for Multiple LLMs: Works with models from Hugging Face, OpenAI, Anthropic, and more via LiteLLM integration.
  • Code Agents: Write and execute Python code snippets to perform actions, reducing the number of steps and LLM calls.
  • Secure Execution: Supports sandboxed environments like E2B for safe code execution.
  • Multi-Agent Systems: Enables collaboration between multiple agents to solve complex tasks.


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.



AI Shopping List Agent - DALL-E

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:

  1. Relatability: Everyone shops. Whether it’s groceries, household items, or even planning for a special event, managing a shopping list is a universal task. By focusing on this use case, I could create something that feels familiar and immediately useful to a broad audience.
  2. Complexity in Simplicity: On the surface, a shopping list seems simple - just a list of items. But when you dig deeper, it involves budgeting, prioritisation, optimisation, and even creativity (like suggesting recipes). This complexity made it an ideal candidate to test the smol agent’s ability to handle multi-step tasks and intricate queries.
  3. DeepSeek’s Potential: I’ve been intrigued by DeepSeek Coder V3 since its release a few weeks ago. Its ability to understand and generate code with precision made it a perfect fit for this project. I wanted to see how well it could handle tasks like calculating costs, optimising lists, and even suggesting recipes - all while writing and executing code behind the scenes.
  4. A Learning Exercise: Building this assistant wasn’t just about showcasing smol agents; it was also a personal challenge. I wanted to push myself to understand the nuances of the framework, experiment with custom prompts, and learn how to fine-tune the agent’s behaviour. The shopping list assistant provided the perfect playground for this exploration.
  5. Real-World Integration: By designing the assistant to interact with an external shopping list application (rather than maintaining the list internally), I could demonstrate how smol agents can integrate with existing systems. This approach highlighted the framework’s potential for real-world applications, where agents need to work seamlessly with other tools and APIs.

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.


Simple Shopping List Schema

Code available here

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

  • call the get_item_details_tool using name=apples to get SKU and price
  • call update_list_tool : SKU, quantity, action=add

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:

  • call the get_shopping_list_tool to get a list of items - SKU + quantity
  • for each item call the get_item_details_tool using SKU to get the item name



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:

  • The agent correctly interprets the budget constraints and relative quantities.
  • It handles multiple item types and performs the necessary calculations to maintain the budget.


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:

  • Develops execution plans independently
  • Writes and runs code as needed
  • Interprets results and adjusts actions accordingly
  • Adapts to various scenarios

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:

  1. Choice of LLM: The selection of the appropriate LLM is crucial. I struggled with previous generation models, but DeepSeek Coder V3 worked out of the box, providing the necessary capabilities for complex tasks.
  2. Tool Descriptions: Detailed tool descriptions, including examples of response formats, are essential. Initially, minimal descriptions led to misunderstandings and incorrect responses from the agent.
  3. Custom System Prompt: Creating a custom system prompt was vital to set the business context for the agent. The default prompts lacked the specific guidelines needed for the shopping list assistant.



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

AI Shopping List Agent - DALL-E

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:

  • The assistant uses code to provide a structured and detailed response, including item names, quantities, and unit prices.
  • I had set up the custom prompt to prohibit the return of data structures on responses, but here we see an example where this didn't work


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:

  • The assistant identifies missing ingredients for a specific recipe and provides clear instructions. It writes code to query existing items in the shopping list
  • Shows potential for further integration with recipe-based shopping list management.


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.

  • Natural Language Processing: Effectively understands and processes complex, conversational queries.
  • Advanced Command Handling: Manages multi-part instructions, monetary values, and relative quantities with precision.
  • Budget Optimisation: Adjusts quantities intelligently to enforce budget constraints while maximising value.
  • Mathematical Accuracy: Solves complex calculations, including quantity adjustments based on financial goals.
  • Structured Output: Presents information in a clear, professional, and easy-to-read format.
  • Robust Error Handling: Gracefully manages edge cases such as empty lists and budget limitations.
  • Context Awareness: Offers smart recipe suggestions and identifies missing ingredients based on shopping list data.




Jan Varga

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

回复

要查看或添加评论,请登录

Jan Varga的更多文章

  • Slack Smarter: Knowledge from Chat

    Slack Smarter: Knowledge from Chat

    Building on the idea of making knowledge sharing easier for engineers, as discussed in my previous article - How to Get…

  • How to Get Your Engineers Engaged in Knowledge Sharing

    How to Get Your Engineers Engaged in Knowledge Sharing

    If you’ve ever tried to encourage engineers to share knowledge, you know it’s not easy. In theory, everyone benefits…

    1 条评论
  • Engineering Reimagined: A GenAI Roadmap for a Future of Innovation

    Engineering Reimagined: A GenAI Roadmap for a Future of Innovation

    Laying the Groundwork for a Revolution: Building Your GenAI Foundation with the Right Tools Before we can unlock the…

    2 条评论
  • Reimagining Banking: A Glimpse into the Future with Generative AI

    Reimagining Banking: A Glimpse into the Future with Generative AI

    Imagine a world where your bank understands you like a close friend, anticipates your needs before you even voice them,…

  • Coding Tests Are Irrelevant: Why It’s Time for a New Approach

    Coding Tests Are Irrelevant: Why It’s Time for a New Approach

    The traditional coding test, once a hallmark of technical interviews, is quickly losing its relevance in today’s…

    4 条评论
  • Command Line Rules: A Nostalgic Rant

    Command Line Rules: A Nostalgic Rant

    Back in the day, it was just you, your terminal, and a handful of scripts that got the job done. A time when control…

  • The Grand Compendium

    The Grand Compendium

    Over the last few months I've posted almost 60 articles across a variety of topics. I've spent the last week organising…

    1 条评论
  • AI in Banking

    AI in Banking

    A consolidated list of my articles on AI in Banking Over the last few months I've posted almost 60 articles across a…

    1 条评论
  • GenAI for Data Analytics

    GenAI for Data Analytics

    A consolidated list of my articles on GenAI for Data Analytics Over the last few months I've posted almost 60 articles…

    2 条评论
  • Introducing CRASH: SRE Training with AI-Powered Incident Simulation

    Introducing CRASH: SRE Training with AI-Powered Incident Simulation

    I spent the morning pondering if ChatGPT could act as an SRE copilot. In the afternoon I worked with ChatGPT to create…

    1 条评论

社区洞察

其他会员也浏览了