kikocis/Based-Agent public
Changesets · Branches · Tags · Compare
| Changeset | 65e76436655c086fcc05e0aaaafad71f1b7553d27fbd6fe102064bf7e30e629d |
|---|---|
| Author | Lincoln Murr <lincoln.murr@coinbase.com> · 2024-10-25 00:18 -04:00 |
| Committer | Lincoln Murr <lincoln.murr@coinbase.com> · 2024-10-25 00:18 -04:00 |
| Parents | 8add4bde3316 |
1 file changed
modified examples/based_agent/run.py +36 −1
| @@ -1,5 +1,40 @@ | ||
| 1 | +import time | |
| 2 | +from swarm import Swarm | |
| 1 | 3 | from swarm.repl import run_demo_loop |
| 2 | 4 | from agents import based_agent |
| 3 | 5 | |
| 6 | +def run_autonomous_loop(agent, interval=20): | |
| 7 | + client = Swarm() | |
| 8 | + messages = [] | |
| 9 | + | |
| 10 | + print("Starting autonomous Based Agent loop...") | |
| 11 | + | |
| 12 | + while True: | |
| 13 | + # 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." | |
| 15 | + messages.append({"role": "user", "content": thought}) | |
| 16 | + | |
| 17 | + print(f"\n[{time.strftime('%Y-%m-%d %H:%M:%S')}] Thinking: {thought}") | |
| 18 | + | |
| 19 | + # Run the agent | |
| 20 | + response = client.run( | |
| 21 | + agent=agent, | |
| 22 | + messages=messages, | |
| 23 | + stream=True | |
| 24 | + ) | |
| 25 | + | |
| 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}") | |
| 33 | + | |
| 34 | + # Wait for the specified interval | |
| 35 | + time.sleep(interval) | |
| 36 | + | |
| 4 | 37 | if __name__ == "__main__": |
| 5 | - run_demo_loop(based_agent, stream=True) | |
| 38 | + # Uncomment the desired mode | |
| 39 | + # run_demo_loop(based_agent, stream=True) # Interactive CLI mode | |
| 40 | + run_autonomous_loop(based_agent) # Autonomous mode |