|
55 | 55 | }, |
56 | 56 | { |
57 | 57 | "cell_type": "markdown", |
58 | | - "id": "c1e3fae2-e573-4a8f-be54-9a1519449f6b", |
| 58 | + "id": "e6281fc2-5412-446c-8c80-3226eaeabe5a", |
59 | 59 | "metadata": {}, |
60 | 60 | "source": [ |
61 | | - "The code in the book uses `asyncio.run()` to execute coroutines. To align the book code with this Jupyter notebook, we'll use the `nest_asyncio` library, which allows for nested async event loops." |
| 61 | + "### Example 1: Instantiating an `LLMAgent`" |
62 | 62 | ] |
63 | 63 | }, |
64 | 64 | { |
65 | 65 | "cell_type": "code", |
66 | 66 | "execution_count": 1, |
67 | | - "id": "dc4524f7-f7fc-4fc9-805d-8a94e5004e9b", |
| 67 | + "id": "bc0ab1b8-aec1-49df-a63e-60ab2317be2f", |
68 | 68 | "metadata": {}, |
69 | 69 | "outputs": [], |
70 | 70 | "source": [ |
71 | | - "# This will allow us to execute `asyncio.run()` calls\n", |
72 | | - "import nest_asyncio\n", |
73 | | - "nest_asyncio.apply()" |
| 71 | + "from llm_agents_from_scratch.llms import OllamaLLM\n", |
| 72 | + "from llm_agents_from_scratch import LLMAgent\n", |
| 73 | + "\n", |
| 74 | + "llm = OllamaLLM(model=\"qwen2.5:3b\")\n", |
| 75 | + "llm_agent = LLMAgent(\n", |
| 76 | + " llm=llm,\n", |
| 77 | + ")" |
74 | 78 | ] |
75 | 79 | }, |
76 | 80 | { |
77 | | - "cell_type": "markdown", |
78 | | - "id": "e6281fc2-5412-446c-8c80-3226eaeabe5a", |
| 81 | + "cell_type": "code", |
| 82 | + "execution_count": 2, |
| 83 | + "id": "7f51d14f-ddcf-46e2-b277-f14053a03349", |
79 | 84 | "metadata": {}, |
| 85 | + "outputs": [ |
| 86 | + { |
| 87 | + "data": { |
| 88 | + "text/plain": [ |
| 89 | + "[]" |
| 90 | + ] |
| 91 | + }, |
| 92 | + "execution_count": 2, |
| 93 | + "metadata": {}, |
| 94 | + "output_type": "execute_result" |
| 95 | + } |
| 96 | + ], |
80 | 97 | "source": [ |
81 | | - "### Example 1: ..." |
| 98 | + "llm_agent.tools" |
82 | 99 | ] |
83 | 100 | }, |
84 | | - { |
85 | | - "cell_type": "code", |
86 | | - "execution_count": null, |
87 | | - "id": "bc0ab1b8-aec1-49df-a63e-60ab2317be2f", |
88 | | - "metadata": {}, |
89 | | - "outputs": [], |
90 | | - "source": [] |
91 | | - }, |
92 | 101 | { |
93 | 102 | "cell_type": "markdown", |
94 | 103 | "id": "b81f366e-b2d0-402b-9fb7-984fbd91b01f", |
95 | 104 | "metadata": {}, |
96 | 105 | "source": [ |
97 | | - "### Example 2: ..." |
| 106 | + "### Example 2: Demo usage of `add_tool()`" |
98 | 107 | ] |
99 | 108 | }, |
100 | 109 | { |
101 | 110 | "cell_type": "code", |
102 | | - "execution_count": null, |
| 111 | + "execution_count": 3, |
103 | 112 | "id": "ba842320-9881-4150-b392-73fe9f9dab79", |
104 | 113 | "metadata": {}, |
105 | 114 | "outputs": [], |
106 | | - "source": [] |
| 115 | + "source": [ |
| 116 | + "from llm_agents_from_scratch.tools import SimpleFunctionTool\n", |
| 117 | + "\n", |
| 118 | + "def add_one(x: int) -> int:\n", |
| 119 | + " \"\"\"A dummy tool for adding one to the supplied number.\"\"\"\n", |
| 120 | + " return x + 1\n", |
| 121 | + " \n", |
| 122 | + "tool = SimpleFunctionTool(func=add_one)" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": 4, |
| 128 | + "id": "94d573ca-adf3-4c2f-b932-344bc2466873", |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [ |
| 131 | + { |
| 132 | + "data": { |
| 133 | + "text/plain": [ |
| 134 | + "[<llm_agents_from_scratch.tools.simple_function.SimpleFunctionTool at 0x7c20ba50c590>]" |
| 135 | + ] |
| 136 | + }, |
| 137 | + "execution_count": 4, |
| 138 | + "metadata": {}, |
| 139 | + "output_type": "execute_result" |
| 140 | + } |
| 141 | + ], |
| 142 | + "source": [ |
| 143 | + "llm_agent.add_tool(tool)\n", |
| 144 | + "llm_agent.tools" |
| 145 | + ] |
107 | 146 | }, |
108 | 147 | { |
109 | 148 | "cell_type": "markdown", |
|
115 | 154 | }, |
116 | 155 | { |
117 | 156 | "cell_type": "code", |
118 | | - "execution_count": 2, |
| 157 | + "execution_count": 5, |
119 | 158 | "id": "83b822c6-9ec3-4c23-8ef6-1c07171accb7", |
120 | 159 | "metadata": {}, |
121 | 160 | "outputs": [], |
|
125 | 164 | }, |
126 | 165 | { |
127 | 166 | "cell_type": "code", |
128 | | - "execution_count": 3, |
| 167 | + "execution_count": 6, |
129 | 168 | "id": "b4b7c322-fb54-4d4a-b1c8-4c7b574440e8", |
130 | 169 | "metadata": {}, |
131 | 170 | "outputs": [], |
|
151 | 190 | }, |
152 | 191 | { |
153 | 192 | "cell_type": "code", |
154 | | - "execution_count": 4, |
| 193 | + "execution_count": 7, |
155 | 194 | "id": "fed674f4-6a68-4741-ada7-0b6a6f34fe3e", |
156 | 195 | "metadata": {}, |
157 | 196 | "outputs": [], |
|
187 | 226 | }, |
188 | 227 | { |
189 | 228 | "cell_type": "code", |
190 | | - "execution_count": 5, |
| 229 | + "execution_count": 8, |
191 | 230 | "id": "00463e83-28fd-4ad9-b375-0e0e5ed9ab81", |
192 | 231 | "metadata": {}, |
193 | 232 | "outputs": [], |
|
207 | 246 | }, |
208 | 247 | { |
209 | 248 | "cell_type": "code", |
210 | | - "execution_count": 6, |
| 249 | + "execution_count": 9, |
211 | 250 | "id": "d8fdc618-e9b0-4726-a25d-85ad858b2e2c", |
212 | 251 | "metadata": {}, |
213 | 252 | "outputs": [], |
|
230 | 269 | }, |
231 | 270 | { |
232 | 271 | "cell_type": "code", |
233 | | - "execution_count": 7, |
| 272 | + "execution_count": 10, |
234 | 273 | "id": "c502a9a8-545b-4094-a52a-06d5101ef9ee", |
235 | 274 | "metadata": {}, |
236 | 275 | "outputs": [], |
|
240 | 279 | }, |
241 | 280 | { |
242 | 281 | "cell_type": "code", |
243 | | - "execution_count": 8, |
| 282 | + "execution_count": 11, |
244 | 283 | "id": "3838db7f-fcd6-432f-8e4c-0eb8ac3706d8", |
245 | 284 | "metadata": {}, |
246 | 285 | "outputs": [], |
|
279 | 318 | }, |
280 | 319 | { |
281 | 320 | "cell_type": "code", |
282 | | - "execution_count": 9, |
| 321 | + "execution_count": 12, |
283 | 322 | "id": "13d883a9-23d3-40ac-9130-39e8eee47d46", |
284 | 323 | "metadata": {}, |
285 | 324 | "outputs": [], |
|
293 | 332 | }, |
294 | 333 | { |
295 | 334 | "cell_type": "code", |
296 | | - "execution_count": 10, |
| 335 | + "execution_count": 13, |
297 | 336 | "id": "7dda06d3-3df2-477c-981c-5d3b45efb9bb", |
298 | 337 | "metadata": {}, |
299 | 338 | "outputs": [ |
|
314 | 353 | "INFO (llm_agents_fs.TaskHandler) : ✅ Step Result: <tool_call>\n", |
315 | 354 | "{\"name\": \"next_number\", \"arguments\": {\"x\":2}}\n", |
316 | 355 | "</tool_call>\n", |
317 | | - "INFO (llm_agents_fs.TaskHandler) : 🧠 New Step: The tool returned the number 2. Now, I need to call `next_number` again with x=2.\n", |
318 | | - "INFO (llm_agents_fs.TaskHandler) : ⚙️ Processing Step: The tool returned the number 2. Now, I need to call `next_number` again with x=2.\n", |
| 356 | + "INFO (llm_agents_fs.TaskHandler) : 🧠 New Step: CALL `next_number` on the current number x = 2\n", |
| 357 | + "INFO (llm_agents_fs.TaskHandler) : ⚙️ Processing Step: CALL `next_number` on the current number x = 2\n", |
319 | 358 | "INFO (llm_agents_fs.TaskHandler) : 🛠️ Executing Tool Call: next_number\n", |
320 | 359 | "INFO (llm_agents_fs.TaskHandler) : ✅ Successful Tool Call: 1\n", |
321 | | - "INFO (llm_agents_fs.TaskHandler) : ✅ Step Result: The tool returned the number 1. Now, we can stop as we have reached the end of the sequence (number 1). \n", |
| 360 | + "INFO (llm_agents_fs.TaskHandler) : ✅ Step Result: The tool `next_number` returned that the next number after 2 is 1. \n", |
322 | 361 | "\n", |
323 | | - "Here is the complete sequence...[TRUNCATED]\n", |
| 362 | + "Given this result, we can now generate the complete sequence from ...[TRUNCATED]\n", |
324 | 363 | "INFO (llm_agents_fs.TaskHandler) : No new step required.\n", |
325 | | - "INFO (llm_agents_fs.LLMAgent) : 🏁 Task completed: [4, 2, 1]\n" |
| 364 | + "INFO (llm_agents_fs.LLMAgent) : 🏁 Task completed: 4 → 2 → 1\n" |
326 | 365 | ] |
327 | 366 | } |
328 | 367 | ], |
|
392 | 431 | { |
393 | 432 | "data": { |
394 | 433 | "text/plain": [ |
395 | | - "TaskResult(task_id='445d0976-1840-4305-bbe4-0123066cf194', content='[4, 2, 1]')" |
| 434 | + "TaskResult(task_id='e0b640d6-02b6-4d3e-8e35-ded9a2f98548', content='4 → 2 → 1')" |
396 | 435 | ] |
397 | 436 | }, |
398 | 437 | "execution_count": 16, |
|
407 | 446 | }, |
408 | 447 | { |
409 | 448 | "cell_type": "code", |
410 | | - "execution_count": 17, |
411 | | - "id": "6d8def33-a936-41e2-9142-67644154502d", |
| 449 | + "execution_count": null, |
| 450 | + "id": "64fedc3d-a140-48fc-a42c-96492af1df08", |
412 | 451 | "metadata": {}, |
413 | | - "outputs": [ |
414 | | - { |
415 | | - "name": "stdout", |
416 | | - "output_type": "stream", |
417 | | - "text": [ |
418 | | - "[4, 2, 1]\n" |
419 | | - ] |
420 | | - } |
421 | | - ], |
| 452 | + "outputs": [], |
422 | 453 | "source": [ |
423 | | - "print(result)" |
| 454 | + "# Alternative execution style (if you don't want/need the handler)\n", |
| 455 | + "# result = await llm_agent.run(task, max_steps=5)" |
424 | 456 | ] |
425 | 457 | }, |
426 | 458 | { |
|
435 | 467 | }, |
436 | 468 | { |
437 | 469 | "cell_type": "code", |
438 | | - "execution_count": 18, |
| 470 | + "execution_count": 17, |
439 | 471 | "id": "0558b406-6b68-4121-a5be-2305913278c0", |
440 | 472 | "metadata": {}, |
441 | 473 | "outputs": [ |
|
469 | 501 | "💬 assistant: I need to make the following tool call(s):\n", |
470 | 502 | "\n", |
471 | 503 | "{\n", |
472 | | - " \"id_\": \"8185def9-4df0-4f3f-abac-d776c98051e5\",\n", |
| 504 | + " \"id_\": \"e7c3409c-9966-4cde-a0e4-6cc8c4441380\",\n", |
473 | 505 | " \"tool_name\": \"next_number\",\n", |
474 | 506 | " \"arguments\": {\n", |
475 | 507 | " \"x\": 4\n", |
476 | 508 | " }\n", |
477 | 509 | "}.\n", |
478 | 510 | "\n", |
479 | 511 | "💬 tool: {\n", |
480 | | - " \"tool_call_id\": \"8185def9-4df0-4f3f-abac-d776c98051e5\",\n", |
| 512 | + " \"tool_call_id\": \"e7c3409c-9966-4cde-a0e4-6cc8c4441380\",\n", |
481 | 513 | " \"content\": \"2\",\n", |
482 | 514 | " \"error\": false\n", |
483 | 515 | "}\n", |
|
490 | 522 | "\n", |
491 | 523 | "=== Task Step Start ===\n", |
492 | 524 | "\n", |
493 | | - "💬 assistant: The current instruction is 'The tool returned the number 2. Now, I need to call `next_number` again with x=2.'\n", |
| 525 | + "💬 assistant: The current instruction is 'CALL `next_number` on the current number x = 2'\n", |
494 | 526 | "\n", |
495 | 527 | "💬 assistant: I need to make the following tool call(s):\n", |
496 | 528 | "\n", |
497 | 529 | "{\n", |
498 | | - " \"id_\": \"1159b8b8-796b-4d94-90df-75a43b9c58ac\",\n", |
| 530 | + " \"id_\": \"5791f937-9050-4bef-9f1a-8bd88bc05b5b\",\n", |
499 | 531 | " \"tool_name\": \"next_number\",\n", |
500 | 532 | " \"arguments\": {\n", |
501 | 533 | " \"x\": 2\n", |
502 | 534 | " }\n", |
503 | 535 | "}.\n", |
504 | 536 | "\n", |
505 | 537 | "💬 tool: {\n", |
506 | | - " \"tool_call_id\": \"1159b8b8-796b-4d94-90df-75a43b9c58ac\",\n", |
| 538 | + " \"tool_call_id\": \"5791f937-9050-4bef-9f1a-8bd88bc05b5b\",\n", |
507 | 539 | " \"content\": \"1\",\n", |
508 | 540 | " \"error\": false\n", |
509 | 541 | "}\n", |
510 | 542 | "\n", |
511 | | - "💬 assistant: The tool returned the number 1. Now, we can stop as we have reached the end of the sequence (number 1). \n", |
| 543 | + "💬 assistant: The tool `next_number` returned that the next number after 2 is 1. \n", |
| 544 | + "\n", |
| 545 | + "Given this result, we can now generate the complete sequence from start to finish:\n", |
512 | 546 | "\n", |
513 | | - "Here is the complete sequence from start to finish:\n", |
| 547 | + "Starting with x=4 and following the function's logic:\n", |
| 548 | + "- Start: x = 4\n", |
| 549 | + "- Next call: x = 2 (as per tool response)\n", |
| 550 | + "- The last step in the sequence: x = 1\n", |
514 | 551 | "\n", |
515 | | - "4 -> 2 -> 1\n", |
| 552 | + "Therefore, the full sequence is:\n", |
516 | 553 | "\n", |
517 | | - "You can now provide this sequence for completion of your task.\n", |
| 554 | + "4 → 2 → 1\n", |
| 555 | + "\n", |
| 556 | + "The task has now been completed.\n", |
518 | 557 | "\n", |
519 | 558 | "=== Task Step End ===\n" |
520 | 559 | ] |
|
523 | 562 | "source": [ |
524 | 563 | "print(handler.rollout)" |
525 | 564 | ] |
| 565 | + }, |
| 566 | + { |
| 567 | + "cell_type": "code", |
| 568 | + "execution_count": null, |
| 569 | + "id": "9feeab74-5691-41e9-8f7d-f62c0a8f53b6", |
| 570 | + "metadata": {}, |
| 571 | + "outputs": [], |
| 572 | + "source": [] |
526 | 573 | } |
527 | 574 | ], |
528 | 575 | "metadata": { |
|
0 commit comments