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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"id": "fc9a9b11",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"## Build and Test the Agent\n"
|
| 9 |
+
]
|
| 10 |
+
},
|
| 11 |
+
{
|
| 12 |
+
"cell_type": "markdown",
|
| 13 |
+
"id": "7c721e15",
|
| 14 |
+
"metadata": {},
|
| 15 |
+
"source": [
|
| 16 |
+
"**Fetch Questions**"
|
| 17 |
+
]
|
| 18 |
+
},
|
| 19 |
+
{
|
| 20 |
+
"cell_type": "code",
|
| 21 |
+
"execution_count": 2,
|
| 22 |
+
"id": "a8ff3339",
|
| 23 |
+
"metadata": {},
|
| 24 |
+
"outputs": [],
|
| 25 |
+
"source": [
|
| 26 |
+
"import requests\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"DEFAULT_API_URL = \"https://agents-course-unit4-scoring.hf.space\"\n",
|
| 29 |
+
"\n",
|
| 30 |
+
"api_url = DEFAULT_API_URL\n",
|
| 31 |
+
"questions_url = f\"{api_url}/questions\"\n",
|
| 32 |
+
"submit_url = f\"{api_url}/submit\"\n",
|
| 33 |
+
"\n",
|
| 34 |
+
"response = requests.get(questions_url)\n",
|
| 35 |
+
"response.raise_for_status()\n",
|
| 36 |
+
"questions_data = response.json()"
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"cell_type": "code",
|
| 41 |
+
"execution_count": 3,
|
| 42 |
+
"id": "da4bec7d",
|
| 43 |
+
"metadata": {},
|
| 44 |
+
"outputs": [
|
| 45 |
+
{
|
| 46 |
+
"name": "stdout",
|
| 47 |
+
"output_type": "stream",
|
| 48 |
+
"text": [
|
| 49 |
+
"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",
|
| 50 |
+
"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",
|
| 51 |
+
"Question 3: .rewsna eht sa \"tfel\" drow eht fo etisoppo eht etirw ,ecnetnes siht dnatsrednu uoy fI\n",
|
| 52 |
+
"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",
|
| 53 |
+
"Question 5: Who nominated the only Featured Article on English Wikipedia about a dinosaur that was promoted in November 2016?\n"
|
| 54 |
+
]
|
| 55 |
+
}
|
| 56 |
+
],
|
| 57 |
+
"source": [
|
| 58 |
+
"for i, item in enumerate(questions_data[0:5]):\n",
|
| 59 |
+
" print(f\"Question {i + 1}: {item['question']}\")\n",
|
| 60 |
+
"\n",
|
| 61 |
+
"\n",
|
| 62 |
+
"# Example\n",
|
| 63 |
+
"#questions_data[3]\n",
|
| 64 |
+
"# questions_data[0:5]\n"
|
| 65 |
+
]
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"cell_type": "markdown",
|
| 69 |
+
"id": "1efa91ee",
|
| 70 |
+
"metadata": {},
|
| 71 |
+
"source": [
|
| 72 |
+
"## BUILD AGENT\n"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "markdown",
|
| 77 |
+
"id": "3b6e6e2b",
|
| 78 |
+
"metadata": {},
|
| 79 |
+
"source": [
|
| 80 |
+
"**Log in to HF for Serverless API**"
|
| 81 |
+
]
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"cell_type": "code",
|
| 85 |
+
"execution_count": 4,
|
| 86 |
+
"id": "131f40b4",
|
| 87 |
+
"metadata": {},
|
| 88 |
+
"outputs": [],
|
| 89 |
+
"source": [
|
| 90 |
+
"#from huggingface_hub import login\n",
|
| 91 |
+
"\n",
|
| 92 |
+
"#login()"
|
| 93 |
+
]
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
"cell_type": "markdown",
|
| 97 |
+
"id": "91d64b9f",
|
| 98 |
+
"metadata": {},
|
| 99 |
+
"source": [
|
| 100 |
+
"**Use a Local LLM Model**\n",
|
| 101 |
+
"\n",
|
| 102 |
+
"**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. "
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": 5,
|
| 108 |
+
"id": "48f92e59",
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"outputs": [],
|
| 111 |
+
"source": [
|
| 112 |
+
"from smolagents import LiteLLMModel\n",
|
| 113 |
+
"\n",
|
| 114 |
+
"model = LiteLLMModel(\n",
|
| 115 |
+
" model_id = \"ollama/llama3.2:latest\",\n",
|
| 116 |
+
" api_base = \"http://127.0.0.1:11434\",\n",
|
| 117 |
+
" num_ctx = 8192,\n",
|
| 118 |
+
"\n",
|
| 119 |
+
")"
|
| 120 |
+
]
|
| 121 |
+
},
|
| 122 |
+
{
|
| 123 |
+
"cell_type": "markdown",
|
| 124 |
+
"id": "7dbaa9ed",
|
| 125 |
+
"metadata": {},
|
| 126 |
+
"source": [
|
| 127 |
+
"### **Small Agents**"
|
| 128 |
+
]
|
| 129 |
+
},
|
| 130 |
+
{
|
| 131 |
+
"cell_type": "code",
|
| 132 |
+
"execution_count": 6,
|
| 133 |
+
"id": "251b8788",
|
| 134 |
+
"metadata": {},
|
| 135 |
+
"outputs": [],
|
| 136 |
+
"source": [
|
| 137 |
+
"from smolagents import CodeAgent, DuckDuckGoSearchTool, ToolCallingAgent\n",
|
| 138 |
+
"\n",
|
| 139 |
+
"code_agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=model,\n",
|
| 140 |
+
" name=\"CodeAgent\",\n",
|
| 141 |
+
" description=\"A code agent that can write and execute code to answer questions.\",\n",
|
| 142 |
+
" max_steps=5,\n",
|
| 143 |
+
")\n",
|
| 144 |
+
"\n",
|
| 145 |
+
"Web_Search_agent = ToolCallingAgent(tools=[DuckDuckGoSearchTool()], model=model,\n",
|
| 146 |
+
" name=\"Web_Search_agent\",\n",
|
| 147 |
+
" description=\"A web search agent that can answer questions using the DuckDuckGo search engine.\",\n",
|
| 148 |
+
" max_steps=5,\n",
|
| 149 |
+
")"
|
| 150 |
+
]
|
| 151 |
+
},
|
| 152 |
+
{
|
| 153 |
+
"cell_type": "code",
|
| 154 |
+
"execution_count": 7,
|
| 155 |
+
"id": "aaf5c107",
|
| 156 |
+
"metadata": {},
|
| 157 |
+
"outputs": [],
|
| 158 |
+
"source": [
|
| 159 |
+
"#agent.run(\"Search for the best music recommendations for a party at the Wayne's mansion.\")\n",
|
| 160 |
+
"\n",
|
| 161 |
+
"# agent.run(questions_data[0]['question'])\n",
|
| 162 |
+
"\n"
|
| 163 |
+
]
|
| 164 |
+
},
|
| 165 |
+
{
|
| 166 |
+
"cell_type": "code",
|
| 167 |
+
"execution_count": 8,
|
| 168 |
+
"id": "c6b67e26",
|
| 169 |
+
"metadata": {},
|
| 170 |
+
"outputs": [],
|
| 171 |
+
"source": [
|
| 172 |
+
"search_tool = DuckDuckGoSearchTool()"
|
| 173 |
+
]
|
| 174 |
+
},
|
| 175 |
+
{
|
| 176 |
+
"cell_type": "code",
|
| 177 |
+
"execution_count": 9,
|
| 178 |
+
"id": "d13e0a38",
|
| 179 |
+
"metadata": {},
|
| 180 |
+
"outputs": [],
|
| 181 |
+
"source": [
|
| 182 |
+
"manager_agent = CodeAgent(\n",
|
| 183 |
+
" model=model,\n",
|
| 184 |
+
" name=\"ManagerAgent\",\n",
|
| 185 |
+
" tools=[search_tool],\n",
|
| 186 |
+
" managed_agents=[code_agent, Web_Search_agent],\n",
|
| 187 |
+
" description=\"A manager agent that coordinates other agents to answer questions.\",\n",
|
| 188 |
+
" additional_authorized_imports=[],\n",
|
| 189 |
+
" planning_interval=5,\n",
|
| 190 |
+
" verbosity_level=2,\n",
|
| 191 |
+
" final_answer_checks=[],\n",
|
| 192 |
+
" max_steps=15,\n",
|
| 193 |
+
")"
|
| 194 |
+
]
|
| 195 |
+
}
|
| 196 |
+
],
|
| 197 |
+
"metadata": {
|
| 198 |
+
"kernelspec": {
|
| 199 |
+
"display_name": "Agents-HuggingFace-ENV",
|
| 200 |
+
"language": "python",
|
| 201 |
+
"name": "python3"
|
| 202 |
+
},
|
| 203 |
+
"language_info": {
|
| 204 |
+
"codemirror_mode": {
|
| 205 |
+
"name": "ipython",
|
| 206 |
+
"version": 3
|
| 207 |
+
},
|
| 208 |
+
"file_extension": ".py",
|
| 209 |
+
"mimetype": "text/x-python",
|
| 210 |
+
"name": "python",
|
| 211 |
+
"nbconvert_exporter": "python",
|
| 212 |
+
"pygments_lexer": "ipython3",
|
| 213 |
+
"version": "3.10.16"
|
| 214 |
+
}
|
| 215 |
+
},
|
| 216 |
+
"nbformat": 4,
|
| 217 |
+
"nbformat_minor": 5
|
| 218 |
+
}
|
requirements.txt
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
-
smolagents
|
| 4 |
-
time
|
|
|
|
| 1 |
gradio
|
| 2 |
requests
|
| 3 |
+
smolagents
|
|
|