Skip to content

Commit b287864

Browse files
committed
Add Llama3 example with Multi-ReAct
1 parent 1a0feb6 commit b287864

File tree

3 files changed

+1080
-49
lines changed

3 files changed

+1080
-49
lines changed

dspy/predict/react.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# TODO: Simplify a lot.
99
# TODO: Divide Action and Action Input like langchain does for ReAct.
1010

11+
# TODO: There's a lot of value in having a stopping condition in the LM calls at `\n\nObservation:`
12+
1113

1214
class ReAct(Module):
1315
def __init__(self, signature, max_iters=5, num_results=3, tools=None):

examples/agents/multi_agent.ipynb

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -331,96 +331,96 @@
331331
},
332332
{
333333
"cell_type": "code",
334-
"execution_count": 6,
334+
"execution_count": 13,
335335
"metadata": {},
336336
"outputs": [
337337
{
338338
"name": "stderr",
339339
"output_type": "stream",
340340
"text": [
341-
"Average Metric: 138 / 300 (46.0): 100%|██████████| 300/300 [00:02<00:00, 137.82it/s]\n"
341+
"Average Metric: 138 / 300 (46.0): 100%|██████████| 300/300 [00:00<00:00, 512.74it/s]\n"
342342
]
343343
},
344344
{
345345
"data": {
346346
"text/html": [
347347
"<style type=\"text/css\">\n",
348-
"#T_fe347 th {\n",
348+
"#T_e09c1 th {\n",
349349
" text-align: left;\n",
350350
"}\n",
351-
"#T_fe347 td {\n",
351+
"#T_e09c1 td {\n",
352352
" text-align: left;\n",
353353
"}\n",
354-
"#T_fe347_row0_col0, #T_fe347_row0_col1, #T_fe347_row0_col2, #T_fe347_row0_col3, #T_fe347_row0_col4, #T_fe347_row0_col5, #T_fe347_row1_col0, #T_fe347_row1_col1, #T_fe347_row1_col2, #T_fe347_row1_col3, #T_fe347_row1_col4, #T_fe347_row1_col5, #T_fe347_row2_col0, #T_fe347_row2_col1, #T_fe347_row2_col2, #T_fe347_row2_col3, #T_fe347_row2_col4, #T_fe347_row2_col5, #T_fe347_row3_col0, #T_fe347_row3_col1, #T_fe347_row3_col2, #T_fe347_row3_col3, #T_fe347_row3_col4, #T_fe347_row3_col5, #T_fe347_row4_col0, #T_fe347_row4_col1, #T_fe347_row4_col2, #T_fe347_row4_col3, #T_fe347_row4_col4, #T_fe347_row4_col5 {\n",
354+
"#T_e09c1_row0_col0, #T_e09c1_row0_col1, #T_e09c1_row0_col2, #T_e09c1_row0_col3, #T_e09c1_row0_col4, #T_e09c1_row0_col5, #T_e09c1_row1_col0, #T_e09c1_row1_col1, #T_e09c1_row1_col2, #T_e09c1_row1_col3, #T_e09c1_row1_col4, #T_e09c1_row1_col5, #T_e09c1_row2_col0, #T_e09c1_row2_col1, #T_e09c1_row2_col2, #T_e09c1_row2_col3, #T_e09c1_row2_col4, #T_e09c1_row2_col5, #T_e09c1_row3_col0, #T_e09c1_row3_col1, #T_e09c1_row3_col2, #T_e09c1_row3_col3, #T_e09c1_row3_col4, #T_e09c1_row3_col5, #T_e09c1_row4_col0, #T_e09c1_row4_col1, #T_e09c1_row4_col2, #T_e09c1_row4_col3, #T_e09c1_row4_col4, #T_e09c1_row4_col5 {\n",
355355
" text-align: left;\n",
356356
" white-space: pre-wrap;\n",
357357
" word-wrap: break-word;\n",
358358
" max-width: 400px;\n",
359359
"}\n",
360360
"</style>\n",
361-
"<table id=\"T_fe347\">\n",
361+
"<table id=\"T_e09c1\">\n",
362362
" <thead>\n",
363363
" <tr>\n",
364364
" <th class=\"blank level0\" >&nbsp;</th>\n",
365-
" <th id=\"T_fe347_level0_col0\" class=\"col_heading level0 col0\" >question</th>\n",
366-
" <th id=\"T_fe347_level0_col1\" class=\"col_heading level0 col1\" >example_answer</th>\n",
367-
" <th id=\"T_fe347_level0_col2\" class=\"col_heading level0 col2\" >gold_titles</th>\n",
368-
" <th id=\"T_fe347_level0_col3\" class=\"col_heading level0 col3\" >observations</th>\n",
369-
" <th id=\"T_fe347_level0_col4\" class=\"col_heading level0 col4\" >pred_answer</th>\n",
370-
" <th id=\"T_fe347_level0_col5\" class=\"col_heading level0 col5\" >answer_exact_match</th>\n",
365+
" <th id=\"T_e09c1_level0_col0\" class=\"col_heading level0 col0\" >question</th>\n",
366+
" <th id=\"T_e09c1_level0_col1\" class=\"col_heading level0 col1\" >example_answer</th>\n",
367+
" <th id=\"T_e09c1_level0_col2\" class=\"col_heading level0 col2\" >gold_titles</th>\n",
368+
" <th id=\"T_e09c1_level0_col3\" class=\"col_heading level0 col3\" >observations</th>\n",
369+
" <th id=\"T_e09c1_level0_col4\" class=\"col_heading level0 col4\" >pred_answer</th>\n",
370+
" <th id=\"T_e09c1_level0_col5\" class=\"col_heading level0 col5\" >answer_exact_match</th>\n",
371371
" </tr>\n",
372372
" </thead>\n",
373373
" <tbody>\n",
374374
" <tr>\n",
375-
" <th id=\"T_fe347_level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
376-
" <td id=\"T_fe347_row0_col0\" class=\"data row0 col0\" >Are both Cangzhou and Qionghai in the Hebei province of China?</td>\n",
377-
" <td id=\"T_fe347_row0_col1\" class=\"data row0 col1\" >no</td>\n",
378-
" <td id=\"T_fe347_row0_col2\" class=\"data row0 col2\" >{'Cangzhou', 'Qionghai'}</td>\n",
379-
" <td id=\"T_fe347_row0_col3\" class=\"data row0 col3\" >[['Cangzhou | Cangzhou () is a prefecture-level city in eastern Hebei province, People\\'s Republic of China. At the 2010 census, Cangzhou\\'s built-up (\"or metro\") area...</td>\n",
380-
" <td id=\"T_fe347_row0_col4\" class=\"data row0 col4\" >no</td>\n",
381-
" <td id=\"T_fe347_row0_col5\" class=\"data row0 col5\" >✔️ [True]</td>\n",
375+
" <th id=\"T_e09c1_level0_row0\" class=\"row_heading level0 row0\" >0</th>\n",
376+
" <td id=\"T_e09c1_row0_col0\" class=\"data row0 col0\" >Are both Cangzhou and Qionghai in the Hebei province of China?</td>\n",
377+
" <td id=\"T_e09c1_row0_col1\" class=\"data row0 col1\" >no</td>\n",
378+
" <td id=\"T_e09c1_row0_col2\" class=\"data row0 col2\" >{'Cangzhou', 'Qionghai'}</td>\n",
379+
" <td id=\"T_e09c1_row0_col3\" class=\"data row0 col3\" >[['Cangzhou | Cangzhou () is a prefecture-level city in eastern Hebei province, People\\'s Republic of China. At the 2010 census, Cangzhou\\'s built-up (\"or metro\") area...</td>\n",
380+
" <td id=\"T_e09c1_row0_col4\" class=\"data row0 col4\" >no</td>\n",
381+
" <td id=\"T_e09c1_row0_col5\" class=\"data row0 col5\" >✔️ [True]</td>\n",
382382
" </tr>\n",
383383
" <tr>\n",
384-
" <th id=\"T_fe347_level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
385-
" <td id=\"T_fe347_row1_col0\" class=\"data row1 col0\" >Who conducts the draft in which Marc-Andre Fleury was drafted to the Vegas Golden Knights for the 2017-18 season?</td>\n",
386-
" <td id=\"T_fe347_row1_col1\" class=\"data row1 col1\" >National Hockey League</td>\n",
387-
" <td id=\"T_fe347_row1_col2\" class=\"data row1 col2\" >{'2017 NHL Expansion Draft', '2017–18 Pittsburgh Penguins season'}</td>\n",
388-
" <td id=\"T_fe347_row1_col3\" class=\"data row1 col3\" >[['2017–18 Pittsburgh Penguins season | The 2017–18 Pittsburgh Penguins season will be the 51st season for the National Hockey League ice hockey team that was...</td>\n",
389-
" <td id=\"T_fe347_row1_col4\" class=\"data row1 col4\" >National Hockey League</td>\n",
390-
" <td id=\"T_fe347_row1_col5\" class=\"data row1 col5\" >✔️ [True]</td>\n",
384+
" <th id=\"T_e09c1_level0_row1\" class=\"row_heading level0 row1\" >1</th>\n",
385+
" <td id=\"T_e09c1_row1_col0\" class=\"data row1 col0\" >Who conducts the draft in which Marc-Andre Fleury was drafted to the Vegas Golden Knights for the 2017-18 season?</td>\n",
386+
" <td id=\"T_e09c1_row1_col1\" class=\"data row1 col1\" >National Hockey League</td>\n",
387+
" <td id=\"T_e09c1_row1_col2\" class=\"data row1 col2\" >{'2017 NHL Expansion Draft', '2017–18 Pittsburgh Penguins season'}</td>\n",
388+
" <td id=\"T_e09c1_row1_col3\" class=\"data row1 col3\" >[['2017–18 Pittsburgh Penguins season | The 2017–18 Pittsburgh Penguins season will be the 51st season for the National Hockey League ice hockey team that was...</td>\n",
389+
" <td id=\"T_e09c1_row1_col4\" class=\"data row1 col4\" >National Hockey League</td>\n",
390+
" <td id=\"T_e09c1_row1_col5\" class=\"data row1 col5\" >✔️ [True]</td>\n",
391391
" </tr>\n",
392392
" <tr>\n",
393-
" <th id=\"T_fe347_level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
394-
" <td id=\"T_fe347_row2_col0\" class=\"data row2 col0\" >The Wings entered a new era, following the retirement of which Canadian retired professional ice hockey player and current general manager of the Tampa Bay...</td>\n",
395-
" <td id=\"T_fe347_row2_col1\" class=\"data row2 col1\" >Steve Yzerman</td>\n",
396-
" <td id=\"T_fe347_row2_col2\" class=\"data row2 col2\" >{'2006–07 Detroit Red Wings season', 'Steve Yzerman'}</td>\n",
397-
" <td id=\"T_fe347_row2_col3\" class=\"data row2 col3\" >[['Steve Yzerman | Stephen Gregory \"Steve\" Yzerman ( ; born May 9, 1965) is a Canadian retired professional ice hockey player and current general manager...</td>\n",
398-
" <td id=\"T_fe347_row2_col4\" class=\"data row2 col4\" >Steve Yzerman</td>\n",
399-
" <td id=\"T_fe347_row2_col5\" class=\"data row2 col5\" >✔️ [True]</td>\n",
393+
" <th id=\"T_e09c1_level0_row2\" class=\"row_heading level0 row2\" >2</th>\n",
394+
" <td id=\"T_e09c1_row2_col0\" class=\"data row2 col0\" >The Wings entered a new era, following the retirement of which Canadian retired professional ice hockey player and current general manager of the Tampa Bay...</td>\n",
395+
" <td id=\"T_e09c1_row2_col1\" class=\"data row2 col1\" >Steve Yzerman</td>\n",
396+
" <td id=\"T_e09c1_row2_col2\" class=\"data row2 col2\" >{'2006–07 Detroit Red Wings season', 'Steve Yzerman'}</td>\n",
397+
" <td id=\"T_e09c1_row2_col3\" class=\"data row2 col3\" >[['Steve Yzerman | Stephen Gregory \"Steve\" Yzerman ( ; born May 9, 1965) is a Canadian retired professional ice hockey player and current general manager...</td>\n",
398+
" <td id=\"T_e09c1_row2_col4\" class=\"data row2 col4\" >Steve Yzerman</td>\n",
399+
" <td id=\"T_e09c1_row2_col5\" class=\"data row2 col5\" >✔️ [True]</td>\n",
400400
" </tr>\n",
401401
" <tr>\n",
402-
" <th id=\"T_fe347_level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
403-
" <td id=\"T_fe347_row3_col0\" class=\"data row3 col0\" >What river is near the Crichton Collegiate Church?</td>\n",
404-
" <td id=\"T_fe347_row3_col1\" class=\"data row3 col1\" >the River Tyne</td>\n",
405-
" <td id=\"T_fe347_row3_col2\" class=\"data row3 col2\" >{'Crichton Collegiate Church', 'Crichton Castle'}</td>\n",
406-
" <td id=\"T_fe347_row3_col3\" class=\"data row3 col3\" >[[\"Crichton Collegiate Church | Crichton Collegiate Church is situated about 0.6 mi south west of the hamlet of Crichton in Midlothian, Scotland. Crichton itself is...</td>\n",
407-
" <td id=\"T_fe347_row3_col4\" class=\"data row3 col4\" >Crichton Collegiate Church is located in Midlothian, Scotland, near the hamlet of Crichton, about 7.5 miles south of Edinburgh.</td>\n",
408-
" <td id=\"T_fe347_row3_col5\" class=\"data row3 col5\" >False</td>\n",
402+
" <th id=\"T_e09c1_level0_row3\" class=\"row_heading level0 row3\" >3</th>\n",
403+
" <td id=\"T_e09c1_row3_col0\" class=\"data row3 col0\" >What river is near the Crichton Collegiate Church?</td>\n",
404+
" <td id=\"T_e09c1_row3_col1\" class=\"data row3 col1\" >the River Tyne</td>\n",
405+
" <td id=\"T_e09c1_row3_col2\" class=\"data row3 col2\" >{'Crichton Collegiate Church', 'Crichton Castle'}</td>\n",
406+
" <td id=\"T_e09c1_row3_col3\" class=\"data row3 col3\" >[[\"Crichton Collegiate Church | Crichton Collegiate Church is situated about 0.6 mi south west of the hamlet of Crichton in Midlothian, Scotland. Crichton itself is...</td>\n",
407+
" <td id=\"T_e09c1_row3_col4\" class=\"data row3 col4\" >Crichton Collegiate Church is located in Midlothian, Scotland, near the hamlet of Crichton, about 7.5 miles south of Edinburgh.</td>\n",
408+
" <td id=\"T_e09c1_row3_col5\" class=\"data row3 col5\" >False</td>\n",
409409
" </tr>\n",
410410
" <tr>\n",
411-
" <th id=\"T_fe347_level0_row4\" class=\"row_heading level0 row4\" >4</th>\n",
412-
" <td id=\"T_fe347_row4_col0\" class=\"data row4 col0\" >In the 10th Century A.D. Ealhswith had a son called Æthelweard by which English king?</td>\n",
413-
" <td id=\"T_fe347_row4_col1\" class=\"data row4 col1\" >King Alfred the Great</td>\n",
414-
" <td id=\"T_fe347_row4_col2\" class=\"data row4 col2\" >{'Ealhswith', 'Æthelweard (son of Alfred)'}</td>\n",
415-
" <td id=\"T_fe347_row4_col3\" class=\"data row4 col3\" >[['Æthelweard (son of Alfred) | Æthelweard (d. 920 or 922) was the younger son of King Alfred the Great and Ealhswith.'], ['Æthelstan of Kent |...</td>\n",
416-
" <td id=\"T_fe347_row4_col4\" class=\"data row4 col4\" >Alfred the Great</td>\n",
417-
" <td id=\"T_fe347_row4_col5\" class=\"data row4 col5\" >False</td>\n",
411+
" <th id=\"T_e09c1_level0_row4\" class=\"row_heading level0 row4\" >4</th>\n",
412+
" <td id=\"T_e09c1_row4_col0\" class=\"data row4 col0\" >In the 10th Century A.D. Ealhswith had a son called Æthelweard by which English king?</td>\n",
413+
" <td id=\"T_e09c1_row4_col1\" class=\"data row4 col1\" >King Alfred the Great</td>\n",
414+
" <td id=\"T_e09c1_row4_col2\" class=\"data row4 col2\" >{'Ealhswith', 'Æthelweard (son of Alfred)'}</td>\n",
415+
" <td id=\"T_e09c1_row4_col3\" class=\"data row4 col3\" >[['Æthelweard (son of Alfred) | Æthelweard (d. 920 or 922) was the younger son of King Alfred the Great and Ealhswith.'], ['Æthelstan of Kent |...</td>\n",
416+
" <td id=\"T_e09c1_row4_col4\" class=\"data row4 col4\" >Alfred the Great</td>\n",
417+
" <td id=\"T_e09c1_row4_col5\" class=\"data row4 col5\" >False</td>\n",
418418
" </tr>\n",
419419
" </tbody>\n",
420420
"</table>\n"
421421
],
422422
"text/plain": [
423-
"<pandas.io.formats.style.Styler at 0x7f216eaed6f0>"
423+
"<pandas.io.formats.style.Styler at 0x7f2163882380>"
424424
]
425425
},
426426
"metadata": {},
@@ -453,7 +453,7 @@
453453
"46.0"
454454
]
455455
},
456-
"execution_count": 6,
456+
"execution_count": 13,
457457
"metadata": {},
458458
"output_type": "execute_result"
459459
}
@@ -857,6 +857,9 @@
857857
"\t\treturn self.aggregate(context=deduplicate(flatten([p.observations for p in preds])), question=question)\n",
858858
"\t\n",
859859
"optimized_aggregator = optimizer.compile(aggregator, trainset=trainset, valset=valset)\n",
860+
"\n",
861+
"# Use it!\n",
862+
"optimized_aggregator(question=\"How many storeys are in the castle that David Gregory inherited?\")\n",
860863
"```"
861864
]
862865
},

0 commit comments

Comments
 (0)