Elffuss BraidHub

kikocis/Based-Agent public

65e76436655c · 1.6 KiBRawPermalink
1import time
2from swarm import Swarm
3from swarm.repl import run_demo_loop
4from agents import based_agent
5
6def 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
37if __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