new interactive groupchat example

pull/916/merge
Kye Gomez 2 days ago
parent cc6bfed4be
commit 5343590656

@ -313,7 +313,7 @@ nav:
- Unique Swarms: "swarms/examples/unique_swarms.md" - Unique Swarms: "swarms/examples/unique_swarms.md"
- Agents as Tools: "swarms/examples/agents_as_tools.md" - Agents as Tools: "swarms/examples/agents_as_tools.md"
- Aggregate Multi-Agent Responses: "swarms/examples/aggregate.md" - Aggregate Multi-Agent Responses: "swarms/examples/aggregate.md"
- Interactive GroupChat Example: "swarms/examples/interactive_groupchat_example.md" - Interactive GroupChat Example: "swarms/examples/igc_example.md"
- Applications: - Applications:
- Swarms DAO: "swarms/examples/swarms_dao.md" - Swarms DAO: "swarms/examples/swarms_dao.md"
- Swarms of Browser Agents: "swarms/examples/swarms_of_browser_agents.md" - Swarms of Browser Agents: "swarms/examples/swarms_of_browser_agents.md"

@ -13,43 +13,11 @@ pip3 install -U swarms
## Environment Variables ## Environment Variables
```txt ```txt
WORKSPACE_DIR=""
OPENAI_API_KEY="" OPENAI_API_KEY=""
ANTHROPIC_API_KEY="" ANTHROPIC_API_KEY=""
``` ```
## Function Parameters
### `workers: List[Callable]` (Required)
A list of Agent instances that will work on the task concurrently. Each agent should be a callable object (typically an Agent instance).
### `task: str` (Required)
The task or question that all agents will work on simultaneously. This should be a clear, specific prompt that allows for diverse perspectives.
### `type: HistoryOutputType` (Optional, Default: "all")
Controls the format of the returned conversation history. Available options:
| Type | Description |
|------|-------------|
| **"all"** | Returns the complete conversation including all agent responses and the final aggregation |
| **"list"** | Returns the conversation as a list format |
| **"dict"** or **"dictionary"** | Returns the conversation as a dictionary format |
| **"string"** or **"str"** | Returns only the final aggregated response as a string |
| **"final"** or **"last"** | Returns only the final aggregated response |
| **"json"** | Returns the conversation in JSON format |
| **"yaml"** | Returns the conversation in YAML format |
| **"xml"** | Returns the conversation in XML format |
| **"dict-all-except-first"** | Returns dictionary format excluding the first message |
| **"str-all-except-first"** | Returns string format excluding the first message |
| **"basemodel"** | Returns the conversation as a base model object |
| **"dict-final"** | Returns dictionary format with only the final response |
### `aggregator_model_name: str` (Optional, Default: "anthropic/claude-3-sonnet-20240229")
The model to use for the aggregator agent that synthesizes all the individual agent responses. This should be a model capable of understanding and summarizing complex multi-agent conversations.
## How It Works ## How It Works
1. **Concurrent Execution**: All agents in the `workers` list run the same task simultaneously 1. **Concurrent Execution**: All agents in the `workers` list run the same task simultaneously
@ -102,35 +70,3 @@ result = aggregate(
print(result) print(result)
``` ```
## Code Example
## Use Cases
| Use Case | Description |
|----------|-------------|
| **Investment Analysis** | Get multiple financial perspectives on investment decisions |
| **Research Synthesis** | Combine insights from different research agents |
| **Problem Solving** | Gather diverse approaches to complex problems |
| **Content Creation** | Generate comprehensive content from multiple specialized agents |
| **Decision Making** | Get balanced recommendations from different expert perspectives |
## Error Handling
The function includes validation for:
- Required parameters (`task` and `workers`)
- Proper data types (workers must be a list of callable objects)
- Agent compatibility
## Performance Considerations
- All agents run concurrently, so total execution time is limited by the slowest agent
- The aggregator agent processes all responses, so consider response length and complexity
- Memory usage scales with the number of agents and their response sizes

@ -0,0 +1,135 @@
## Interactive Groupchat Examples
The Interactive GroupChat is a powerful multi-agent architecture that enables dynamic collaboration between multiple AI agents. This architecture allows agents to communicate with each other, respond to mentions using `@agent_name` syntax, and work together to solve complex tasks through structured conversation flows.
### Architecture Description
The Interactive GroupChat implements a **collaborative swarm architecture** where multiple specialized agents work together in a coordinated manner. Key features include:
- **Mention-based Communication**: Agents can be directed to specific tasks using `@agent_name` syntax
- **Flexible Speaker Functions**: Multiple speaking order strategies (round robin, random, priority-based)
- **Enhanced Collaboration**: Agents build upon each other's responses and avoid redundancy
- **Interactive Sessions**: Support for both automated and interactive conversation modes
- **Context Awareness**: Agents maintain conversation history and context
For comprehensive documentation on Interactive GroupChat, visit: [Interactive GroupChat Documentation](https://docs.swarms.world/en/latest/swarms/structs/interactive_groupchat/)
### Step-by-Step Showcase
* **Agent Creation**: Define specialized agents with unique expertise and system prompts
* **GroupChat Initialization**: Create the InteractiveGroupChat structure with desired speaker function
* **Task Definition**: Formulate tasks using `@agent_name` mentions to direct specific agents
* **Execution**: Run the group chat to generate collaborative responses
* **Response Processing**: Handle the coordinated output from multiple agents
* **Iteration**: Chain multiple tasks for complex workflows
## Installation
Install the swarms package using pip:
```bash
pip install -U swarms
```
## Basic Setup
1. First, set up your environment variables:
```python
WORKSPACE_DIR="agent_workspace"
OPENAI_API_KEY=""
```
## Code
```python
"""
InteractiveGroupChat Speaker Function Examples
This example demonstrates how to use different speaker functions in the InteractiveGroupChat:
- Round Robin: Agents speak in a fixed order, cycling through the list
- Random: Agents speak in random order
- Priority: Agents speak based on priority weights
- Custom: User-defined speaker functions
The example also shows how agents can mention each other using @agent_name syntax.
"""
from swarms import Agent
from swarms.structs.interactive_groupchat import (
InteractiveGroupChat,
random_speaker,
)
def create_example_agents():
"""Create example agents for demonstration."""
# Create agents with different expertise
analyst = Agent(
agent_name="analyst",
system_prompt="You are a data analyst. You excel at analyzing data, creating charts, and providing insights.",
model_name="gpt-4.1",
streaming_on=True,
print_on=True,
)
researcher = Agent(
agent_name="researcher",
system_prompt="You are a research specialist. You are great at gathering information, fact-checking, and providing detailed research.",
model_name="gpt-4.1",
streaming_on=True,
print_on=True,
)
writer = Agent(
agent_name="writer",
system_prompt="You are a content writer. You excel at writing clear, engaging content and summarizing information.",
model_name="gpt-4.1",
streaming_on=True,
print_on=True,
)
return [analyst, researcher, writer]
def example_random():
agents = create_example_agents()
# Create group chat with random speaker function
group_chat = InteractiveGroupChat(
name="Random Team",
description="A team that speaks in random order",
agents=agents,
speaker_function=random_speaker,
interactive=False,
)
# Test the random behavior
task = "Let's create a marketing strategy. @analyst @researcher @writer please contribute."
response = group_chat.run(task)
print(f"Response:\n{response}\n")
if __name__ == "__main__":
# example_round_robin()
example_random()
```
## Connect With Us
Join our community of agent engineers and researchers for technical support, cutting-edge updates, and exclusive access to world-class agent engineering insights!
| Platform | Description | Link |
|----------|-------------|------|
| 📚 Documentation | Official documentation and guides | [docs.swarms.world](https://docs.swarms.world) |
| 📝 Blog | Latest updates and technical articles | [Medium](https://medium.com/@kyeg) |
| 💬 Discord | Live chat and community support | [Join Discord](https://discord.gg/jM3Z6M9uMq) |
| 🐦 Twitter | Latest news and announcements | [@kyegomez](https://twitter.com/kyegomez) |
| 👥 LinkedIn | Professional network and updates | [The Swarm Corporation](https://www.linkedin.com/company/the-swarm-corporation) |
| 📺 YouTube | Tutorials and demos | [Swarms Channel](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ) |
| 🎫 Events | Join our community events | [Sign up here](https://lu.ma/5p2jnc2v) |
| 🚀 Onboarding Session | Get onboarded with Kye Gomez, creator and lead maintainer of Swarms | [Book Session](https://cal.com/swarms/swarms-onboarding-session) |
Loading…
Cancel
Save