Skip to content

Commit 70bd47b

Browse files
committed
chapter 6 updates and adding new trees
1 parent 3dd7f1c commit 70bd47b

11 files changed

+715
-12
lines changed

‎agentic_btree_coding_challenge.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import textwrap
2+
import time
3+
4+
import py_trees
5+
6+
from playground.behavior_trees import (
7+
create_assistant_action_on_thread,
8+
create_assistant_condition,
9+
)
10+
from playground.assistants_api import api
11+
12+
# Create the root node (sequence)
13+
root = py_trees.composites.Sequence("RootSequence", memory=True)
14+
15+
16+
thread = api.create_thread()
17+
challenge = textwrap.dedent("""
18+
Plant the Grass
19+
20+
You will be given a matrix representing a field g and two numbers x, y coordinate.
21+
22+
There are three types of possible characters in the matrix:
23+
24+
x representing a rock.
25+
o representing a dirt space.
26+
+ representing a grassed space.
27+
28+
You have to simulate grass growing from the position (x, y).
29+
Grass can grow in all four directions (up, left, right, down).
30+
Grass can only grow on dirt spaces and can't go past rocks.
31+
32+
Return the simulated matrix.
33+
Examples
34+
35+
simulate_grass([
36+
"xxxxxxx",
37+
"xooooox",
38+
"xxxxoox"
39+
"xoooxxx"
40+
"xxxxxxx"
41+
], 1, 1) ➞ [
42+
"xxxxxxx",
43+
"x+++++x",
44+
"xxxx++x"
45+
"xoooxxx"
46+
"xxxxxxx"
47+
]
48+
49+
Notes
50+
51+
There will always be rocks on the perimeter
52+
""")
53+
judge_test_cases = textwrap.dedent("""
54+
Test.assert_equals(simulate_grass(["xxxxxxx","xooooox","xxxxoox","xoooxxx","xxxxxxx"], 1, 1), ["xxxxxxx","x+++++x","xxxx++x","xoooxxx","xxxxxxx"])
55+
Test.assert_equals(simulate_grass(["xxxxxxx","xoxooox","xxoooox","xooxxxx","xoxooox","xoxooox","xxxxxxx"], 2, 3), ["xxxxxxx","xox+++x","xx++++x","x++xxxx","x+xooox","x+xooox","xxxxxxx"])
56+
Test.assert_equals(simulate_grass(["xxxxxx","xoxoox","xxooox","xoooox","xoooox","xxxxxx"], 1, 1), ["xxxxxx","x+xoox","xxooox","xoooox","xoooox","xxxxxx"])
57+
Test.assert_equals(simulate_grass(["xxxxx","xooox","xooox","xooox","xxxxx"], 1, 1),["xxxxx","x+++x","x+++x","x+++x","xxxxx"])
58+
Test.assert_equals(simulate_grass(["xxxxxx","xxxxox","xxooox","xoooxx","xooxxx","xooxxx","xxooox","xxxoxx","xxxxxx"], 4, 1),["xxxxxx","xxxx+x","xx+++x","x+++xx","x++xxx","x++xxx","xx+++x","xxx+xx","xxxxxx"])
59+
Test.assert_equals(simulate_grass(["xxxxxxxxxxx", "xoxooooooox", "xoxoxxxxxox", "xoxoxoooxox", "xoxoxoxoxox", "xoxoxoxoxox", "xoxoxxxoxox", "xoxoooooxox", "xoxxxxxxxox", "xooooooooox", "xxxxxxxxxxx"], 1, 1), ["xxxxxxxxxxx", "x+x+++++++x", "x+x+xxxxx+x", "x+x+x+++x+x", "x+x+x+x+x+x", "x+x+x+x+x+x", "x+x+xxx+x+x", "x+x+++++x+x", "x+xxxxxxx+x", "x+++++++++x", "xxxxxxxxxxx"])
60+
""")
61+
62+
hacker = create_assistant_action_on_thread(
63+
thread=thread,
64+
action_name="Hacker",
65+
assistant_name="Python Coding Assistant",
66+
assistant_instructions=textwrap.dedent(f"""
67+
Challenge goal:
68+
{challenge}
69+
Solve the challenge and output the final solution to a file called solution.py
70+
"""),
71+
)
72+
root.add_child(hacker)
73+
74+
judge = create_assistant_action_on_thread(
75+
thread=thread,
76+
action_name="Judge solution",
77+
assistant_name="Coding Challenge Judge",
78+
assistant_instructions=textwrap.dedent(
79+
f"""
80+
Challenge goal:
81+
{challenge}
82+
Load the solution from the file solution.py.
83+
Then confirm is a solution to the challenge and test it with the following test cases:
84+
{judge_test_cases}
85+
Run the code for the solution and confirm it passes all the test cases.
86+
If the solution passes all tests save the solution to a file called judged_solution.py
87+
""",
88+
),
89+
)
90+
root.add_child(judge)
91+
92+
# verifier operates on a different thread, essentially in closed room
93+
verifier = create_assistant_condition(
94+
condition_name="Verify solution",
95+
assistant_name="Python Coding Assistant",
96+
assistant_instructions=textwrap.dedent(
97+
f"""
98+
Challenge goal:
99+
{challenge}
100+
Load the file called judged_solution.py and verify that the solution is correct by running the code and confirm it passes all the test cases:
101+
{judge_test_cases}
102+
If the solution is correct, return only the single word SUCCESS, otherwise return the single word FAILURE.
103+
""",
104+
),
105+
)
106+
root.add_child(verifier)
107+
108+
# Create the behavior tree
109+
tree = py_trees.trees.BehaviourTree(root)
110+
111+
# Tick the tree to run it
112+
# for i in range(1000):
113+
# print(f"Tick {i + 1}")
114+
# tree.tick()
115+
# time.sleep(30) # Simulate time between ticks
116+
117+
while True:
118+
tree.tick()
119+
time.sleep(20) # Simulate time between ticks
120+
if root.status == py_trees.common.Status.SUCCESS:
121+
break

