{ "cells": [ { "cell_type": "markdown", "id": "fc9a9b11", "metadata": {}, "source": [ "## Build and Test the Agent\n" ] }, { "cell_type": "markdown", "id": "7c721e15", "metadata": {}, "source": [ "**Fetch Questions**" ] }, { "cell_type": "code", "execution_count": 2, "id": "a8ff3339", "metadata": {}, "outputs": [], "source": [ "import requests\n", "\n", "DEFAULT_API_URL = \"https://agents-course-unit4-scoring.hf.space\"\n", "\n", "api_url = DEFAULT_API_URL\n", "questions_url = f\"{api_url}/questions\"\n", "submit_url = f\"{api_url}/submit\"\n", "\n", "response = requests.get(questions_url)\n", "response.raise_for_status()\n", "questions_data = response.json()" ] }, { "cell_type": "code", "execution_count": 3, "id": "da4bec7d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Question 1: How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia.\n", "Question 2: In the video https://www.youtube.com/watch?v=L1vXCYZAYYM, what is the highest number of bird species to be on camera simultaneously?\n", "Question 3: .rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI\n", "Question 4: Review the chess position provided in the image. It is black's turn. Provide the correct next move for black which guarantees a win. Please provide your response in algebraic notation.\n", "Question 5: Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?\n" ] } ], "source": [ "for i, item in enumerate(questions_data[0:5]):\n", " print(f\"Question {i + 1}: {item['question']}\")\n", "\n", "\n", "# Example\n", "#questions_data[3]\n", "# questions_data[0:5]\n" ] }, { "cell_type": "markdown", "id": "1efa91ee", "metadata": {}, "source": [ "## BUILD AGENT\n" ] }, { "cell_type": "markdown", "id": "3b6e6e2b", "metadata": {}, "source": [ "**Log in to HF for Serverless API**" ] }, { "cell_type": "code", "execution_count": 4, "id": "131f40b4", "metadata": {}, "outputs": [], "source": [ "#from huggingface_hub import login\n", "\n", "#login()" ] }, { "cell_type": "markdown", "id": "91d64b9f", "metadata": {}, "source": [ "**Use a Local LLM Model**\n", "\n", "**LiteLLMModel** allows to easily use a wide range of LLM models. To use local ollama model the model name must be prefixed by `ollama/` at the model_id. " ] }, { "cell_type": "code", "execution_count": 5, "id": "48f92e59", "metadata": {}, "outputs": [], "source": [ "from smolagents import LiteLLMModel\n", "\n", "model = LiteLLMModel(\n", " model_id = \"ollama/llama3.2:latest\",\n", " api_base = \"http://127.0.0.1:11434\",\n", " num_ctx = 8192,\n", "\n", ")" ] }, { "cell_type": "markdown", "id": "7dbaa9ed", "metadata": {}, "source": [ "### **Small Agents**" ] }, { "cell_type": "code", "execution_count": 6, "id": "251b8788", "metadata": {}, "outputs": [], "source": [ "from smolagents import CodeAgent, DuckDuckGoSearchTool, ToolCallingAgent\n", "\n", "code_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=model,\n", " name=\"CodeAgent\",\n", " description=\"A code agent that can write and execute code to answer questions.\",\n", " max_steps=5,\n", ")\n", "\n", "Web_Search_agent = ToolCallingAgent(tools=[DuckDuckGoSearchTool()], model=model,\n", " name=\"Web_Search_agent\",\n", " description=\"A web search agent that can answer questions using the DuckDuckGo search engine.\",\n", " max_steps=5,\n", ")" ] }, { "cell_type": "code", "execution_count": 7, "id": "aaf5c107", "metadata": {}, "outputs": [], "source": [ "#agent.run(\"Search for the best music recommendations for a party at the Wayne's mansion.\")\n", "\n", "# agent.run(questions_data[0]['question'])\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "c6b67e26", "metadata": {}, "outputs": [], "source": [ "search_tool = DuckDuckGoSearchTool()" ] }, { "cell_type": "code", "execution_count": 9, "id": "d13e0a38", "metadata": {}, "outputs": [], "source": [ "manager_agent = CodeAgent(\n", " model=model,\n", " name=\"ManagerAgent\",\n", " tools=[search_tool],\n", " managed_agents=[code_agent, Web_Search_agent],\n", " description=\"A manager agent that coordinates other agents to answer questions.\",\n", " additional_authorized_imports=[],\n", " planning_interval=5,\n", " verbosity_level=2,\n", " final_answer_checks=[],\n", " max_steps=15,\n", ")" ] } ], "metadata": { "kernelspec": { "display_name": "Agents-HuggingFace-ENV", "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.10.16" } }, "nbformat": 4, "nbformat_minor": 5 }