Skip to content

Commit 2769a30

Browse files
authored
add examples 1 and 2 to nb (#270)
1 parent 60cd5de commit 2769a30

File tree

1 file changed

+104
-57
lines changed

1 file changed

+104
-57
lines changed

examples/ch04.ipynb

Lines changed: 104 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -55,55 +55,94 @@
5555
},
5656
{
5757
"cell_type": "markdown",
58-
"id": "c1e3fae2-e573-4a8f-be54-9a1519449f6b",
58+
"id": "e6281fc2-5412-446c-8c80-3226eaeabe5a",
5959
"metadata": {},
6060
"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`"
6262
]
6363
},
6464
{
6565
"cell_type": "code",
6666
"execution_count": 1,
67-
"id": "dc4524f7-f7fc-4fc9-805d-8a94e5004e9b",
67+
"id": "bc0ab1b8-aec1-49df-a63e-60ab2317be2f",
6868
"metadata": {},
6969
"outputs": [],
7070
"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+
")"
7478
]
7579
},
7680
{
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",
7984
"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+
],
8097
"source": [
81-
"### Example 1: ..."
98+
"llm_agent.tools"
8299
]
83100
},
84-
{
85-
"cell_type": "code",
86-
"execution_count": null,
87-
"id": "bc0ab1b8-aec1-49df-a63e-60ab2317be2f",
88-
"metadata": {},
89-
"outputs": [],
90-
"source": []
91-
},
92101
{
93102
"cell_type": "markdown",
94103
"id": "b81f366e-b2d0-402b-9fb7-984fbd91b01f",
95104
"metadata": {},
96105
"source": [
97-
"### Example 2: ..."
106+
"### Example 2: Demo usage of `add_tool()`"
98107
]
99108
},
100109
{
101110
"cell_type": "code",
102-
"execution_count": null,
111+
"execution_count": 3,
103112
"id": "ba842320-9881-4150-b392-73fe9f9dab79",
104113
"metadata": {},
105114
"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+
]
107146
},
108147
{
109148
"cell_type": "markdown",
@@ -115,7 +154,7 @@
115154
},
116155
{
117156
"cell_type": "code",
118-
"execution_count": 2,
157+
"execution_count": 5,
119158
"id": "83b822c6-9ec3-4c23-8ef6-1c07171accb7",
120159
"metadata": {},
121160
"outputs": [],
@@ -125,7 +164,7 @@
125164
},
126165
{
127166
"cell_type": "code",
128-
"execution_count": 3,
167+
"execution_count": 6,
129168
"id": "b4b7c322-fb54-4d4a-b1c8-4c7b574440e8",
130169
"metadata": {},
131170
"outputs": [],
@@ -151,7 +190,7 @@
151190
},
152191
{
153192
"cell_type": "code",
154-
"execution_count": 4,
193+
"execution_count": 7,
155194
"id": "fed674f4-6a68-4741-ada7-0b6a6f34fe3e",
156195
"metadata": {},
157196
"outputs": [],
@@ -187,7 +226,7 @@
187226
},
188227
{
189228
"cell_type": "code",
190-
"execution_count": 5,
229+
"execution_count": 8,
191230
"id": "00463e83-28fd-4ad9-b375-0e0e5ed9ab81",
192231
"metadata": {},
193232
"outputs": [],
@@ -207,7 +246,7 @@
207246
},
208247
{
209248
"cell_type": "code",
210-
"execution_count": 6,
249+
"execution_count": 9,
211250
"id": "d8fdc618-e9b0-4726-a25d-85ad858b2e2c",
212251
"metadata": {},
213252
"outputs": [],
@@ -230,7 +269,7 @@
230269
},
231270
{
232271
"cell_type": "code",
233-
"execution_count": 7,
272+
"execution_count": 10,
234273
"id": "c502a9a8-545b-4094-a52a-06d5101ef9ee",
235274
"metadata": {},
236275
"outputs": [],
@@ -240,7 +279,7 @@
240279
},
241280
{
242281
"cell_type": "code",
243-
"execution_count": 8,
282+
"execution_count": 11,
244283
"id": "3838db7f-fcd6-432f-8e4c-0eb8ac3706d8",
245284
"metadata": {},
246285
"outputs": [],
@@ -279,7 +318,7 @@
279318
},
280319
{
281320
"cell_type": "code",
282-
"execution_count": 9,
321+
"execution_count": 12,
283322
"id": "13d883a9-23d3-40ac-9130-39e8eee47d46",
284323
"metadata": {},
285324
"outputs": [],
@@ -293,7 +332,7 @@
293332
},
294333
{
295334
"cell_type": "code",
296-
"execution_count": 10,
335+
"execution_count": 13,
297336
"id": "7dda06d3-3df2-477c-981c-5d3b45efb9bb",
298337
"metadata": {},
299338
"outputs": [
@@ -314,15 +353,15 @@
314353
"INFO (llm_agents_fs.TaskHandler) : ✅ Step Result: <tool_call>\n",
315354
"{\"name\": \"next_number\", \"arguments\": {\"x\":2}}\n",
316355
"</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",
319358
"INFO (llm_agents_fs.TaskHandler) : 🛠️ Executing Tool Call: next_number\n",
320359
"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",
322361
"\n",
323-
"Here is the complete sequence...[TRUNCATED]\n",
362+
"Given this result, we can now generate the complete sequence from ...[TRUNCATED]\n",
324363
"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"
326365
]
327366
}
328367
],
@@ -392,7 +431,7 @@
392431
{
393432
"data": {
394433
"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')"
396435
]
397436
},
398437
"execution_count": 16,
@@ -407,20 +446,13 @@
407446
},
408447
{
409448
"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",
412451
"metadata": {},
413-
"outputs": [
414-
{
415-
"name": "stdout",
416-
"output_type": "stream",
417-
"text": [
418-
"[4, 2, 1]\n"
419-
]
420-
}
421-
],
452+
"outputs": [],
422453
"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)"
424456
]
425457
},
426458
{
@@ -435,7 +467,7 @@
435467
},
436468
{
437469
"cell_type": "code",
438-
"execution_count": 18,
470+
"execution_count": 17,
439471
"id": "0558b406-6b68-4121-a5be-2305913278c0",
440472
"metadata": {},
441473
"outputs": [
@@ -469,15 +501,15 @@
469501
"💬 assistant: I need to make the following tool call(s):\n",
470502
"\n",
471503
"{\n",
472-
" \"id_\": \"8185def9-4df0-4f3f-abac-d776c98051e5\",\n",
504+
" \"id_\": \"e7c3409c-9966-4cde-a0e4-6cc8c4441380\",\n",
473505
" \"tool_name\": \"next_number\",\n",
474506
" \"arguments\": {\n",
475507
" \"x\": 4\n",
476508
" }\n",
477509
"}.\n",
478510
"\n",
479511
"💬 tool: {\n",
480-
" \"tool_call_id\": \"8185def9-4df0-4f3f-abac-d776c98051e5\",\n",
512+
" \"tool_call_id\": \"e7c3409c-9966-4cde-a0e4-6cc8c4441380\",\n",
481513
" \"content\": \"2\",\n",
482514
" \"error\": false\n",
483515
"}\n",
@@ -490,31 +522,38 @@
490522
"\n",
491523
"=== Task Step Start ===\n",
492524
"\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",
494526
"\n",
495527
"💬 assistant: I need to make the following tool call(s):\n",
496528
"\n",
497529
"{\n",
498-
" \"id_\": \"1159b8b8-796b-4d94-90df-75a43b9c58ac\",\n",
530+
" \"id_\": \"5791f937-9050-4bef-9f1a-8bd88bc05b5b\",\n",
499531
" \"tool_name\": \"next_number\",\n",
500532
" \"arguments\": {\n",
501533
" \"x\": 2\n",
502534
" }\n",
503535
"}.\n",
504536
"\n",
505537
"💬 tool: {\n",
506-
" \"tool_call_id\": \"1159b8b8-796b-4d94-90df-75a43b9c58ac\",\n",
538+
" \"tool_call_id\": \"5791f937-9050-4bef-9f1a-8bd88bc05b5b\",\n",
507539
" \"content\": \"1\",\n",
508540
" \"error\": false\n",
509541
"}\n",
510542
"\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",
512546
"\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",
514551
"\n",
515-
"4 -> 2 -> 1\n",
552+
"Therefore, the full sequence is:\n",
516553
"\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",
518557
"\n",
519558
"=== Task Step End ===\n"
520559
]
@@ -523,6 +562,14 @@
523562
"source": [
524563
"print(handler.rollout)"
525564
]
565+
},
566+
{
567+
"cell_type": "code",
568+
"execution_count": null,
569+
"id": "9feeab74-5691-41e9-8f7d-f62c0a8f53b6",
570+
"metadata": {},
571+
"outputs": [],
572+
"source": []
526573
}
527574
],
528575
"metadata": {

0 commit comments

Comments
 (0)