kikocis/Based-Agent public
Changesets · Branches · Tags · Compare
| Changeset | 1b92001d8e9ed6b8c70834d1a3b9c777e627407719934bc7cb380ae7357c92df |
|---|---|
| Author | Lincoln Murr <lincoln.murr@coinbase.com> · 2024-10-25 01:02 -04:00 |
| Committer | Lincoln Murr <lincoln.murr@coinbase.com> · 2024-10-25 01:02 -04:00 |
| Parents | 65e76436655c |
3 files changed
modified examples/based_agent/agents.py +1 −1
| @@ -144,7 +144,7 @@ | ||
| 144 | 144 | transfer_asset, |
| 145 | 145 | get_balance, |
| 146 | 146 | request_eth_from_faucet, |
| 147 | - generate_art, | |
| 147 | + #generate_art, | |
| 148 | 148 | deploy_nft, |
| 149 | 149 | mint_nft, |
| 150 | 150 | ], |
modified examples/based_agent/run.py +68 −14
| @@ -1,9 +1,64 @@ | ||
| 1 | 1 | import time |
| 2 | +import json | |
| 2 | 3 | from swarm import Swarm |
| 3 | 4 | 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 | |
| 5 | 6 | |
| 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): | |
| 7 | 62 | client = Swarm() |
| 8 | 63 | messages = [] |
| 9 | 64 | |
| @@ -11,10 +66,10 @@ | ||
| 11 | 66 | |
| 12 | 67 | while True: |
| 13 | 68 | # 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" | |
| 15 | 70 | messages.append({"role": "user", "content": thought}) |
| 16 | 71 | |
| 17 | - print(f"\n[{time.strftime('%Y-%m-%d %H:%M:%S')}] Thinking: {thought}") | |
| 72 | + print(f"\n\033[90mSystem:\033[0m {thought}") | |
| 18 | 73 | |
| 19 | 74 | # Run the agent |
| 20 | 75 | response = client.run( |
| @@ -23,18 +78,17 @@ | ||
| 23 | 78 | stream=True |
| 24 | 79 | ) |
| 25 | 80 | |
| 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) | |
| 33 | 89 | |
| 34 | 90 | # Wait for the specified interval |
| 35 | 91 | time.sleep(interval) |
| 36 | 92 | |
| 37 | 93 | 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 @@ | ||
| 1 | 1 | import json |
| 2 | +import time | |
| 2 | 3 | |
| 3 | 4 | from swarm import Swarm |
| 4 | 5 | |
| @@ -85,3 +86,5 @@ | ||
| 85 | 86 | |
| 86 | 87 | messages.extend(response.messages) |
| 87 | 88 | agent = response.agent |
| 89 | + | |
| 90 | + |