DEV Community

Roberto Çemeri
Roberto Çemeri

Posted on

My Journey through Hugging Face AI Agent Course – Unit 2.1 Final Quiz Solutions

My Solutions to the Hugging Face AI Agent Course - Unit 2.1 Final Quiz

I recently completed the Hugging Face AI Agent Course and wanted to share my experience, along with the solutions I came up with for the Unit 2.1 Final Quiz. This unit covered important concepts like building basic code agents, multi-agent systems, and configuring agent security, all of which were exciting to work on. Here are the solutions I implemented for each question, along with some takeaways from the course.

Question 1: Create a Basic Code Agent with Web Search Capability
Solution:


from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel

agent = CodeAgent(
    tools=[DuckDuckGoSearchTool()],
    model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
)
Enter fullscreen mode Exit fullscreen mode

In this task, I created a basic CodeAgent that integrates a DuckDuckGo search tool to allow the agent to perform web searches. The model used is the Qwen/Qwen2.5-Coder-32B-Instruct from Hugging Face. This setup enables the agent to search the web for information, which is a fundamental feature for any intelligent assistant.

Question 2: Set Up a Multi-Agent System with Manager and Web Search Agents
Solution:

from smolagents import CodeAgent, ToolCallingAgent, DuckDuckGoSearchTool, HfApiModel, VisitWebpageTool

web_agent = ToolCallingAgent(
    tools=[DuckDuckGoSearchTool(), VisitWebpageTool()], 
    model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
    max_steps=10,
    name="search", 
    description="Agent to perform web searches and visit webpages."
)

manager_agent = CodeAgent(
    model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),
    managed_agents=[web_agent],
    additional_authorized_imports=["pandas", "time", "numpy"]  # Corrected imports
)
Enter fullscreen mode Exit fullscreen mode

For this task, I created a multi-agent system consisting of a manager agent and a web search agent. The web agent uses tools for performing web searches (DuckDuckGoSearchTool) and visiting webpages (VisitWebpageTool). The manager agent oversees the web agent, with authorized imports for additional tools like pandas, time, and numpy.

Question 3: Configure Agent Security Settings
Solution:

from smolagents import CodeAgent, HfApiModel
from smolagents.sandbox import E2BSandbox

model = HfApiModel("Qwen/Qwen2.5-Coder-32B-Instruct")

agent = CodeAgent(
    tools=[],
    model=model,
    sandbox=E2BSandbox(),  # Configure the sandbox
    additional_authorized_imports=["numpy"],  # Authorize numpy import
)
Enter fullscreen mode Exit fullscreen mode

Security is a crucial aspect when deploying AI agents. In this question, I configured the agent with the E2B sandbox to ensure that the execution environment is isolated. Additionally, I specified authorized imports (numpy) to limit the agent's access to only essential libraries, ensuring safe execution.

Question 4: Implement a Tool-Calling Agent
Solution:

from smolagents import ToolCallingAgent, HfApiModel, DuckDuckGoSearchTool

agent = ToolCallingAgent(
    tools=[DuckDuckGoSearchTool()],  
    model=HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"),  
    name="SearchAgent",  
    description="An agent that uses DuckDuckGo to search the web.",  
    max_steps=5,  
)
Enter fullscreen mode Exit fullscreen mode

For this task, I implemented a Tool-Calling Agent that uses the DuckDuckGo search tool to perform web searches. I set the maximum steps (max_steps=5) to limit how many times the agent will invoke the tool, ensuring efficiency and preventing endless loops.

Question 5: Set Up Model Integration
Solution:

from smolagents import HfApiModel, LiteLLMModel

# Initialize Hugging Face model
hf_model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")

# Initialize LiteLLM model as an alternative model
other_model = LiteLLMModel(model_id="anthropic/claude-3-sonnet")

# Set the model to hf_model or alternative model
model = hf_model  # Alternatively, you can switch this to `other_model`
Enter fullscreen mode Exit fullscreen mode

In the final task, I set up model integration by configuring two models: a Hugging Face model (hf_model) and a LiteLLM model (other_model). This provides flexibility by allowing me to switch between models based on requirements, providing an alternative model option for different use cases.

My Takeaways

Tool Integration: I learned the importance of integrating multiple tools into AI agents to extend their capabilities. For instance, the web search tools in the DuckDuckGoSearchTool were a valuable resource for building agents that can gather real-time information from the web.

Security: The use of sandboxes and controlling imports is vital to ensure that agents operate in a safe and controlled environment. This is something that I will definitely keep in mind when working on future AI agent projects.

Model Flexibility: Using different models (like Hugging Face models and LiteLLM) opened my eyes to the flexibility in model selection. Different models may suit different tasks, and having the ability to switch models based on needs is a key feature of building adaptable agents.

Multi-Agent Systems: This was my first time building a multi-agent system, and I found it fascinating how agents can work in tandem, with one being the manager and others performing specific tasks. This approach is essential when building more complex systems.

If you're working on AI agents or exploring the Hugging Face ecosystem, I highly recommend going through this course. It’s a great way to learn how to integrate models, tools, and security practices while building intelligent agents!

Top comments (1)

Collapse
 
alexeyslvv profile image
AlexeySlvv

Sorry, but how did you get the solution for question 3? I don't understand because there is no sandbox package with E2Bsandbox in smolagents.