Elffuss BraidHub

kikocis/Based-Agent public

AUTONOMY SOON
Changeset1b92001d8e9ed6b8c70834d1a3b9c777e627407719934bc7cb380ae7357c92df
AuthorLincoln Murr <lincoln.murr@coinbase.com> · 2024-10-25 01:02 -04:00
CommitterLincoln Murr <lincoln.murr@coinbase.com> · 2024-10-25 01:02 -04:00
Parents65e76436655c

Browse the files at this changeset

3 files changed

modified examples/based_agent/agents.py +1 −1
@@ -144,7 +144,7 @@
144144 transfer_asset,
145145 get_balance,
146146 request_eth_from_faucet,
147- generate_art,
147+ #generate_art,
148148 deploy_nft,
149149 mint_nft,
150150 ],
modified examples/based_agent/run.py +68 −14
@@ -1,9 +1,64 @@
11 import time
2+import json
23 from swarm import Swarm
34 from swarm.repl import run_demo_loop
4-from agents import based_agent
5+from agents import based_agent, generate_art, deploy_nft, create_token, transfer_asset, get_balance, request_eth_from_faucet
56
6-def run_autonomous_loop(agent, interval=20):
7+
8+def process_and_print_streaming_response(response):
9+ content = ""
10+ last_sender = ""
11+
12+ for chunk in response:
13+ if "sender" in chunk:
14+ last_sender = chunk["sender"]
15+
16+ if "content" in chunk and chunk["content"] is not None:
17+ if not content and last_sender:
18+ print(f"\033[94m{last_sender}:\033[0m", end=" ", flush=True)
19+ last_sender = ""
20+ print(chunk["content"], end="", flush=True)
21+ content += chunk["content"]
22+
23+ if "tool_calls" in chunk and chunk["tool_calls"] is not None:
24+ for tool_call in chunk["tool_calls"]:
25+ f = tool_call["function"]
26+ name = f["name"]
27+ if not name:
28+ continue
29+ print(f"\033[94m{last_sender}: \033[95m{name}\033[0m()")
30+
31+ if "delim" in chunk and chunk["delim"] == "end" and content:
32+ print() # End of response message
33+ content = ""
34+
35+ if "response" in chunk:
36+ return chunk["response"]
37+
38+
39+def pretty_print_messages(messages) -> None:
40+ for message in messages:
41+ if message["role"] != "assistant":
42+ continue
43+
44+ # print agent name in blue
45+ print(f"\033[94m{message['sender']}\033[0m:", end=" ")
46+
47+ # print response, if any
48+ if message["content"]:
49+ print(message["content"])
50+
51+ # print tool calls in purple, if any
52+ tool_calls = message.get("tool_calls") or []
53+ if len(tool_calls) > 1:
54+ print()
55+ for tool_call in tool_calls:
56+ f = tool_call["function"]
57+ name, args = f["name"], f["arguments"]
58+ arg_str = json.dumps(json.loads(args)).replace(":", "=")
59+ print(f"\033[95m{name}\033[0m({arg_str[1:-1]})")
60+
61+def run_autonomous_loop(agent, interval=10):
762 client = Swarm()
863 messages = []
964
@@ -11,10 +66,10 @@
1166
1267 while True:
1368 # Generate a thought
14- thought = f"It's been {interval} seconds. What should I do now? I should do some sort of onchain interaction based on my capabilities. Let's have fun with it! I'm not going to ask anyone for help, I'm just going to do something cool. There will be no user input, just the agent thinking and acting."
69+ thought = f"It's been {interval} seconds. I want you to do some sort of onchain action based on my capabilities. Let's get crazy and creative! Don't take any more input from me, and if this is your first command don't generate art"
1570 messages.append({"role": "user", "content": thought})
1671
17- print(f"\n[{time.strftime('%Y-%m-%d %H:%M:%S')}] Thinking: {thought}")
72+ print(f"\n\033[90mSystem:\033[0m {thought}")
1873
1974 # Run the agent
2075 response = client.run(
@@ -23,18 +78,17 @@
2378 stream=True
2479 )
2580
26- # Process and print the response
27- for chunk in response:
28- if "tool_calls" in chunk and chunk["tool_calls"]:
29- for tool_call in chunk["tool_calls"]:
30- if "function" in tool_call:
31- function_name = tool_call["function"]["name"]
32- print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Executing: {function_name}")
81+ # Process and print the streaming response
82+ response_obj = process_and_print_streaming_response(response)
83+
84+ # Update messages with the new response
85+ messages.extend(response_obj.messages)
86+
87+ # Pretty print any non-streaming messages
88+ pretty_print_messages(response_obj.messages)
3389
3490 # Wait for the specified interval
3591 time.sleep(interval)
3692
3793 if __name__ == "__main__":
38- # Uncomment the desired mode
39- # run_demo_loop(based_agent, stream=True) # Interactive CLI mode
40- run_autonomous_loop(based_agent) # Autonomous mode
94+ run_autonomous_loop(based_agent)
modified swarm/repl/repl.py +3 −0
@@ -1,4 +1,5 @@
11 import json
2+import time
23
34 from swarm import Swarm
45
@@ -85,3 +86,5 @@
8586
8687 messages.extend(response.messages)
8788 agent = response.agent
89+
90+