Skip to content

Commit 3dd7f1c

Browse files
committed
Renaming, another example, remove agentops
1 parent 987593d commit 3dd7f1c

File tree

9 files changed

+147
-21
lines changed

9 files changed

+147
-21
lines changed

‎.env.default

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
OPENAI_API_KEY="your openai api key here"
22

3-
AGENTOPS_API_KEY = "your agentops api key here"
4-
53
X_EMAIL = "twitter email here"
64
X_USERNAME = "twitter username here"
75
X_PASSWORD = "twitter password here"

‎agentic_btree_on_thread.py

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
# selector = py_trees.composites.Selector("Hacker Team Selector", memory=True)
16+
# root.add_child(selector)
17+
18+
19+
thread = api.create_thread()
20+
challenge = textwrap.dedent("""
21+
Given a string of digits, return the longest substring with alternating odd/even or even/odd digits.
22+
If two or more substrings have the same length, return the substring that occurs first.
23+
Examples
24+
25+
longest_substring("225424272163254474441338664823") ➞ "272163254"
26+
# substrings = 254, 272163254, 474, 41, 38, 23
27+
28+
longest_substring("594127169973391692147228678476") ➞ "16921472"
29+
# substrings = 94127, 169, 16921472, 678, 476
30+
31+
longest_substring("721449827599186159274227324466") ➞ "7214"
32+
# substrings = 7214, 498, 27, 18, 61, 9274, 27, 32
33+
# 7214 and 9274 have same length, but 7214 occurs first.
34+
35+
Notes
36+
37+
The minimum alternating substring size is 2, and there will always be at least one alternating substring.
38+
""")
39+
judge_test_cases = textwrap.dedent("""
40+
Test.assert_equals(longest_substring("844929328912985315632725682153"), "56327256")
41+
Test.assert_equals(longest_substring("769697538272129475593767931733"), "27212947")
42+
Test.assert_equals(longest_substring("937948289456111258444958189244"), "894561")
43+
Test.assert_equals(longest_substring("736237766362158694825822899262"), "636")
44+
Test.assert_equals(longest_substring("369715978955362655737322836233"), "369")
45+
Test.assert_equals(longest_substring("345724969853525333273796592356"), "496985")
46+
Test.assert_equals(longest_substring("548915548581127334254139969136"), "8581")
47+
Test.assert_equals(longest_substring("417922164857852157775176959188"), "78521")
48+
Test.assert_equals(longest_substring("251346385699223913113161144327"), "638569")
49+
Test.assert_equals(longest_substring("483563951878576456268539849244"), "18785")
50+
Test.assert_equals(longest_substring("853667717122615664748443484823"), "474")
51+
Test.assert_equals(longest_substring("398785511683322662883368457392"), "98785")
52+
Test.assert_equals(longest_substring("368293545763611759335443678239"), "76361")
53+
Test.assert_equals(longest_substring("775195358448494712934755311372"), "4947")
54+
Test.assert_equals(longest_substring("646113733929969155976523363762"), "76523")
55+
Test.assert_equals(longest_substring("575337321726324966478369152265"), "478369")
56+
Test.assert_equals(longest_substring("754388489999793138912431545258"), "545258")
57+
Test.assert_equals(longest_substring("198644286258141856918653955964"), "2581418569")
58+
Test.assert_equals(longest_substring("643349187319779695864213682274"), "349")
59+
Test.assert_equals(longest_substring("919331281193713636178478295857"), "36361")
60+
Test.assert_equals(longest_substring("2846286484444288886666448822244466688822247"), "47")
61+
""")
62+
63+
hacker = create_assistant_action_on_thread(
64+
thread=thread,
65+
action_name="Hacker",
66+
assistant_name="Python Coding Assistant",
67+
assistant_instructions=textwrap.dedent(f"""
68+
Challenge goal:
69+
{challenge}
70+
Solve the challenge and output the final solution to a file called solution.py
71+
"""),
72+
)
73+
root.add_child(hacker)
74+
75+
judge = create_assistant_action_on_thread(
76+
thread=thread,
77+
action_name="Judge solution",
78+
assistant_name="Coding Challenge Judge",
79+
assistant_instructions=textwrap.dedent(
80+
f"""
81+
Challenge goal:
82+
{challenge}
83+
Load the solution from the file solution.py.
84+
Then confirm is a solution to the challenge and test it with the following test cases:
85+
{judge_test_cases}
86+
Run the code for the solution and confirm it passes all the test cases.
87+
If the solution passes all tests save the solution to a file called judged_solution.py
88+
""",
89+
),
90+
)
91+
root.add_child(judge)
92+
93+
# verifier operates on a different thread, essentially in closed room
94+
verifier = create_assistant_condition(
95+
condition_name="Verify solution",
96+
assistant_name="Python Coding Assistant",
97+
assistant_instructions=textwrap.dedent(
98+
f"""
99+
Challenge goal:
100+
{challenge}
101+
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:
102+
{judge_test_cases}
103+
If the solution is correct, return only the single word SUCCESS, otherwise return the single word FAILURE.
104+
""",
105+
),
106+
)
107+
root.add_child(verifier)
108+
109+
# Create the behavior tree
110+
tree = py_trees.trees.BehaviourTree(root)
111+
112+
# Tick the tree to run it
113+
# for i in range(1000):
114+
# print(f"Tick {i + 1}")
115+
# tree.tick()
116+
# time.sleep(30) # Simulate time between ticks
117+
118+
while True:
119+
tree.tick()
120+
time.sleep(20) # Simulate time between ticks
121+
if root.status == py_trees.common.Status.SUCCESS:
122+
break
File renamed without changes.
File renamed without changes.
File renamed without changes.