‎agentic_btree_on_thread.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
# Create the root node (sequence)
1313
root = py_trees.composites.Sequence("RootSequence", memory=True)
1414

15-
# selector = py_trees.composites.Selector("Hacker Team Selector", memory=True)
16-
# root.add_child(selector)
17-
1815

1916
thread = api.create_thread()
2017
challenge = textwrap.dedent("""

‎agentic_btree_video_poster_v1.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import time
2+
3+
import py_trees
4+
5+
from playground.behavior_trees import (
6+
create_assistant_action,
7+
)
8+
9+
10+
# Create the root node (sequence)
11+
root = py_trees.composites.Sequence("RootSequence", memory=True)
12+
13+
search_term = "GPT Agents"
14+
search_youtube_action = create_assistant_action(
15+
action_name=f"Search YouTube({search_term})",
16+
assistant_name="YouTube Researcher v2",
17+
assistant_instructions=f"""
18+
Search Term: {search_term}
19+
Use the query "{search_term}" to search for videos on YouTube.
20+
then for each video download the transcript and summarize it for relevance to {search_term}
21+
be sure to include a link to each of the videos,
22+
and then save all summarizations to a file called youtube_transcripts.txt
23+
If you encounter any errors, please return just the word FAILURE.
24+
""",
25+
)
26+
root.add_child(search_youtube_action)
27+
28+
write_post_action = create_assistant_action(
29+
action_name="Write Post",
30+
assistant_name="Twitter Post Writer",
31+
assistant_instructions="""
32+
Load the file called youtube_transcripts.txt,
33+
analyze the contents for references to search term at the top and then select
34+
the most exciting and relevant video related to:
35+
eductional, entertaining, or informative, to post on Twitter.
36+
Then write a Twitter post that is relevant to the video,
37+
and include a link to the video, along
38+
with exciting highlights or mentions,
39+
and save it to a file called youtube_twitter_post.txt.
40+
If you encounter any errors, please return just the word FAILURE.
41+
""",
42+
)
43+
root.add_child(write_post_action)
44+
45+
post_action = create_assistant_action(
46+
action_name="Post",
47+
assistant_name="Social Media Assistant",
48+
assistant_instructions="""
49+
Load the file called youtube_twitter_post.txt and post the content to Twitter.
50+
If the content is empty please do not post anything.
51+
If you encounter any errors, please return just the word FAILURE.
52+
""",
53+
)
54+
root.add_child(post_action)
55+
56+
57+
# Create the behavior tree
58+
tree = py_trees.trees.BehaviourTree(root)
59+
60+
# Tick the tree to run it
61+
while True:
62+
tree.tick()
63+
time.sleep(30) # Simulate time between ticks

‎agentic_btree_video_poster_v2.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import time
2+
3+
import py_trees
4+
5+
from playground.behavior_trees import (
6+
create_assistant_action,
7+
)
8+
9+
search_term = "GPT Agents"
10+
# Create the root node (sequence)
11+
root = py_trees.composites.Sequence("RootSequence", memory=True)
12+
13+
selector = py_trees.composites.Selector("Search Selector", memory=True)
14+
root.add_child(selector)
15+
16+
search_term = "GPT Agents"
17+
search_youtube_action = create_assistant_action(
18+
action_name=f"Search YouTube({search_term})",
19+
assistant_name="YouTube Researcher v2",
20+
assistant_instructions=f"""
21+
Search Term: {search_term}
22+
Use the query "{search_term}" to search for videos on YouTube.
23+
then for each video download the transcript and summarize it for relevance to {search_term}
24+
be sure to include a link to each of the videos,
25+
and then save all summarizations to a file called youtube_transcripts.txt
26+
If you encounter any errors, please return just the word FAILURE.
27+
""",
28+
)
29+
selector.add_child(search_youtube_action)
30+
31+
search_term = "OpenAI"
32+
search_youtube_action = create_assistant_action(
33+
action_name=f"Search YouTube({search_term})",
34+
assistant_name="YouTube Researcher v2",
35+
assistant_instructions=f"""
36+
Search Term: {search_term}
37+
Use the query "{search_term}" to search for videos on YouTube.
38+
then for each video download the transcript and summarize it for relevance to {search_term}
39+
be sure to include a link to each of the videos,
40+
and then save all summarizations to a file called youtube_transcripts.txt
41+
If you encounter any errors, please return just the word FAILURE.
42+
""",
43+
)
44+
selector.add_child(search_youtube_action)
45+
46+
search_term = "GPT-4o"
47+
search_youtube_action = create_assistant_action(
48+
action_name=f"Search YouTube({search_term})",
49+
assistant_name="YouTube Researcher v2",
50+
assistant_instructions=f"""
51+
Search Term: {search_term}
52+
Use the query "{search_term}" to search for videos on YouTube.
53+
then for each video download the transcript and summarize it for relevance to {search_term}
54+
be sure to include a link to each of the videos,
55+
and then save all summarizations to a file called youtube_transcripts.txt
56+
If you encounter any errors, please return just the word FAILURE.
57+
""",
58+
)
59+
selector.add_child(search_youtube_action)
60+
61+
write_post_action = create_assistant_action(
62+
action_name="Write Post",
63+
assistant_name="Twitter Post Writer",
64+
assistant_instructions="""
65+
Load the file called youtube_transcripts.txt,
66+
analyze the contents for references to search term at the top and then select
67+
the most exciting and relevant video related to:
68+
eductional, entertaining, or informative, to post on Twitter.
69+
Then write a Twitter post that is relevant to the video,
70+
and include a link to the video, along
71+
with exciting highlights or mentions,
72+
and save it to a file called youtube_twitter_post.txt.
73+
If you encounter any errors, please return just the word FAILURE.
74+
""",
75+
)
76+
root.add_child(write_post_action)
77+
78+
post_action = create_assistant_action(
79+
action_name="Post",
80+
assistant_name="Social Media Assistant",
81+
assistant_instructions="""
82+
Load the file called youtube_twitter_post.txt and post the content to Twitter.
83+
If the content is empty please do not post anything.
84+
If you encounter any errors, please return just the word FAILURE.
85+
""",
86+
)
87+
root.add_child(post_action)
88+
89+
90+
# Create the behavior tree
91+
tree = py_trees.trees.BehaviourTree(root)
92+
93+
# Tick the tree to run it
94+
while True:
95+
tree.tick()
96+
time.sleep(30) # Simulate time between ticks

‎agentic_conversation_btree.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import textwrap
2+
import time
3+
4+
import py_trees
5+
6+
from playground.behavior_trees import (
7+
create_assistant_action_on_thread,
8+
create_assistant_condition_on_thread,
9+
)
10+
from playground.assistants_api import api
11+
12+
# Create the root node (sequence)
13+
root = py_trees.composites.Sequence("RootSequence", memory=True)
14+
bug_file = """
15+
def add_numbers(num1, num2):
16+
# Intentional error: '+' should be used instead of '-'
17+
return num1 - num2
18+
19+
def subtract_numbers(num1, num2):
20+
return num1 - num2
21+
22+
def multiply_numbers(num1, num2):
23+
return num3 * num2
24+
25+
def divide_numbers(num1, num2):
26+
return num1 / num2
27+
28+
def main():
29+
print("Performing arithmetic operations with predefined values:")
30+
31+
add_result = add_numbers()
32+
print(f"Addition Result: {add_result}")
33+
34+
subtract_result = subtract_numbers()
35+
print(f"Subtraction Result: {subtract_result}")
36+
37+
multiply_result = multiply_numbers()
38+
print(f"Multiplication Result: {multiply_result}")
39+
40+
divide_result = divide_numbers()
41+
print(f"Division Result: {divide_result}")
42+
43+
if __name__ == "__main__":
44+
main()
45+
"""
46+
47+
thread = api.create_thread()
48+
49+
debug_code = create_assistant_action_on_thread(
50+
thread=thread,
51+
action_name="Debug code",
52+
assistant_name="Python Debugger",
53+
assistant_instructions=textwrap.dedent(f"""
54+
Here is the code with bugs in it:
55+
{bug_file}
56+
Run the code to identify the bugs and fix them.
57+
Be sure to test the code to ensure it runs without errors or throws any exceptions.
58+
"""),
59+
)
60+
root.add_child(debug_code)
61+
62+
verify = create_assistant_condition_on_thread(
63+
thread=thread,
64+
condition_name="Verify",
65+
assistant_name="Python Coding Assistant",
66+
assistant_instructions=textwrap.dedent(
67+
"""
68+
Verify the solution fixes the bug and there are no more issues.
69+
Verify that no exceptions are thrown when the code is run.
70+
Reply with SUCCESS if the solution is correct, otherwise return FAILURE.
71+
If you are happy with the solution, save the code to a file called fixed_bug.py.
72+
""",
73+
),
74+
)
75+
root.add_child(verify)
76+
77+
78+
# Create the behavior tree
79+
tree = py_trees.trees.BehaviourTree(root)
80+
81+
while True:
82+
tree.tick()
83+
if root.status == py_trees.common.Status.SUCCESS:
84+
break
85+
time.sleep(20) # Simulate time between ticks

0 commit comments

Comments
 (0)