|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "93a2ace1-2f96-4bf0-9c7f-55e37799ad09", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Our First LLM Agent" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 1, |
| 14 | + "id": "1c074c89-e290-4f16-b4c3-80dbcb2541a2", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "from logging import DEBUG\n", |
| 19 | + "\n", |
| 20 | + "from llm_agents_from_scratch import LLMAgent\n", |
| 21 | + "from llm_agents_from_scratch.logger import get_logger, set_log_level\n", |
| 22 | + "\n", |
| 23 | + "set_log_level(DEBUG)\n", |
| 24 | + "nb_logger = get_logger(\"notebook\")" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "id": "37a14d79-bd26-4b19-966d-4f29047cebb5", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "## Create an `LLMAgent`" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "id": "d48e30aa-caa8-4984-9c98-048c86880586", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "### Define the backbone LLM" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 2, |
| 46 | + "id": "578979fe-3de7-4eec-acd6-fb90c876481b", |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "from llm_agents_from_scratch.llms import OllamaLLM" |
| 51 | + ] |
| 52 | + }, |
| 53 | + { |
| 54 | + "cell_type": "code", |
| 55 | + "execution_count": 3, |
| 56 | + "id": "65f3a83d-b3bc-4e76-ba47-7e24120d33b3", |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "llm = OllamaLLM(model=\"qwen2.5:3b\")" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "markdown", |
| 65 | + "id": "e173fe6d-239e-4129-a97a-10999f2a0732", |
| 66 | + "metadata": {}, |
| 67 | + "source": [ |
| 68 | + "### Define Tools" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": 4, |
| 74 | + "id": "7477b302-af47-49de-a7e3-741586d90261", |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "from llm_agents_from_scratch import (\n", |
| 79 | + " SimpleFunctionTool,\n", |
| 80 | + ")" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "markdown", |
| 85 | + "id": "eb5cd91c-2123-48f4-9548-4d4cfa960bda", |
| 86 | + "metadata": {}, |
| 87 | + "source": [ |
| 88 | + "#### Simple function tools\n", |
| 89 | + "\n", |
| 90 | + "These tools use manual and simple conversions from Python types to valid JSON schema types." |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": 5, |
| 96 | + "id": "823e9414-2c20-4c33-8205-08bd162e7da2", |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "def add_one(x: str) -> float:\n", |
| 101 | + " \"\"\"Adds one to a given number.\"\"\"\n", |
| 102 | + " return float(x) + 1" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 6, |
| 108 | + "id": "6d6dc843-df79-415a-b87c-d6d7e01df77f", |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "add_one_tool = SimpleFunctionTool(func=add_one)" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": 7, |
| 118 | + "id": "0ac20ebb-d9e7-4fc8-92cc-326950c6cb9c", |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [ |
| 121 | + { |
| 122 | + "data": { |
| 123 | + "text/plain": [ |
| 124 | + "ToolCallResult(tool_call=ToolCall(tool_name='add_one', arguments={'x': '3.15'}), content='4.15', error=False)" |
| 125 | + ] |
| 126 | + }, |
| 127 | + "execution_count": 7, |
| 128 | + "metadata": {}, |
| 129 | + "output_type": "execute_result" |
| 130 | + } |
| 131 | + ], |
| 132 | + "source": [ |
| 133 | + "from llm_agents_from_scratch.data_structures.tool import ToolCall\n", |
| 134 | + "\n", |
| 135 | + "# test the tool\n", |
| 136 | + "tool_call = ToolCall(\n", |
| 137 | + " tool_name=add_one_tool.name,\n", |
| 138 | + " arguments={\"x\": \"3.15\"},\n", |
| 139 | + ")\n", |
| 140 | + "add_one_tool(tool_call)" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "markdown", |
| 145 | + "id": "3dff626b-eab2-4b44-8220-ad2b19f502c3", |
| 146 | + "metadata": {}, |
| 147 | + "source": [ |
| 148 | + "### Define the LLM Agent" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "code", |
| 153 | + "execution_count": 8, |
| 154 | + "id": "7ebe27bb-de33-4406-9e11-a5f427ef7ac7", |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [], |
| 157 | + "source": [ |
| 158 | + "agent = LLMAgent(\n", |
| 159 | + " llm=llm,\n", |
| 160 | + " tools=[add_one_tool],\n", |
| 161 | + ")" |
| 162 | + ] |
| 163 | + }, |
| 164 | + { |
| 165 | + "cell_type": "markdown", |
| 166 | + "id": "fcbe332e-5a33-4f1e-b111-ca84e02596ee", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "## Run a simple task" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": 9, |
| 175 | + "id": "4d09a484-9eb7-4525-923e-d00ebfe0434a", |
| 176 | + "metadata": {}, |
| 177 | + "outputs": [], |
| 178 | + "source": [ |
| 179 | + "from llm_agents_from_scratch.data_structures.agent import Task" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "code", |
| 184 | + "execution_count": 10, |
| 185 | + "id": "1d5d512e-d400-4398-980f-a4142d367add", |
| 186 | + "metadata": {}, |
| 187 | + "outputs": [], |
| 188 | + "source": [ |
| 189 | + "task = Task(\n", |
| 190 | + " instruction=\"Add one to fifty-five point three. Use an appropriate tool!\",\n", |
| 191 | + ")" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": 11, |
| 197 | + "id": "e99cca54-0d21-4c9d-9a17-bf595d110fe5", |
| 198 | + "metadata": {}, |
| 199 | + "outputs": [ |
| 200 | + { |
| 201 | + "name": "stdout", |
| 202 | + "output_type": "stream", |
| 203 | + "text": [ |
| 204 | + "INFO (llm_agents_fs.LLMAgent) : 🚀 Starting task: Add one to fifty-five point three. Use an appropriate tool!\n", |
| 205 | + "DEBUG (llm_agents_fs.TaskHandler) : 🧵 Rollout: \n", |
| 206 | + "INFO (llm_agents_fs.TaskHandler) : 🧠 New Step: Add one to fifty-five point three. Use an appropriate tool!\n", |
| 207 | + "INFO (llm_agents_fs.TaskHandler) : ⚙️ Processing Step: Add one to fifty-five point three. Use an appropriate tool!\n", |
| 208 | + "DEBUG (llm_agents_fs.TaskHandler) : 🧵 Rollout: \n", |
| 209 | + "DEBUG (llm_agents_fs.TaskHandler) : 💬 SYSTEM: You are a helpful assistant.\n", |
| 210 | + "DEBUG (llm_agents_fs.TaskHandler) : 💬 USER: Add one to fifty-five point three. Use an appropriate tool!\n", |
| 211 | + "DEBUG (llm_agents_fs.TaskHandler) : 💬 ASSISTANT: \n", |
| 212 | + "INFO (llm_agents_fs.TaskHandler) : 🛠️ Executing Tool Call: add_one\n", |
| 213 | + "INFO (llm_agents_fs.TaskHandler) : ✅ Successful Tool Call: 56.3\n", |
| 214 | + "INFO (llm_agents_fs.TaskHandler) : ✅ Step Result: After adding one to fifty-five point three, the result is 56.3.\n", |
| 215 | + "DEBUG (llm_agents_fs.TaskHandler) : 🧵 Rollout: user: Add one to fifty-five point three. Use an appropriate tool!\n", |
| 216 | + "assistant: I need to make a tool call(s) to add_one\n", |
| 217 | + "tool: \n", |
| 218 | + "The below is a tool call response for a given tool call.\n", |
| 219 | + "<tool-call>\n", |
| 220 | + "tool name: add_one\n", |
| 221 | + "arguments: {'x': '55.3'}\n", |
| 222 | + "</tool-call>\n", |
| 223 | + "\n", |
| 224 | + "<result>\n", |
| 225 | + "56.3\n", |
| 226 | + "</result>\n", |
| 227 | + "\n", |
| 228 | + "assistant: After adding one to fifty-five point three, the result is 56.3.\n", |
| 229 | + "INFO (llm_agents_fs.TaskHandler) : 🧠 New Step: There is sufficient context to complete the task within the provided history — another tool call may not even be necessary! Proceed with completion and mark this as the last step. NOTE: that the real user has not seen this dialogue, and so just provide the final task result without referencing it.\n", |
| 230 | + "INFO (llm_agents_fs.TaskHandler) : ⚙️ Processing Step: There is sufficient context to complete the task within the provided history — another tool call may not even be necessary! Proceed with completion and mark this as the last step. NOTE: that the real user has not seen this dialogue, and so just provide the final task result without referencing it.\n", |
| 231 | + "DEBUG (llm_agents_fs.TaskHandler) : 🧵 Rollout: user: Add one to fifty-five point three. Use an appropriate tool!\n", |
| 232 | + "assistant: I need to make a tool call(s) to add_one\n", |
| 233 | + "tool: \n", |
| 234 | + "The below is a tool call response for a given tool call.\n", |
| 235 | + "<tool-call>\n", |
| 236 | + "tool name: add_one\n", |
| 237 | + "arguments: {'x': '55.3'}\n", |
| 238 | + "</tool-call>\n", |
| 239 | + "\n", |
| 240 | + "<result>\n", |
| 241 | + "56.3\n", |
| 242 | + "</result>\n", |
| 243 | + "\n", |
| 244 | + "assistant: After adding one to fifty-five point three, the result is 56.3.\n", |
| 245 | + "DEBUG (llm_agents_fs.TaskHandler) : 💬 SYSTEM: You are a helpful assistant.\n", |
| 246 | + "\n", |
| 247 | + "Here is some past dialogue and context, where another assistant was working\n", |
| 248 | + "towards completing the task.\n", |
| 249 | + "\n", |
| 250 | + "<history>\n", |
| 251 | + "user: Add one to fifty-five point three. Use an appropriate tool!\n", |
| 252 | + "assistant: I need to make a tool call(s) to add_one\n", |
| 253 | + "tool: \n", |
| 254 | + "The below is a tool call response for a given tool call.\n", |
| 255 | + "<tool-call>\n", |
| 256 | + "tool name: add_one\n", |
| 257 | + "arguments: {'x': '55.3'}\n", |
| 258 | + "</tool-call>\n", |
| 259 | + "\n", |
| 260 | + "<result>\n", |
| 261 | + "56.3\n", |
| 262 | + "</result>\n", |
| 263 | + "\n", |
| 264 | + "assistant: After adding one to fifty-five point three, the result is 56.3.\n", |
| 265 | + "</history>\n", |
| 266 | + "\n", |
| 267 | + "DEBUG (llm_agents_fs.TaskHandler) : 💬 USER: There is sufficient context to complete the task within the provided history — another tool call may not even be necessary! Proceed with completion and mark this as the last step. NOTE: that the real user has not seen this dialogue, and so just provide the final task result without referencing it.\n", |
| 268 | + "DEBUG (llm_agents_fs.TaskHandler) : 💬 ASSISTANT: \n", |
| 269 | + "INFO (llm_agents_fs.TaskHandler) : 🛠️ Executing Tool Call: add_one\n", |
| 270 | + "INFO (llm_agents_fs.TaskHandler) : ✅ Successful Tool Call: 56.3\n", |
| 271 | + "INFO (llm_agents_fs.TaskHandler) : ✅ Step Result: After adding one to fifty-five point three, the result is 56.3.\n", |
| 272 | + "INFO (llm_agents_fs.LLMAgent) : 🏁 Task completed: After adding one to fifty-five point three, the result is 56.3.\n" |
| 273 | + ] |
| 274 | + } |
| 275 | + ], |
| 276 | + "source": [ |
| 277 | + "result = await agent.run(task)" |
| 278 | + ] |
| 279 | + }, |
| 280 | + { |
| 281 | + "cell_type": "code", |
| 282 | + "execution_count": 12, |
| 283 | + "id": "0fde7c61-f0d9-4ef9-b0b6-90386667bbb8", |
| 284 | + "metadata": {}, |
| 285 | + "outputs": [ |
| 286 | + { |
| 287 | + "data": { |
| 288 | + "text/plain": [ |
| 289 | + "TaskResult(task=Task(instruction='Add one to fifty-five point three. Use an appropriate tool!'), content='After adding one to fifty-five point three, the result is 56.3.', rollout=\"user: Add one to fifty-five point three. Use an appropriate tool!\\nassistant: I need to make a tool call(s) to add_one\\ntool: \\nThe below is a tool call response for a given tool call.\\n<tool-call>\\ntool name: add_one\\narguments: {'x': '55.3'}\\n</tool-call>\\n\\n<result>\\n56.3\\n</result>\\n\\nassistant: After adding one to fifty-five point three, the result is 56.3.user: There is sufficient context to complete the task within the provided history — another tool call may not even be necessary! Proceed with completion and mark this as the last step. NOTE: that the real user has not seen this dialogue, and so just provide the final task result without referencing it.\\nassistant: I need to make a tool call(s) to add_one\\ntool: \\nThe below is a tool call response for a given tool call.\\n<tool-call>\\ntool name: add_one\\narguments: {'x': '55.3'}\\n</tool-call>\\n\\n<result>\\n56.3\\n</result>\\n\\nassistant: After adding one to fifty-five point three, the result is 56.3.\", error=False)" |
| 290 | + ] |
| 291 | + }, |
| 292 | + "execution_count": 12, |
| 293 | + "metadata": {}, |
| 294 | + "output_type": "execute_result" |
| 295 | + } |
| 296 | + ], |
| 297 | + "source": [ |
| 298 | + "result" |
| 299 | + ] |
| 300 | + }, |
| 301 | + { |
| 302 | + "cell_type": "code", |
| 303 | + "execution_count": null, |
| 304 | + "id": "93b1cfa1-1ac0-420d-b36e-007de5cdfc7f", |
| 305 | + "metadata": {}, |
| 306 | + "outputs": [], |
| 307 | + "source": [] |
| 308 | + } |
| 309 | + ], |
| 310 | + "metadata": { |
| 311 | + "kernelspec": { |
| 312 | + "display_name": "Python 3 (ipykernel)", |
| 313 | + "language": "python", |
| 314 | + "name": "python3" |
| 315 | + }, |
| 316 | + "language_info": { |
| 317 | + "codemirror_mode": { |
| 318 | + "name": "ipython", |
| 319 | + "version": 3 |
| 320 | + }, |
| 321 | + "file_extension": ".py", |
| 322 | + "mimetype": "text/x-python", |
| 323 | + "name": "python", |
| 324 | + "nbconvert_exporter": "python", |
| 325 | + "pygments_lexer": "ipython3", |
| 326 | + "version": "3.13.5" |
| 327 | + } |
| 328 | + }, |
| 329 | + "nbformat": 4, |
| 330 | + "nbformat_minor": 5 |
| 331 | +} |
0 commit comments