8_agents/notebooks/agents.ipynb (196 lines of code) (raw):

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Building AI Agents\n", "\n", "This notebook contains exercises to help you learn how to build different types of agents using the `smolagents` library. We'll progress from basic to more complex implementations.\n", "\n", "## Setup\n", "\n", "First, let's install the required packages:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install smolagents\n", "\n", "# Install the requirements in Google Colab\n", "# !pip install transformers datasets trl huggingface_hub\n", "\n", "# Authenticate to Hugging Face\n", "from huggingface_hub import login\n", "\n", "login()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 🐢 Exercise 1: Basic Code Agent\n", "\n", "Let's start by creating a simple code agent that can answer programming-related questions using web search." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel\n", "\n", "# Initialize the agent\n", "agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())\n", "\n", "# Test the agent\n", "response = agent.run(\"What's the difference between a list and a tuple in Python?\")\n", "print(response)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 🤔 Exercise 1 Challenge\n", "Try asking the agent to explain different programming concepts and evaluate its responses. How well does it handle:\n", "1. Basic syntax questions\n", "2. Language-specific features\n", "3. Code examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 🐕 Exercise 2: Agent with Custom Functions\n", "\n", "Now let's create an agent that can perform specific tasks using custom functions. We'll implement a simple calculator tool." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from smolagents import CodeAgent, tool\n", "from typing import Union\n", "\n", "\n", "@tool\n", "def calculate(operation: str, numbers: object) -> float:\n", " \"\"\"Performs basic mathematical operations on a list of numbers.\n", "\n", " Args:\n", " operation: One of 'sum', 'average', 'multiply', 'min', 'max'\n", " numbers: List of numbers to operate on\n", "\n", " Returns:\n", " float: Result of the operation\n", " \"\"\"\n", " if operation == \"sum\":\n", " return sum(numbers)\n", " elif operation == \"average\":\n", " return sum(numbers) / len(numbers)\n", " elif operation == \"multiply\":\n", " result = 1\n", " for n in numbers:\n", " result *= n\n", " return result\n", " elif operation == \"min\":\n", " return min(numbers)\n", " elif operation == \"max\":\n", " return max(numbers)\n", " else:\n", " raise ValueError(f\"Unknown operation: {operation}\")\n", "\n", "\n", "# Create agent with custom tool\n", "math_agent = CodeAgent(tools=[calculate], model=HfApiModel())\n", "\n", "# Test the agent\n", "response = math_agent.run(\"What is the average of 10, 15, 20, 25, and 30?\")\n", "print(response)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 🤔 Exercise 2 Challenge\n", "1. Add more mathematical operations to the calculator tool\n", "2. Create a new custom tool (e.g., for string manipulation or date calculations)\n", "3. Combine multiple custom tools in one agent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 🦁 Exercise 3: Advanced Retrieval Agent\n", "\n", "Finally, let's build a more sophisticated agent that combines web search with memory to maintain context across conversations." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from smolagents import CodeAgent, DuckDuckGoSearchTool\n", "\n", "# Initialize the agent with memory\n", "research_agent = CodeAgent(...) # TODO: Define the agent\n", "\n", "# Test with a multi-turn conversation\n", "questions = [\n", " \"What are the main types of machine learning?\",\n", " \"Can you explain supervised learning in more detail?\",\n", " \"What are some popular algorithms for this type?\",\n", "]\n", "\n", "# TODO: Test the agent" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 🤔 Exercise 3 Challenge\n", "1. Test how well the agent maintains context across different topics\n", "2. Implement a custom knowledge base tool (as shown in the retrieval_agents.md example)\n", "3. Create a hybrid agent that combines code understanding with research capabilities" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.8" } }, "nbformat": 4, "nbformat_minor": 2 }