File size: 5,575 Bytes
7eef1dc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | {
"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
}
|