‎logs/logs.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Running on local URL: http://127.0.0.1:7860
2+
3+
To create a public link, set `share=True` in `launch()`.

‎playground/assistants_api.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
from dotenv import load_dotenv
66

77
from playground.assistants_utils import EventHandler
8-
import agentops
98

10-
from playground.utils import id_to_uuid
119

1210
load_dotenv()
1311

@@ -174,16 +172,6 @@ def stream_worker(assistant_id, thread_id, event_handler):
174172
# yield history
175173
message["files"].append(eh.images.pop())
176174

177-
agentops.record(
178-
agentops.LLMEvent(
179-
thread_id=id_to_uuid(thread_id),
180-
agent_id=id_to_uuid(assistant_id),
181-
prompt=prompt,
182-
completion=message["text"] + eh.internal_context,
183-
model=assistant.model,
184-
)
185-
)
186-
187175
return message
188176

189177

‎playground/behavior_trees.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import py_trees
55

66
from playground.assistants_api import api
7-
import agentops
87

98

109
# Define the FunctionWrapper class
@@ -45,12 +44,9 @@ def long_running_process(self):
4544
# Simulate a long-running process
4645
try:
4746
print("%s: Thread started, running process..." % self.name)
48-
agentops.init()
49-
agentops.start_session()
5047
result = self.function_wrapper()
5148
print(result)
5249
if "FAILURE" in result["text"]:
53-
agentops.end_session("Fail")
5450
print("%s: Thread completed with failure." % self.name)
5551
return
5652

@@ -59,11 +55,9 @@ def long_running_process(self):
5955
else:
6056
self.thread_success = True
6157

62-
agentops.end_session("Success")
6358
print("%s: Thread completed successfully." % self.name)
6459
except Exception as e:
6560
print("%s: Exception in thread: %s" % (self.name, str(e)))
66-
agentops.end_session("Fail")
6761
finally:
6862
self.thread_running = False
6963

@@ -105,3 +99,25 @@ def create_assistant_condition(condition_name, assistant_name, assistant_instruc
10599
return ActionWrapper(
106100
name=condition_name, function_wrapper=function_wrapper, is_condition=True
107101
)
102+
103+
104+
def create_assistant_action_on_thread(
105+
thread, action_name, assistant_name, assistant_instructions
106+
):
107+
assistant = api.get_assistant_by_name(assistant_name)
108+
function_wrapper = FunctionWrapper(
109+
api.call_assistant_with_thread, thread, assistant.id, assistant_instructions
110+
)
111+
return ActionWrapper(name=action_name, function_wrapper=function_wrapper)
112+
113+
114+
def create_assistant_condition_on_thread(
115+
thread, condition_name, assistant_name, assistant_instructions
116+
):
117+
assistant = api.get_assistant_by_name(assistant_name)
118+
function_wrapper = FunctionWrapper(
119+
api.call_assistant_with_thread, thread, assistant.id, assistant_instructions
120+
)
121+
return ActionWrapper(
122+
name=condition_name, function_wrapper=function_wrapper, is_condition=True
123+
)

‎requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
gradio
22
openai
3-
agentops
43
wikipedia
54
twitter-api-client
65
python-dotenv

0 commit comments

Comments
 (0)