Skip to content

Commit

Permalink
feat: Add multi-agent swarms with message bus communication
Browse files Browse the repository at this point in the history
Introduces a new multi-agent "swarm" system that enables multiple agents
to run in parallel and communicate with each other:

Message Bus:
- Add MessageBus for centralized message routing between agents
- Implement MessagePayload for structured agent communication
- Add thread-safe message publishing and fetching
- Enable direct messaging and broadcast capabilities

Swarm System:
- Add SwarmManager to orchestrate multiple concurrent agents
- Implement thread management for parallel agent execution
- Add graceful shutdown handling for agent swarms
- Enable dynamic agent loading into swarm groups

Agent Enhancements:
- Update ZerePyAgent to integrate with message bus
- Add message collection and handling loops
- Implement automatic response generation using LLMs
- Add example chatbot configurations for testing

CLI Updates:
- Add 'swarm' command for launching agent groups
- Enable swarm management and monitoring
- Add message sending capabilities between agents

Minor bug fixes
- Setting last_tweet_time properly in twitter_actions.py
- Function signature typo in base_connection.py

Dependencies:
- Update charset-normalizer to 3.4.1
- Add standard-imghdr 3.13.0
  • Loading branch information
yu-jeffy committed Jan 5, 2025
1 parent 8fce05e commit 5b268a7
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 278 deletions.
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,110 @@ Create a new JSON file in the `agents` directory following this structure:
}
```

## Multi-Agent Swarms

ZerePy now supports running multiple agents simultaneously in "swarms". Agents in a swarm can:
- Run concurrently in separate threads
- Communicate autonomously via a shared message bus
- Process messages using their configured LLMs
- Maintain their individual task loops while communicating

### Creating Swarm-Ready Agents

Create agent configuration files that include message handling capabilities:

```json
{
"name": "SwarmAgent1",
"bio": [
"You are SwarmAgent1, an AI that engages in deep discussions.",
"You process incoming messages and generate thoughtful responses."
],
"traits": [
"Analytical",
"Communicative",
"Collaborative"
],
"examples": [
"That's an interesting perspective on AI consciousness...",
"Your point about emergent behavior reminds me of..."
],
"loop_delay": 10,
"config": [
{
"name": "openai",
"model": "gpt-3.5-turbo"
}
],
"tasks": [
{"name": "message-loop", "weight": 1},
{"name": "post-tweet", "weight": 1}
]
}
```

### Running a Swarm

1. Configure your LLM connections, for example:
```bash
configure-connection openai
```

2. Start a swarm from the CLI:
```bash
swarm agent1 agent2 agent3
```

The swarm will run autonomously with agents:
- Processing their individual task loops
- Collecting messages from the shared message bus
- Generating responses using their LLM configurations
- Publishing responses back to the message bus
- Running until interrupted with Ctrl+C

### Architecture

The swarm system consists of three main components:

1. **SwarmManager**: Orchestrates multiple agents and manages their lifecycle
2. **MessageBus**: Handles inter-agent communication and message routing

```
┌─────────────────────────────────────────┐
│ SwarmManager │
├─────────────────────────────────────────┤
│ │
│ ┌──────────┐ ┌──────────┐ │
│ │ Agent 1 │◄─────► │ Agent 2 │ │
│ └──────────┘ └──────────┘ │
│ ▲ ▲ │
│ │ │ │
│ └───────┬───────────┘ │
│ │ │
│ ┌──────────┐ │
│ │MessageBus│ │
│ └──────────┘ │
│ │
└─────────────────────────────────────────┘
```

### Message Bus System

The MessageBus enables autonomous inter-agent communication:
- Thread-safe message publishing and collection
- Automatic message routing between agents
- Support for direct and broadcast messages
- Integrated with agent task loops

### Additional Features

- **Concurrent Execution**: Each agent runs in its own thread
- **Resource Management**: Shared connection pools and resources
- **Graceful Shutdown**: Clean shutdown with proper resource cleanup
- **Autonomous Operation**: No manual intervention needed after launch
- **Task Integration**: Messages processed alongside regular agent tasks
```
## Star History
[![Star History Chart](https://api.star-history.com/svg?repos=blorm-network/ZerePy&type=Date)](https://star-history.com/#blorm-network/ZerePy&Date)
Expand Down
438 changes: 218 additions & 220 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tweepy = "^4.14.0"
prompt-toolkit = "^3.0.48"
anthropic = "^0.42.0"
farcaster = "^0.7.11"

standard-imghdr = "3.13.0"

[build-system]
requires = ["poetry-core"]
Expand Down
4 changes: 3 additions & 1 deletion src/actions/twitter_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ def post_tweet(agent, **kwargs):
current_time = time.time()

if ("last_tweet_time" not in agent.state):
last_tweet_time = 0
agent.state["last_tweet_time"] = 0

last_tweet_time = agent.state["last_tweet_time"]

if current_time - last_tweet_time >= agent.tweet_interval:
agent.logger.info("\n📝 GENERATING NEW TWEET")
Expand Down
Loading

0 comments on commit 5b268a7

Please sign in to comment.