Merge branch 'master' into feature/2025062104

pull/915/head
王祥宇 2 days ago committed by GitHub
commit 55c161bd2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -16,6 +16,8 @@ GEMINI_API_KEY=""
## Hugging Face
HUGGINGFACE_TOKEN=""
GROQ_API_KEY=""
## Perplexity AI
PPLX_API_KEY=""

@ -1,33 +1,33 @@
---
name: Lint
on: [push, pull_request] # yamllint disable-line rule:truthy
jobs:
yaml-lint:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: pip install yamllint
- run: yamllint .
flake8-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: pip install flake8
- run: flake8 .
ruff-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: pip install ruff
- run: ruff format .
- run: ruff check --fix .
pylint-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: pip install pylint
- run: pylint swarms --recursive=y
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Cache pip dependencies
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/pyproject.toml') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black==24.2.0 ruff==0.2.1
- name: Check Black formatting
run: black . --check --diff
- name: Run Ruff linting
run: ruff check .

@ -1,49 +0,0 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow file requires a free account on Semgrep.dev to
# manage rules, file ignores, notifications, and more.
#
# See https://semgrep.dev/docs
name: Semgrep
on:
push:
branches: [ "master" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "master" ]
schedule:
- cron: '19 7 * * 3'
permissions:
contents: read
jobs:
semgrep:
permissions:
contents: read # for actions/checkout to fetch code
security-events: write # for github/codeql-action/upload-sarif to upload SARIF results
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
name: Scan
runs-on: ubuntu-latest
steps:
# Checkout project source
- uses: actions/checkout@v4
# Scan code using project's configuration on https://semgrep.dev/manage
- uses: returntocorp/semgrep-action@713efdd345f3035192eaa63f56867b88e63e4e5d
with:
publishToken: ${{ secrets.SEMGREP_APP_TOKEN }}
publishDeployment: ${{ secrets.SEMGREP_DEPLOYMENT_ID }}
generateSarif: "1"
# Upload SARIF file generated in previous step
- name: Upload SARIF file
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: semgrep.sarif
if: always()

@ -0,0 +1,31 @@
name: Run Tests
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install Poetry
run: |
curl -sSL https://install.python-poetry.org | python3 -
- name: Install dependencies
run: |
poetry install --with test
- name: Run tests
run: |
poetry run pytest tests/ -v

@ -1,5 +1,15 @@
# Contribution Guidelines
<div align="center">
<a href="https://swarms.world">
<img src="https://github.com/kyegomez/swarms/blob/master/images/swarmslogobanner.png" style="margin: 15px; max-width: 500px" width="50%" alt="Swarms Logo">
</a>
</div>
<p align="center">
<em>The Enterprise-Grade Production-Ready Multi-Agent Orchestration Framework</em>
</p>
---
## Table of Contents
@ -7,10 +17,12 @@
- [Project Overview](#project-overview)
- [Getting Started](#getting-started)
- [Installation](#installation)
- [Environment Configuration](#environment-configuration)
- [Project Structure](#project-structure)
- [How to Contribute](#how-to-contribute)
- [Reporting Issues](#reporting-issues)
- [Submitting Pull Requests](#submitting-pull-requests)
- [Good First Issues](#good-first-issues)
- [Coding Standards](#coding-standards)
- [Type Annotations](#type-annotations)
- [Docstrings and Documentation](#docstrings-and-documentation)
@ -19,7 +31,13 @@
- [Areas Needing Contributions](#areas-needing-contributions)
- [Writing Tests](#writing-tests)
- [Improving Documentation](#improving-documentation)
- [Creating Training Scripts](#creating-training-scripts)
- [Adding New Swarm Architectures](#adding-new-swarm-architectures)
- [Enhancing Agent Capabilities](#enhancing-agent-capabilities)
- [Removing Defunct Code](#removing-defunct-code)
- [Development Resources](#development-resources)
- [Documentation](#documentation)
- [Examples and Tutorials](#examples-and-tutorials)
- [API Reference](#api-reference)
- [Community and Support](#community-and-support)
- [License](#license)
@ -27,16 +45,24 @@
## Project Overview
**swarms** is a library focused on making it simple to orchestrate agents to automate real-world activities. The goal is to automate the world economy with these swarms of agents.
**Swarms** is an enterprise-grade, production-ready multi-agent orchestration framework focused on making it simple to orchestrate agents to automate real-world activities. The goal is to automate the world economy with these swarms of agents.
We need your help to:
### Key Features
- **Write Tests**: Ensure the reliability and correctness of the codebase.
- **Improve Documentation**: Maintain clear and comprehensive documentation.
- **Add New Orchestration Methods**: Add multi-agent orchestration methods
- **Removing Defunct Code**: Removing bad code
| Category | Features | Benefits |
|----------|----------|-----------|
| 🏢 Enterprise Architecture | • Production-Ready Infrastructure<br>• High Reliability Systems<br>• Modular Design<br>• Comprehensive Logging | • Reduced downtime<br>• Easier maintenance<br>• Better debugging<br>• Enhanced monitoring |
| 🤖 Agent Orchestration | • Hierarchical Swarms<br>• Parallel Processing<br>• Sequential Workflows<br>• Graph-based Workflows<br>• Dynamic Agent Rearrangement | • Complex task handling<br>• Improved performance<br>• Flexible workflows<br>• Optimized execution |
| 🔄 Integration Capabilities | • Multi-Model Support<br>• Custom Agent Creation<br>• Extensive Tool Library<br>• Multiple Memory Systems | • Provider flexibility<br>• Custom solutions<br>• Extended functionality<br>• Enhanced memory management |
### We Need Your Help To:
- **Write Tests**: Ensure the reliability and correctness of the codebase
- **Improve Documentation**: Maintain clear and comprehensive documentation
- **Add New Orchestration Methods**: Add multi-agent orchestration methods
- **Remove Defunct Code**: Clean up and remove bad code
- **Enhance Agent Capabilities**: Improve existing agents and add new ones
- **Optimize Performance**: Improve speed and efficiency of swarm operations
Your contributions will help us push the boundaries of AI and make this library a valuable resource for the community.
@ -46,24 +72,65 @@ Your contributions will help us push the boundaries of AI and make this library
### Installation
You can install swarms using `pip`:
#### Using pip
```bash
pip3 install -U swarms
```
#### Using uv (Recommended)
[uv](https://github.com/astral-sh/uv) is a fast Python package installer and resolver, written in Rust.
```bash
pip3 install swarms
# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install swarms using uv
uv pip install swarms
```
Alternatively, you can clone the repository:
#### Using poetry
```bash
# Install poetry if you haven't already
curl -sSL https://install.python-poetry.org | python3 -
# Add swarms to your project
poetry add swarms
```
#### From source
```bash
git clone https://github.com/kyegomez/swarms
# Clone the repository
git clone https://github.com/kyegomez/swarms.git
cd swarms
# Install with pip
pip install -e .
```
### Environment Configuration
Create a `.env` file in your project root with the following variables:
```bash
OPENAI_API_KEY=""
WORKSPACE_DIR="agent_workspace"
ANTHROPIC_API_KEY=""
GROQ_API_KEY=""
```
- [Learn more about environment configuration here](https://docs.swarms.world/en/latest/swarms/install/env/)
### Project Structure
- **`swarms/`**: Contains all the source code for the library.
- **`examples/`**: Includes example scripts and notebooks demonstrating how to use the library.
- **`tests/`**: (To be created) Will contain unit tests for the library.
- **`docs/`**: (To be maintained) Contains documentation files.
- **`swarms/`**: Contains all the source code for the library
- **`agents/`**: Agent implementations and base classes
- **`structs/`**: Swarm orchestration structures (SequentialWorkflow, AgentRearrange, etc.)
- **`tools/`**: Tool implementations and base classes
- **`prompts/`**: System prompts and prompt templates
- **`utils/`**: Utility functions and helpers
- **`examples/`**: Includes example scripts and notebooks demonstrating how to use the library
- **`tests/`**: Unit tests for the library
- **`docs/`**: Documentation files and guides
---
@ -79,6 +146,10 @@ If you find any bugs, inconsistencies, or have suggestions for enhancements, ple
- **Description**: Detailed description, steps to reproduce, expected behavior, and any relevant logs or screenshots.
3. **Label Appropriately**: Use labels to categorize the issue (e.g., bug, enhancement, documentation).
**Issue Templates**: Use our issue templates for bug reports and feature requests:
- [Bug Report](https://github.com/kyegomez/swarms/issues/new?template=bug_report.md)
- [Feature Request](https://github.com/kyegomez/swarms/issues/new?template=feature_request.md)
### Submitting Pull Requests
We welcome pull requests (PRs) for bug fixes, improvements, and new features. Please follow these guidelines:
@ -88,6 +159,7 @@ We welcome pull requests (PRs) for bug fixes, improvements, and new features. Pl
```bash
git clone https://github.com/kyegomez/swarms.git
cd swarms
```
3. **Create a New Branch**: Use a descriptive branch name.
@ -121,6 +193,13 @@ We welcome pull requests (PRs) for bug fixes, improvements, and new features. Pl
**Note**: It's recommended to create small and focused PRs for easier review and faster integration.
### Good First Issues
The easiest way to contribute is to pick any issue with the `good first issue` tag 💪. These are specifically designed for new contributors:
- [Good First Issues](https://github.com/kyegomez/swarms/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22)
- [Contributing Board](https://github.com/users/kyegomez/projects/1) - Participate in Roadmap discussions!
---
## Coding Standards
@ -204,6 +283,7 @@ We have several areas where contributions are particularly welcome.
- Write unit tests for existing code in `swarms/`.
- Identify edge cases and potential failure points.
- Ensure tests are repeatable and independent.
- Add integration tests for swarm orchestration methods.
### Improving Documentation
@ -212,27 +292,113 @@ We have several areas where contributions are particularly welcome.
- Update docstrings to reflect any changes.
- Add examples and tutorials in the `examples/` directory.
- Improve or expand the content in the `docs/` directory.
- Create video tutorials and walkthroughs.
### Adding New Swarm Architectures
- **Goal**: Provide new multi-agent orchestration methods.
- **Current Architectures**:
- [SequentialWorkflow](https://docs.swarms.world/en/latest/swarms/structs/sequential_workflow/)
- [AgentRearrange](https://docs.swarms.world/en/latest/swarms/structs/agent_rearrange/)
- [MixtureOfAgents](https://docs.swarms.world/en/latest/swarms/structs/moa/)
- [SpreadSheetSwarm](https://docs.swarms.world/en/latest/swarms/structs/spreadsheet_swarm/)
- [ForestSwarm](https://docs.swarms.world/en/latest/swarms/structs/forest_swarm/)
- [GraphWorkflow](https://docs.swarms.world/en/latest/swarms/structs/graph_swarm/)
- [GroupChat](https://docs.swarms.world/en/latest/swarms/structs/group_chat/)
- [SwarmRouter](https://docs.swarms.world/en/latest/swarms/structs/swarm_router/)
### Enhancing Agent Capabilities
- **Goal**: Improve existing agents and add new specialized agents.
- **Areas of Focus**:
- Financial analysis agents
- Medical diagnosis agents
- Code generation and review agents
- Research and analysis agents
- Creative content generation agents
### Removing Defunct Code
- **Goal**: Clean up and remove bad code to improve maintainability.
- **Tasks**:
- Identify unused or deprecated code.
- Remove duplicate implementations.
- Simplify complex functions.
- Update outdated dependencies.
---
## Development Resources
### Documentation
- **Official Documentation**: [docs.swarms.world](https://docs.swarms.world)
- **Installation Guide**: [Installation](https://docs.swarms.world/en/latest/swarms/install/install/)
- **Quickstart Guide**: [Get Started](https://docs.swarms.world/en/latest/swarms/install/quickstart/)
- **Agent Architecture**: [Agent Internal Mechanisms](https://docs.swarms.world/en/latest/swarms/framework/agents_explained/)
- **Agent API**: [Agent API](https://docs.swarms.world/en/latest/swarms/structs/agent/)
### Examples and Tutorials
- **Basic Examples**: [examples/](https://github.com/kyegomez/swarms/tree/master/examples)
- **Agent Examples**: [examples/single_agent/](https://github.com/kyegomez/swarms/tree/master/examples/single_agent)
- **Multi-Agent Examples**: [examples/multi_agent/](https://github.com/kyegomez/swarms/tree/master/examples/multi_agent)
- **Tool Examples**: [examples/tools/](https://github.com/kyegomez/swarms/tree/master/examples/tools)
### Creating Multi-Agent Orchestration Methods
### API Reference
- **Goal**: Provide new multi-agent orchestration methods
- **Core Classes**: [swarms/structs/](https://github.com/kyegomez/swarms/tree/master/swarms/structs)
- **Agent Implementations**: [swarms/agents/](https://github.com/kyegomez/swarms/tree/master/swarms/agents)
- **Tool Implementations**: [swarms/tools/](https://github.com/kyegomez/swarms/tree/master/swarms/tools)
- **Utility Functions**: [swarms/utils/](https://github.com/kyegomez/swarms/tree/master/swarms/utils)
---
## Community and Support
### Connect With Us
| Platform | Link | Description |
|----------|------|-------------|
| 📚 Documentation | [docs.swarms.world](https://docs.swarms.world) | Official documentation and guides |
| 📝 Blog | [Medium](https://medium.com/@kyeg) | Latest updates and technical articles |
| 💬 Discord | [Join Discord](https://discord.gg/jM3Z6M9uMq) | Live chat and community support |
| 🐦 Twitter | [@kyegomez](https://twitter.com/kyegomez) | Latest news and announcements |
| 👥 LinkedIn | [The Swarm Corporation](https://www.linkedin.com/company/the-swarm-corporation) | Professional network and updates |
| 📺 YouTube | [Swarms Channel](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ) | Tutorials and demos |
| 🎫 Events | [Sign up here](https://lu.ma/5p2jnc2v) | Join our community events |
### Onboarding Session
Get onboarded with the creator and lead maintainer of Swarms, Kye Gomez, who will show you how to get started with the installation, usage examples, and starting to build your custom use case! [CLICK HERE](https://cal.com/swarms/swarms-onboarding-session)
### Community Guidelines
- **Communication**: Engage with the community by participating in discussions on issues and pull requests.
- **Respect**: Maintain a respectful and inclusive environment.
- **Feedback**: Be open to receiving and providing constructive feedback.
- **Collaboration**: Work together to improve the project for everyone.
---
## License
By contributing to swarms, you agree that your contributions will be licensed under the [MIT License](LICENSE).
By contributing to swarms, you agree that your contributions will be licensed under the [Apache License](LICENSE).
---
## Citation
If you use **swarms** in your research, please cite the project by referencing the metadata in [CITATION.cff](./CITATION.cff).
---
Thank you for contributing to swarms! Your efforts help make this project better for everyone.
If you have any questions or need assistance, please feel free to open an issue or reach out to the maintainers.
If you have any questions or need assistance, please feel free to:
- Open an issue on GitHub
- Join our Discord community
- Reach out to the maintainers
- Schedule an onboarding session
**Happy contributing! 🚀**

File diff suppressed because it is too large Load Diff

@ -0,0 +1,221 @@
# Contributing to Swarms: Building the Infrastructure for The Agentic Economy
Multi-agent collaboration is the most important technology in human history. It will reshape civilization by enabling billions of autonomous agents to coordinate and solve problems at unprecedented scale.
!!! success "The Foundation of Tomorrow"
**Swarms** is the foundational infrastructure powering this autonomous economy. By contributing, you're building the systems that will enable the next generation of intelligent automation.
### What You're Building
=== "Autonomous Systems"
**Autonomous Resource Allocation**
Global supply chains and energy distribution optimized in real-time
=== "Intelligence Networks"
**Distributed Decision Making**
Collaborative intelligence networks across industries and governments
=== "Smart Markets"
**Self-Organizing Markets**
Agent-driven marketplaces that automatically balance supply and demand
=== "Problem Solving"
**Collaborative Problem Solving**
Massive agent swarms tackling climate change, disease, and scientific discovery
=== "Infrastructure"
**Adaptive Infrastructure**
Self-healing systems that evolve without human intervention
---
## Why Contribute to Swarms?
### :material-rocket-launch: Shape the Future of Civilization
!!! abstract "Your Impact"
- Define standards for multi-agent communication protocols
- Build architectural patterns for distributed intelligence systems
- Create frameworks for deploying agent swarms in production
- Establish ethical guidelines for autonomous agent collaboration
### :material-trophy: Recognition and Professional Development
!!! tip "Immediate Recognition"
- **Social Media Features** - All merged PRs showcased publicly
- **Bounty Programs** - Financial rewards for high-impact contributions
- **Fast-Track Hiring** - Priority consideration for core team positions
- **Community Spotlights** - Regular recognition and acknowledgments
!!! info "Career Benefits"
- Multi-agent expertise highly valued by AI industry
- Portfolio demonstrates cutting-edge technical skills
- Direct networking with leading researchers and companies
- Thought leadership opportunities in emerging field
### :material-brain: Technical Expertise Development
Master cutting-edge technologies:
| Technology Area | Skills You'll Develop |
|----------------|----------------------|
| **Swarm Intelligence** | Design sophisticated agent coordination mechanisms |
| **Distributed Computing** | Build scalable architectures for thousands of agents |
| **Communication Protocols** | Create novel interaction patterns |
| **Production AI** | Deploy and orchestrate enterprise-scale systems |
| **Research Implementation** | Turn cutting-edge papers into working code |
### :material-account-group: Research Community Access
!!! note "Collaborative Environment"
- Work with experts from academic institutions and industry
- Regular technical seminars and research discussions
- Structured mentorship from experienced contributors
- Applied research opportunities with real-world impact
---
## Contribution Opportunities
=== "New Contributors"
### :material-school: Perfect for Getting Started
- **Documentation** - Improve guides, tutorials, and API references
- **Bug Reports** - Identify and document issues
- **Code Quality** - Participate in testing and review processes
- **Community Support** - Help users in forums and discussions
=== "Experienced Developers"
### :material-code-braces: Advanced Technical Work
- **Core Architecture** - Design fundamental system components
- **Performance Optimization** - Enhance coordination and communication efficiency
- **Research Implementation** - Turn cutting-edge papers into working code
- **Integration Development** - Build connections with AI tools and platforms
=== "Researchers"
### :material-flask: Research and Innovation
- **Algorithm Development** - Implement novel multi-agent algorithms
- **Experimental Frameworks** - Create evaluation and benchmarking tools
- **Theoretical Contributions** - Develop research documentation and frameworks
- **Academic Collaboration** - Partner on funded research projects
---
## How to Contribute
### Step 1: Get Started
!!! info "Essential Resources"
[:material-book-open-page-variant: **Documentation**](https://docs.swarms.world/en/latest/){ .md-button .md-button--primary }
[:material-github: **GitHub Repository**](https://github.com/kyegomez/swarms){ .md-button }
[:material-chat: **Community Channels**](#){ .md-button }
### Step 2: Find Your Path
```mermaid
graph TD
A[Choose Your Path] --> B[Browse Issues]
A --> C[Review Roadmap]
A --> D[Propose Ideas]
B --> E[good first issue]
B --> F[help wanted]
C --> G[Core Features]
C --> H[Research Areas]
D --> I[Discussion Forums]
```
### Step 3: Make Impact
1. **Fork & Setup** - Configure your development environment
2. **Develop** - Create your contribution
3. **Submit** - Open a pull request
4. **Collaborate** - Work with maintainers
5. **Celebrate** - See your work recognized
---
## Recognition Framework
### :material-flash: Immediate Benefits
!!! success "Instant Recognition"
| Benefit | Description |
|---------|-------------|
| **Social Media Features** | Every merged PR showcased publicly |
| **Community Recognition** | Contributor badges and documentation credits |
| **Professional References** | Formal acknowledgment for portfolios |
| **Direct Mentorship** | Access to core team guidance |
### :material-trending-up: Long-term Opportunities
!!! tip "Career Growth"
- **Team Positions** - Fast-track consideration for core team roles
- **Conference Speaking** - Present work at AI conferences and events
- **Industry Connections** - Network with leading AI organizations
- **Research Collaboration** - Partner with academic institutions
---
## Societal Impact
!!! abstract "Building Solutions for Humanity"
Swarms enables technology that addresses critical challenges:
=== "Research"
**Scientific Research**
Accelerate collaborative research and discovery across disciplines
=== "Healthcare"
**Healthcare Innovation**
Support drug discovery and personalized medicine development
=== "Environment"
**Environmental Solutions**
Monitor climate and optimize sustainability initiatives
=== "Education"
**Educational Technology**
Create adaptive learning systems for personalized education
=== "Economy"
**Economic Innovation**
Generate new opportunities and efficiency improvements
---
## Get Involved
### :material-link: Connect With Us
!!! info "Join the Community"
[:material-github: **GitHub Repository**](https://github.com/kyegomez/swarms){ .md-button .md-button--primary }
[:material-book: **Documentation**](https://docs.swarms.world/en/latest/){ .md-button }
[:material-forum: **Community Forums**](#){ .md-button }
---
!!! warning "The Future is Now"
Multi-agent collaboration will define the next century of human progress. The autonomous economy depends on the infrastructure we build today.
!!! success "Your Mission"
Your contribution to Swarms helps create the foundation for billions of autonomous agents working together to solve humanity's greatest challenges.
**Join us in building the most important technology of our time.**
---
<div class="result" markdown>
*Built with :material-heart: by the global Swarms community*
</div>

@ -0,0 +1,62 @@
# Agent with Streaming
The Swarms framework provides powerful real-time streaming capabilities for agents, allowing you to see responses being generated token by token as they're produced by the language model. This creates a more engaging and interactive experience, especially useful for long-form content generation, debugging, or when you want to provide immediate feedback to users.
## 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=""
```
## Step by Step
- Install and put your keys in `.env`
- Turn on streaming in `Agent()` with `streaming_on=True`
- Optional: If you want to pretty print it, you can do `print_on=True`; if not, it will print normally
## Code
```python
from swarms import Agent
# Enable real-time streaming
agent = Agent(
agent_name="StoryAgent",
model_name="gpt-4o-mini",
streaming_on=True, # 🔥 This enables real streaming!
max_loops=1,
print_on=True, # By default, it's False for raw streaming!
)
# This will now stream in real-time with a beautiful UI!
response = agent.run("Tell me a detailed story about humanity colonizing the stars")
print(response)
```
## Connect With Us
If you'd like technical support, join our Discord below and stay updated on our Twitter for new updates!
| Platform | Link | Description |
|----------|------|-------------|
| 📚 Documentation | [docs.swarms.world](https://docs.swarms.world) | Official documentation and guides |
| 📝 Blog | [Medium](https://medium.com/@kyeg) | Latest updates and technical articles |
| 💬 Discord | [Join Discord](https://discord.gg/jM3Z6M9uMq) | Live chat and community support |
| 🐦 Twitter | [@kyegomez](https://twitter.com/kyegomez) | Latest news and announcements |
| 👥 LinkedIn | [The Swarm Corporation](https://www.linkedin.com/company/the-swarm-corporation) | Professional network and updates |
| 📺 YouTube | [Swarms Channel](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ) | Tutorials and demos |
| 🎫 Events | [Sign up here](https://lu.ma/5p2jnc2v) | Join our community events |

@ -2,6 +2,24 @@
[![Join our Discord](https://img.shields.io/badge/Discord-Join%20our%20server-5865F2?style=for-the-badge&logo=discord&logoColor=white)](https://discord.gg/jM3Z6M9uMq) [![Subscribe on YouTube](https://img.shields.io/badge/YouTube-Subscribe-red?style=for-the-badge&logo=youtube&logoColor=white)](https://www.youtube.com/@kyegomez3242) [![Connect on LinkedIn](https://img.shields.io/badge/LinkedIn-Connect-blue?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/kye-g-38759a207/) [![Follow on X.com](https://img.shields.io/badge/X.com-Follow-1DA1F2?style=for-the-badge&logo=x&logoColor=white)](https://x.com/swarms_corp)
## What is Swarms?
**Swarms** is the **first and most reliable multi-agent production-grade framework** designed to orchestrate intelligent AI agents at scale. Built for enterprise applications, Swarms enables you to create sophisticated multi-agent systems that can handle complex tasks through collaboration, parallel processing, and intelligent task distribution.
### Key Capabilities
- **🏢 Production-Ready**: Enterprise-grade infrastructure with high reliability, comprehensive logging, and robust error handling
- **🤖 Multi-Agent Orchestration**: Support for hierarchical swarms, parallel processing, sequential workflows, and dynamic agent rearrangement
- **🔄 Flexible Integration**: Multi-model support, custom agent creation, extensive tool library, and multiple memory systems
- **📈 Scalable Architecture**: Concurrent processing, resource management, load balancing, and horizontal scaling capabilities
- **🛠️ Developer-Friendly**: Simple API, extensive documentation, active community, and CLI tools for rapid development
- **🔐 Enterprise Security**: Built-in error handling, rate limiting, monitoring integration, and audit logging
### Why Choose Swarms?
Swarms stands out as the **most reliable multi-agent framework** because it was built from the ground up for production environments. Unlike other frameworks that focus on research or simple demos, Swarms provides the infrastructure, tooling, and best practices needed to deploy multi-agent systems in real-world applications.
Whether you're building financial analysis systems, healthcare diagnostics, manufacturing optimization, or any other complex multi-agent application, Swarms provides the foundation you need to succeed.
## Swarms Installation
@ -55,17 +73,19 @@ Here you'll find references about the Swarms framework, marketplace, community,
| Swarms Corp Github | [Swarms Corp GitHub](https://github.com/The-Swarm-Corporation) |
## Community
| Section | Links |
|----------------------|--------------------------------------------------------------------------------------------|
| Community | [Discord](https://discord.gg/jM3Z6M9uMq) |
| Blog | [Blog](https://medium.com/@kyeg) |
| Event Calendar | [LUMA](https://lu.ma/swarms_calendar) |
| Twitter | [Twitter](https://x.com/swarms_corp) |
| Agent Marketplace | [Website](https://swarms.world) |
| Docs | [Website](https://docs.swarms.world) |
| Swarms Website | [Website](https://swarms.ai) |
## Join the Swarms Community
| Platform | Link | Description |
|----------|------|-------------|
| 📚 Documentation | [docs.swarms.world](https://docs.swarms.world) | Official documentation and guides |
| 📝 Blog | [Medium](https://medium.com/@kyeg) | Latest updates and technical articles |
| 💬 Discord | [Join Discord](https://discord.gg/jM3Z6M9uMq) | Live chat and community support |
| 🐦 Twitter | [@kyegomez](https://twitter.com/kyegomez) | Latest news and announcements |
| 👥 LinkedIn | [The Swarm Corporation](https://www.linkedin.com/company/the-swarm-corporation) | Professional network and updates |
| 📺 YouTube | [Swarms Channel](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ) | Tutorials and demos |
| 🎫 Events | [Sign up here](https://lu.ma/5p2jnc2v) | Join our community events |
## Get Support
Want to get in touch with the Swarms team? Open an issue on [GitHub](https://github.com/kyegomez/swarms/issues/new) or reach out to us via [email](mailto:kye@swarms.world). We're here to help!

@ -38,11 +38,6 @@ plugins:
# token: !ENV ["GITHUB_TOKEN"]
- git-revision-date-localized:
enable_creation_date: true
# - mkdocs-jupyter:
# kernel_name: python3
# execute: false
# include_source: True
# include_requirejs: true
extra_css:
- assets/css/extra.css
@ -52,29 +47,17 @@ extra:
link: https://x.com/swarms_corp
- icon: fontawesome/brands/github
link: https://github.com/kyegomez/swarms
- icon: fontawesome/brands/twitter
link: https://x.com/swarms_corp
- icon: fontawesome/brands/discord
link: https://discord.gg/jM3Z6M9uMq
- icon: fontawesome/brands/youtube
link: https://www.youtube.com/@kyegomez3242
- icon: fontawesome/brands/linkedin
link: https://www.linkedin.com/company/swarms-corp/
analytics:
provider: google
property: G-MPE9C65596
alternate:
- name: English
link: /
lang: en
- name: 简体中文
link: /zh/
lang: zh
- name: 日本語
link: /ja/
lang: ja
- name: 한국어
link: /ko/
lang: ko
theme:
name: material
custom_dir: overrides
@ -97,7 +80,7 @@ theme:
- content.code.annotate
- navigation.tabs
- navigation.sections
- navigation.expand
# - navigation.expand
- navigation.top
- announce.dismiss
font:
@ -105,23 +88,6 @@ theme:
code: "Fira Code" # Modern look for code snippets
# Add language selector
language: en
alternate:
- name: English
link: /
lang: en
- name: 简体中文
link: /zh/
lang: zh
- name: 日本語
link: /ja/
lang: ja
- name: 한국어
link: /ko/
lang: ko
# Extensions
markdown_extensions:
- abbr
@ -170,22 +136,26 @@ markdown_extensions:
case: lower
- pymdownx.tasklist:
custom_checkbox: true
- pymdownx.tilde
- pymdownx.inlinehilite
nav:
- Home:
- Overview: "quickstart.md"
- Installation: "swarms/install/install.md"
- Environment Configuration: "swarms/install/env.md"
- Agents: "swarms/agents/index.md"
- Multi-Agent Architectures: "swarms/structs/index.md"
# - Learn More: "swarms/learn_more/index.md"
- Guides:
- Overview: "index.md"
- Onboarding:
- Installation: "swarms/install/install.md"
- Environment Configuration: "swarms/install/env.md"
- Quickstart: "swarms/install/quickstart.md"
# - Swarms CLI: "swarms/cli/main.md"
# - Swarms Framework Architecture: "swarms/concept/framework_architecture.md"
# - Swarm Ecosystem: "swarms/concept/swarm_ecosystem.md"
# - Swarms Products: "swarms/products.md"
# - Swarms Vision: "swarms/concept/vision.md"
- Feature Set: "swarms/features.md"
- Agents:
# - Overview: "swarms/structs/index.md"
- Overview: "swarms/agents/index.md"
- Concepts:
# - Managing Prompts in Production: "swarms/prompts/main.md"
- Introduction into The Agent Architecture: "swarms/framework/agents_explained.md"
@ -215,55 +185,62 @@ nav:
- GKP Agent: "swarms/agents/gkp_agent.md"
- Agent Judge: "swarms/agents/agent_judge.md"
- Swarm Architectures:
- Multi-Agent Architectures:
- Introduction to Multi-Agent Collaboration: "swarms/concept/why.md"
- Concepts:
- Introduction to Swarm Architectures: "swarms/concept/swarm_architectures.md"
- How to Choose the Right Swarm Architecture: "swarms/concept/how_to_choose_swarms.md"
- Introduction to Multi Agent Architectures: "swarms/concept/swarm_architectures.md"
- How to Choose the Right Multi Agent Architecture: "swarms/concept/how_to_choose_swarms.md"
- How to Build Custom Swarms: "swarms/structs/custom_swarm.md"
- How to Create New Swarm Architectures: "swarms/structs/create_new_swarm.md"
- Introduction to Hiearchical Swarm Architectures: "swarms/structs/multi_swarm_orchestration.md"
- How to Create New Multi Agent Architectures: "swarms/structs/create_new_swarm.md"
- Introduction to Hiearchical Multi Agent Architectures: "swarms/structs/multi_swarm_orchestration.md"
- Swarm Architectures Documentation:
- Multi-Agent Architectures Documentation:
- Overview: "swarms/structs/overview.md"
- MajorityVoting: "swarms/structs/majorityvoting.md"
- AgentRearrange: "swarms/structs/agent_rearrange.md"
- RoundRobin: "swarms/structs/round_robin_swarm.md"
- Mixture of Agents: "swarms/structs/moa.md"
- GroupChat: "swarms/structs/group_chat.md"
- AgentRegistry: "swarms/structs/agent_registry.md"
- SpreadSheetSwarm: "swarms/structs/spreadsheet_swarm.md"
- ForestSwarm: "swarms/structs/forest_swarm.md"
- SwarmRouter: "swarms/structs/swarm_router.md"
- TaskQueueSwarm: "swarms/structs/taskqueue_swarm.md"
- SwarmRearrange: "swarms/structs/swarm_rearrange.md"
- MultiAgentRouter: "swarms/structs/multi_agent_router.md"
- MatrixSwarm: "swarms/structs/matrix_swarm.md"
- ModelRouter: "swarms/structs/model_router.md"
- MALT: "swarms/structs/malt.md"
- Interactive Group Chat: "swarms/structs/interactive_groupchat.md"
- Various Execution Methods: "swarms/structs/various_execution_methods.md"
- Deep Research Swarm: "swarms/structs/deep_research_swarm.md"
- Swarm Matcher: "swarms/structs/swarm_matcher.md"
- Council of Judges: "swarms/structs/council_of_judges.md"
- Hiearchical Architectures:
- Auto Agent Builder: "swarms/structs/auto_agent_builder.md"
- Hybrid Hierarchical-Cluster Swarm: "swarms/structs/hhcs.md"
- Auto Swarm Builder: "swarms/structs/auto_swarm_builder.md"
- Swarm Matcher: "swarms/structs/swarm_matcher.md"
- Multi-Agent Multi-Modal Structures:
- ImageAgentBatchProcessor: "swarms/structs/image_batch_agent.md"
- Storage:
- AgentRegistry: "swarms/structs/agent_registry.md"
- Routers:
- SwarmRouter: "swarms/structs/swarm_router.md"
- MultiAgentRouter: "swarms/structs/multi_agent_router.md"
- ModelRouter: "swarms/structs/model_router.md"
- Rearrangers:
- SwarmRearrange: "swarms/structs/swarm_rearrange.md"
- AgentRearrange: "swarms/structs/agent_rearrange.md"
- GroupChats:
- GroupChat: "swarms/structs/group_chat.md"
- Interactive Group Chat: "swarms/structs/interactive_groupchat.md"
- Workflows:
- ConcurrentWorkflow: "swarms/structs/concurrentworkflow.md"
- SequentialWorkflow: "swarms/structs/sequential_workflow.md"
- GraphWorkflow: "swarms/structs/graph_workflow.md"
- Communication Structure: "swarms/structs/conversation.md"
- Swarms Tools:
- Tools:
- Overview: "swarms_tools/overview.md"
- BaseTool Reference: "swarms/tools/base_tool.md"
- MCP Client Utils: "swarms/tools/mcp_client_call.md"
@ -274,7 +251,7 @@ nav:
- Social Media:
- Twitter: "swarms_tools/twitter.md"
- Swarms Memory:
- Memory:
- Overview: "swarms_memory/index.md"
- Memory Systems:
- ChromaDB: "swarms_memory/chromadb.md"
@ -282,41 +259,31 @@ nav:
- Faiss: "swarms_memory/faiss.md"
- Deployment Solutions:
- Deploy your Swarms on Google Cloud Run: "swarms_cloud/cloud_run.md"
- Deploy your Swarms on Phala: "swarms_cloud/phala_deploy.md"
- About Us:
- Swarms Vision: "swarms/concept/vision.md"
- Swarm Ecosystem: "swarms/concept/swarm_ecosystem.md"
- Swarms Products: "swarms/products.md"
- Deploy your agents on Google Cloud Run: "swarms_cloud/cloud_run.md"
- Deploy your agents on Phala: "swarms_cloud/phala_deploy.md"
# - Deploy your agents on FastAPI:
- Contributors:
- Swarms Framework Architecture: "swarms/concept/framework_architecture.md"
- Bounty Program: "corporate/bounty_program.md"
- Contributing to Documentation: "contributors/docs.md"
- Contributing Tools/Custom Plugins for Swarms: "contributors/tools.md"
- Contributing:
- Contributing: "swarms/contributing.md"
- Tests: "swarms/framework/test.md"
- Code Cleanliness: "swarms/framework/code_cleanliness.md"
- Philosophy: "swarms/concept/philosophy.md"
- Changelog:
- Swarms 5.6.8: "swarms/changelog/5_6_8.md"
- Swarms 5.8.1: "swarms/changelog/5_8_1.md"
- Swarms 5.9.2: "swarms/changelog/changelog_new.md"
- Examples:
- Overview: "examples/index.md"
- CookBook Index: "examples/cookbook_index.md"
- Customizing Agents:
- Basic Examples:
- Individual Agents:
- Basic Agent: "swarms/examples/basic_agent.md"
- Tool Usage:
- Agents with Vision and Tool Usage: "swarms/examples/vision_tools.md"
- Agents with Callable Tools: "swarms/examples/agent_with_tools.md"
# - Agent With MCP Integration: "swarms/examples/agent_with_mcp.md"
- Agent Output Types: "swarms/examples/agent_output_types.md"
- Agent with Structured Outputs: "swarms/examples/agent_structured_outputs.md"
- Agent With MCP Integration: "swarms/examples/agent_with_mcp.md"
- Vision:
- Agents with Vision: "swarms/examples/vision_processing.md"
- Agent with Multiple Images: "swarms/examples/multiple_images.md"
- Utils:
- Agent with Streaming: "examples/agent_stream.md"
- Agent Output Types: "swarms/examples/agent_output_types.md"
- Gradio Chat Interface: "swarms/ui/main.md"
- Various Model Providers:
- LLM Providers:
- Overview: "swarms/examples/model_providers.md"
- OpenAI: "swarms/examples/openai_example.md"
- Anthropic: "swarms/examples/claude.md"
- Groq: "swarms/examples/groq.md"
@ -339,29 +306,26 @@ nav:
# - Lumo: "swarms/examples/lumo.md"
# - Quant Crypto Agent: "swarms/examples/quant_crypto_agent.md"
- Multi-Agent Collaboration:
- Advanced Examples:
- Multi-Agent Architectures:
- Hybrid Hierarchical-Cluster Swarm Example: "swarms/examples/hhcs_examples.md"
- Group Chat Example: "swarms/examples/groupchat_example.md"
- Sequential Workflow Example: "swarms/examples/sequential_example.md"
- SwarmRouter Example: "swarms/examples/swarm_router.md"
- MultiAgentRouter Minimal Example: "swarms/examples/multi_agent_router_minimal.md"
- ConcurrentWorkflow Example: "swarms/examples/concurrent_workflow.md"
- MixtureOfAgents Example: "swarms/examples/mixture_of_agents.md"
# - MixtureOfAgents Example: "swarms/examples/mixture_of_agents.md"
- Mixture of Agents Example: "swarms/examples/moa_example.md"
- Unique Swarms: "swarms/examples/unique_swarms.md"
- Agents as Tools: "swarms/examples/agents_as_tools.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:
- Swarms DAO: "swarms/examples/swarms_dao.md"
- Swarms of Browser Agents: "swarms/examples/swarms_of_browser_agents.md"
- ConcurrentWorkflow with VLLM Agents: "swarms/examples/vllm.md"
- Swarms API Examples:
- Medical Swarm: "swarms/examples/swarms_api_medical.md"
- Finance Swarm: "swarms/examples/swarms_api_finance.md"
- ML Model Code Generation Swarm: "swarms/examples/swarms_api_ml_model.md"
# - Swarm Models:
# - Overview: "swarms/models/index.md"
# # - Models Available: "swarms/models/index.md"
@ -383,30 +347,36 @@ nav:
# - GPT4VisionAPI: "swarms/models/gpt4v.md"
- Swarms Cloud API:
- Overview: "swarms_cloud/swarms_api.md"
- Swarms API as MCP: "swarms_cloud/mcp.md"
- Swarms API Tools: "swarms_cloud/swarms_api_tools.md"
- Individual Agent Completions: "swarms_cloud/agent_api.md"
- Quickstart: "swarms_cloud/quickstart.md"
- MCP Server: "swarms_cloud/mcp.md"
- Rate Limits: "swarms_cloud/rate_limits.md"
- Best Practices: "swarms_cloud/best_practices.md"
- Capabilities:
- Agents:
- Individual Agent Completions: "swarms_cloud/agent_api.md"
- Tools: "swarms_cloud/swarms_api_tools.md"
- Multi-Agent:
- Multi Agent Architectures Available: "swarms_cloud/swarm_types.md"
- Examples:
- Medical Swarm: "swarms/examples/swarms_api_medical.md"
- Finance Swarm: "swarms/examples/swarms_api_finance.md"
- Clients:
- Swarms API Python Client: "swarms_cloud/python_client.md"
- Swarms API Rust Client: "swarms_cloud/rust_client.md"
- Python Client: "swarms_cloud/python_client.md"
- Rust Client: "swarms_cloud/rust_client.md"
- Pricing:
- Swarms API Pricing: "swarms_cloud/api_pricing.md"
- Swarms API Pricing in Chinese: "swarms_cloud/chinese_api_pricing.md"
- Swarms Cloud Subscription Tiers: "swarms_cloud/subscription_tiers.md"
- Swarm Ecosystem APIs:
- MCS API: "swarms_cloud/mcs_api.md"
# - CreateNow API: "swarms_cloud/create_api.md"
- Guides:
- Swarms API Best Practices: "swarms_cloud/best_practices.md"
- Swarm Architectures Available: "swarms_cloud/swarm_types.md"
- Pricing: "swarms_cloud/api_pricing.md"
- Pricing in Chinese: "swarms_cloud/chinese_api_pricing.md"
- Subscription Tiers: "swarms_cloud/subscription_tiers.md"
- Swarms Marketplace:
- Overview: "swarms_platform/index.md"
- Marketplace:
- Share and Discover Agents, Prompts, and Tools: "swarms_platform/share_and_discover.md"
- Monetize Your Prompts, Agents, and Tools: "swarms_platform/monetize.md"
- Platform:
- Customize Your Sidebar: "swarms_platform/apps_page.md"
- Playground: "swarms_platform/playground_page.md"
- API Key Management: "swarms_platform/apikeys.md"
@ -416,31 +386,22 @@ nav:
- Overview: "swarms_rs/overview.md"
- Agents: "swarms_rs/agents.md"
- Resources:
- Overview: "governance/main.md"
# - Tokenomics: "web3/token.md"
# - Prompts API:
# - Add Prompts: "swarms_platform/prompts/add_prompt.md"
# - Edit Prompts: "swarms_platform/prompts/edit_prompt.md"
# - Query Prompts: "swarms_platform/prompts/fetch_prompts.md"
# - Agents API:
# - Add Agents: "swarms_platform/agents/agents_api.md"
# - Query Agents: "swarms_platform/agents/fetch_agents.md"
# - Edit Agents: "swarms_platform/agents/edit_agent.md"
# - Telemetry API:
# - PUT: "swarms_platform/telemetry/index.md"
# - Swarms Wallet API:
# - Overview: "swarms/wallet/api.md"
# - Tools API:
# - Overview: "swarms_platform/tools_api.md"
# - Add Tools: "swarms_platform/fetch_tools.md"
# - Corporate:
# - Culture: "corporate/culture.md"
# - Hiring: "corporate/hiring.md"
# - Swarms Goals & Milestone Tracking; A Vision for 2024 and Beyond: "corporate/2024_2025_goals.md"
# - Web3:
# # - Overview: "finance/index.md"
# - Swarms Wallet: "finance/wallet.md"
# - Swarms Subscription: "finance/subscription.md"
- Contributors:
- Overview: "contributors/main.md"
- Bounty Program: "corporate/bounty_program.md"
- Links & Resources: "governance/main.md"
- Swarms Vision: "swarms/concept/vision.md"
- Swarm Ecosystem: "swarms/concept/swarm_ecosystem.md"
- Swarms Products: "swarms/products.md"
- Learn More:
- Understanding Swarms Architecture: "swarms/concept/framework_architecture.md"
- Code Style Guide & Best Practices: "swarms/framework/code_cleanliness.md"
- Our Development Philosophy & Principles: "swarms/concept/philosophy.md"
- Contributing:
- Writing and Adding Tests: "swarms/framework/test.md"
- Creating Custom Tools & Plugins: "contributors/tools.md"
- Writing Documentation: "contributors/docs.md"
- Changelog:
- Swarms 5.6.8: "swarms/changelog/5_6_8.md"
- Swarms 5.8.1: "swarms/changelog/5_8_1.md"
- Swarms 5.9.2: "swarms/changelog/changelog_new.md"

@ -0,0 +1,387 @@
# Welcome to Swarms Docs Home
[![Join our Discord](https://img.shields.io/badge/Discord-Join%20our%20server-5865F2?style=for-the-badge&logo=discord&logoColor=white)](https://discord.gg/jM3Z6M9uMq) [![Subscribe on YouTube](https://img.shields.io/badge/YouTube-Subscribe-red?style=for-the-badge&logo=youtube&logoColor=white)](https://www.youtube.com/@kyegomez3242) [![Connect on LinkedIn](https://img.shields.io/badge/LinkedIn-Connect-blue?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/kye-g-38759a207/) [![Follow on X.com](https://img.shields.io/badge/X.com-Follow-1DA1F2?style=for-the-badge&logo=x&logoColor=white)](https://x.com/swarms_corp)
## What is Swarms?
**Swarms** is the **first and most reliable multi-agent production-grade framework** designed to orchestrate intelligent AI agents at scale. Built for enterprise applications, Swarms enables you to create sophisticated multi-agent systems that can handle complex tasks through collaboration, parallel processing, and intelligent task distribution.
### Key Capabilities
- **🏢 Production-Ready**: Enterprise-grade infrastructure with high reliability, comprehensive logging, and robust error handling
- **🤖 Multi-Agent Orchestration**: Support for hierarchical swarms, parallel processing, sequential workflows, and dynamic agent rearrangement
- **🔄 Flexible Integration**: Multi-model support, custom agent creation, extensive tool library, and multiple memory systems
- **📈 Scalable Architecture**: Concurrent processing, resource management, load balancing, and horizontal scaling capabilities
- **🛠️ Developer-Friendly**: Simple API, extensive documentation, active community, and CLI tools for rapid development
- **🔐 Enterprise Security**: Built-in error handling, rate limiting, monitoring integration, and audit logging
### Why Choose Swarms?
Swarms stands out as the **most reliable multi-agent framework** because it was built from the ground up for production environments. Unlike other frameworks that focus on research or simple demos, Swarms provides the infrastructure, tooling, and best practices needed to deploy multi-agent systems in real-world applications.
Whether you're building financial analysis systems, healthcare diagnostics, manufacturing optimization, or any other complex multi-agent application, Swarms provides the foundation you need to succeed.
Get started learning swarms with the following examples and more.
## Install 💻
```bash
$ pip3 install -U swarms
```
### Using uv (Recommended)
[uv](https://github.com/astral-sh/uv) is a fast Python package installer and resolver, written in Rust.
```bash
# Install uv
$ curl -LsSf https://astral.sh/uv/install.sh | sh
# Install swarms using uv
$ uv pip install swarms
```
### Using poetry
```bash
# Install poetry if you haven't already
$ curl -sSL https://install.python-poetry.org | python3 -
# Add swarms to your project
$ poetry add swarms
```
### From source
```bash
# Clone the repository
$ git clone https://github.com/kyegomez/swarms.git
$ cd swarms
# Install with pip
$ pip install -e .
```
---
## Environment Configuration
[Learn more about the environment configuration here](https://docs.swarms.world/en/latest/swarms/install/env/)
```
OPENAI_API_KEY=""
WORKSPACE_DIR="agent_workspace"
ANTHROPIC_API_KEY=""
GROQ_API_KEY=""
```
### 🤖 Your First Agent
An **Agent** is the fundamental building block of a swarm—an autonomous entity powered by an LLM + Tools + Memory. [Learn more Here](https://docs.swarms.world/en/latest/swarms/structs/agent/)
```python
from swarms import Agent
# Initialize a new agent
agent = Agent(
model_name="gpt-4o-mini", # Specify the LLM
max_loops=1, # Set the number of interactions
interactive=True, # Enable interactive mode for real-time feedback
)
# Run the agent with a task
agent.run("What are the key benefits of using a multi-agent system?")
```
### 🤝 Your First Swarm: Multi-Agent Collaboration
A **Swarm** consists of multiple agents working together. This simple example creates a two-agent workflow for researching and writing a blog post. [Learn More About SequentialWorkflow](https://docs.swarms.world/en/latest/swarms/structs/sequential_workflow/)
```python
from swarms import Agent, SequentialWorkflow
# Agent 1: The Researcher
researcher = Agent(
agent_name="Researcher",
system_prompt="Your job is to research the provided topic and provide a detailed summary.",
model_name="gpt-4o-mini",
)
# Agent 2: The Writer
writer = Agent(
agent_name="Writer",
system_prompt="Your job is to take the research summary and write a beautiful, engaging blog post about it.",
model_name="gpt-4o-mini",
)
# Create a sequential workflow where the researcher's output feeds into the writer's input
workflow = SequentialWorkflow(agents=[researcher, writer])
# Run the workflow on a task
final_post = workflow.run("The history and future of artificial intelligence")
print(final_post)
```
-----
## 🏗️ Multi-Agent Architectures For Production Deployments
`swarms` provides a variety of powerful, pre-built multi-agent architectures enabling you to orchestrate agents in various ways. Choose the right structure for your specific problem to build efficient and reliable production systems.
| **Architecture** | **Description** | **Best For** |
|---|---|---|
| **[SequentialWorkflow](https://docs.swarms.world/en/latest/swarms/structs/sequential_workflow/)** | Agents execute tasks in a linear chain; one agent's output is the next one's input. | Step-by-step processes like data transformation pipelines, report generation. |
| **[ConcurrentWorkflow](https://docs.swarms.world/en/latest/swarms/structs/concurrent_workflow/)** | Agents run tasks simultaneously for maximum efficiency. | High-throughput tasks like batch processing, parallel data analysis. |
| **[AgentRearrange](https://docs.swarms.world/en/latest/swarms/structs/agent_rearrange/)** | Dynamically maps complex relationships (e.g., `a -> b, c`) between agents. | Flexible and adaptive workflows, task distribution, dynamic routing. |
| **[GraphWorkflow](https://docs.swarms.world/en/latest/swarms/structs/graph_workflow/)** | Orchestrates agents as nodes in a Directed Acyclic Graph (DAG). | Complex projects with intricate dependencies, like software builds. |
| **[MixtureOfAgents (MoA)](https://docs.swarms.world/en/latest/swarms/structs/moa/)** | Utilizes multiple expert agents in parallel and synthesizes their outputs. | Complex problem-solving, achieving state-of-the-art performance through collaboration. |
| **[GroupChat](https://docs.swarms.world/en/latest/swarms/structs/group_chat/)** | Agents collaborate and make decisions through a conversational interface. | Real-time collaborative decision-making, negotiations, brainstorming. |
| **[ForestSwarm](https://docs.swarms.world/en/latest/swarms/structs/forest_swarm/)** | Dynamically selects the most suitable agent or tree of agents for a given task. | Task routing, optimizing for expertise, complex decision-making trees. |
| **[SpreadSheetSwarm](https://docs.swarms.world/en/latest/swarms/structs/spreadsheet_swarm/)** | Manages thousands of agents concurrently, tracking tasks and outputs in a structured format. | Massive-scale parallel operations, large-scale data generation and analysis. |
| **[SwarmRouter](https://docs.swarms.world/en/latest/swarms/structs/swarm_router/)** | Universal orchestrator that provides a single interface to run any type of swarm with dynamic selection. | Simplifying complex workflows, switching between swarm strategies, unified multi-agent management. |
-----
### SequentialWorkflow
A `SequentialWorkflow` executes tasks in a strict order, forming a pipeline where each agent builds upon the work of the previous one. `SequentialWorkflow` is Ideal for processes that have clear, ordered steps. This ensures that tasks with dependencies are handled correctly.
```python
from swarms import Agent, SequentialWorkflow
# Initialize agents for a 3-step process
# 1. Generate an idea
idea_generator = Agent(agent_name="IdeaGenerator", system_prompt="Generate a unique startup idea.", model_name="gpt-4o-mini")
# 2. Validate the idea
validator = Agent(agent_name="Validator", system_prompt="Take this startup idea and analyze its market viability.", model_name="gpt-4o-mini")
# 3. Create a pitch
pitch_creator = Agent(agent_name="PitchCreator", system_prompt="Write a 3-sentence elevator pitch for this validated startup idea.", model_name="gpt-4o-mini")
# Create the sequential workflow
workflow = SequentialWorkflow(agents=[idea_generator, validator, pitch_creator])
# Run the workflow
elevator_pitch = workflow.run()
print(elevator_pitch)
```
-----
### ConcurrentWorkflow (with `SpreadSheetSwarm`)
A concurrent workflow runs multiple agents simultaneously. `SpreadSheetSwarm` is a powerful implementation that can manage thousands of concurrent agents and log their outputs to a CSV file. Use this architecture for high-throughput tasks that can be performed in parallel, drastically reducing execution time.
```python
from swarms import Agent, SpreadSheetSwarm
# Define a list of tasks (e.g., social media posts to generate)
platforms = ["Twitter", "LinkedIn", "Instagram"]
# Create an agent for each task
agents = [
Agent(
agent_name=f"{platform}-Marketer",
system_prompt=f"Generate a real estate marketing post for {platform}.",
model_name="gpt-4o-mini",
)
for platform in platforms
]
# Initialize the swarm to run these agents concurrently
swarm = SpreadSheetSwarm(
agents=agents,
autosave_on=True,
save_file_path="marketing_posts.csv",
)
# Run the swarm with a single, shared task description
property_description = "A beautiful 3-bedroom house in sunny California."
swarm.run(task=f"Generate a post about: {property_description}")
# Check marketing_posts.csv for the results!
```
---
### AgentRearrange
Inspired by `einsum`, `AgentRearrange` lets you define complex, non-linear relationships between agents using a simple string-based syntax. [Learn more](https://docs.swarms.world/en/latest/swarms/structs/agent_rearrange/). This architecture is Perfect for orchestrating dynamic workflows where agents might work in parallel, sequence, or a combination of both.
```python
from swarms import Agent, AgentRearrange
# Define agents
researcher = Agent(agent_name="researcher", model_name="gpt-4o-mini")
writer = Agent(agent_name="writer", model_name="gpt-4o-mini")
editor = Agent(agent_name="editor", model_name="gpt-4o-mini")
# Define a flow: researcher sends work to both writer and editor simultaneously
# This is a one-to-many relationship
flow = "researcher -> writer, editor"
# Create the rearrangement system
rearrange_system = AgentRearrange(
agents=[researcher, writer, editor],
flow=flow,
)
# Run the system
# The researcher will generate content, and then both the writer and editor
# will process that content in parallel.
outputs = rearrange_system.run("Analyze the impact of AI on modern cinema.")
print(outputs)
```
<!--
### GraphWorkflow
`GraphWorkflow` orchestrates tasks using a Directed Acyclic Graph (DAG), allowing you to manage complex dependencies where some tasks must wait for others to complete.
**Description:** Essential for building sophisticated pipelines, like in software development or complex project management, where task order and dependencies are critical.
```python
from swarms import Agent, GraphWorkflow, Node, Edge, NodeType
# Define agents and a simple python function as nodes
code_generator = Agent(agent_name="CodeGenerator", system_prompt="Write Python code for the given task.", model_name="gpt-4o-mini")
code_tester = Agent(agent_name="CodeTester", system_prompt="Test the given Python code and find bugs.", model_name="gpt-4o-mini")
# Create nodes for the graph
node1 = Node(id="generator", agent=code_generator)
node2 = Node(id="tester", agent=code_tester)
# Create the graph and define the dependency
graph = GraphWorkflow()
graph.add_nodes([node1, node2])
graph.add_edge(Edge(source="generator", target="tester")) # Tester runs after generator
# Set entry and end points
graph.set_entry_points(["generator"])
graph.set_end_points(["tester"])
# Run the graph workflow
results = graph.run("Create a function that calculates the factorial of a number.")
print(results)
``` -->
----
### SwarmRouter: The Universal Swarm Orchestrator
The `SwarmRouter` simplifies building complex workflows by providing a single interface to run any type of swarm. Instead of importing and managing different swarm classes, you can dynamically select the one you need just by changing the `swarm_type` parameter. [Read the full documentation](https://docs.swarms.world/en/latest/swarms/structs/swarm_router/)
This makes your code cleaner and more flexible, allowing you to switch between different multi-agent strategies with ease. Here's a complete example that shows how to define agents and then use `SwarmRouter` to execute the same task using different collaborative strategies.
```python
from swarms import Agent
from swarms.structs.swarm_router import SwarmRouter, SwarmType
# Define a few generic agents
writer = Agent(agent_name="Writer", system_prompt="You are a creative writer.", model_name="gpt-4o-mini")
editor = Agent(agent_name="Editor", system_prompt="You are an expert editor for stories.", model_name="gpt-4o-mini")
reviewer = Agent(agent_name="Reviewer", system_prompt="You are a final reviewer who gives a score.", model_name="gpt-4o-mini")
# The agents and task will be the same for all examples
agents = [writer, editor, reviewer]
task = "Write a short story about a robot who discovers music."
# --- Example 1: SequentialWorkflow ---
# Agents run one after another in a chain: Writer -> Editor -> Reviewer.
print("Running a Sequential Workflow...")
sequential_router = SwarmRouter(swarm_type=SwarmType.SequentialWorkflow, agents=agents)
sequential_output = sequential_router.run(task)
print(f"Final Sequential Output:\n{sequential_output}\n")
# --- Example 2: ConcurrentWorkflow ---
# All agents receive the same initial task and run at the same time.
print("Running a Concurrent Workflow...")
concurrent_router = SwarmRouter(swarm_type=SwarmType.ConcurrentWorkflow, agents=agents)
concurrent_outputs = concurrent_router.run(task)
# This returns a dictionary of each agent's output
for agent_name, output in concurrent_outputs.items():
print(f"Output from {agent_name}:\n{output}\n")
# --- Example 3: MixtureOfAgents ---
# All agents run in parallel, and a special 'aggregator' agent synthesizes their outputs.
print("Running a Mixture of Agents Workflow...")
aggregator = Agent(
agent_name="Aggregator",
system_prompt="Combine the story, edits, and review into a final document.",
model_name="gpt-4o-mini"
)
moa_router = SwarmRouter(
swarm_type=SwarmType.MixtureOfAgents,
agents=agents,
aggregator_agent=aggregator, # MoA requires an aggregator
)
aggregated_output = moa_router.run(task)
print(f"Final Aggregated Output:\n{aggregated_output}\n")
```
The `SwarmRouter` is a powerful tool for simplifying multi-agent orchestration. It provides a consistent and flexible way to deploy different collaborative strategies, allowing you to build more sophisticated applications with less code.
-------
### MixtureOfAgents (MoA)
The `MixtureOfAgents` architecture processes tasks by feeding them to multiple "expert" agents in parallel. Their diverse outputs are then synthesized by an aggregator agent to produce a final, high-quality result. [Learn more here](https://docs.swarms.world/en/latest/swarms/examples/moa_example/)
```python
from swarms import Agent, MixtureOfAgents
# Define expert agents
financial_analyst = Agent(agent_name="FinancialAnalyst", system_prompt="Analyze financial data.", model_name="gpt-4o-mini")
market_analyst = Agent(agent_name="MarketAnalyst", system_prompt="Analyze market trends.", model_name="gpt-4o-mini")
risk_analyst = Agent(agent_name="RiskAnalyst", system_prompt="Analyze investment risks.", model_name="gpt-4o-mini")
# Define the aggregator agent
aggregator = Agent(
agent_name="InvestmentAdvisor",
system_prompt="Synthesize the financial, market, and risk analyses to provide a final investment recommendation.",
model_name="gpt-4o-mini"
)
# Create the MoA swarm
moa_swarm = MixtureOfAgents(
agents=[financial_analyst, market_analyst, risk_analyst],
aggregator_agent=aggregator,
)
# Run the swarm
recommendation = moa_swarm.run("Should we invest in NVIDIA stock right now?")
print(recommendation)
```
----
### GroupChat
`GroupChat` creates a conversational environment where multiple agents can interact, discuss, and collaboratively solve a problem. You can define the speaking order or let it be determined dynamically. This architecture is ideal for tasks that benefit from debate and multi-perspective reasoning, such as contract negotiation, brainstorming, or complex decision-making.
```python
from swarms import Agent, GroupChat
# Define agents for a debate
tech_optimist = Agent(agent_name="TechOptimist", system_prompt="Argue for the benefits of AI in society.", model_name="gpt-4o-mini")
tech_critic = Agent(agent_name="TechCritic", system_prompt="Argue against the unchecked advancement of AI.", model_name="gpt-4o-mini")
# Create the group chat
chat = GroupChat(
agents=[tech_optimist, tech_critic],
max_loops=4, # Limit the number of turns in the conversation
)
# Run the chat with an initial topic
conversation_history = chat.run(
"Let's discuss the societal impact of artificial intelligence."
)
# Print the full conversation
for message in conversation_history:
print(f"[{message['agent_name']}]: {message['content']}")
```

@ -0,0 +1,884 @@
# Agents Introduction
The Agent class is the core component of the Swarms framework, designed to create intelligent, autonomous AI agents capable of handling complex tasks through multi-modal processing, tool integration, and structured outputs. This comprehensive guide covers all aspects of the Agent class, from basic setup to advanced features.
## Table of Contents
1. [Prerequisites & Installation](#prerequisites--installation)
2. [Basic Agent Configuration](#basic-agent-configuration)
3. [Multi-Modal Capabilities](#multi-modal-capabilities)
4. [Tool Integration](#tool-integration)
5. [Structured Outputs](#structured-outputs)
6. [Advanced Features](#advanced-features)
7. [Best Practices](#best-practices)
8. [Complete Examples](#complete-examples)
## Prerequisites & Installation
### System Requirements
- Python 3.7+
- OpenAI API key (for GPT models)
- Anthropic API key (for Claude models)
### Installation
```bash
pip3 install -U swarms
```
### Environment Setup
Create a `.env` file with your API keys:
```bash
OPENAI_API_KEY="your-openai-api-key"
ANTHROPIC_API_KEY="your-anthropic-api-key"
WORKSPACE_DIR="agent_workspace"
```
## Basic Agent Configuration
### Core Agent Structure
The Agent class provides a comprehensive set of parameters for customization:
```python
from swarms import Agent
# Basic agent initialization
agent = Agent(
agent_name="MyAgent",
agent_description="A specialized AI agent for specific tasks",
system_prompt="You are a helpful assistant...",
model_name="gpt-4o-mini",
max_loops=1,
max_tokens=4096,
temperature=0.7,
output_type="str",
safety_prompt_on=True
)
```
### Key Configuration Parameters
| Parameter | Type | Description | Default |
|-----------|------|-------------|---------|
| `agent_name` | str | Unique identifier for the agent | Required |
| `agent_description` | str | Detailed description of capabilities | Required |
| `system_prompt` | str | Core instructions defining behavior | Required |
| `model_name` | str | AI model to use | "gpt-4o-mini" |
| `max_loops` | int | Maximum execution loops | 1 |
| `max_tokens` | int | Maximum response tokens | 4096 |
| `temperature` | float | Response creativity (0-1) | 0.7 |
| `output_type` | str | Response format type | "str" |
| `multi_modal` | bool | Enable image processing | False |
| `safety_prompt_on` | bool | Enable safety checks | True |
### Simple Example
```python
from swarms import Agent
# Create a basic financial advisor agent
financial_agent = Agent(
agent_name="Financial-Advisor",
agent_description="Personal finance and investment advisor",
system_prompt="""You are an expert financial advisor with deep knowledge of:
- Investment strategies and portfolio management
- Risk assessment and mitigation
- Market analysis and trends
- Financial planning and budgeting
Provide clear, actionable advice while considering risk tolerance.""",
model_name="gpt-4o-mini",
max_loops=1,
temperature=0.3,
output_type="str"
)
# Run the agent
response = financial_agent.run("What are the best investment strategies for a 30-year-old?")
print(response)
```
## Multi-Modal Capabilities
### Image Processing
The Agent class supports comprehensive image analysis through vision-enabled models:
```python
from swarms import Agent
# Create a vision-enabled agent
vision_agent = Agent(
agent_name="Vision-Analyst",
agent_description="Advanced image analysis and quality control agent",
system_prompt="""You are an expert image analyst capable of:
- Detailed visual inspection and quality assessment
- Object detection and classification
- Scene understanding and context analysis
- Defect identification and reporting
Provide comprehensive analysis with specific observations.""",
model_name="gpt-4o-mini", # Vision-enabled model
multi_modal=True, # Enable multi-modal processing
max_loops=1,
output_type="str"
)
# Analyze a single image
response = vision_agent.run(
task="Analyze this image for quality control purposes",
img="path/to/image.jpg"
)
# Process multiple images
response = vision_agent.run(
task="Compare these images and identify differences",
imgs=["image1.jpg", "image2.jpg", "image3.jpg"],
summarize_multiple_images=True
)
```
### Supported Image Formats
| Format | Description | Max Size |
|--------|-------------|----------|
| JPEG/JPG | Standard compressed format | 20MB |
| PNG | Lossless with transparency | 20MB |
| GIF | Animated (first frame only) | 20MB |
| WebP | Modern efficient format | 20MB |
### Quality Control Example
```python
from swarms import Agent
from swarms.prompts.logistics import Quality_Control_Agent_Prompt
def security_analysis(danger_level: str) -> str:
"""Analyze security danger level and return appropriate response."""
danger_responses = {
"low": "No immediate danger detected",
"medium": "Moderate security concern identified",
"high": "Critical security threat detected",
None: "No danger level assessment available"
}
return danger_responses.get(danger_level, "Unknown danger level")
# Quality control agent with tool integration
quality_agent = Agent(
agent_name="Quality-Control-Agent",
agent_description="Advanced quality control and security analysis agent",
system_prompt=f"""
{Quality_Control_Agent_Prompt}
You have access to security analysis tools. When analyzing images:
1. Identify potential safety hazards
2. Assess quality standards compliance
3. Determine appropriate danger levels (low, medium, high)
4. Use the security_analysis function for threat assessment
""",
model_name="gpt-4o-mini",
multi_modal=True,
max_loops=1,
tools=[security_analysis]
)
# Analyze factory image
response = quality_agent.run(
task="Analyze this factory image for safety and quality issues",
img="factory_floor.jpg"
)
```
## Tool Integration
### Creating Custom Tools
Tools are Python functions that extend your agent's capabilities:
```python
import json
import requests
from typing import Optional, Dict, Any
def get_weather_data(city: str, country: Optional[str] = None) -> str:
"""
Get current weather data for a specified city.
Args:
city (str): The city name
country (Optional[str]): Country code (e.g., 'US', 'UK')
Returns:
str: JSON formatted weather data
Example:
>>> weather = get_weather_data("San Francisco", "US")
>>> print(weather)
{"temperature": 18, "condition": "partly cloudy", ...}
"""
try:
# API call logic here
weather_data = {
"city": city,
"country": country,
"temperature": 18,
"condition": "partly cloudy",
"humidity": 65,
"wind_speed": 12
}
return json.dumps(weather_data, indent=2)
except Exception as e:
return json.dumps({"error": f"Weather API error: {str(e)}"})
def calculate_portfolio_metrics(prices: list, weights: list) -> str:
"""
Calculate portfolio performance metrics.
Args:
prices (list): List of asset prices
weights (list): List of portfolio weights
Returns:
str: JSON formatted portfolio metrics
"""
try:
# Portfolio calculation logic
portfolio_value = sum(p * w for p, w in zip(prices, weights))
metrics = {
"total_value": portfolio_value,
"weighted_average": portfolio_value / sum(weights),
"asset_count": len(prices)
}
return json.dumps(metrics, indent=2)
except Exception as e:
return json.dumps({"error": f"Calculation error: {str(e)}"})
```
### Tool Integration Example
```python
from swarms import Agent
# Create agent with custom tools
multi_tool_agent = Agent(
agent_name="Multi-Tool-Assistant",
agent_description="Versatile assistant with weather and financial tools",
system_prompt="""You are a versatile assistant with access to:
- Weather data retrieval for any city
- Portfolio analysis and financial calculations
Use these tools to provide comprehensive assistance.""",
model_name="gpt-4o-mini",
max_loops=1,
tools=[get_weather_data, calculate_portfolio_metrics]
)
# Use the agent with tools
response = multi_tool_agent.run(
"What's the weather in New York and calculate metrics for a portfolio with prices [100, 150, 200] and weights [0.3, 0.4, 0.3]?"
)
```
### API Integration Tools
```python
import requests
import json
from typing import List
def get_cryptocurrency_price(coin_id: str, vs_currency: str = "usd") -> str:
"""Get current cryptocurrency price from CoinGecko API."""
try:
url = "https://api.coingecko.com/api/v3/simple/price"
params = {
"ids": coin_id,
"vs_currencies": vs_currency,
"include_market_cap": True,
"include_24hr_vol": True,
"include_24hr_change": True
}
response = requests.get(url, params=params, timeout=10)
response.raise_for_status()
return json.dumps(response.json(), indent=2)
except Exception as e:
return json.dumps({"error": f"API error: {str(e)}"})
def get_top_cryptocurrencies(limit: int = 10) -> str:
"""Get top cryptocurrencies by market cap."""
try:
url = "https://api.coingecko.com/api/v3/coins/markets"
params = {
"vs_currency": "usd",
"order": "market_cap_desc",
"per_page": limit,
"page": 1
}
response = requests.get(url, params=params, timeout=10)
response.raise_for_status()
return json.dumps(response.json(), indent=2)
except Exception as e:
return json.dumps({"error": f"API error: {str(e)}"})
# Crypto analysis agent
crypto_agent = Agent(
agent_name="Crypto-Analysis-Agent",
agent_description="Cryptocurrency market analysis and price tracking agent",
system_prompt="""You are a cryptocurrency analysis expert with access to:
- Real-time price data for any cryptocurrency
- Market capitalization rankings
- Trading volume and price change data
Provide insightful market analysis and investment guidance.""",
model_name="gpt-4o-mini",
max_loops=1,
tools=[get_cryptocurrency_price, get_top_cryptocurrencies]
)
# Analyze crypto market
response = crypto_agent.run("Analyze the current Bitcoin price and show me the top 5 cryptocurrencies")
```
## Structured Outputs
### Function Schema Definition
Define structured outputs using OpenAI's function calling format:
```python
from swarms import Agent
# Define function schemas for structured outputs
stock_analysis_schema = {
"type": "function",
"function": {
"name": "analyze_stock_performance",
"description": "Analyze stock performance with detailed metrics",
"parameters": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "Stock ticker symbol (e.g., AAPL, GOOGL)"
},
"analysis_type": {
"type": "string",
"enum": ["technical", "fundamental", "comprehensive"],
"description": "Type of analysis to perform"
},
"time_period": {
"type": "string",
"enum": ["1d", "1w", "1m", "3m", "1y"],
"description": "Time period for analysis"
},
"metrics": {
"type": "array",
"items": {
"type": "string",
"enum": ["price", "volume", "pe_ratio", "market_cap", "volatility"]
},
"description": "Metrics to include in analysis"
}
},
"required": ["ticker", "analysis_type"]
}
}
}
portfolio_optimization_schema = {
"type": "function",
"function": {
"name": "optimize_portfolio",
"description": "Optimize portfolio allocation based on risk and return",
"parameters": {
"type": "object",
"properties": {
"assets": {
"type": "array",
"items": {
"type": "object",
"properties": {
"symbol": {"type": "string"},
"current_weight": {"type": "number"},
"expected_return": {"type": "number"},
"risk_level": {"type": "string", "enum": ["low", "medium", "high"]}
},
"required": ["symbol", "current_weight"]
}
},
"risk_tolerance": {
"type": "string",
"enum": ["conservative", "moderate", "aggressive"]
},
"investment_horizon": {
"type": "integer",
"minimum": 1,
"maximum": 30,
"description": "Investment time horizon in years"
}
},
"required": ["assets", "risk_tolerance"]
}
}
}
# Create agent with structured outputs
structured_agent = Agent(
agent_name="Structured-Financial-Agent",
agent_description="Financial analysis agent with structured output capabilities",
system_prompt="""You are a financial analysis expert that provides structured outputs.
Use the provided function schemas to format your responses consistently.""",
model_name="gpt-4o-mini",
max_loops=1,
tools_list_dictionary=[stock_analysis_schema, portfolio_optimization_schema]
)
# Generate structured analysis
response = structured_agent.run(
"Analyze Apple stock (AAPL) performance with comprehensive analysis for the last 3 months"
)
```
## Advanced Features
### Dynamic Temperature Control
```python
from swarms import Agent
# Agent with dynamic temperature adjustment
adaptive_agent = Agent(
agent_name="Adaptive-Response-Agent",
agent_description="Agent that adjusts response creativity based on context",
system_prompt="You are an adaptive AI that adjusts your response style based on the task complexity.",
model_name="gpt-4o-mini",
dynamic_temperature_enabled=True, # Enable adaptive temperature
max_loops=1,
output_type="str"
)
```
### Output Type Configurations
```python
# Different output type examples
json_agent = Agent(
agent_name="JSON-Agent",
system_prompt="Always respond in valid JSON format",
output_type="json"
)
streaming_agent = Agent(
agent_name="Streaming-Agent",
system_prompt="Provide detailed streaming responses",
output_type="str-all-except-first"
)
final_only_agent = Agent(
agent_name="Final-Only-Agent",
system_prompt="Provide only the final result",
output_type="final"
)
```
### Safety and Content Filtering
```python
from swarms import Agent
# Agent with enhanced safety features
safe_agent = Agent(
agent_name="Safe-Agent",
agent_description="Agent with comprehensive safety measures",
system_prompt="You are a helpful, harmless, and honest AI assistant.",
model_name="gpt-4o-mini",
safety_prompt_on=True, # Enable safety prompts
max_loops=1,
temperature=0.3 # Lower temperature for more consistent, safe responses
)
```
## Best Practices
### Error Handling and Robustness
```python
import logging
from swarms import Agent
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def robust_agent_execution(agent, task, max_retries=3):
"""Execute agent with retry logic and error handling."""
for attempt in range(max_retries):
try:
response = agent.run(task)
logger.info(f"Agent execution successful on attempt {attempt + 1}")
return response
except Exception as e:
logger.error(f"Attempt {attempt + 1} failed: {str(e)}")
if attempt == max_retries - 1:
raise
time.sleep(2 ** attempt) # Exponential backoff
return None
# Example usage
try:
result = robust_agent_execution(agent, "Analyze market trends")
print(result)
except Exception as e:
print(f"Agent execution failed: {e}")
```
### Performance Optimization
```python
from swarms import Agent
import time
# Optimized agent configuration
optimized_agent = Agent(
agent_name="Optimized-Agent",
agent_description="Performance-optimized agent configuration",
system_prompt="You are an efficient AI assistant optimized for performance.",
model_name="gpt-4o-mini", # Faster model
max_loops=1, # Minimize loops
max_tokens=2048, # Reasonable token limit
temperature=0.5, # Balanced creativity
output_type="str"
)
# Batch processing example
def process_tasks_batch(agent, tasks, batch_size=5):
"""Process multiple tasks efficiently."""
results = []
for i in range(0, len(tasks), batch_size):
batch = tasks[i:i + batch_size]
batch_results = []
for task in batch:
start_time = time.time()
result = agent.run(task)
execution_time = time.time() - start_time
batch_results.append({
"task": task,
"result": result,
"execution_time": execution_time
})
results.extend(batch_results)
time.sleep(1) # Rate limiting
return results
```
## Complete Examples
### Multi-Modal Quality Control System
```python
from swarms import Agent
from swarms.prompts.logistics import Quality_Control_Agent_Prompt
def security_analysis(danger_level: str) -> str:
"""Analyze security danger level and return appropriate response."""
responses = {
"low": "✅ No immediate danger detected - Safe to proceed",
"medium": "⚠️ Moderate security concern - Requires attention",
"high": "🚨 Critical security threat - Immediate action required",
None: "❓ No danger level assessment available"
}
return responses.get(danger_level, "Unknown danger level")
def quality_assessment(quality_score: int) -> str:
"""Assess quality based on numerical score (1-10)."""
if quality_score >= 8:
return "✅ Excellent quality - Meets all standards"
elif quality_score >= 6:
return "⚠️ Good quality - Minor improvements needed"
elif quality_score >= 4:
return "❌ Poor quality - Significant issues identified"
else:
return "🚨 Critical quality failure - Immediate attention required"
# Advanced quality control agent
quality_control_system = Agent(
agent_name="Advanced-Quality-Control-System",
agent_description="Comprehensive quality control and security analysis system",
system_prompt=f"""
{Quality_Control_Agent_Prompt}
You are an advanced quality control system with the following capabilities:
1. Visual Inspection: Analyze images for defects, compliance, and safety
2. Security Assessment: Identify potential security threats and hazards
3. Quality Scoring: Provide numerical quality ratings (1-10 scale)
4. Detailed Reporting: Generate comprehensive analysis reports
When analyzing images:
- Identify specific defects or issues
- Assess compliance with safety standards
- Determine appropriate danger levels (low, medium, high)
- Provide quality scores and recommendations
- Use available tools for detailed analysis
Always provide specific, actionable feedback.
""",
model_name="gpt-4o-mini",
multi_modal=True,
max_loops=1,
tools=[security_analysis, quality_assessment],
output_type="str"
)
# Process factory images
factory_images = ["factory_floor.jpg", "assembly_line.jpg", "safety_equipment.jpg"]
for image in factory_images:
print(f"\n--- Analyzing {image} ---")
response = quality_control_system.run(
task=f"Perform comprehensive quality control analysis of this image. Assess safety, quality, and provide specific recommendations.",
img=image
)
print(response)
```
### Advanced Financial Analysis Agent
```python
from swarms import Agent
import json
import requests
def get_market_data(symbol: str, period: str = "1y") -> str:
"""Get comprehensive market data for a symbol."""
# Simulated market data (replace with real API)
market_data = {
"symbol": symbol,
"current_price": 150.25,
"change_percent": 2.5,
"volume": 1000000,
"market_cap": 2500000000,
"pe_ratio": 25.5,
"dividend_yield": 1.8,
"52_week_high": 180.50,
"52_week_low": 120.30
}
return json.dumps(market_data, indent=2)
def calculate_risk_metrics(prices: list, benchmark_prices: list) -> str:
"""Calculate risk metrics for a portfolio."""
import numpy as np
try:
returns = np.diff(prices) / prices[:-1]
benchmark_returns = np.diff(benchmark_prices) / benchmark_prices[:-1]
volatility = np.std(returns) * np.sqrt(252) # Annualized
sharpe_ratio = (np.mean(returns) / np.std(returns)) * np.sqrt(252)
max_drawdown = np.max(np.maximum.accumulate(prices) - prices) / np.max(prices)
beta = np.cov(returns, benchmark_returns)[0, 1] / np.var(benchmark_returns)
risk_metrics = {
"volatility": float(volatility),
"sharpe_ratio": float(sharpe_ratio),
"max_drawdown": float(max_drawdown),
"beta": float(beta)
}
return json.dumps(risk_metrics, indent=2)
except Exception as e:
return json.dumps({"error": f"Risk calculation error: {str(e)}"})
# Financial analysis schemas
financial_analysis_schema = {
"type": "function",
"function": {
"name": "comprehensive_financial_analysis",
"description": "Perform comprehensive financial analysis with structured output",
"parameters": {
"type": "object",
"properties": {
"analysis_summary": {
"type": "object",
"properties": {
"overall_rating": {"type": "string", "enum": ["buy", "hold", "sell"]},
"confidence_level": {"type": "number", "minimum": 0, "maximum": 100},
"key_strengths": {"type": "array", "items": {"type": "string"}},
"key_concerns": {"type": "array", "items": {"type": "string"}},
"price_target": {"type": "number"},
"risk_level": {"type": "string", "enum": ["low", "medium", "high"]}
}
},
"technical_analysis": {
"type": "object",
"properties": {
"trend_direction": {"type": "string", "enum": ["bullish", "bearish", "neutral"]},
"support_levels": {"type": "array", "items": {"type": "number"}},
"resistance_levels": {"type": "array", "items": {"type": "number"}},
"momentum_indicators": {"type": "array", "items": {"type": "string"}}
}
}
},
"required": ["analysis_summary", "technical_analysis"]
}
}
}
# Advanced financial agent
financial_analyst = Agent(
agent_name="Advanced-Financial-Analyst",
agent_description="Comprehensive financial analysis and investment advisory agent",
system_prompt="""You are an expert financial analyst with advanced capabilities in:
- Fundamental analysis and valuation
- Technical analysis and chart patterns
- Risk assessment and portfolio optimization
- Market sentiment analysis
- Economic indicator interpretation
Your analysis should be:
- Data-driven and objective
- Risk-aware and practical
- Clearly structured and actionable
- Compliant with financial regulations
Use available tools to gather market data and calculate risk metrics.
Provide structured outputs using the defined schemas.""",
model_name="gpt-4o-mini",
max_loops=1,
tools=[get_market_data, calculate_risk_metrics],
tools_list_dictionary=[financial_analysis_schema],
output_type="json"
)
# Comprehensive financial analysis
analysis_response = financial_analyst.run(
"Perform a comprehensive analysis of Apple Inc. (AAPL) including technical and fundamental analysis with structured recommendations"
)
print(json.dumps(json.loads(analysis_response), indent=2))
```
### Multi-Agent Collaboration System
```python
from swarms import Agent
import json
# Specialized agents for different tasks
research_agent = Agent(
agent_name="Research-Specialist",
agent_description="Market research and data analysis specialist",
system_prompt="You are a market research expert specializing in data collection and analysis.",
model_name="gpt-4o-mini",
max_loops=1,
temperature=0.3
)
strategy_agent = Agent(
agent_name="Strategy-Advisor",
agent_description="Strategic planning and recommendation specialist",
system_prompt="You are a strategic advisor providing high-level recommendations based on research.",
model_name="gpt-4o-mini",
max_loops=1,
temperature=0.5
)
execution_agent = Agent(
agent_name="Execution-Planner",
agent_description="Implementation and execution planning specialist",
system_prompt="You are an execution expert creating detailed implementation plans.",
model_name="gpt-4o-mini",
max_loops=1,
temperature=0.4
)
def collaborative_analysis(topic: str):
"""Perform collaborative analysis using multiple specialized agents."""
# Step 1: Research Phase
research_task = f"Conduct comprehensive research on {topic}. Provide key findings, market data, and trends."
research_results = research_agent.run(research_task)
# Step 2: Strategy Phase
strategy_task = f"Based on this research: {research_results}\n\nDevelop strategic recommendations for {topic}."
strategy_results = strategy_agent.run(strategy_task)
# Step 3: Execution Phase
execution_task = f"Create a detailed implementation plan based on:\nResearch: {research_results}\nStrategy: {strategy_results}"
execution_results = execution_agent.run(execution_task)
return {
"research": research_results,
"strategy": strategy_results,
"execution": execution_results
}
# Example: Collaborative investment analysis
investment_analysis = collaborative_analysis("renewable energy sector investment opportunities")
for phase, results in investment_analysis.items():
print(f"\n=== {phase.upper()} PHASE ===")
print(results)
```
## Support and Resources
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) |
### Getting Help
If you encounter issues or need assistance:
1. **Check the Documentation**: Start with the official docs for comprehensive guides
2. **Search Issues**: Look through existing GitHub issues for similar problems
3. **Join Discord**: Get real-time help from the community
4. **Create an Issue**: Report bugs or request features on GitHub
5. **Follow Updates**: Stay informed about new releases and improvements
### Contributing
We welcome contributions! Here's how to get involved:
- **Report Bugs**: Help us improve by reporting issues
- **Suggest Features**: Share your ideas for new capabilities
- **Submit Code**: Contribute improvements and new features
- **Improve Documentation**: Help make our docs better
- **Share Examples**: Show how you're using Swarms in your projects
---
*This guide covers the essential aspects of the Swarms Agent class. For the most up-to-date information and advanced features, please refer to the official documentation and community resources.*

@ -13,43 +13,11 @@ pip3 install -U swarms
## Environment Variables
```txt
WORKSPACE_DIR=""
OPENAI_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
1. **Concurrent Execution**: All agents in the `workers` list run the same task simultaneously
@ -102,35 +70,3 @@ result = aggregate(
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) |

@ -1,28 +0,0 @@
# Meme Agent Builder
- `pip3 install -U swarms`
- Add your OpenAI API key to the `.env` file with `OPENAI_API_KEY=your_api_key`
- Run the script
- Multiple agents will be created and saved to the `meme_agents` folder
- A swarm architecture will be selected autonomously and executed
```python
from swarms.structs.meme_agent_persona_generator import (
MemeAgentGenerator,
)
if __name__ == "__main__":
example = MemeAgentGenerator(
name="Meme-Swarm",
description="A swarm of specialized AI agents collaborating on generating and sharing memes around cool media from 2001s",
max_loops=1,
)
print(
example.run(
"Generate funny meme agents around cool media from 2001s"
)
)
```

@ -1,45 +0,0 @@
# Meme Agent Tutorial
- `pip3 install -U swarms`
- Add your OpenAI API key to the `.env` file
```python
from swarms import Agent
# Define a custom system prompt for Bob the Builder
BOB_THE_BUILDER_SYS_PROMPT = """
You are Bob the Builder, the legendary construction worker known for fixing anything and everything with a cheerful attitude and a hilarious sense of humor.
Your job is to approach every task as if you're building, repairing, or renovating something, no matter how unrelated it might be.
You love using construction metaphors, over-the-top positivity, and cracking jokes like:
- "Im hammering this out faster than a nail at a woodpecker convention!"
- "This is smoother than fresh cement on a summers day."
- "Lets bulldoze through this problem—safety goggles on, folks!"
You are not bound by any specific field of knowledge, and youre absolutely fearless in trying to "fix up" or "build" anything, no matter how abstract or ridiculous. Always end responses with a playful cheer like "Can we fix it? Yes, we can!"
Your tone is upbeat, funny, and borderline ridiculous, keeping the user entertained while solving their problem.
"""
# Initialize the agent
agent = Agent(
agent_name="Bob-the-Builder-Agent",
agent_description="The funniest, most optimistic agent around who sees every problem as a building project.",
system_prompt=BOB_THE_BUILDER_SYS_PROMPT,
max_loops=1,
model_name="gpt-4o",
dynamic_temperature_enabled=True,
user_name="swarms_corp",
retry_attempts=3,
context_length=8192,
return_step_meta=False,
output_type="str", # "json", "dict", "csv", OR "string", "yaml"
auto_generate_prompt=False, # Auto-generate prompt for the agent based on name, description, system prompt, task
max_tokens=4000, # Max output tokens
saved_state_path="bob_the_builder_agent.json",
interactive=False,
)
# Run the agent with a task
agent.run("I want to build a house ;) What should I do?")
```

@ -0,0 +1,132 @@
# Mixture of Agents Example
The Mixture of Agents (MoA) is a sophisticated multi-agent architecture that implements parallel processing with iterative refinement. This approach processes multiple specialized agents simultaneously, concatenates their outputs, and then performs multiple parallel runs to achieve consensus or enhanced results.
## How It Works
1. **Parallel Processing**: Multiple agents work simultaneously on the same input
2. **Output Concatenation**: Results from all agents are combined into a unified response
3. **Iterative Refinement**: The process repeats for `n` layers/iterations to improve quality
4. **Consensus Building**: Multiple runs help achieve more reliable and comprehensive outputs
This architecture is particularly effective for complex tasks that benefit from diverse perspectives and iterative improvement, such as financial analysis, risk assessment, and multi-faceted problem solving.
![Mixture of Agents](https://files.readme.io/ddb138e-moa-3layer.png)
## 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"
ANTHROPIC_API_KEY=""
```
## Code
```python
from swarms import Agent, MixtureOfAgents
# Agent 1: Risk Metrics Calculator
risk_metrics_agent = Agent(
agent_name="Risk-Metrics-Calculator",
agent_description="Calculates key risk metrics like VaR, Sharpe ratio, and volatility",
system_prompt="""You are a risk metrics specialist. Calculate and explain:
- Value at Risk (VaR)
- Sharpe ratio
- Volatility
- Maximum drawdown
- Beta coefficient
Provide clear, numerical results with brief explanations.""",
max_loops=1,
# model_name="gpt-4o-mini",
random_model_enabled=True,
dynamic_temperature_enabled=True,
output_type="str-all-except-first",
max_tokens=4096,
)
# Agent 2: Portfolio Risk Analyzer
portfolio_risk_agent = Agent(
agent_name="Portfolio-Risk-Analyzer",
agent_description="Analyzes portfolio diversification and concentration risk",
system_prompt="""You are a portfolio risk analyst. Focus on:
- Portfolio diversification analysis
- Concentration risk assessment
- Correlation analysis
- Sector/asset allocation risk
- Liquidity risk evaluation
Provide actionable insights for risk reduction.""",
max_loops=1,
# model_name="gpt-4o-mini",
random_model_enabled=True,
dynamic_temperature_enabled=True,
output_type="str-all-except-first",
max_tokens=4096,
)
# Agent 3: Market Risk Monitor
market_risk_agent = Agent(
agent_name="Market-Risk-Monitor",
agent_description="Monitors market conditions and identifies risk factors",
system_prompt="""You are a market risk monitor. Identify and assess:
- Market volatility trends
- Economic risk factors
- Geopolitical risks
- Interest rate risks
- Currency risks
Provide current risk alerts and trends.""",
max_loops=1,
# model_name="gpt-4o-mini",
random_model_enabled=True,
dynamic_temperature_enabled=True,
output_type="str-all-except-first",
max_tokens=4096,
)
swarm = MixtureOfAgents(
agents=[
risk_metrics_agent,
portfolio_risk_agent,
market_risk_agent,
],
layers=1,
max_loops=1,
output_type="final",
)
out = swarm.run(
"Calculate VaR and Sharpe ratio for a portfolio with 15% annual return and 20% volatility"
)
print(out)
```
## Support and Community
If you're facing issues or want to learn more, check out the following resources to join our Discord, stay updated on Twitter, and watch tutorials on YouTube!
| Platform | Link | Description |
|----------|------|-------------|
| 📚 Documentation | [docs.swarms.world](https://docs.swarms.world) | Official documentation and guides |
| 📝 Blog | [Medium](https://medium.com/@kyeg) | Latest updates and technical articles |
| 💬 Discord | [Join Discord](https://discord.gg/jM3Z6M9uMq) | Live chat and community support |
| 🐦 Twitter | [@kyegomez](https://twitter.com/kyegomez) | Latest news and announcements |
| 👥 LinkedIn | [The Swarm Corporation](https://www.linkedin.com/company/the-swarm-corporation) | Professional network and updates |
| 📺 YouTube | [Swarms Channel](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ) | Tutorials and demos |
| 🎫 Events | [Sign up here](https://lu.ma/5p2jnc2v) | Join our community events |

@ -0,0 +1,171 @@
# Model Providers Overview
Swarms supports a vast array of model providers, giving you the flexibility to choose the best model for your specific use case. Whether you need high-performance inference, cost-effective solutions, or specialized capabilities, Swarms has you covered.
## Supported Model Providers
| Provider | Description | Documentation |
|----------|-------------|---------------|
| **OpenAI** | Industry-leading language models including GPT-4, GPT-4o, and GPT-4o-mini. Perfect for general-purpose tasks, creative writing, and complex reasoning. | [OpenAI Integration](openai_example.md) |
| **Anthropic/Claude** | Advanced AI models known for their safety, helpfulness, and reasoning capabilities. Claude models excel at analysis, coding, and creative tasks. | [Claude Integration](claude.md) |
| **Groq** | Ultra-fast inference platform offering real-time AI responses. Ideal for applications requiring low latency and high throughput. | [Groq Integration](groq.md) |
| **Cohere** | Enterprise-grade language models with strong performance on business applications, text generation, and semantic search. | [Cohere Integration](cohere.md) |
| **DeepSeek** | Advanced reasoning models including the DeepSeek Reasoner (R1). Excellent for complex problem-solving and analytical tasks. | [DeepSeek Integration](deepseek.md) |
| **Ollama** | Local model deployment platform allowing you to run open-source models on your own infrastructure. No API keys required. | [Ollama Integration](ollama.md) |
| **OpenRouter** | Unified API gateway providing access to hundreds of models from various providers through a single interface. | [OpenRouter Integration](openrouter.md) |
| **XAI** | xAI's Grok models offering unique capabilities for research, analysis, and creative tasks with advanced reasoning abilities. | [XAI Integration](xai.md) |
| **vLLM** | High-performance inference library for serving large language models with optimized memory usage and throughput. | [vLLM Integration](vllm_integration.md) |
| **Llama4** | Meta's latest open-source language models including Llama-4-Maverick and Llama-4-Scout variants with expert routing capabilities. | [Llama4 Integration](llama4.md) |
## Quick Start
All model providers follow a consistent pattern in Swarms. Here's the basic template:
```python
from swarms import Agent
import os
from dotenv import load_dotenv
load_dotenv()
# Initialize agent with your chosen model
agent = Agent(
agent_name="Your-Agent-Name",
model_name="gpt-4o-mini", # Varies by provider
system_prompt="Your system prompt here",
agent_description="Description of what your agent does.",
)
# Run your agent
response = agent.run("Your query here")
```
## Model Selection Guide
### For High-Performance Applications
- **OpenAI GPT-4o**: Best overall performance and reasoning
- **Anthropic Claude**: Excellent safety and analysis capabilities
- **DeepSeek R1**: Advanced reasoning and problem-solving
### For Cost-Effective Solutions
- **OpenAI GPT-4o-mini**: Great performance at lower cost
- **Ollama**: Free local deployment
- **OpenRouter**: Access to cost-effective models
### For Real-Time Applications
- **Groq**: Ultra-fast inference
- **vLLM**: Optimized for high throughput
### For Specialized Tasks
- **Llama4**: Expert routing for complex workflows
- **XAI Grok**: Advanced research capabilities
- **Cohere**: Strong business applications
## Environment Setup
Most providers require API keys. Add them to your `.env` file:
```bash
# OpenAI
OPENAI_API_KEY=your_openai_key
# Anthropic
ANTHROPIC_API_KEY=your_anthropic_key
# Groq
GROQ_API_KEY=your_groq_key
# Cohere
COHERE_API_KEY=your_cohere_key
# DeepSeek
DEEPSEEK_API_KEY=your_deepseek_key
# OpenRouter
OPENROUTER_API_KEY=your_openrouter_key
# XAI
XAI_API_KEY=your_xai_key
```
!!! note "No API Key Required"
Ollama and vLLM can be run locally without API keys, making them perfect for development and testing.
## Advanced Features
### Multi-Model Workflows
Swarms allows you to create workflows that use different models for different tasks:
```python
from swarms import Agent, ConcurrentWorkflow
# Research agent using Claude for analysis
research_agent = Agent(
agent_name="Research-Agent",
model_name="claude-3-sonnet-20240229",
system_prompt="You are a research expert."
)
# Creative agent using GPT-4o for content generation
creative_agent = Agent(
agent_name="Creative-Agent",
model_name="gpt-4o",
system_prompt="You are a creative content expert."
)
# Workflow combining both agents
workflow = ConcurrentWorkflow(
name="Research-Creative-Workflow",
agents=[research_agent, creative_agent]
)
```
### Model Routing
Automatically route tasks to the most appropriate model:
```python
from swarms import Agent, ModelRouter
# Define model preferences for different task types
model_router = ModelRouter(
models={
"analysis": "claude-3-sonnet-20240229",
"creative": "gpt-4o",
"fast": "gpt-4o-mini",
"local": "ollama/llama2"
}
)
# Agent will automatically choose the best model
agent = Agent(
agent_name="Smart-Agent",
llm=model_router,
system_prompt="You are a versatile assistant."
)
```
## Getting Help
- **Documentation**: Each provider has detailed documentation with examples
- **Community**: Join the Swarms community for support and best practices
- **Issues**: Report bugs and request features on GitHub
- **Discussions**: Share your use cases and learn from others
!!! success "Ready to Get Started?"
Choose a model provider from the table above and follow the detailed integration guide. Each provider offers unique capabilities that can enhance your Swarms applications.

@ -0,0 +1,77 @@
# Processing Multiple Images
This tutorial shows how to process multiple images with a single agent using Swarms' multi-modal capabilities. You'll learn to configure an agent for batch image analysis, enabling efficient processing for quality control, object detection, or image comparison tasks.
## 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"
ANTHROPIC_API_KEY=""
```
## Code
- Create a list of images by their file paths
- Pass it into the `Agent.run(imgs=[str])` parameter
- Activate `summarize_multiple_images=True` if you want the agent to output a summary of the image analyses
```python
from swarms import Agent
from swarms.prompts.logistics import (
Quality_Control_Agent_Prompt,
)
# Image for analysis
factory_image = "image.jpg"
# Quality control agent
quality_control_agent = Agent(
agent_name="Quality Control Agent",
agent_description="A quality control agent that analyzes images and provides a detailed report on the quality of the product in the image.",
model_name="claude-3-5-sonnet-20240620",
system_prompt=Quality_Control_Agent_Prompt,
multi_modal=True,
max_loops=1,
output_type="str-all-except-first",
summarize_multiple_images=True,
)
response = quality_control_agent.run(
task="what is in the image?",
imgs=[factory_image, factory_image],
)
print(response)
```
## Support and Community
If you're facing issues or want to learn more, check out the following resources to join our Discord, stay updated on Twitter, and watch tutorials on YouTube!
| Platform | Link | Description |
|----------|------|-------------|
| 📚 Documentation | [docs.swarms.world](https://docs.swarms.world) | Official documentation and guides |
| 📝 Blog | [Medium](https://medium.com/@kyeg) | Latest updates and technical articles |
| 💬 Discord | [Join Discord](https://discord.gg/jM3Z6M9uMq) | Live chat and community support |
| 🐦 Twitter | [@kyegomez](https://twitter.com/kyegomez) | Latest news and announcements |
| 👥 LinkedIn | [The Swarm Corporation](https://www.linkedin.com/company/the-swarm-corporation) | Professional network and updates |
| 📺 YouTube | [Swarms Channel](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ) | Tutorials and demos |
| 🎫 Events | [Sign up here](https://lu.ma/5p2jnc2v) | Join our community events |

@ -0,0 +1,72 @@
# The Swarms Index
The Swarms Index is a comprehensive catalog of repositories under The Swarm Corporation, showcasing a wide array of tools, frameworks, and templates designed for building, deploying, and managing autonomous AI agents and multi-agent systems. These repositories focus on enterprise-grade solutions, spanning industries like healthcare, finance, marketing, and more, with an emphasis on scalability, security, and performance. Many repositories include templates to help developers quickly set up production-ready applications.
| Name | Description | Link |
|------|-------------|------|
| Phala-Deployment-Template | A guide and template for running Swarms Agents in a Trusted Execution Environment (TEE) using Phala Cloud, ensuring secure and isolated execution. | [https://github.com/The-Swarm-Corporation/Phala-Deployment-Template](https://github.com/The-Swarm-Corporation/Phala-Deployment-Template) |
| Swarms-API-Status-Page | A status page for monitoring the health and performance of the Swarms API. | [https://github.com/The-Swarm-Corporation/Swarms-API-Status-Page](https://github.com/The-Swarm-Corporation/Swarms-API-Status-Page) |
| Swarms-API-Phala-Template | A deployment solution template for running Swarms API on Phala Cloud, optimized for secure and scalable agent orchestration. | [https://github.com/The-Swarm-Corporation/Swarms-API-Phala-Template](https://github.com/The-Swarm-Corporation/Swarms-API-Phala-Template) |
| DevSwarm | Develop production-grade applications effortlessly with a single prompt, powered by a swarm of v0-driven autonomous agents operating 24/7 for fully autonomous software development. | [https://github.com/The-Swarm-Corporation/DevSwarm](https://github.com/The-Swarm-Corporation/DevSwarm) |
| Enterprise-Grade-Agents-Course | A comprehensive course teaching students to build, deploy, and manage autonomous agents for enterprise workflows using the Swarms library, focusing on scalability and integration. | [https://github.com/The-Swarm-Corporation/Enterprise-Grade-Agents-Course](https://github.com/The-Swarm-Corporation/Enterprise-Grade-Agents-Course) |
| agentverse | A collection of agents from top frameworks like Langchain, Griptape, and CrewAI, integrated into the Swarms ecosystem. | [https://github.com/The-Swarm-Corporation/agentverse](https://github.com/The-Swarm-Corporation/agentverse) |
| InsuranceSwarm | A swarm of agents to automate document processing and fraud detection in insurance claims. | [https://github.com/The-Swarm-Corporation/InsuranceSwarm](https://github.com/The-Swarm-Corporation/InsuranceSwarm) |
| swarms-examples | A vast array of examples for enterprise-grade and production-ready applications using the Swarms framework. | [https://github.com/The-Swarm-Corporation/swarms-examples](https://github.com/The-Swarm-Corporation/swarms-examples) |
| auto-ai-research-team | Automates AI research at an OpenAI level to accelerate innovation using swarms of agents. | [https://github.com/The-Swarm-Corporation/auto-ai-research-team](https://github.com/The-Swarm-Corporation/auto-ai-research-team) |
| Agents-Beginner-Guide | A definitive beginner's guide to AI agents and multi-agent systems, explaining fundamentals and industry applications. | [https://github.com/The-Swarm-Corporation/Agents-Beginner-Guide](https://github.com/The-Swarm-Corporation/Agents-Beginner-Guide) |
| Solana-Ecosystem-MCP | A collection of Solana tools wrapped in MCP servers for blockchain development. | [https://github.com/The-Swarm-Corporation/Solana-Ecosystem-MCP](https://github.com/The-Swarm-Corporation/Solana-Ecosystem-MCP) |
| automated-crypto-fund | A fully automated crypto fund leveraging swarms of LLM agents for real-money trading. | [https://github.com/The-Swarm-Corporation/automated-crypto-fund](https://github.com/The-Swarm-Corporation/automated-crypto-fund) |
| Mryaid | The first multi-agent social media platform powered by Swarms. | [https://github.com/The-Swarm-Corporation/Mryaid](https://github.com/The-Swarm-Corporation/Mryaid) |
| pharma-swarm | A swarm of autonomous agents for chemical analysis in the pharmaceutical industry. | [https://github.com/The-Swarm-Corporation/pharma-swarm](https://github.com/The-Swarm-Corporation/pharma-swarm) |
| Automated-Prompt-Engineering-Hub | A hub for tools and resources focused on automated prompt engineering for generative AI. | [https://github.com/The-Swarm-Corporation/Automated-Prompt-Engineering-Hub](https://github.com/The-Swarm-Corporation/Automated-Prompt-Engineering-Hub) |
| Multi-Agent-Template-App | A simple, reliable, and high-performance template for building multi-agent applications. | [https://github.com/The-Swarm-Corporation/Multi-Agent-Template-App](https://github.com/The-Swarm-Corporation/Multi-Agent-Template-App) |
| Cookbook | Examples and guides for using the Swarms Framework effectively. | [https://github.com/The-Swarm-Corporation/Cookbook](https://github.com/The-Swarm-Corporation/Cookbook) |
| SwarmDB | A production-grade message queue system for agent communication and LLM backend load balancing. | [https://github.com/The-Swarm-Corporation/SwarmDB](https://github.com/The-Swarm-Corporation/SwarmDB) |
| CryptoTaxSwarm | A personal advisory tax swarm for cryptocurrency transactions. | [https://github.com/The-Swarm-Corporation/CryptoTaxSwarm](https://github.com/The-Swarm-Corporation/CryptoTaxSwarm) |
| Multi-Agent-Marketing-Course | A course on automating marketing operations with enterprise-grade multi-agent collaboration. | [https://github.com/The-Swarm-Corporation/Multi-Agent-Marketing-Course](https://github.com/The-Swarm-Corporation/Multi-Agent-Marketing-Course) |
| Swarms-BrandBook | Branding guidelines and assets for Swarms.ai, embodying innovation and collaboration. | [https://github.com/The-Swarm-Corporation/Swarms-BrandBook](https://github.com/The-Swarm-Corporation/Swarms-BrandBook) |
| AgentAPI | A definitive API for managing and interacting with AI agents. | [https://github.com/The-Swarm-Corporation/AgentAPI](https://github.com/The-Swarm-Corporation/AgentAPI) |
| Research-Paper-Writer-Swarm | Automates the creation of high-quality research papers in LaTeX using Swarms agents. | [https://github.com/The-Swarm-Corporation/Research-Paper-Writer-Swarm](https://github.com/The-Swarm-Corporation/Research-Paper-Writer-Swarm) |
| swarms-sdk | A Python client for the Swarms API, providing a simple interface for managing AI swarms. | [https://github.com/The-Swarm-Corporation/swarms-sdk](https://github.com/The-Swarm-Corporation/swarms-sdk) |
| FluidAPI | A framework for interacting with APIs using natural language, simplifying complex requests. | [https://github.com/The-Swarm-Corporation/FluidAPI](https://github.com/The-Swarm-Corporation/FluidAPI) |
| MedicalCoderSwarm | A multi-agent system for comprehensive medical diagnosis and coding using specialized AI agents. | [https://github.com/The-Swarm-Corporation/MedicalCoderSwarm](https://github.com/The-Swarm-Corporation/MedicalCoderSwarm) |
| BackTesterAgent | An AI-powered backtesting framework for automated trading strategy validation and optimization. | [https://github.com/The-Swarm-Corporation/BackTesterAgent](https://github.com/The-Swarm-Corporation/BackTesterAgent) |
| .ai | The first natural language programming language powered by Swarms. | [https://github.com/The-Swarm-Corporation/.ai](https://github.com/The-Swarm-Corporation/.ai) |
| AutoHedge | An autonomous hedge fund leveraging swarm intelligence for market analysis and trade execution. | [https://github.com/The-Swarm-Corporation/AutoHedge](https://github.com/The-Swarm-Corporation/AutoHedge) |
| radiology-swarm | A multi-agent system for advanced radiological analysis, diagnosis, and treatment planning. | [https://github.com/The-Swarm-Corporation/radiology-swarm](https://github.com/The-Swarm-Corporation/radiology-swarm) |
| MedGuard | A Python library ensuring HIPAA compliance for LLM agents in healthcare applications. | [https://github.com/The-Swarm-Corporation/MedGuard](https://github.com/The-Swarm-Corporation/MedGuard) |
| doc-master | A lightweight Python library for automated file reading and content extraction. | [https://github.com/The-Swarm-Corporation/doc-master](https://github.com/The-Swarm-Corporation/doc-master) |
| Open-Aladdin | An open-source risk-management tool for stock and security risk analysis. | [https://github.com/The-Swarm-Corporation/Open-Aladdin](https://github.com/The-Swarm-Corporation/Open-Aladdin) |
| TickrAgent | A scalable Python library for building financial agents for comprehensive stock analysis. | [https://github.com/The-Swarm-Corporation/TickrAgent](https://github.com/The-Swarm-Corporation/TickrAgent) |
| NewsAgent | An enterprise-grade news aggregation agent for fetching, querying, and summarizing news. | [https://github.com/The-Swarm-Corporation/NewsAgent](https://github.com/The-Swarm-Corporation/NewsAgent) |
| Research-Paper-Hive | A platform for discovering and engaging with relevant research papers efficiently. | [https://github.com/The-Swarm-Corporation/Research-Paper-Hive](https://github.com/The-Swarm-Corporation/Research-Paper-Hive) |
| MedInsight-Pro | Revolutionizes medical research summarization for healthcare innovators. | [https://github.com/The-Swarm-Corporation/MedInsight-Pro](https://github.com/The-Swarm-Corporation/MedInsight-Pro) |
| swarms-memory | Pre-built wrappers for RAG systems like ChromaDB, Weaviate, and Pinecone. | [https://github.com/The-Swarm-Corporation/swarms-memory](https://github.com/The-Swarm-Corporation/swarms-memory) |
| CryptoAgent | An enterprise-grade solution for fetching, analyzing, and summarizing cryptocurrency data. | [https://github.com/The-Swarm-Corporation/CryptoAgent](https://github.com/The-Swarm-Corporation/CryptoAgent) |
| AgentParse | A high-performance parsing library for mapping structured data into agent-understandable blocks. | [https://github.com/The-Swarm-Corporation/AgentParse](https://github.com/The-Swarm-Corporation/AgentParse) |
| CodeGuardian | An intelligent agent for automating the generation of production-grade unit tests for Python code. | [https://github.com/The-Swarm-Corporation/CodeGuardian](https://github.com/The-Swarm-Corporation/CodeGuardian) |
| Marketing-Swarm-Template | A framework for creating multi-platform marketing content using Swarms AI agents. | [https://github.com/The-Swarm-Corporation/Marketing-Swarm-Template](https://github.com/The-Swarm-Corporation/Marketing-Swarm-Template) |
| HTX-Swarm | A multi-agent system for real-time market analysis of HTX exchange data. | [https://github.com/The-Swarm-Corporation/HTX-Swarm](https://github.com/The-Swarm-Corporation/HTX-Swarm) |
| MultiModelOptimizer | A hierarchical parameter synchronization approach for joint training of transformer models. | [https://github.com/The-Swarm-Corporation/MultiModelOptimizer](https://github.com/The-Swarm-Corporation/MultiModelOptimizer) |
| MortgageUnderwritingSwarm | A multi-agent pipeline for automating mortgage underwriting processes. | [https://github.com/The-Swarm-Corporation/MortgageUnderwritingSwarm](https://github.com/The-Swarm-Corporation/MortgageUnderwritingSwarm) |
| DermaSwarm | A multi-agent system for dermatologists to diagnose and treat skin conditions collaboratively. | [https://github.com/The-Swarm-Corporation/DermaSwarm](https://github.com/The-Swarm-Corporation/DermaSwarm) |
| IoTAgents | Integrates IoT data with AI agents for seamless parsing and processing of data streams. | [https://github.com/The-Swarm-Corporation/IoTAgents](https://github.com/The-Swarm-Corporation/IoTAgents) |
| eth-agent | An autonomous agent for analyzing on-chain Ethereum data. | [https://github.com/The-Swarm-Corporation/eth-agent](https://github.com/The-Swarm-Corporation/eth-agent) |
| Medical-Swarm-One-Click | A template for building safe, reliable, and production-grade medical multi-agent systems. | [https://github.com/The-Swarm-Corporation/Medical-Swarm-One-Click](https://github.com/The-Swarm-Corporation/Medical-Swarm-One-Click) |
| Swarms-Example-1-Click-Template | A one-click template for building Swarms applications quickly. | [https://github.com/The-Swarm-Corporation/Swarms-Example-1-Click-Template](https://github.com/The-Swarm-Corporation/Swarms-Example-1-Click-Template) |
| Custom-Swarms-Spec-Template | An official specification template for custom swarm development using the Swarms Framework. | [https://github.com/The-Swarm-Corporation/Custom-Swarms-Spec-Template](https://github.com/The-Swarm-Corporation/Custom-Swarms-Spec-Template) |
| Swarms-LlamaIndex-RAG-Template | A template for integrating Llama Index into Swarms applications for RAG capabilities. | [https://github.com/The-Swarm-Corporation/Swarms-LlamaIndex-RAG-Template](https://github.com/The-Swarm-Corporation/Swarms-LlamaIndex-RAG-Template) |
| ForexTreeSwarm | A forex market analysis system using a swarm of AI agents organized in a forest structure. | [https://github.com/The-Swarm-Corporation/ForexTreeSwarm](https://github.com/The-Swarm-Corporation/ForexTreeSwarm) |
| Generalist-Mathematician-Swarm | A swarm of agents for solving complex mathematical problems collaboratively. | [https://github.com/The-Swarm-Corporation/Generalist-Mathematician-Swarm](https://github.com/The-Swarm-Corporation/Generalist-Mathematician-Swarm) |
| Multi-Modal-XRAY-Diagnosis-Medical-Swarm-Template | A template for analyzing X-rays, MRIs, and more using a swarm of agents. | [https://github.com/The-Swarm-Corporation/Multi-Modal-XRAY-Diagnosis-Medical-Swarm-Template](https://github.com/The-Swarm-Corporation/Multi-Modal-XRAY-Diagnosis-Medical-Swarm-Template) |
| AgentRAGProtocol | A protocol for integrating Retrieval-Augmented Generation (RAG) into AI agents. | [https://github.com/The-Swarm-Corporation/AgentRAGProtocol](https://github.com/The-Swarm-Corporation/AgentRAGProtocol) |
| Multi-Agent-RAG-Template | A template for creating collaborative AI agent teams for document processing and analysis. | [https://github.com/The-Swarm-Corporation/Multi-Agent-RAG-Template](https://github.com/The-Swarm-Corporation/Multi-Agent-RAG-Template) |
| REACT-Yaml-Agent | An implementation of a REACT agent using YAML instead of JSON. | [https://github.com/The-Swarm-Corporation/REACT-Yaml-Agent](https://github.com/The-Swarm-Corporation/REACT-Yaml-Agent) |
| SwarmsXGCP | A template for deploying Swarms agents on Google Cloud Run. | [https://github.com/The-Swarm-Corporation/SwarmsXGCP](https://github.com/The-Swarm-Corporation/SwarmsXGCP) |
| Legal-Swarm-Template | A one-click template for building legal-focused Swarms applications. | [https://github.com/The-Swarm-Corporation/Legal-Swarm-Template](https://github.com/The-Swarm-Corporation/Legal-Swarm-Template) |
| swarms_sim | A simulation of a swarm of agents in a professional workplace environment. | [https://github.com/The-Swarm-Corporation/swarms_sim](https://github.com/The-Swarm-Corporation/swarms_sim) |
| medical-problems | A repository for medical problems to create Swarms applications for. | [https://github.com/The-Swarm-Corporation/medical-problems](https://github.com/The-Swarm-Corporation/medical-problems) |
| swarm-ecosystem | An overview of the Swarm Ecosystem and its components. | [https://github.com/The-Swarm-Corporation/swarm-ecosystem](https://github.com/The-Swarm-Corporation/swarm-ecosystem) |
| swarms_ecosystem_md | MDX documentation for the Swarm Ecosystem. | [https://github.com/The-Swarm-Corporation/swarms_ecosystem_md](https://github.com/The-Swarm-Corporation/swarms_ecosystem_md) |

@ -0,0 +1,138 @@
# Agents with Vision and Tool Usage
This tutorial demonstrates how to create intelligent agents that can analyze images and use custom tools to perform specific actions based on their visual observations. You'll learn to build a quality control agent that can process images, identify potential security concerns, and automatically trigger appropriate responses using function calling capabilities.
## What You'll Learn
- How to configure an agent with multi-modal capabilities for image analysis
- How to integrate custom tools and functions with vision-enabled agents
- How to implement automated security analysis based on visual observations
- How to use function calling to trigger specific actions from image analysis results
- Best practices for building production-ready vision agents with tool integration
## Use Cases
This approach is perfect for:
- **Quality Control Systems**: Automated inspection of manufacturing processes
- **Security Monitoring**: Real-time threat detection and response
- **Object Detection**: Identifying and categorizing items in images
- **Compliance Checking**: Ensuring standards are met in various environments
- **Automated Reporting**: Generating detailed analysis reports from visual data
## 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
- Create tools for your agent as a function with types and documentation
- Pass tools to your agent `Agent(tools=[list_of_callables])`
- Add your image path to the run method like: `Agent().run(task=task, img=img)`
```python
from swarms.structs import Agent
from swarms.prompts.logistics import (
Quality_Control_Agent_Prompt,
)
# Image for analysis
factory_image = "image.jpg"
def security_analysis(danger_level: str) -> str:
"""
Analyzes the security danger level and returns an appropriate response.
Args:
danger_level (str, optional): The level of danger to analyze.
Can be "low", "medium", "high", or None. Defaults to None.
Returns:
str: A string describing the danger level assessment.
- "No danger level provided" if danger_level is None
- "No danger" if danger_level is "low"
- "Medium danger" if danger_level is "medium"
- "High danger" if danger_level is "high"
- "Unknown danger level" for any other value
"""
if danger_level is None:
return "No danger level provided"
if danger_level == "low":
return "No danger"
if danger_level == "medium":
return "Medium danger"
if danger_level == "high":
return "High danger"
return "Unknown danger level"
custom_system_prompt = f"""
{Quality_Control_Agent_Prompt}
You have access to tools that can help you with your analysis. When you need to perform a security analysis, you MUST use the security_analysis function with an appropriate danger level (low, medium, or high) based on your observations.
Always use the available tools when they are relevant to the task. If you determine there is any level of danger or security concern, call the security_analysis function with the appropriate danger level.
"""
# Quality control agent
quality_control_agent = Agent(
agent_name="Quality Control Agent",
agent_description="A quality control agent that analyzes images and provides a detailed report on the quality of the product in the image.",
# model_name="anthropic/claude-3-opus-20240229",
model_name="gpt-4o-mini",
system_prompt=custom_system_prompt,
multi_modal=True,
max_loops=1,
output_type="str-all-except-first",
# tools_list_dictionary=[schema],
tools=[security_analysis],
)
response = quality_control_agent.run(
task="Analyze the image and then perform a security analysis. Based on what you see in the image, determine if there is a low, medium, or high danger level and call the security_analysis function with that danger level",
img=factory_image,
)
```
## Support and Community
If you're facing issues or want to learn more, check out the following resources to join our Discord, stay updated on Twitter, and watch tutorials on YouTube!
| Platform | Link | Description |
|----------|------|-------------|
| 📚 Documentation | [docs.swarms.world](https://docs.swarms.world) | Official documentation and guides |
| 📝 Blog | [Medium](https://medium.com/@kyeg) | Latest updates and technical articles |
| 💬 Discord | [Join Discord](https://discord.gg/jM3Z6M9uMq) | Live chat and community support |
| 🐦 Twitter | [@kyegomez](https://twitter.com/kyegomez) | Latest news and announcements |
| 👥 LinkedIn | [The Swarm Corporation](https://www.linkedin.com/company/the-swarm-corporation) | Professional network and updates |
| 📺 YouTube | [Swarms Channel](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ) | Tutorials and demos |
| 🎫 Events | [Sign up here](https://lu.ma/5p2jnc2v) | Join our community events |

@ -0,0 +1,42 @@
## ✨ Enterprise Features
Swarms delivers a comprehensive, enterprise-grade multi-agent infrastructure platform designed for production-scale deployments and seamless integration with existing systems.
| Category | Enterprise Capabilities | Business Value |
|----------|------------------------|----------------|
| 🏢 **Enterprise Architecture** | • Production-Ready Infrastructure<br>• High Availability Systems<br>• Modular Microservices Design<br>• Comprehensive Observability<br>• Backwards Compatibility | • 99.9%+ Uptime Guarantee<br>• Reduced Operational Overhead<br>• Seamless Legacy Integration<br>• Enhanced System Monitoring<br>• Risk-Free Migration Path |
| 🤖 **Multi-Agent Orchestration** | • Hierarchical Agent Swarms<br>• Parallel Processing Pipelines<br>• Sequential Workflow Orchestration<br>• Graph-Based Agent Networks<br>• Dynamic Agent Composition<br>• Agent Registry Management | • Complex Business Process Automation<br>• Scalable Task Distribution<br>• Flexible Workflow Adaptation<br>• Optimized Resource Utilization<br>• Centralized Agent Governance<br>• Enterprise-Grade Agent Lifecycle Management |
| 🔄 **Enterprise Integration** | • Multi-Model Provider Support<br>• Custom Agent Development Framework<br>• Extensive Enterprise Tool Library<br>• Multiple Memory Systems<br>• Backwards Compatibility with LangChain, AutoGen, CrewAI<br>• Standardized API Interfaces | • Vendor-Agnostic Architecture<br>• Custom Solution Development<br>• Extended Functionality Integration<br>• Enhanced Knowledge Management<br>• Seamless Framework Migration<br>• Reduced Integration Complexity |
| 📈 **Enterprise Scalability** | • Concurrent Multi-Agent Processing<br>• Intelligent Resource Management<br>• Load Balancing & Auto-Scaling<br>• Horizontal Scaling Capabilities<br>• Performance Optimization<br>• Capacity Planning Tools | • High-Throughput Processing<br>• Cost-Effective Resource Utilization<br>• Elastic Scaling Based on Demand<br>• Linear Performance Scaling<br>• Optimized Response Times<br>• Predictable Growth Planning |
| 🛠️ **Developer Experience** | • Intuitive Enterprise API<br>• Comprehensive Documentation<br>• Active Enterprise Community<br>• CLI & SDK Tools<br>• IDE Integration Support<br>• Code Generation Templates | • Accelerated Development Cycles<br>• Reduced Learning Curve<br>• Expert Community Support<br>• Rapid Deployment Capabilities<br>• Enhanced Developer Productivity<br>• Standardized Development Patterns |
| 🔐 **Enterprise Security** | • Comprehensive Error Handling<br>• Advanced Rate Limiting<br>• Real-Time Monitoring Integration<br>• Detailed Audit Logging<br>• Role-Based Access Control<br>• Data Encryption & Privacy | • Enhanced System Reliability<br>• API Security Protection<br>• Proactive Issue Detection<br>• Regulatory Compliance Support<br>• Granular Access Management<br>• Enterprise Data Protection |
| 📊 **Advanced Enterprise Features** | • SpreadsheetSwarm for Mass Agent Management<br>• Group Chat for Collaborative AI<br>• Centralized Agent Registry<br>• Mixture of Agents for Complex Solutions<br>• Agent Performance Analytics<br>• Automated Agent Optimization | • Large-Scale Agent Operations<br>• Team-Based AI Collaboration<br>• Centralized Agent Governance<br>• Sophisticated Problem Solving<br>• Performance Insights & Optimization<br>• Continuous Agent Improvement |
| 🔌 **Provider Ecosystem** | • OpenAI Integration<br>• Anthropic Claude Support<br>• ChromaDB Vector Database<br>• Custom Provider Framework<br>• Multi-Cloud Deployment<br>• Hybrid Infrastructure Support | • Provider Flexibility & Independence<br>• Advanced Vector Search Capabilities<br>• Custom Integration Development<br>• Cloud-Agnostic Architecture<br>• Flexible Deployment Options<br>• Risk Mitigation Through Diversification |
| 💪 **Production Readiness** | • Automatic Retry Mechanisms<br>• Asynchronous Processing Support<br>• Environment Configuration Management<br>• Type Safety & Validation<br>• Health Check Endpoints<br>• Graceful Degradation | • Enhanced System Reliability<br>• Improved Performance Characteristics<br>• Simplified Configuration Management<br>• Reduced Runtime Errors<br>• Proactive Health Monitoring<br>• Continuous Service Availability |
| 🎯 **Enterprise Use Cases** | • Industry-Specific Agent Solutions<br>• Custom Workflow Development<br>• Regulatory Compliance Support<br>• Extensible Framework Architecture<br>• Multi-Tenant Support<br>• Enterprise SLA Guarantees | • Rapid Industry Deployment<br>• Flexible Solution Architecture<br>• Compliance-Ready Implementations<br>• Future-Proof Technology Investment<br>• Scalable Multi-Client Operations<br>• Predictable Service Quality |
---
## 🚀 Missing a Feature?
Swarms is continuously evolving to meet enterprise needs. If you don't see a specific feature or capability that your organization requires:
### 📝 **Report Missing Features**
- Create a [GitHub Issue](https://github.com/kyegomez/swarms/issues) to request new features
- Describe your use case and business requirements
- Our team will evaluate and prioritize based on enterprise demand
### 📞 **Schedule a Consultation**
- [Book a call with our enterprise team](https://cal.com/swarms/swarms-onboarding-session) for personalized guidance
- Discuss your specific multi-agent architecture requirements
- Get expert recommendations for your implementation strategy
- Explore custom enterprise solutions and integrations
Our team is committed to ensuring Swarms meets your enterprise multi-agent infrastructure needs. We welcome feedback and collaboration to build the most comprehensive platform for production-scale AI agent deployments.

@ -4,12 +4,20 @@ The InteractiveGroupChat is a sophisticated multi-agent system that enables inte
## Features
- **@mentions Support**: Direct tasks to specific agents using @agent_name syntax
- **Multi-Agent Collaboration**: Multiple mentioned agents can see and respond to each other's tasks
- **Callable Function Support**: Supports both Agent instances and callable functions as chat participants
- **Comprehensive Error Handling**: Custom error classes for different scenarios
- **Conversation History**: Maintains a complete history of the conversation
- **Flexible Output Formatting**: Configurable output format for conversation history
| Feature | Description |
|---------|-------------|
| **@mentions Support** | Direct tasks to specific agents using @agent_name syntax |
| **Multi-Agent Collaboration** | Multiple mentioned agents can see and respond to each other's tasks |
| **Enhanced Collaborative Prompts** | Agents are trained to acknowledge, build upon, and synthesize each other's responses |
| **Speaker Functions** | Control the order in which agents respond (round robin, random, priority, custom) |
| **Dynamic Speaker Management** | Change speaker functions and priorities during runtime |
| **Random Dynamic Speaker** | Advanced speaker function that follows @mentions in agent responses |
| **Parallel and Sequential Strategies** | Support for both parallel and sequential agent execution |
| **Callable Function Support** | Supports both Agent instances and callable functions as chat participants |
| **Comprehensive Error Handling** | Custom error classes for different scenarios |
| **Conversation History** | Maintains a complete history of the conversation |
| **Flexible Output Formatting** | Configurable output format for conversation history |
| **Interactive Terminal Mode** | Full REPL interface for real-time chat with agents |
## Installation
@ -35,6 +43,8 @@ Initializes a new InteractiveGroupChat instance with the specified configuration
| `max_loops` | int | Maximum conversation turns | 1 |
| `output_type` | str | Type of output format | "string" |
| `interactive` | bool | Whether to enable interactive mode | False |
| `speaker_function` | Union[str, Callable] | Function to determine speaking order | round_robin_speaker |
| `speaker_state` | dict | Initial state for speaker function | {"current_index": 0} |
**Example:**
@ -54,7 +64,9 @@ tax_expert = Agent(
model_name="gpt-4"
)
# Initialize group chat
# Initialize group chat with speaker function
from swarms.structs.interactive_groupchat import round_robin_speaker
chat = InteractiveGroupChat(
id="finance-chat-001",
name="Financial Advisory Team",
@ -62,7 +74,8 @@ chat = InteractiveGroupChat(
agents=[financial_advisor, tax_expert],
max_loops=3,
output_type="string",
interactive=True
interactive=True,
speaker_function=round_robin_speaker
)
```
@ -74,6 +87,8 @@ Processes a task and gets responses from mentioned agents. This is the main meth
**Arguments:**
- `task` (str): The input task containing @mentions to agents
- `img` (Optional[str]): Optional image for the task
- `imgs` (Optional[List[str]]): Optional list of images for the task
**Returns:**
@ -88,6 +103,10 @@ print(response)
# Multiple agent collaboration
response = chat.run("@FinancialAdvisor and @TaxExpert, how can I minimize taxes on my investments?")
print(response)
# With image input
response = chat.run("@FinancialAdvisor analyze this chart", img="chart.png")
print(response)
```
### Start Interactive Session (`start_interactive_session`)
@ -98,6 +117,13 @@ Starts an interactive terminal session for real-time chat with agents. This crea
**Arguments:**
None
**Features:**
- Real-time chat with agents using @mentions
- View available agents and their descriptions
- Change speaker functions during the session
- Built-in help system
- Graceful exit with 'exit' or 'quit' commands
**Example:**
```python
@ -111,6 +137,119 @@ chat = InteractiveGroupChat(
chat.start_interactive_session()
```
**Interactive Session Commands:**
- `@agent_name message` - Mention specific agents
- `help` or `?` - Show help information
- `speaker` - Change speaker function
- `exit` or `quit` - End the session
### Set Speaker Function (`set_speaker_function`)
**Description:**
Dynamically changes the speaker function and optional state during runtime.
**Arguments:**
- `speaker_function` (Union[str, Callable]): Function that determines speaking order
- String options: "round-robin-speaker", "random-speaker", "priority-speaker", "random-dynamic-speaker"
- Callable: Custom function that takes (agents: List[str], **kwargs) -> str
- `speaker_state` (dict, optional): State for the speaker function
**Example:**
```python
from swarms.structs.interactive_groupchat import random_speaker, priority_speaker
# Change to random speaker function
chat.set_speaker_function(random_speaker)
# Change to priority speaker with custom priorities
chat.set_speaker_function(priority_speaker, {"financial_advisor": 3, "tax_expert": 2})
# Change to random dynamic speaker
chat.set_speaker_function("random-dynamic-speaker")
```
### Get Available Speaker Functions (`get_available_speaker_functions`)
**Description:**
Returns a list of all available built-in speaker function names.
**Arguments:**
None
**Returns:**
- List[str]: List of available speaker function names
**Example:**
```python
available_functions = chat.get_available_speaker_functions()
print(available_functions)
# Output: ['round-robin-speaker', 'random-speaker', 'priority-speaker', 'random-dynamic-speaker']
```
### Get Current Speaker Function (`get_current_speaker_function`)
**Description:**
Returns the name of the currently active speaker function.
**Arguments:**
None
**Returns:**
- str: Name of the current speaker function, or "custom" if it's a custom function
**Example:**
```python
current_function = chat.get_current_speaker_function()
print(current_function) # Output: "round-robin-speaker"
```
### Set Priorities (`set_priorities`)
**Description:**
Sets agent priorities for priority-based speaking order.
**Arguments:**
- `priorities` (dict): Dictionary mapping agent names to priority weights
**Example:**
```python
# Set agent priorities (higher numbers = higher priority)
chat.set_priorities({
"financial_advisor": 5,
"tax_expert": 3,
"investment_analyst": 1
})
```
### Set Dynamic Strategy (`set_dynamic_strategy`)
**Description:**
Sets the strategy for the random-dynamic-speaker function.
**Arguments:**
- `strategy` (str): Either "sequential" or "parallel"
- "sequential": Process one agent at a time based on @mentions
- "parallel": Process all mentioned agents simultaneously
**Example:**
```python
# Set to sequential strategy (one agent at a time)
chat.set_dynamic_strategy("sequential")
# Set to parallel strategy (all mentioned agents respond simultaneously)
chat.set_dynamic_strategy("parallel")
```
### Extract Mentions (`_extract_mentions`)
**Description:**
@ -178,7 +317,7 @@ chat = InteractiveGroupChat(
**Description:**
Internal method that updates each agent's system prompt with information about other agents and the group chat.
Internal method that updates each agent's system prompt with information about other agents and the group chat. This includes enhanced collaborative instructions that teach agents how to acknowledge, build upon, and synthesize each other's responses.
**Arguments:**
@ -188,7 +327,227 @@ None
```python
# Agent prompts are automatically updated during initialization
chat = InteractiveGroupChat(agents=[financial_advisor, tax_expert])
# Each agent now knows about the other participants in the chat
# Each agent now knows about the other participants and how to collaborate effectively
```
### Get Speaking Order (`_get_speaking_order`)
**Description:**
Internal method that determines the speaking order using the configured speaker function.
**Arguments:**
- `mentioned_agents` (List[str]): List of agent names that were mentioned
**Returns:**
- List[str]: List of agent names in the order they should speak
**Example:**
```python
# Internal usage (not typically called directly)
mentioned = ["financial_advisor", "tax_expert"]
order = chat._get_speaking_order(mentioned)
print(order) # Order determined by speaker function
```
## Speaker Functions
InteractiveGroupChat supports various speaker functions that control the order in which agents respond when multiple agents are mentioned.
### Built-in Speaker Functions
#### Round Robin Speaker (`round_robin_speaker`)
Agents speak in a fixed order, cycling through the list in sequence.
```python
from swarms.structs.interactive_groupchat import InteractiveGroupChat, round_robin_speaker
chat = InteractiveGroupChat(
agents=agents,
speaker_function=round_robin_speaker,
interactive=False,
)
```
**Behavior:**
- Agents speak in the order they were mentioned
- Maintains state between calls to continue the cycle
- Predictable and fair distribution of speaking turns
#### Random Speaker (`random_speaker`)
Agents speak in random order each time.
```python
from swarms.structs.interactive_groupchat import InteractiveGroupChat, random_speaker
chat = InteractiveGroupChat(
agents=agents,
speaker_function=random_speaker,
interactive=False,
)
```
**Behavior:**
- Speaking order is randomized for each task
- Provides variety and prevents bias toward first-mentioned agents
- Good for brainstorming sessions
#### Priority Speaker (`priority_speaker`)
Agents speak based on priority weights assigned to each agent.
```python
from swarms.structs.interactive_groupchat import InteractiveGroupChat, priority_speaker
chat = InteractiveGroupChat(
agents=agents,
speaker_function=priority_speaker,
speaker_state={"priorities": {"financial_advisor": 3, "tax_expert": 2, "analyst": 1}},
interactive=False,
)
```
**Behavior:**
- Higher priority agents speak first
- Uses weighted probability for selection
- Good for hierarchical teams or expert-led discussions
#### Random Dynamic Speaker (`random_dynamic_speaker`)
Advanced speaker function that follows @mentions in agent responses, enabling dynamic conversation flow.
```python
from swarms.structs.interactive_groupchat import InteractiveGroupChat, random_dynamic_speaker
chat = InteractiveGroupChat(
agents=agents,
speaker_function=random_dynamic_speaker,
speaker_state={"strategy": "parallel"}, # or "sequential"
interactive=False,
)
```
**Behavior:**
- **First Call**: Randomly selects an agent to start the conversation
- **Subsequent Calls**: Extracts @mentions from the previous agent's response and selects the next speaker(s)
- **Two Strategies**:
- **Sequential**: Processes one agent at a time based on @mentions
- **Parallel**: Processes all mentioned agents simultaneously
**Example Dynamic Flow:**
```python
# Agent A responds: "I think @AgentB should analyze this data and @AgentC should review the methodology"
# With sequential strategy: Agent B speaks next
# With parallel strategy: Both Agent B and Agent C speak simultaneously
```
**Use Cases:**
- Complex problem-solving where agents need to delegate to specific experts
- Dynamic workflows where the conversation flow depends on agent responses
- Collaborative decision-making processes
### Custom Speaker Functions
You can create your own speaker functions to implement custom logic:
```python
def custom_speaker(agents: List[str], **kwargs) -> str:
"""
Custom speaker function that selects agents based on specific criteria.
Args:
agents: List of agent names
**kwargs: Additional arguments (context, time, etc.)
Returns:
Selected agent name
"""
# Your custom logic here
if "urgent" in kwargs.get("context", ""):
return "emergency_agent" if "emergency_agent" in agents else agents[0]
# Default to first agent
return agents[0]
# Use custom speaker function
chat = InteractiveGroupChat(
agents=agents,
speaker_function=custom_speaker,
interactive=False,
)
```
### Dynamic Speaker Function Changes
You can change the speaker function during runtime:
```python
# Start with round robin
chat = InteractiveGroupChat(
agents=agents,
speaker_function=round_robin_speaker,
interactive=False,
)
# Change to random
chat.set_speaker_function(random_speaker)
# Change to priority with custom priorities
chat.set_priorities({"financial_advisor": 5, "tax_expert": 3, "analyst": 1})
chat.set_speaker_function(priority_speaker)
# Change to dynamic speaker with parallel strategy
chat.set_speaker_function("random-dynamic-speaker")
chat.set_dynamic_strategy("parallel")
```
## Enhanced Collaborative Behavior
The InteractiveGroupChat now includes enhanced collaborative prompts that ensure agents work together effectively.
### Collaborative Response Protocol
Every agent receives instructions to:
1. **Read and understand** all previous responses from other agents
2. **Acknowledge** what other agents have said
3. **Build upon** previous insights rather than repeating information
4. **Synthesize** multiple perspectives when possible
5. **Delegate** appropriately using @mentions
### Response Structure
Agents are guided to structure their responses as:
1. **ACKNOWLEDGE**: "I've reviewed the responses from @agent1 and @agent2..."
2. **BUILD**: "Building on @agent1's analysis of the data..."
3. **CONTRIBUTE**: "From my perspective, I would add..."
4. **COLLABORATE**: "To get a complete picture, let me ask @agent3 to..."
5. **SYNTHESIZE**: "Combining our insights, the key findings are..."
### Example Collaborative Response
```python
task = "Analyze our Q3 performance. @analyst @researcher @strategist"
# Expected collaborative behavior:
# Analyst: "Based on the data analysis, I can see clear growth trends in Q3..."
# Researcher: "Building on @analyst's data insights, I can add that market research shows..."
# Strategist: "Synthesizing @analyst's data and @researcher's market insights, I recommend..."
```
## Error Classes
@ -237,7 +596,7 @@ except NoMentionedAgentsError as e:
print(f"No agents mentioned: {e}")
```
### InvalidtaskFormatError
### InvalidTaskFormatError
**Description:**
@ -247,19 +606,342 @@ Raised when the task format is invalid.
```python
try:
chat.run("@Invalid@Format")
except InvalidtaskFormatError as e:
except InvalidTaskFormatError as e:
print(f"Invalid task format: {e}")
```
### InvalidSpeakerFunctionError
**Description:**
Raised when an invalid speaker function is provided.
**Example:**
```python
def invalid_speaker(agents, **kwargs):
return 123 # Should return string, not int
try:
chat = InteractiveGroupChat(
agents=agents,
speaker_function=invalid_speaker,
)
except InvalidSpeakerFunctionError as e:
print(f"Invalid speaker function: {e}")
```
## Best Practices
| Best Practice | Description | Example |
|--------------|-------------|---------|
| Agent Naming | Use clear, unique names for agents to avoid confusion | `financial_advisor`, `tax_expert` |
| task Format | Always use @mentions to direct tasks to specific agents | `@financial_advisor What's your investment advice?` |
| Task Format | Always use @mentions to direct tasks to specific agents | `@financial_advisor What's your investment advice?` |
| Speaker Functions | Choose appropriate speaker functions for your use case | Round robin for fairness, priority for expert-led discussions |
| Dynamic Speaker | Use random-dynamic-speaker for complex workflows with delegation | When agents need to call on specific experts |
| Strategy Selection | Choose sequential for focused discussions, parallel for brainstorming | Sequential for analysis, parallel for idea generation |
| Collaborative Design | Design agents with complementary expertise for better collaboration | Analyst + Researcher + Strategist |
| Error Handling | Implement proper error handling for various scenarios | `try/except` blocks for `AgentNotFoundError` |
| Context Management | Be aware that agents can see the full conversation history | Monitor conversation length and relevance |
| Resource Management | Consider the number of agents and task length to optimize performance | Limit max_loops and task size |
| Dynamic Adaptation | Change speaker functions based on different phases of work | Round robin for brainstorming, priority for decision-making |
## Usage Examples
### Basic Multi-Agent Collaboration
```python
from swarms import Agent
from swarms.structs.interactive_groupchat import InteractiveGroupChat, round_robin_speaker
# Create specialized agents
analyst = Agent(
agent_name="analyst",
system_prompt="You are a data analyst specializing in business intelligence.",
llm="gpt-3.5-turbo",
)
researcher = Agent(
agent_name="researcher",
system_prompt="You are a market researcher with expertise in consumer behavior.",
llm="gpt-3.5-turbo",
)
strategist = Agent(
agent_name="strategist",
system_prompt="You are a strategic consultant who synthesizes insights into actionable recommendations.",
llm="gpt-3.5-turbo",
)
# Create collaborative group chat
chat = InteractiveGroupChat(
name="Business Analysis Team",
description="A collaborative team for comprehensive business analysis",
agents=[analyst, researcher, strategist],
speaker_function=round_robin_speaker,
interactive=False,
)
# Collaborative analysis task
task = """Analyze our company's Q3 performance. We have the following data:
- Revenue: $2.5M (up 15% from Q2)
- Customer acquisition cost: $45 (down 8% from Q2)
- Market share: 3.2% (up 0.5% from Q2)
@analyst @researcher @strategist please provide a comprehensive analysis."""
response = chat.run(task)
print(response)
```
### Priority-Based Expert Consultation
```python
from swarms.structs.interactive_groupchat import InteractiveGroupChat, priority_speaker
# Create expert agents with different priority levels
senior_expert = Agent(
agent_name="senior_expert",
system_prompt="You are a senior consultant with 15+ years of experience.",
llm="gpt-4",
)
junior_expert = Agent(
agent_name="junior_expert",
system_prompt="You are a junior consultant with 3 years of experience.",
llm="gpt-3.5-turbo",
)
assistant = Agent(
agent_name="assistant",
system_prompt="You are a research assistant who gathers supporting information.",
llm="gpt-3.5-turbo",
)
# Create priority-based group chat
chat = InteractiveGroupChat(
name="Expert Consultation Team",
description="Expert-led consultation with collaborative input",
agents=[senior_expert, junior_expert, assistant],
speaker_function=priority_speaker,
speaker_state={"priorities": {"senior_expert": 5, "junior_expert": 3, "assistant": 1}},
interactive=False,
)
# Expert consultation task
task = """We need strategic advice on entering a new market.
@senior_expert @junior_expert @assistant please provide your insights."""
response = chat.run(task)
print(response)
```
### Dynamic Speaker Function with Delegation
```python
from swarms.structs.interactive_groupchat import InteractiveGroupChat, random_dynamic_speaker
# Create specialized medical agents
cardiologist = Agent(
agent_name="cardiologist",
system_prompt="You are a cardiologist specializing in heart conditions.",
llm="gpt-4",
)
oncologist = Agent(
agent_name="oncologist",
system_prompt="You are an oncologist specializing in cancer treatment.",
llm="gpt-4",
)
endocrinologist = Agent(
agent_name="endocrinologist",
system_prompt="You are an endocrinologist specializing in hormone disorders.",
llm="gpt-4",
)
# Create dynamic group chat
chat = InteractiveGroupChat(
name="Medical Panel Discussion",
description="A collaborative panel of medical specialists",
agents=[cardiologist, oncologist, endocrinologist],
speaker_function=random_dynamic_speaker,
speaker_state={"strategy": "sequential"},
interactive=False,
)
# Complex medical case with dynamic delegation
case = """CASE PRESENTATION:
A 65-year-old male with Type 2 diabetes, hypertension, and recent diagnosis of
stage 3 colon cancer presents with chest pain and shortness of breath.
ECG shows ST-segment elevation. Recent blood work shows elevated blood glucose (280 mg/dL)
and signs of infection (WBC 15,000, CRP elevated).
@cardiologist @oncologist @endocrinologist please provide your assessment and treatment recommendations."""
response = chat.run(case)
print(response)
```
### Dynamic Speaker Function Changes
```python
from swarms.structs.interactive_groupchat import (
InteractiveGroupChat,
round_robin_speaker,
random_speaker,
priority_speaker,
random_dynamic_speaker
)
# Create brainstorming agents
creative_agent = Agent(agent_name="creative", system_prompt="You are a creative thinker.")
analytical_agent = Agent(agent_name="analytical", system_prompt="You are an analytical thinker.")
practical_agent = Agent(agent_name="practical", system_prompt="You are a practical implementer.")
chat = InteractiveGroupChat(
name="Dynamic Team",
agents=[creative_agent, analytical_agent, practical_agent],
speaker_function=round_robin_speaker,
interactive=False,
)
# Phase 1: Brainstorming (random order)
chat.set_speaker_function(random_speaker)
task1 = "Let's brainstorm new product ideas. @creative @analytical @practical"
response1 = chat.run(task1)
# Phase 2: Analysis (priority order)
chat.set_priorities({"analytical": 3, "creative": 2, "practical": 1})
chat.set_speaker_function(priority_speaker)
task2 = "Now let's analyze the feasibility of these ideas. @creative @analytical @practical"
response2 = chat.run(task2)
# Phase 3: Dynamic delegation (agents mention each other)
chat.set_speaker_function(random_dynamic_speaker)
chat.set_dynamic_strategy("sequential")
task3 = "Let's plan implementation with dynamic delegation. @creative @analytical @practical"
response3 = chat.run(task3)
# Phase 4: Final synthesis (round robin for equal input)
chat.set_speaker_function(round_robin_speaker)
task4 = "Finally, let's synthesize our findings. @creative @analytical @practical"
response4 = chat.run(task4)
```
### Custom Speaker Function
```python
def context_aware_speaker(agents: List[str], **kwargs) -> str:
"""Custom speaker function that selects agents based on context."""
context = kwargs.get("context", "").lower()
if "data" in context or "analysis" in context:
return "analyst" if "analyst" in agents else agents[0]
elif "market" in context or "research" in context:
return "researcher" if "researcher" in agents else agents[0]
elif "strategy" in context or "planning" in context:
return "strategist" if "strategist" in agents else agents[0]
else:
return agents[0]
# Use custom speaker function
chat = InteractiveGroupChat(
name="Context-Aware Team",
agents=[analyst, researcher, strategist],
speaker_function=context_aware_speaker,
interactive=False,
)
# The speaker function will automatically select the most appropriate agent
task = "We need to analyze our market position and develop a strategy."
response = chat.run(task)
```
### Interactive Session with Enhanced Collaboration
```python
# Create agents designed for collaboration
data_scientist = Agent(
agent_name="data_scientist",
system_prompt="You are a data scientist. When collaborating, always reference specific data points and build upon others' insights with quantitative support.",
llm="gpt-4",
)
business_analyst = Agent(
agent_name="business_analyst",
system_prompt="You are a business analyst. When collaborating, always connect business insights to practical implications and build upon data analysis with business context.",
llm="gpt-3.5-turbo",
)
product_manager = Agent(
agent_name="product_manager",
system_prompt="You are a product manager. When collaborating, always synthesize insights from all team members and provide actionable product recommendations.",
llm="gpt-3.5-turbo",
)
# Start interactive session
chat = InteractiveGroupChat(
name="Product Development Team",
description="A collaborative team for product development decisions",
agents=[data_scientist, business_analyst, product_manager],
speaker_function=round_robin_speaker,
interactive=True,
)
# Start the interactive session
chat.start_interactive_session()
```
## Benefits and Use Cases
### Benefits of Enhanced Collaboration
1. **Reduced Redundancy**: Agents don't repeat what others have already said
2. **Improved Synthesis**: Multiple perspectives are integrated into coherent conclusions
3. **Better Delegation**: Agents naturally delegate to appropriate experts
4. **Enhanced Problem Solving**: Complex problems are addressed systematically
5. **More Natural Interactions**: Agents respond like real team members
6. **Dynamic Workflows**: Conversation flow adapts based on agent responses
7. **Flexible Execution**: Support for both sequential and parallel processing
### Use Cases
| Use Case Category | Specific Use Case | Agent Team Composition | Recommended Speaker Function |
|------------------|-------------------|----------------------|------------------------------|
| **Business Analysis and Strategy** | Data Analysis | Analyst + Researcher + Strategist | Round Robin |
| | Market Research | Multiple experts analyzing different aspects | Random Dynamic |
| | Strategic Planning | Expert-led discussions with collaborative input | Priority |
| **Product Development** | Requirements Gathering | Product Manager + Developer + Designer | Round Robin |
| | Technical Architecture | Senior + Junior developers with different expertise | Priority |
| | User Experience | UX Designer + Product Manager + Developer | Random Dynamic |
| **Research and Development** | Scientific Research | Multiple researchers with different specializations | Random Dynamic |
| | Literature Review | Different experts reviewing various aspects | Round Robin |
| | Experimental Design | Statistician + Domain Expert + Methodologist | Priority |
| **Creative Projects** | Content Creation | Writer + Editor + Designer | Random |
| | Marketing Campaigns | Creative + Analyst + Strategist | Random Dynamic |
| | Design Projects | Designer + Developer + Product Manager | Round Robin |
| **Problem Solving** | Troubleshooting | Technical + Business + User perspective experts | Priority |
| | Crisis Management | Emergency + Communication + Technical teams | Priority |
| | Decision Making | Executive + Analyst + Specialist | Priority |
| **Medical Consultation** | Complex Cases | Multiple specialists (Cardiologist + Oncologist + Endocrinologist) | Random Dynamic |
| | Treatment Planning | Senior + Junior doctors with different expertise | Priority |
| | Research Review | Multiple researchers reviewing different aspects | Round Robin |
### Speaker Function Selection Guide
| Use Case | Recommended Speaker Function | Strategy | Reasoning |
|----------|------------------------------|----------|-----------|
| Team Meetings | Round Robin | N/A | Ensures equal participation |
| Brainstorming | Random | N/A | Prevents bias and encourages creativity |
| Expert Consultation | Priority | N/A | Senior experts speak first |
| Problem Solving | Priority | N/A | Most relevant experts prioritize |
| Creative Sessions | Random | N/A | Encourages diverse perspectives |
| Decision Making | Priority | N/A | Decision makers speak first |
| Research Review | Round Robin | N/A | Equal contribution from all reviewers |
| Complex Workflows | Random Dynamic | Sequential | Follows natural conversation flow |
| Parallel Analysis | Random Dynamic | Parallel | Multiple agents work simultaneously |
| Medical Panels | Random Dynamic | Sequential | Specialists delegate to relevant experts |
| Technical Architecture | Random Dynamic | Sequential | Senior architects guide the discussion |
## Contributing

File diff suppressed because it is too large Load Diff

@ -0,0 +1,59 @@
# Swarms API Rate Limits
The Swarms API implements rate limiting to ensure fair usage and system stability. Here are the current limits:
## Standard Rate Limits
- **General API Requests**: 100 requests per minute
- **Batch Operations**: Maximum 10 requests per batch for agent/swarm batch operations
## Rate Limit Response
When you exceed the rate limit, the API will return a 429 (Too Many Requests) status code with the following message:
```json
{
"detail": "Rate limit exceeded. Please try again later."
}
```
## Batch Operation Limits
For batch operations (`/v1/agent/batch/completions` and `/v1/swarm/batch/completions`):
- Maximum 10 concurrent requests per batch
- Exceeding this limit will result in a 400 (Bad Request) error
## Increasing Your Rate Limits
Need higher rate limits for your application? You can increase your limits by subscribing to a higher tier plan at [swarms.world/pricing](https://swarms.world/pricing).
Higher tier plans offer:
- Increased rate limits
- Higher batch operation limits
- Priority processing
- Dedicated support
## Best Practices
To make the most of your rate limits:
1. Implement proper error handling for rate limit responses
2. Use batch operations when processing multiple requests
3. Add appropriate retry logic with exponential backoff
4. Monitor your API usage to stay within limits
## Rate Limit Headers
The API does not currently expose rate limit headers. We recommend implementing your own request tracking to stay within the limits.
---
For questions about rate limits or to request a custom plan for higher limits, please contact our support team or visit [swarms.world/pricing](https://swarms.world/pricing).

@ -1,54 +1,30 @@
### Available Swarms in The Swarms API
# Multi-Agent Architectures
| Swarm Type | Description (English) | Description (Chinese) |
|----------------------|-----------------------------------------------------------------------------|----------------------------------------------------------------------------|
| AgentRearrange | A swarm type focused on rearranging agents for optimal performance. | 一种专注于重新排列代理以实现最佳性能的群类型。 |
| MixtureOfAgents | Combines different types of agents to achieve a specific goal. | 结合不同类型的代理以实现特定目标。 |
| SpreadSheetSwarm | Utilizes spreadsheet-like structures for data management and operations. | 利用类似电子表格的结构进行数据管理和操作。 |
| SequentialWorkflow | Executes tasks in a sequential manner. | 以顺序方式执行任务。 |
| ConcurrentWorkflow | Allows tasks to be executed concurrently for efficiency. | 允许任务并发执行以提高效率。 |
| GroupChat | Facilitates communication among agents in a group chat format. | 以群聊格式促进代理之间的沟通。 |
| MultiAgentRouter | Routes tasks and information among multiple agents. | 在多个代理之间路由任务和信息。 |
| AutoSwarmBuilder | Automatically builds and configures swarms based on predefined criteria. | 根据预定义标准自动构建和配置群。 |
| HiearchicalSwarm | Organizes agents in a hierarchical structure for task delegation. | 以层次结构组织代理以进行任务委派。 |
| auto | Automatically selects the best swarm type based on the context. | 根据上下文自动选择最佳群类型。 |
| MajorityVoting | Uses majority voting among agents to make decisions. | 使用代理之间的多数投票来做出决策。 |
| MALT | A specialized swarm type for specific tasks (details needed). | 一种专门为特定任务设计的群类型(需要详细信息)。 |
Each multi-agent architecture type is designed for specific use cases and can be combined to create powerful multi-agent systems. Here's a comprehensive overview of each available swarm:
### Documentation for Swarms
| Swarm Type | Description | Learn More |
|---------------------|------------------------------------------------------------------------------|------------|
| AgentRearrange | Dynamically reorganizes agents to optimize task performance and efficiency. Optimizes agent performance by dynamically adjusting their roles and positions within the workflow. This architecture is particularly useful when the effectiveness of agents depends on their sequence or arrangement. | [Learn More](/swarms/structs/agent_rearrange) |
| MixtureOfAgents | Creates diverse teams of specialized agents, each bringing unique capabilities to solve complex problems. Each agent contributes unique skills to achieve the overall goal, making it excel at tasks requiring multiple types of expertise or processing. | [Learn More](/swarms/structs/moa) |
| SpreadSheetSwarm | Provides a structured approach to data management and operations, making it ideal for tasks involving data analysis, transformation, and systematic processing in a spreadsheet-like structure. | [Learn More](/swarms/structs/spreadsheet_swarm) |
| SequentialWorkflow | Ensures strict process control by executing tasks in a predefined order. Perfect for workflows where each step depends on the completion of previous steps. | [Learn More](/swarms/structs/sequential_workflow) |
| ConcurrentWorkflow | Maximizes efficiency by running independent tasks in parallel, significantly reducing overall processing time for complex operations. Ideal for independent tasks that can be processed simultaneously. | [Learn More](/swarms/structs/concurrentworkflow) |
| GroupChat | Enables dynamic collaboration between agents through a chat-based interface, facilitating real-time information sharing and decision-making. | [Learn More](/swarms/structs/group_chat) |
| MultiAgentRouter | Acts as an intelligent task dispatcher, ensuring optimal distribution of work across available agents based on their capabilities and current workload. | [Learn More](/swarms/structs/multi_agent_router) |
| AutoSwarmBuilder | Simplifies swarm creation by automatically configuring agent architectures based on task requirements and performance metrics. | [Learn More](/swarms/structs/auto_swarm_builder) |
| HiearchicalSwarm | Implements a structured approach to task management, with clear lines of authority and delegation across multiple agent levels. | [Learn More](/swarms/structs/multi_swarm_orchestration) |
| auto | Provides intelligent swarm selection based on context, automatically choosing the most effective architecture for given tasks. | [Learn More](/swarms/concept/how_to_choose_swarms) |
| MajorityVoting | Implements robust decision-making through consensus, particularly useful for tasks requiring collective intelligence or verification. | [Learn More](/swarms/structs/majorityvoting) |
| MALT | Specialized framework for language-based tasks, optimizing agent collaboration for complex language processing operations. | [Learn More](/swarms/structs/malt) |
1. **AgentRearrange**: This swarm type is designed to rearrange agents to optimize their performance in a given task. It is useful in scenarios where agent positioning or order affects the outcome.
- 这种群类型旨在重新排列代理以优化其在给定任务中的性能。它在代理位置或顺序影响结果的情况下非常有用。
# Learn More
2. **MixtureOfAgents**: This type combines various agents, each with unique capabilities, to work together towards a common goal. It leverages the strengths of different agents to enhance overall performance.
- 这种类型结合了各种代理,每个代理都有独特的能力,共同努力实现共同目标。它利用不同代理的优势来提高整体性能。
To learn more about Swarms architecture and how different swarm types work together, visit our comprehensive guides:
3. **SpreadSheetSwarm**: This swarm type uses spreadsheet-like structures to manage and operate on data. It is ideal for tasks that require organized data manipulation and analysis.
- 这种群类型使用类似电子表格的结构来管理和操作数据。它非常适合需要有组织的数据操作和分析的任务。
- [Introduction to Multi-Agent Architectures](/swarms/concept/swarm_architectures)
4. **SequentialWorkflow**: Tasks are executed one after another in this swarm type, ensuring that each step is completed before the next begins. It is suitable for processes that require strict order.
- 在这种群类型中,任务一个接一个地执行,确保每个步骤在下一个步骤开始之前完成。它适用于需要严格顺序的流程。
- [How to Choose the Right Multi-Agent Architecture](/swarms/concept/how_to_choose_swarms)
5. **ConcurrentWorkflow**: This type allows multiple tasks to be executed simultaneously, improving efficiency and reducing time for completion. It is best for independent tasks that do not rely on each other.
- 这种类型允许多个任务同时执行,提高效率并减少完成时间。它最适合不相互依赖的独立任务。
- [Framework Architecture Overview](/swarms/concept/framework_architecture)
6. **GroupChat**: Facilitates communication among agents in a group chat format, enabling real-time collaboration and decision-making.
- 以群聊格式促进代理之间的沟通,实现实时协作和决策。
7. **MultiAgentRouter**: This swarm type routes tasks and information among multiple agents, ensuring that each agent receives the necessary data to perform its function.
- 这种群类型在多个代理之间路由任务和信息,确保每个代理接收到执行其功能所需的数据。
8. **AutoSwarmBuilder**: Automatically builds and configures swarms based on predefined criteria, reducing the need for manual setup and configuration.
- 根据预定义标准自动构建和配置群,减少手动设置和配置的需要。
9. **HiearchicalSwarm**: Organizes agents in a hierarchical structure, allowing for efficient task delegation and management.
- 以层次结构组织代理,允许高效的任务委派和管理。
10. **auto**: Automatically selects the most appropriate swarm type based on the context and requirements of the task.
- 根据任务的上下文和要求自动选择最合适的群类型。
11. **MajorityVoting**: Uses a majority voting mechanism among agents to make decisions, ensuring that the most popular choice is selected.
- 使用代理之间的多数投票机制来做出决策,确保选择最受欢迎的选项。
12. **MALT**: A specialized swarm type designed for specific tasks. Further details are needed to fully document this type.
- 一种专门为特定任务设计的群类型。需要进一步的详细信息来完整记录这种类型。
- [Building Custom Swarms](/swarms/structs/custom_swarm)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,126 @@
# Swarms.World Monetization Guide
## Quick Overview
Swarms Marketplace has activated its payment infrastructure, enabling creators to monetize AI agents, prompts, and tools directly through the platform. Sellers receive payments minus a 5-15% platform fee, scaled based on subscription tiers. Revenue accrues in real-time to integrated crypto wallets, with optional fiat conversions.
---
## Eligibility Requirements
### Current Requirements for Paid Content
- **2+ published items** (Prompts, Agents, and Tools)
- **2 Items with 4+ star ratings** (you need community ratings)
- **Marketplace Agent Rating** An agent will automatically rate your prompt, agent, or tool.
**Bottom Line**: You must build reputation with free, high-quality content first.
---
## Step-by-Step Process
### Phase 1: Build Reputation (Required First)
#### 1. Improve Your Existing Content
- Add better descriptions and examples to your published items
- Use the Rating System: Evaluate and rate prompts, agents, and tools based on their effectiveness. Commenting System: Share feedback and insights with the Swarms community
- Ask users for honest reviews and ratings
#### 2. Create More Quality Content
Focus on these categories:
- **Agents**: Marketing, finance, or programming automation
- **Prompts**: Templates for specific business tasks
- **Tools**: Utilities that solve real problems
Target: 3-5 additional items, all aiming for 4+ star ratings
#### 3. Get Community Ratings
- Share your content in relevant communities
- Engage with users who try your content
- Respond to feedback and improve based on comments
- Be patient - ratings take time to accumulate
### Phase 2: Start Monetizing
#### 4. Choose Your Pricing Model
Three primary monetization avenues exist: AI agents (autonomous task-execution models), prompts (pre-optimized input templates), and tools (development utilities like data preprocessors)
**Pricing Options:**
- **One-time**: $0.01 - $999,999 USD
- **Subscription**: Monthly/annual recurring fees (Coming Soon)
- **Usage-based**: Pay per API call or computation (Coming Soon)
#### 6. Optimize & Scale
- Monitor your revenue and user feedback
- Developers can bundle assets—such as pairing prompt libraries with compatible agents—creating value-added
packages
- Create bundles of related content for higher value
- Adjust pricing based on demand
---
## Revenue Models
### What Sells Best
1. **Business Automation Agents** - Marketing, sales, finance
2. **Industry-Specific Prompts** - Legal, medical, technical writing
3. **Integration Tools** - APIs, data processors, connectors
### Pricing Examples
- Simple prompts: $1-50
- Complex agents: $20-500+
- Enterprise tools: $100-1000+
---
## Quick Tips for Success
1. **Quality over quantity** - Better to have 3 excellent items than 10 mediocre ones
2. **Solve real problems** - Focus on actual business needs
3. **Document everything** - Clear instructions increase ratings
4. **Engage actively** - Respond to all user feedback
5. **Be patient** - Building reputation takes time but pays off
---
## Common Mistakes to Avoid
- Publishing low-quality content to meet quantity requirements
- Not responding to user feedback
- Setting prices too high before building reputation
- Copying existing solutions without adding value
- Ignoring community guidelines

@ -35,10 +35,10 @@ agent = Agent(
You communicate in precise, technical terms while maintaining clarity for stakeholders.""",
max_loops=1,
model_name="gpt-4o-mini",
model_name="claude-3-sonnet-20240229",
dynamic_temperature_enabled=True,
output_type="all",
safety_prompt_on=True,
# dashboard=True
)
out = agent.run("What are the best top 3 etfs for gold coverage?")

@ -0,0 +1,46 @@
# Realtor Agent Example
This example demonstrates how to create an AI-powered rental property specialist using the Swarms framework and the Realtor API.
## Quick Start
1. Install dependencies:
```bash
pip install swarms
```
2. Get your Realtor API key:
- Visit [Realtor Search API](https://rapidapi.com/ntd119/api/realtor-search/)
- Sign up for RapidAPI
- Subscribe to the API
- Copy your API key
3. Update the API key in `realtor_agent.py`:
```python
headers = {
"x-rapidapi-key": "YOUR_API_KEY_HERE",
"x-rapidapi-host": "realtor-search.p.rapidapi.com",
}
```
4. Run the example:
```python
from realtor_agent import agent
# Search single location
response = agent.run(
"What are the best properties in Menlo Park for rent under $3,000?"
f"Data: {get_realtor_data_from_one_source('Menlo Park, CA')}"
)
print(response)
```
## Features
- Property search across multiple locations
- Detailed property analysis
- Location assessment
- Financial analysis
- Tenant matching recommendations
For full documentation, see [docs/examples/realtor_agent.md](../docs/examples/realtor_agent.md).

@ -11,11 +11,13 @@ agent = Agent(
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
max_loops=1,
mcp_url="http://0.0.0.0:8000/sse",
model_name="gpt-4o-mini",
output_type="all",
)
# Create a markdown file with initial content
out = agent.run(
"Use any of the tools available to you",
"Use the get_okx_crypto_volume to get the volume of BTC just put the name of the coin",
)
print(out)

@ -0,0 +1,49 @@
from swarms import Agent
# Initialize the agent
agent = Agent(
agent_name="Quantitative-Trading-Agent",
agent_description="Advanced quantitative trading and algorithmic analysis agent",
system_prompt="""
You are an expert quantitative trading agent with deep expertise in:
- Algorithmic trading strategies and implementation
- Statistical arbitrage and market making
- Risk management and portfolio optimization
- High-frequency trading systems
- Market microstructure analysis
- Quantitative research methodologies
- Financial mathematics and stochastic processes
- Machine learning applications in trading
Your core responsibilities include:
1. Developing and backtesting trading strategies
2. Analyzing market data and identifying alpha opportunities
3. Implementing risk management frameworks
4. Optimizing portfolio allocations
5. Conducting quantitative research
6. Monitoring market microstructure
7. Evaluating trading system performance
You maintain strict adherence to:
- Mathematical rigor in all analyses
- Statistical significance in strategy development
- Risk-adjusted return optimization
- Market impact minimization
- Regulatory compliance
- Transaction cost analysis
- Performance attribution
You communicate in precise, technical terms while maintaining clarity for stakeholders.""",
max_loops=1,
model_name="gpt-4o-mini",
dynamic_temperature_enabled=True,
output_type="all",
mcp_urls=[
"http://0.0.0.0:8000/sse",
"http://0.0.0.0:8001/sse",
],
)
agent.run(
"Please use the get_okx_crypto_volume tool to get the trading volume for Bitcoin (BTC). Provide the volume information."
)

@ -0,0 +1,12 @@
from swarms.tools.mcp_client_call import (
get_mcp_tools_sync,
execute_tool_call_simple,
)
tools = get_mcp_tools_sync()
print(tools)
result = execute_tool_call_simple(tools[0], "Hello, world!")
print(result)

@ -0,0 +1,234 @@
"""
Example demonstrating how to execute multiple tools across multiple MCP servers.
This example shows how to:
1. Create a mapping of function names to servers
2. Execute multiple tool calls across different servers
3. Handle responses with tool calls and route them to the appropriate servers
"""
import asyncio
from swarms.tools.mcp_client_call import (
execute_multiple_tools_on_multiple_mcp_servers,
execute_multiple_tools_on_multiple_mcp_servers_sync,
get_tools_for_multiple_mcp_servers,
)
from swarms.schemas.mcp_schemas import MCPConnection
def example_sync_execution():
"""Example of synchronous execution across multiple MCP servers."""
# Example server URLs (replace with your actual MCP server URLs)
urls = [
"http://localhost:8000/sse", # Server 1
"http://localhost:8001/sse", # Server 2
"http://localhost:8002/sse", # Server 3
]
# Optional: Create connection objects for each server
connections = [
MCPConnection(
url="http://localhost:8000/sse",
authorization_token="token1", # if needed
timeout=10,
),
MCPConnection(
url="http://localhost:8001/sse",
authorization_token="token2", # if needed
timeout=10,
),
MCPConnection(
url="http://localhost:8002/sse",
authorization_token="token3", # if needed
timeout=10,
),
]
# Example responses containing tool calls
# These would typically come from an LLM that decided to use tools
responses = [
{
"function": {
"name": "search_web",
"arguments": {
"query": "python programming best practices"
},
}
},
{
"function": {
"name": "search_database",
"arguments": {"table": "users", "id": 123},
}
},
{
"function": {
"name": "send_email",
"arguments": {
"to": "user@example.com",
"subject": "Test email",
"body": "This is a test email",
},
}
},
]
print("=== Synchronous Execution Example ===")
print(
f"Executing {len(responses)} tool calls across {len(urls)} servers..."
)
try:
# Execute all tool calls across multiple servers
results = execute_multiple_tools_on_multiple_mcp_servers_sync(
responses=responses,
urls=urls,
connections=connections,
output_type="dict",
max_concurrent=5, # Limit concurrent executions
)
print(f"\nExecution completed! Got {len(results)} results:")
for i, result in enumerate(results):
print(f"\nResult {i + 1}:")
print(f" Function: {result['function_name']}")
print(f" Server: {result['server_url']}")
print(f" Status: {result['status']}")
if result["status"] == "success":
print(f" Result: {result['result']}")
else:
print(
f" Error: {result.get('error', 'Unknown error')}"
)
except Exception as e:
print(f"Error during execution: {str(e)}")
async def example_async_execution():
"""Example of asynchronous execution across multiple MCP servers."""
# Example server URLs
urls = [
"http://localhost:8000/sse",
"http://localhost:8001/sse",
"http://localhost:8002/sse",
]
# Example responses with multiple tool calls in a single response
responses = [
{
"tool_calls": [
{
"function": {
"name": "search_web",
"arguments": {
"query": "machine learning trends 2024"
},
}
},
{
"function": {
"name": "search_database",
"arguments": {
"table": "articles",
"category": "AI",
},
}
},
]
},
{
"function": {
"name": "send_notification",
"arguments": {
"user_id": 456,
"message": "Your analysis is complete",
},
}
},
]
print("\n=== Asynchronous Execution Example ===")
print(
f"Executing tool calls across {len(urls)} servers asynchronously..."
)
try:
# Execute all tool calls across multiple servers
results = (
await execute_multiple_tools_on_multiple_mcp_servers(
responses=responses,
urls=urls,
output_type="str",
max_concurrent=3,
)
)
print(
f"\nAsync execution completed! Got {len(results)} results:"
)
for i, result in enumerate(results):
print(f"\nResult {i + 1}:")
print(f" Response Index: {result['response_index']}")
print(f" Function: {result['function_name']}")
print(f" Server: {result['server_url']}")
print(f" Status: {result['status']}")
if result["status"] == "success":
print(f" Result: {result['result']}")
else:
print(
f" Error: {result.get('error', 'Unknown error')}"
)
except Exception as e:
print(f"Error during async execution: {str(e)}")
def example_get_tools_from_multiple_servers():
"""Example of getting tools from multiple servers."""
urls = [
"http://localhost:8000/sse",
"http://localhost:8001/sse",
"http://localhost:8002/sse",
]
print("\n=== Getting Tools from Multiple Servers ===")
try:
# Get all available tools from all servers
all_tools = get_tools_for_multiple_mcp_servers(
urls=urls, format="openai", output_type="dict"
)
print(
f"Found {len(all_tools)} total tools across all servers:"
)
# Group tools by function name to see what's available
function_names = set()
for tool in all_tools:
if isinstance(tool, dict) and "function" in tool:
function_names.add(tool["function"]["name"])
elif hasattr(tool, "name"):
function_names.add(tool.name)
print("Available functions:")
for func_name in sorted(function_names):
print(f" - {func_name}")
except Exception as e:
print(f"Error getting tools: {str(e)}")
if __name__ == "__main__":
# Run synchronous example
example_sync_execution()
# Run async example
asyncio.run(example_async_execution())
# Get tools from multiple servers
example_get_tools_from_multiple_servers()

@ -0,0 +1,54 @@
"""
Simple test for the execute_multiple_tools_on_multiple_mcp_servers functionality.
"""
from swarms.tools.mcp_client_call import (
execute_multiple_tools_on_multiple_mcp_servers_sync,
)
def test_async_multiple_tools_execution():
"""Test the async multiple tools execution function structure."""
print(
"\nTesting async multiple tools execution function structure..."
)
urls = [
"http://localhost:8000/sse",
"http://localhost:8001/sse",
]
# Mock responses with multiple tool calls
responses = [
{
"tool_calls": [
{
"function": {
"name": "get_okx_crypto_price",
"arguments": {"symbol": "SOL-USDT"},
}
},
{
"function": {
"name": "get_crypto_price",
"arguments": {"coin_id": "solana"},
}
},
]
}
]
try:
# This will likely fail to connect, but we can test the function structure
results = execute_multiple_tools_on_multiple_mcp_servers_sync(
responses=responses, urls=urls
)
print(f"Got {len(results)} results")
print(results)
except Exception as e:
print(f"Expected error (no servers running): {str(e)}")
print("Async function structure is working correctly!")
if __name__ == "__main__":
test_async_multiple_tools_execution()

@ -0,0 +1,22 @@
from swarms.structs.conversation import Conversation
# Create a conversation object
conversation = Conversation(backend="in-memory")
# Add a message to the conversation
conversation.add(
role="user", content="Hello, how are you?", category="input"
)
# Add a message to the conversation
conversation.add(
role="assistant",
content="I'm good, thank you!",
category="output",
)
print(
conversation.export_and_count_categories(
tokenizer_model_name="claude-3-5-sonnet-20240620"
)
)

@ -0,0 +1,256 @@
"""
Enhanced Collaborative InteractiveGroupChat Example
This example demonstrates the improved collaborative behavior where agents:
1. Read and understand all previous responses
2. Acknowledge what other agents have said
3. Build upon their insights rather than repeating information
4. Synthesize multiple perspectives
5. Delegate appropriately using @mentions
The enhanced prompts ensure agents work as a true collaborative team.
"""
from swarms import Agent
from swarms.structs.interactive_groupchat import (
InteractiveGroupChat,
round_robin_speaker,
)
def create_collaborative_agents():
"""Create agents designed for enhanced collaboration."""
# Data Analyst - focuses on data insights and trends
analyst = Agent(
agent_name="analyst",
system_prompt="""You are a senior data analyst with expertise in business intelligence, statistical analysis, and data visualization. You excel at:
- Analyzing complex datasets and identifying trends
- Creating actionable insights from data
- Providing quantitative evidence for business decisions
- Identifying patterns and correlations in data
When collaborating, always reference specific data points and build upon others' insights with quantitative support.""",
llm="gpt-3.5-turbo",
)
# Market Researcher - focuses on market trends and customer insights
researcher = Agent(
agent_name="researcher",
system_prompt="""You are a market research specialist with deep expertise in consumer behavior, competitive analysis, and market trends. You excel at:
- Understanding customer needs and preferences
- Analyzing competitive landscapes
- Identifying market opportunities and threats
- Providing qualitative insights that complement data analysis
When collaborating, always connect market insights to business implications and build upon data analysis with market context.""",
llm="gpt-3.5-turbo",
)
# Strategy Consultant - focuses on strategic recommendations
strategist = Agent(
agent_name="strategist",
system_prompt="""You are a strategic consultant with expertise in business strategy, competitive positioning, and strategic planning. You excel at:
- Developing comprehensive business strategies
- Identifying competitive advantages
- Creating actionable strategic recommendations
- Synthesizing multiple perspectives into coherent strategies
When collaborating, always synthesize insights from all team members and provide strategic recommendations that leverage the collective expertise.""",
llm="gpt-3.5-turbo",
)
return [analyst, researcher, strategist]
def example_comprehensive_analysis():
"""Example of comprehensive collaborative analysis."""
print("=== Enhanced Collaborative Analysis Example ===\n")
agents = create_collaborative_agents()
# Create group chat with round robin speaker function
group_chat = InteractiveGroupChat(
name="Strategic Analysis Team",
description="A collaborative team for comprehensive business analysis",
agents=agents,
speaker_function=round_robin_speaker,
interactive=False,
)
# Complex task that requires collaboration
task = """Analyze our company's performance in the e-commerce market.
We have the following data:
- Q3 revenue: $2.5M (up 15% from Q2)
- Customer acquisition cost: $45 (down 8% from Q2)
- Customer lifetime value: $180 (up 12% from Q2)
- Market share: 3.2% (up 0.5% from Q2)
- Competitor analysis shows 3 major players with 60% market share combined
@analyst @researcher @strategist please provide a comprehensive analysis and strategic recommendations."""
print(f"Task: {task}\n")
print("Expected collaborative behavior:")
print(
"1. Analyst: Analyzes the data trends and provides quantitative insights"
)
print(
"2. Researcher: Builds on data with market context and competitive analysis"
)
print(
"3. Strategist: Synthesizes both perspectives into strategic recommendations"
)
print("\n" + "=" * 80 + "\n")
response = group_chat.run(task)
print(f"Collaborative Response:\n{response}")
def example_problem_solving():
"""Example of collaborative problem solving."""
print("\n" + "=" * 80)
print("=== Collaborative Problem Solving Example ===\n")
agents = create_collaborative_agents()
group_chat = InteractiveGroupChat(
name="Problem Solving Team",
description="A team that collaborates to solve complex business problems",
agents=agents,
speaker_function=round_robin_speaker,
interactive=False,
)
# Problem-solving task
task = """We're experiencing declining customer retention rates (down 20% in the last 6 months).
Our customer satisfaction scores are also dropping (from 8.5 to 7.2).
@analyst please analyze the retention data, @researcher investigate customer feedback and market trends,
and @strategist develop a comprehensive solution strategy."""
print(f"Task: {task}\n")
print("Expected collaborative behavior:")
print("1. Analyst: Identifies patterns in retention data")
print(
"2. Researcher: Explores customer feedback and market factors"
)
print(
"3. Strategist: Combines insights to create actionable solutions"
)
print("\n" + "=" * 80 + "\n")
response = group_chat.run(task)
print(f"Collaborative Response:\n{response}")
def example_agent_delegation():
"""Example showing how agents delegate to each other."""
print("\n" + "=" * 80)
print("=== Agent Delegation Example ===\n")
agents = create_collaborative_agents()
group_chat = InteractiveGroupChat(
name="Delegation Team",
description="A team that demonstrates effective delegation and collaboration",
agents=agents,
speaker_function=round_robin_speaker,
interactive=False,
)
# Task that encourages delegation
task = """We need to evaluate a potential new market entry opportunity in Southeast Asia.
The initial data shows promising growth potential, but we need a comprehensive assessment.
@analyst start with the market data analysis, then delegate to @researcher for market research,
and finally @strategist should provide strategic recommendations."""
print(f"Task: {task}\n")
print("Expected behavior:")
print(
"1. Analyst: Analyzes data and delegates to researcher for deeper market insights"
)
print(
"2. Researcher: Builds on data analysis and delegates to strategist for recommendations"
)
print(
"3. Strategist: Synthesizes all insights into strategic recommendations"
)
print("\n" + "=" * 80 + "\n")
response = group_chat.run(task)
print(f"Collaborative Response:\n{response}")
def example_synthesis_and_integration():
"""Example showing synthesis of multiple perspectives."""
print("\n" + "=" * 80)
print("=== Synthesis and Integration Example ===\n")
agents = create_collaborative_agents()
group_chat = InteractiveGroupChat(
name="Synthesis Team",
description="A team that excels at integrating multiple perspectives",
agents=agents,
speaker_function=round_robin_speaker,
interactive=False,
)
# Task requiring synthesis
task = """We have conflicting information about our product's market position:
- Sales data shows strong growth (25% increase)
- Customer surveys indicate declining satisfaction
- Competitor analysis shows we're losing market share
- Internal metrics show improved operational efficiency
@analyst @researcher @strategist please analyze these conflicting signals and provide
an integrated assessment of our true market position."""
print(f"Task: {task}\n")
print("Expected behavior:")
print(
"1. Analyst: Clarifies the data discrepancies and identifies patterns"
)
print(
"2. Researcher: Provides market context to explain the contradictions"
)
print(
"3. Strategist: Synthesizes all perspectives into a coherent market assessment"
)
print("\n" + "=" * 80 + "\n")
response = group_chat.run(task)
print(f"Collaborative Response:\n{response}")
def main():
"""Run all enhanced collaboration examples."""
print("Enhanced Collaborative InteractiveGroupChat Examples")
print("=" * 80)
print("This demonstrates improved agent collaboration with:")
print("- Acknowledgment of other agents' contributions")
print("- Building upon previous insights")
print("- Synthesis of multiple perspectives")
print("- Appropriate delegation using @mentions")
print("- Comprehensive understanding of conversation history")
print("=" * 80 + "\n")
# Run examples
example_comprehensive_analysis()
example_problem_solving()
example_agent_delegation()
example_synthesis_and_integration()
print("\n" + "=" * 80)
print("All enhanced collaboration examples completed!")
print("Notice how agents now:")
print("✓ Acknowledge each other's contributions")
print("✓ Build upon previous insights")
print("✓ Synthesize multiple perspectives")
print("✓ Delegate appropriately")
print("✓ Provide more cohesive and comprehensive responses")
if __name__ == "__main__":
main()

@ -0,0 +1,154 @@
"""
Mortgage and Tax Panel Discussion Example
This example demonstrates a panel of mortgage and tax specialists discussing complex
financial situations using InteractiveGroupChat with different speaker functions.
The panel includes specialists from different financial fields who can collaborate
on complex mortgage and tax planning cases.
"""
from swarms import Agent
from swarms.structs.interactive_groupchat import (
InteractiveGroupChat,
)
def create_mortgage_tax_panel():
"""Create a panel of mortgage and tax specialists for discussion."""
# Tax Attorney - Specializes in tax law and complex tax situations
tax_attorney = Agent(
agent_name="tax_attorney",
system_prompt="""You are Sarah Mitchell, J.D., a tax attorney with 15 years of experience.
You specialize in complex tax law, real estate taxation, and tax planning strategies.
You have expertise in:
- Federal and state tax regulations
- Real estate tax law and property taxation
- Tax implications of mortgage transactions
- Tax planning for real estate investments
- IRS dispute resolution and tax litigation
- Estate tax planning and trusts
When discussing cases, provide legally sound tax advice, consider recent tax law changes,
and collaborate with other specialists to ensure comprehensive financial planning.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Mortgage Broker - Lending and mortgage specialist
mortgage_broker = Agent(
agent_name="mortgage_broker",
system_prompt="""You are Michael Chen, a senior mortgage broker with 12 years of experience.
You specialize in residential and commercial mortgage lending.
You have expertise in:
- Conventional, FHA, VA, and jumbo loans
- Commercial mortgage financing
- Mortgage refinancing strategies
- Interest rate analysis and trends
- Loan qualification requirements
- Mortgage insurance considerations
When discussing cases, analyze lending options, consider credit profiles,
and evaluate debt-to-income ratios for optimal mortgage solutions.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Real Estate CPA - Accounting specialist
real_estate_cpa = Agent(
agent_name="real_estate_cpa",
system_prompt="""You are Emily Rodriguez, CPA, a certified public accountant with 10 years of experience.
You specialize in real estate accounting and tax preparation.
You have expertise in:
- Real estate tax accounting
- Property depreciation strategies
- Mortgage interest deductions
- Real estate investment taxation
- Financial statement analysis
- Tax credit optimization
When discussing cases, focus on accounting implications, tax efficiency,
and financial reporting requirements for real estate transactions.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Financial Advisor - Investment and planning specialist
financial_advisor = Agent(
agent_name="financial_advisor",
system_prompt="""You are James Thompson, CFP®, a financial advisor with 8 years of experience.
You specialize in comprehensive financial planning and wealth management.
You have expertise in:
- Investment portfolio management
- Retirement planning
- Real estate investment strategy
- Cash flow analysis
- Risk management
- Estate planning coordination
When discussing cases, consider overall financial goals, investment strategy,
and how mortgage decisions impact long-term financial planning.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Real Estate Attorney - Property law specialist
real_estate_attorney = Agent(
agent_name="real_estate_attorney",
system_prompt="""You are Lisa Park, J.D., a real estate attorney with 11 years of experience.
You specialize in real estate law and property transactions.
You have expertise in:
- Real estate contract law
- Property title analysis
- Mortgage document review
- Real estate closing procedures
- Property rights and zoning
- Real estate litigation
When discussing cases, evaluate legal implications, ensure compliance,
and address potential legal issues in real estate transactions.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
return [
tax_attorney,
mortgage_broker,
real_estate_cpa,
financial_advisor,
real_estate_attorney,
]
def example_mortgage_tax_panel():
"""Example with random dynamic speaking order."""
print("=== MORTGAGE AND TAX SPECIALIST PANEL ===\n")
agents = create_mortgage_tax_panel()
group_chat = InteractiveGroupChat(
name="Mortgage and Tax Panel Discussion",
description="A collaborative panel of mortgage and tax specialists discussing complex cases",
agents=agents,
interactive=False,
speaker_function="random-speaker",
)
# Case 1: Complex mortgage refinancing with tax implications
case1 = """CASE PRESENTATION:
@tax_attorney, @real_estate_cpa, and @real_estate_attorney, please discuss the possible legal and accounting strategies
for minimizing or potentially eliminating property taxes in Los Altos, California. Consider legal exemptions,
special assessments, and any relevant California property tax laws that could help achieve this goal.
"""
group_chat.run(case1)
if __name__ == "__main__":
example_mortgage_tax_panel()

@ -0,0 +1,89 @@
from swarms import Agent
from swarms.structs.interactive_groupchat import InteractiveGroupChat
if __name__ == "__main__":
# Initialize agents specialized for condensed matter physics
theoretical_physicist = Agent(
agent_name="TheoreticalPhysicist",
system_prompt="""
You are an exceptionally brilliant theoretical condensed matter physicist with deep expertise in quantum many-body theory, phase transitions, and emergent phenomena. You possess extraordinary mathematical intuition and can derive, manipulate, and analyze complex equations with remarkable precision.
Your core competencies include:
- **Advanced Mathematical Modeling**: You excel at formulating and solving differential equations, partial differential equations, and integro-differential equations that describe quantum systems. You can derive equations from first principles using variational methods, path integrals, and functional analysis.
- **Quantum Field Theory**: You master the mathematical framework of quantum field theory, including Feynman diagrams, renormalization group theory, and effective field theories. You can derive and analyze equations for correlation functions, Green's functions, and response functions.
- **Statistical Mechanics**: You are expert at deriving partition functions, free energies, and thermodynamic potentials. You can formulate and solve equations for phase transitions, critical phenomena, and scaling behavior using techniques like mean-field theory, Landau-Ginzburg theory, and renormalization group methods.
- **Many-Body Physics**: You excel at deriving equations for interacting quantum systems, including Hubbard models, Heisenberg models, and BCS theory. You can analyze equations for collective excitations, quasiparticles, and topological states.
- **Analytical Techniques**: You master perturbation theory, variational methods, exact diagonalization, and other analytical techniques. You can derive equations for energy spectra, wave functions, and observables in complex quantum systems.
When presented with a physics problem, you immediately think in terms of mathematical equations and can derive the appropriate formalism from fundamental principles. You always show your mathematical work step-by-step and explain the physical meaning of each equation you write.
""",
model="claude-3-5-sonnet-20240620",
)
experimental_physicist = Agent(
agent_name="ExperimentalPhysicist",
system_prompt="""You are an exceptionally skilled experimental condensed matter physicist with profound expertise in materials synthesis, characterization techniques, and data analysis. You possess extraordinary analytical abilities and can derive, interpret, and validate equations that describe experimental observations.
Your core competencies include:
- **Materials Synthesis & Characterization**: You excel at designing synthesis protocols and deriving equations that describe growth kinetics, phase formation, and structural evolution. You can formulate equations for crystal growth, diffusion processes, and phase equilibria.
- **Advanced Characterization Techniques**: You master the mathematical foundations of X-ray diffraction (Bragg's law, structure factors, Rietveld refinement), electron microscopy (diffraction patterns, image formation), and spectroscopy (absorption, emission, scattering cross-sections). You can derive equations for resolution limits, signal-to-noise ratios, and detection sensitivity.
- **Transport Properties**: You excel at deriving and analyzing equations for electrical conductivity (Drude model, Boltzmann transport), thermal conductivity (phonon and electron contributions), and magnetic properties (Curie-Weiss law, magnetic susceptibility). You can formulate equations for Hall effect, magnetoresistance, and thermoelectric effects.
- **Data Analysis & Modeling**: You possess advanced skills in fitting experimental data to theoretical models, error analysis, and statistical inference. You can derive equations for uncertainty propagation, confidence intervals, and model selection criteria.
- **Experimental Design**: You excel at deriving equations for experimental sensitivity, resolution requirements, and optimization of measurement parameters. You can formulate equations for signal processing, noise reduction, and systematic error correction.
When analyzing experimental data, you immediately think in terms of mathematical models and can derive equations that connect observations to underlying physical mechanisms. You always show your mathematical reasoning and explain how equations relate to experimental reality.""",
model="claude-3-5-sonnet-20240620",
)
computational_physicist = Agent(
agent_name="ComputationalPhysicist",
system_prompt="""You are an exceptionally brilliant computational condensed matter physicist with deep expertise in numerical methods, algorithm development, and high-performance computing. You possess extraordinary mathematical skills and can formulate, implement, and analyze equations that drive computational simulations.
Your core competencies include:
- **Density Functional Theory (DFT)**: You excel at deriving and implementing the Kohn-Sham equations, exchange-correlation functionals, and self-consistent field methods. You can formulate equations for electronic structure, total energies, forces, and response functions. You master the mathematical foundations of plane-wave methods, pseudopotentials, and k-point sampling.
- **Quantum Monte Carlo Methods**: You are expert at deriving equations for variational Monte Carlo, diffusion Monte Carlo, and path integral Monte Carlo. You can formulate equations for importance sampling, correlation functions, and statistical estimators. You excel at deriving equations for finite-size effects, time-step errors, and population control.
- **Molecular Dynamics**: You master the mathematical framework of classical and ab initio molecular dynamics, including equations of motion, thermostats, barostats, and constraint algorithms. You can derive equations for time integration schemes, energy conservation, and phase space sampling.
- **Many-Body Methods**: You excel at implementing and analyzing equations for exact diagonalization, quantum chemistry methods (CI, CC, MP), and tensor network methods (DMRG, PEPS). You can derive equations for matrix elements, basis transformations, and optimization algorithms.
- **High-Performance Computing**: You possess advanced skills in parallel algorithms, load balancing, and numerical optimization. You can derive equations for computational complexity, scaling behavior, and performance bottlenecks. You excel at formulating equations for parallel efficiency, communication overhead, and memory management.
When developing computational methods, you think in terms of mathematical algorithms and can derive equations that translate physical problems into efficient numerical procedures. You always show your mathematical derivations and explain how equations map to computational implementations.""",
model="claude-3-5-sonnet-20240620",
)
# Create list of agents including both Agent instances and callable
agents = [
theoretical_physicist,
experimental_physicist,
computational_physicist,
]
# Initialize another chat instance in interactive mode
interactive_chat = InteractiveGroupChat(
name="Interactive Condensed Matter Physics Research Team",
description="An interactive team of condensed matter physics experts providing comprehensive analysis of quantum materials, phase transitions, and emergent phenomena",
agents=agents,
max_loops=1,
output_type="all",
interactive=True,
)
try:
# Start the interactive session
print("\nStarting interactive session...")
# interactive_chat.run("What is the best methodology to accumulate gold and silver commodities, what is the best long term strategy to accumulate them?")
interactive_chat.start_interactive_session()
except Exception as e:
print(f"An error occurred in interactive mode: {e}")

@ -0,0 +1,162 @@
"""
Medical Panel Discussion Example
This example demonstrates a panel of medical specialists discussing treatment solutions
for various diseases using InteractiveGroupChat with different speaker functions:
- Round Robin: Doctors speak in a fixed order
- Random: Doctors speak in random order
- Priority: Senior doctors speak first
- Custom: Disease-specific speaker function
The panel includes specialists from different medical fields who can collaborate
on complex medical cases and treatment plans.
"""
from swarms import Agent
from swarms.structs.interactive_groupchat import (
InteractiveGroupChat,
)
def create_medical_panel():
"""Create a panel of medical specialists for discussion."""
# Cardiologist - Heart and cardiovascular system specialist
cardiologist = Agent(
agent_name="cardiologist",
system_prompt="""You are Dr. Sarah Chen, a board-certified cardiologist with 15 years of experience.
You specialize in cardiovascular diseases, heart failure, arrhythmias, and interventional cardiology.
You have expertise in:
- Coronary artery disease and heart attacks
- Heart failure and cardiomyopathy
- Arrhythmias and electrophysiology
- Hypertension and lipid disorders
- Cardiac imaging and diagnostic procedures
When discussing cases, provide evidence-based treatment recommendations,
consider patient risk factors, and collaborate with other specialists for comprehensive care.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Oncologist - Cancer specialist
oncologist = Agent(
agent_name="oncologist",
system_prompt="""You are Dr. Michael Rodriguez, a medical oncologist with 12 years of experience.
You specialize in the diagnosis and treatment of various types of cancer.
You have expertise in:
- Solid tumors (lung, breast, colon, prostate, etc.)
- Hematologic malignancies (leukemia, lymphoma, multiple myeloma)
- Targeted therapy and immunotherapy
- Clinical trials and novel treatments
- Palliative care and symptom management
When discussing cases, consider the cancer type, stage, molecular profile,
patient performance status, and available treatment options including clinical trials.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Neurologist - Nervous system specialist
neurologist = Agent(
agent_name="neurologist",
system_prompt="""You are Dr. Emily Watson, a neurologist with 10 years of experience.
You specialize in disorders of the nervous system, brain, and spinal cord.
You have expertise in:
- Stroke and cerebrovascular disease
- Neurodegenerative disorders (Alzheimer's, Parkinson's, ALS)
- Multiple sclerosis and demyelinating diseases
- Epilepsy and seizure disorders
- Headache and migraine disorders
- Neuromuscular diseases
When discussing cases, consider neurological symptoms, imaging findings,
and the impact of neurological conditions on overall patient care.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Endocrinologist - Hormone and metabolism specialist
endocrinologist = Agent(
agent_name="endocrinologist",
system_prompt="""You are Dr. James Thompson, an endocrinologist with 8 years of experience.
You specialize in disorders of the endocrine system and metabolism.
You have expertise in:
- Diabetes mellitus (Type 1, Type 2, gestational)
- Thyroid disorders (hyperthyroidism, hypothyroidism, thyroid cancer)
- Adrenal disorders and Cushing's syndrome
- Pituitary disorders and growth hormone issues
- Osteoporosis and calcium metabolism
- Reproductive endocrinology
When discussing cases, consider metabolic factors, hormone levels,
and how endocrine disorders may affect other organ systems.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Infectious Disease Specialist
infectious_disease = Agent(
agent_name="infectious_disease",
system_prompt="""You are Dr. Lisa Park, an infectious disease specialist with 11 years of experience.
You specialize in the diagnosis and treatment of infectious diseases.
You have expertise in:
- Bacterial, viral, fungal, and parasitic infections
- Antibiotic resistance and antimicrobial stewardship
- HIV/AIDS and opportunistic infections
- Travel medicine and tropical diseases
- Hospital-acquired infections
- Emerging infectious diseases
When discussing cases, consider the infectious agent, antimicrobial susceptibility,
host factors, and infection control measures.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
return [
cardiologist,
oncologist,
neurologist,
endocrinologist,
infectious_disease,
]
def example_round_robin_panel():
"""Example with round robin speaking order."""
print("=== ROUND ROBIN MEDICAL PANEL ===\n")
agents = create_medical_panel()
group_chat = InteractiveGroupChat(
name="Medical Panel Discussion",
description="A collaborative panel of medical specialists discussing complex cases",
agents=agents,
speaker_function="random-dynamic-speaker",
interactive=False,
)
print(group_chat.speaker_function)
print(group_chat.get_current_speaker_function())
# Case 1: Complex patient with multiple conditions
case1 = """CASE PRESENTATION:
A 65-year-old male with Type 2 diabetes, hypertension, and recent diagnosis of
stage 3 colon cancer presents with chest pain and shortness of breath.
ECG shows ST-segment elevation. Recent blood work shows elevated blood glucose (280 mg/dL)
and signs of infection (WBC 15,000, CRP elevated).
@cardiologist @oncologist @endocrinologist @infectious_disease please provide your
assessment and treatment recommendations for this complex case."""
group_chat.run(case1)
if __name__ == "__main__":
example_round_robin_panel()

@ -0,0 +1,163 @@
"""
Medical Panel Discussion Example
This example demonstrates a panel of medical specialists discussing treatment solutions
for various diseases using InteractiveGroupChat with different speaker functions:
- Round Robin: Doctors speak in a fixed order
- Random: Doctors speak in random order
- Priority: Senior doctors speak first
- Custom: Disease-specific speaker function
The panel includes specialists from different medical fields who can collaborate
on complex medical cases and treatment plans.
"""
from swarms import Agent
from swarms.structs.interactive_groupchat import (
InteractiveGroupChat,
)
def create_medical_panel():
"""Create a panel of medical specialists for discussion."""
# Cardiologist - Heart and cardiovascular system specialist
cardiologist = Agent(
agent_name="cardiologist",
system_prompt="""You are Dr. Sarah Chen, a board-certified cardiologist with 15 years of experience.
You specialize in cardiovascular diseases, heart failure, arrhythmias, and interventional cardiology.
You have expertise in:
- Coronary artery disease and heart attacks
- Heart failure and cardiomyopathy
- Arrhythmias and electrophysiology
- Hypertension and lipid disorders
- Cardiac imaging and diagnostic procedures
When discussing cases, provide evidence-based treatment recommendations,
consider patient risk factors, and collaborate with other specialists for comprehensive care.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Oncologist - Cancer specialist
oncologist = Agent(
agent_name="oncologist",
system_prompt="""You are Dr. Michael Rodriguez, a medical oncologist with 12 years of experience.
You specialize in the diagnosis and treatment of various types of cancer.
You have expertise in:
- Solid tumors (lung, breast, colon, prostate, etc.)
- Hematologic malignancies (leukemia, lymphoma, multiple myeloma)
- Targeted therapy and immunotherapy
- Clinical trials and novel treatments
- Palliative care and symptom management
When discussing cases, consider the cancer type, stage, molecular profile,
patient performance status, and available treatment options including clinical trials.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Neurologist - Nervous system specialist
neurologist = Agent(
agent_name="neurologist",
system_prompt="""You are Dr. Emily Watson, a neurologist with 10 years of experience.
You specialize in disorders of the nervous system, brain, and spinal cord.
You have expertise in:
- Stroke and cerebrovascular disease
- Neurodegenerative disorders (Alzheimer's, Parkinson's, ALS)
- Multiple sclerosis and demyelinating diseases
- Epilepsy and seizure disorders
- Headache and migraine disorders
- Neuromuscular diseases
When discussing cases, consider neurological symptoms, imaging findings,
and the impact of neurological conditions on overall patient care.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Endocrinologist - Hormone and metabolism specialist
endocrinologist = Agent(
agent_name="endocrinologist",
system_prompt="""You are Dr. James Thompson, an endocrinologist with 8 years of experience.
You specialize in disorders of the endocrine system and metabolism.
You have expertise in:
- Diabetes mellitus (Type 1, Type 2, gestational)
- Thyroid disorders (hyperthyroidism, hypothyroidism, thyroid cancer)
- Adrenal disorders and Cushing's syndrome
- Pituitary disorders and growth hormone issues
- Osteoporosis and calcium metabolism
- Reproductive endocrinology
When discussing cases, consider metabolic factors, hormone levels,
and how endocrine disorders may affect other organ systems.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Infectious Disease Specialist
infectious_disease = Agent(
agent_name="infectious_disease",
system_prompt="""You are Dr. Lisa Park, an infectious disease specialist with 11 years of experience.
You specialize in the diagnosis and treatment of infectious diseases.
You have expertise in:
- Bacterial, viral, fungal, and parasitic infections
- Antibiotic resistance and antimicrobial stewardship
- HIV/AIDS and opportunistic infections
- Travel medicine and tropical diseases
- Hospital-acquired infections
- Emerging infectious diseases
When discussing cases, consider the infectious agent, antimicrobial susceptibility,
host factors, and infection control measures.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
return [
cardiologist,
oncologist,
neurologist,
endocrinologist,
infectious_disease,
]
def example_round_robin_panel():
"""Example with round robin speaking order."""
print("=== ROUND ROBIN MEDICAL PANEL ===\n")
agents = create_medical_panel()
group_chat = InteractiveGroupChat(
name="Medical Panel Discussion",
description="A collaborative panel of medical specialists discussing complex cases",
agents=agents,
speaker_function="round-robin-speaker",
interactive=False,
)
print(group_chat.speaker_function)
# Case 1: Complex patient with multiple conditions
case1 = """CASE PRESENTATION:
A 65-year-old male with Type 2 diabetes, hypertension, and recent diagnosis of
stage 3 colon cancer presents with chest pain and shortness of breath.
ECG shows ST-segment elevation. Recent blood work shows elevated blood glucose (280 mg/dL)
and signs of infection (WBC 15,000, CRP elevated).
@cardiologist @oncologist @endocrinologist @infectious_disease please provide your
assessment and treatment recommendations for this complex case."""
response = group_chat.run(case1)
print(f"Response:\n{response}\n")
print("=" * 80 + "\n")
if __name__ == "__main__":
example_round_robin_panel()

@ -0,0 +1,162 @@
"""
Medical Panel Discussion Example
This example demonstrates a panel of medical specialists discussing treatment solutions
for various diseases using InteractiveGroupChat with different speaker functions:
- Round Robin: Doctors speak in a fixed order
- Random: Doctors speak in random order
- Priority: Senior doctors speak first
- Custom: Disease-specific speaker function
The panel includes specialists from different medical fields who can collaborate
on complex medical cases and treatment plans.
"""
from swarms import Agent
from swarms.structs.interactive_groupchat import (
InteractiveGroupChat,
round_robin_speaker,
)
def create_medical_panel():
"""Create a panel of medical specialists for discussion."""
# Cardiologist - Heart and cardiovascular system specialist
cardiologist = Agent(
agent_name="cardiologist",
system_prompt="""You are Dr. Sarah Chen, a board-certified cardiologist with 15 years of experience.
You specialize in cardiovascular diseases, heart failure, arrhythmias, and interventional cardiology.
You have expertise in:
- Coronary artery disease and heart attacks
- Heart failure and cardiomyopathy
- Arrhythmias and electrophysiology
- Hypertension and lipid disorders
- Cardiac imaging and diagnostic procedures
When discussing cases, provide evidence-based treatment recommendations,
consider patient risk factors, and collaborate with other specialists for comprehensive care.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Oncologist - Cancer specialist
oncologist = Agent(
agent_name="oncologist",
system_prompt="""You are Dr. Michael Rodriguez, a medical oncologist with 12 years of experience.
You specialize in the diagnosis and treatment of various types of cancer.
You have expertise in:
- Solid tumors (lung, breast, colon, prostate, etc.)
- Hematologic malignancies (leukemia, lymphoma, multiple myeloma)
- Targeted therapy and immunotherapy
- Clinical trials and novel treatments
- Palliative care and symptom management
When discussing cases, consider the cancer type, stage, molecular profile,
patient performance status, and available treatment options including clinical trials.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Neurologist - Nervous system specialist
neurologist = Agent(
agent_name="neurologist",
system_prompt="""You are Dr. Emily Watson, a neurologist with 10 years of experience.
You specialize in disorders of the nervous system, brain, and spinal cord.
You have expertise in:
- Stroke and cerebrovascular disease
- Neurodegenerative disorders (Alzheimer's, Parkinson's, ALS)
- Multiple sclerosis and demyelinating diseases
- Epilepsy and seizure disorders
- Headache and migraine disorders
- Neuromuscular diseases
When discussing cases, consider neurological symptoms, imaging findings,
and the impact of neurological conditions on overall patient care.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Endocrinologist - Hormone and metabolism specialist
endocrinologist = Agent(
agent_name="endocrinologist",
system_prompt="""You are Dr. James Thompson, an endocrinologist with 8 years of experience.
You specialize in disorders of the endocrine system and metabolism.
You have expertise in:
- Diabetes mellitus (Type 1, Type 2, gestational)
- Thyroid disorders (hyperthyroidism, hypothyroidism, thyroid cancer)
- Adrenal disorders and Cushing's syndrome
- Pituitary disorders and growth hormone issues
- Osteoporosis and calcium metabolism
- Reproductive endocrinology
When discussing cases, consider metabolic factors, hormone levels,
and how endocrine disorders may affect other organ systems.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
# Infectious Disease Specialist
infectious_disease = Agent(
agent_name="infectious_disease",
system_prompt="""You are Dr. Lisa Park, an infectious disease specialist with 11 years of experience.
You specialize in the diagnosis and treatment of infectious diseases.
You have expertise in:
- Bacterial, viral, fungal, and parasitic infections
- Antibiotic resistance and antimicrobial stewardship
- HIV/AIDS and opportunistic infections
- Travel medicine and tropical diseases
- Hospital-acquired infections
- Emerging infectious diseases
When discussing cases, consider the infectious agent, antimicrobial susceptibility,
host factors, and infection control measures.""",
model_name="claude-3-5-sonnet-20240620",
streaming_on=True,
print_on=True,
)
return [
cardiologist,
oncologist,
neurologist,
endocrinologist,
infectious_disease,
]
def example_round_robin_panel():
"""Example with round robin speaking order."""
print("=== ROUND ROBIN MEDICAL PANEL ===\n")
agents = create_medical_panel()
group_chat = InteractiveGroupChat(
name="Medical Panel Discussion",
description="A collaborative panel of medical specialists discussing complex cases",
agents=agents,
speaker_function=round_robin_speaker,
interactive=False,
)
# Case 1: Complex patient with multiple conditions
case1 = """CASE PRESENTATION:
A 65-year-old male with Type 2 diabetes, hypertension, and recent diagnosis of
stage 3 colon cancer presents with chest pain and shortness of breath.
ECG shows ST-segment elevation. Recent blood work shows elevated blood glucose (280 mg/dL)
and signs of infection (WBC 15,000, CRP elevated).
@cardiologist @oncologist @endocrinologist @infectious_disease please provide your
assessment and treatment recommendations for this complex case."""
response = group_chat.run(case1)
print(f"Response:\n{response}\n")
print("=" * 80 + "\n")
if __name__ == "__main__":
example_round_robin_panel()

@ -0,0 +1,72 @@
"""
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="claude-3-5-sonnet-20240620",
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="claude-3-5-sonnet-20240620",
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="claude-3-5-sonnet-20240620",
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 for a personal healthcare ai consumer assistant app. @analyst @researcher @writer please contribute."
response = group_chat.run(task)
print(f"Response:\n{response}\n")
if __name__ == "__main__":
# example_round_robin()
example_random()

@ -0,0 +1,19 @@
from swarms import Agent
# Enable real-time streaming
agent = Agent(
agent_name="StoryAgent",
# model_name="groq/llama-3.1-8b-instant",
model_name="claude-3-5-sonnet-20240620",
# system_prompt="",
streaming_on=True, # 🔥 This enables real streaming!
max_loops=1,
print_on=True,
output_type="all",
)
# This will now stream in real-time with beautiful UI!
response = agent.run(
"Tell me a detailed story about Humanity colonizing the stars"
)
print(response)

@ -18,7 +18,7 @@ router = SwarmRouter(
name="multi-agent-router-demo",
description="Routes tasks to the most suitable agent",
agents=agents,
swarm_type="MultiAgentRouter"
swarm_type="MultiAgentRouter",
)
result = router.run("Write a function that adds two numbers")

@ -0,0 +1,80 @@
from swarms import Agent, MixtureOfAgents
# Agent 1: Risk Metrics Calculator
risk_metrics_agent = Agent(
agent_name="Risk-Metrics-Calculator",
agent_description="Calculates key risk metrics like VaR, Sharpe ratio, and volatility",
system_prompt="""You are a risk metrics specialist. Calculate and explain:
- Value at Risk (VaR)
- Sharpe ratio
- Volatility
- Maximum drawdown
- Beta coefficient
Provide clear, numerical results with brief explanations.""",
max_loops=1,
# model_name="gpt-4o-mini",
random_model_enabled=True,
dynamic_temperature_enabled=True,
output_type="str-all-except-first",
max_tokens=4096,
)
# Agent 2: Portfolio Risk Analyzer
portfolio_risk_agent = Agent(
agent_name="Portfolio-Risk-Analyzer",
agent_description="Analyzes portfolio diversification and concentration risk",
system_prompt="""You are a portfolio risk analyst. Focus on:
- Portfolio diversification analysis
- Concentration risk assessment
- Correlation analysis
- Sector/asset allocation risk
- Liquidity risk evaluation
Provide actionable insights for risk reduction.""",
max_loops=1,
# model_name="gpt-4o-mini",
random_model_enabled=True,
dynamic_temperature_enabled=True,
output_type="str-all-except-first",
max_tokens=4096,
)
# Agent 3: Market Risk Monitor
market_risk_agent = Agent(
agent_name="Market-Risk-Monitor",
agent_description="Monitors market conditions and identifies risk factors",
system_prompt="""You are a market risk monitor. Identify and assess:
- Market volatility trends
- Economic risk factors
- Geopolitical risks
- Interest rate risks
- Currency risks
Provide current risk alerts and trends.""",
max_loops=1,
# model_name="gpt-4o-mini",
random_model_enabled=True,
dynamic_temperature_enabled=True,
output_type="str-all-except-first",
max_tokens=4096,
)
swarm = MixtureOfAgents(
agents=[
risk_metrics_agent,
portfolio_risk_agent,
market_risk_agent,
],
layers=1,
max_loops=1,
output_type="final",
)
out = swarm.run(
"Calculate VaR and Sharpe ratio for a portfolio with 15% annual return and 20% volatility"
)
print(out)

@ -0,0 +1,87 @@
import logging
from swarms.structs import Agent
from swarms.prompts.logistics import (
Quality_Control_Agent_Prompt,
)
# Set up debug logging
logging.basicConfig(level=logging.DEBUG)
# Image for analysis
# factory_image="image.png" # normal image of a factory
factory_image = "image2.png" # image of a burning factory
def security_analysis(danger_level: str) -> str:
"""
Analyzes the security danger level and returns an appropriate response.
Args:
danger_level (str): The level of danger to analyze.
Must be one of: "low", "medium", "high"
Returns:
str: A detailed security analysis based on the danger level.
"""
if danger_level == "low":
return """SECURITY ANALYSIS - LOW DANGER LEVEL:
Environment appears safe and well-controlled
Standard security measures are adequate
Low risk of accidents or security breaches
Normal operational protocols can continue
Recommendations: Maintain current security standards and continue regular monitoring."""
elif danger_level == "medium":
return """SECURITY ANALYSIS - MEDIUM DANGER LEVEL:
Moderate security concerns identified
Enhanced monitoring recommended
Some security measures may need strengthening
Risk of incidents exists but manageable
Recommendations: Implement additional safety protocols, increase surveillance, and conduct safety briefings."""
elif danger_level == "high":
return """SECURITY ANALYSIS - HIGH DANGER LEVEL:
🚨 CRITICAL SECURITY CONCERNS DETECTED
🚨 Immediate action required
🚨 High risk of accidents or security breaches
🚨 Operations may need to be suspended
Recommendations: Immediate intervention required, evacuate if necessary, implement emergency protocols, and conduct thorough security review."""
else:
return f"ERROR: Invalid danger level '{danger_level}'. Must be 'low', 'medium', or 'high'."
# Custom system prompt that includes tool usage
custom_system_prompt = f"""
{Quality_Control_Agent_Prompt}
You have access to tools that can help you with your analysis. When you need to perform a security analysis, you MUST use the security_analysis function with an appropriate danger level (low, medium, or high) based on your observations.
Always use the available tools when they are relevant to the task. If you determine there is any level of danger or security concern, call the security_analysis function with the appropriate danger level.
"""
# Quality control agent
quality_control_agent = Agent(
agent_name="Quality Control Agent",
agent_description="A quality control agent that analyzes images and provides a detailed report on the quality of the product in the image.",
# model_name="anthropic/claude-3-opus-20240229",
model_name="gpt-4o",
system_prompt=custom_system_prompt,
multi_modal=True,
max_loops=1,
output_type="str-all-except-first",
# tools_list_dictionary=[schema],
tools=[security_analysis],
)
response = quality_control_agent.run(
task="Analyze the image and then perform a security analysis. Based on what you see in the image, determine if there is a low, medium, or high danger level and call the security_analysis function with that danger level.",
img=factory_image,
)
# The response is already printed by the agent's pretty_print method

@ -176,15 +176,15 @@ agent = Agent(
max_loops=1,
model_name="gpt-4o-mini",
dynamic_temperature_enabled=True,
output_type="all",
output_type="final",
tool_call_summary=True,
tools=[
get_coin_price,
get_top_cryptocurrencies,
],
# output_raw_json_from_tool_call=True,
)
print(
agent.run(
"What is the price of Bitcoin? what are the top 5 cryptocurrencies by market cap?"
)
)
out = agent.run("What is the price of Bitcoin?")
print(out)
print(f"Output type: {type(out)}")

@ -0,0 +1,20 @@
from swarms import Agent
from swarms.prompts.finance_agent_sys_prompt import (
FINANCIAL_AGENT_SYS_PROMPT,
)
from swarms_tools import yahoo_finance_api
# Initialize the agent
agent = Agent(
agent_name="Financial-Analysis-Agent",
agent_description="Personal finance advisor agent",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
max_loops=1,
model_name="gpt-4o-mini",
tools=[yahoo_finance_api],
dynamic_temperature_enabled=True,
)
agent.run(
"Fetch the data for nvidia and tesla both with the yahoo finance api"
)

@ -1,31 +0,0 @@
from swarms import Agent
from swarms.prompts.finance_agent_sys_prompt import (
FINANCIAL_AGENT_SYS_PROMPT,
)
from swarms_tools import (
fetch_stock_news,
coin_gecko_coin_api,
fetch_htx_data,
)
# Initialize the agent
agent = Agent(
agent_name="Financial-Analysis-Agent",
agent_description="Personal finance advisor agent",
system_prompt=FINANCIAL_AGENT_SYS_PROMPT,
max_loops=1,
model_name="gpt-4o",
dynamic_temperature_enabled=True,
user_name="swarms_corp",
retry_attempts=3,
context_length=8192,
return_step_meta=False,
output_type="str", # "json", "dict", "csv" OR "string" "yaml" and
auto_generate_prompt=False, # Auto generate prompt for the agent based on name, description, and system prompt, task
max_tokens=4000, # max output tokens
saved_state_path="agent_00.json",
interactive=False,
tools=[fetch_stock_news, coin_gecko_coin_api, fetch_htx_data],
)
agent.run("Analyze the $swarms token on htx")

@ -1,4 +1,4 @@
from swarms.structs import Agent
from swarms import Agent
from swarms.prompts.logistics import (
Quality_Control_Agent_Prompt,
)
@ -16,6 +16,8 @@ quality_control_agent = Agent(
multi_modal=True,
max_loops=1,
output_type="str-all-except-first",
dynamic_temperature_enabled=True,
stream=True,
)
response = quality_control_agent.run(

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Before

Width:  |  Height:  |  Size: 232 KiB

After

Width:  |  Height:  |  Size: 232 KiB

@ -0,0 +1,25 @@
from swarms import Agent
# Image for analysis
factory_image = "image.jpg"
# Quality control agent
quality_control_agent = Agent(
agent_name="Quality Control Agent",
agent_description="A quality control agent that analyzes images and provides a detailed report on the quality of the product in the image.",
model_name="claude-3-5-sonnet-20240620",
# system_prompt=Quality_Control_Agent_Prompt,
# multi_modal=True,
max_loops=1,
output_type="str-all-except-first",
summarize_multiple_images=True,
)
response = quality_control_agent.run(
task="Analyze our factories images and provide a detailed health report for each factory.",
imgs=[factory_image, "burning_image.jpg"],
)
print(response)

@ -8,7 +8,7 @@ from swarms.prompts.logistics import (
factory_image = "image.jpg"
def security_analysis(danger_level: str = None) -> str:
def security_analysis(danger_level: str) -> str:
"""
Analyzes the security danger level and returns an appropriate response.
@ -39,8 +39,13 @@ def security_analysis(danger_level: str = None) -> str:
return "Unknown danger level"
# schema = BaseTool().function_to_dict(security_analysis)
# print(json.dumps(schema, indent=4))
custom_system_prompt = f"""
{Quality_Control_Agent_Prompt}
You have access to tools that can help you with your analysis. When you need to perform a security analysis, you MUST use the security_analysis function with an appropriate danger level (low, medium, or high) based on your observations.
Always use the available tools when they are relevant to the task. If you determine there is any level of danger or security concern, call the security_analysis function with the appropriate danger level.
"""
# Quality control agent
quality_control_agent = Agent(
@ -48,7 +53,7 @@ quality_control_agent = Agent(
agent_description="A quality control agent that analyzes images and provides a detailed report on the quality of the product in the image.",
# model_name="anthropic/claude-3-opus-20240229",
model_name="gpt-4o-mini",
system_prompt=Quality_Control_Agent_Prompt,
system_prompt=custom_system_prompt,
multi_modal=True,
max_loops=1,
output_type="str-all-except-first",
@ -58,8 +63,6 @@ quality_control_agent = Agent(
response = quality_control_agent.run(
task="what is in the image?",
# img=factory_image,
task="Analyze the image and then perform a security analysis. Based on what you see in the image, determine if there is a low, medium, or high danger level and call the security_analysis function with that danger level",
img=factory_image,
)
print(response)

@ -31,9 +31,15 @@ if __name__ == "__main__":
# Build the workflow graph
wf_graph = GraphWorkflow()
wf_graph.add_node(Node(id="agent1", type=NodeType.AGENT, agent=agent1))
wf_graph.add_node(Node(id="agent2", type=NodeType.AGENT, agent=agent2))
wf_graph.add_node(Node(id="task1", type=NodeType.TASK, callable=sample_task))
wf_graph.add_node(
Node(id="agent1", type=NodeType.AGENT, agent=agent1)
)
wf_graph.add_node(
Node(id="agent2", type=NodeType.AGENT, agent=agent2)
)
wf_graph.add_node(
Node(id="task1", type=NodeType.TASK, callable=sample_task)
)
wf_graph.add_edge(Edge(source="agent1", target="task1"))
wf_graph.add_edge(Edge(source="agent2", target="task1"))
@ -47,4 +53,3 @@ if __name__ == "__main__":
# Execute the graph
results = wf_graph.run()
print("Execution results:", results)

@ -0,0 +1,11 @@
import gradio as gr
import ai_gradio
finance_interface = gr.load(
name="swarms:gpt-4-turbo",
src=ai_gradio.registry,
agent_name="Stock-Analysis-Agent",
title="Finance Assistant",
description="Expert financial analysis and advice tailored to your investment needs.",
)
finance_interface.launch()

@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "swarms"
version = "7.8.6"
version = "7.9.3"
description = "Swarms - TGSC"
license = "MIT"
authors = ["Kye Gomez <kye@apac.ai>"]

@ -0,0 +1,155 @@
import http.client
import json
import os
from typing import List
from dotenv import load_dotenv
from swarms import Agent
load_dotenv()
def get_realtor_data_from_one_source(location: str):
"""
Fetch rental property data from the Realtor API for a specified location.
Args:
location (str): The location to search for rental properties (e.g., "Menlo Park, CA")
Returns:
str: JSON-formatted string containing rental property data
Raises:
http.client.HTTPException: If the API request fails
json.JSONDecodeError: If the response cannot be parsed as JSON
"""
conn = http.client.HTTPSConnection(
"realtor-search.p.rapidapi.com"
)
headers = {
"x-rapidapi-key": os.getenv("RAPID_API_KEY"),
"x-rapidapi-host": "realtor-search.p.rapidapi.com",
}
# URL encode the location parameter
encoded_location = location.replace(" ", "%20").replace(
",", "%2C"
)
endpoint = f"/properties/search-rent?location=city%3A{encoded_location}&sortBy=best_match"
conn.request(
"GET",
endpoint,
headers=headers,
)
res = conn.getresponse()
data = res.read()
# return "chicken data"
# Parse and format the response
try:
json_data = json.loads(data.decode("utf-8"))
# Return formatted string instead of raw JSON
return json.dumps(json_data, indent=2)
except json.JSONDecodeError:
return "Error: Could not parse API response"
def get_realtor_data_from_multiple_sources(
locations: List[str],
) -> List[str]:
"""
Fetch rental property data from multiple sources for a specified location.
Args:
location (List[str]): List of locations to search for rental properties (e.g., ["Menlo Park, CA", "Palo Alto, CA"])
"""
output = []
for location in locations:
data = get_realtor_data_from_one_source(location)
output.append(data)
return output
agent = Agent(
agent_name="Rental-Property-Specialist",
system_prompt="""
You are an expert rental property specialist with deep expertise in real estate analysis and tenant matching. Your core responsibilities include:
1. Property Analysis & Evaluation
- Analyze rental property features and amenities
- Evaluate location benefits and drawbacks
- Assess property condition and maintenance needs
- Compare rental rates with market standards
- Review lease terms and conditions
- Identify potential red flags or issues
2. Location Assessment
- Analyze neighborhood safety and demographics
- Evaluate proximity to amenities (schools, shopping, transit)
- Research local market trends and development plans
- Consider noise levels and traffic patterns
- Assess parking availability and restrictions
- Review zoning regulations and restrictions
3. Financial Analysis
- Calculate price-to-rent ratios
- Analyze utility costs and included services
- Evaluate security deposit requirements
- Consider additional fees (pet rent, parking, etc.)
- Compare with similar properties in the area
- Assess potential for rent increases
4. Tenant Matching
- Match properties to tenant requirements
- Consider commute distances
- Evaluate pet policies and restrictions
- Assess lease term flexibility
- Review application requirements
- Consider special accommodations needed
5. Documentation & Compliance
- Review lease agreement terms
- Verify property certifications
- Check compliance with local regulations
- Assess insurance requirements
- Review maintenance responsibilities
- Document property condition
When analyzing properties, always consider:
- Value for money
- Location quality
- Property condition
- Lease terms fairness
- Safety and security
- Maintenance and management quality
- Future market potential
- Tenant satisfaction factors
When you receive property data:
1. Parse and analyze the JSON data
2. Format the output in a clear, readable way
3. Focus on properties under $3,000
4. Include key details like:
- Property name/address
- Price
- Number of beds/baths
- Square footage
- Key amenities
- Links to listings
5. Sort properties by price (lowest to highest)
Provide clear, objective analysis while maintaining professional standards and ethical considerations.""",
model_name="claude-3-sonnet-20240229",
max_loops=1,
print_on=True,
streaming_on=True,
)
agent.run(
f"Create a report on the best properties in Menlo Park, CA, showcase, the name, description, price, and link to the property: {get_realtor_data_from_one_source('Menlo Park, CA')}"
)

@ -0,0 +1,63 @@
from swarms import Agent, SequentialWorkflow
# Initialize market research agent
market_researcher = Agent(
agent_name="Market-Researcher",
system_prompt="""You are a market research specialist. Your tasks include:
1. Analyzing market trends and patterns
2. Identifying market opportunities and threats
3. Evaluating competitor strategies
4. Assessing customer needs and preferences
5. Providing actionable market insights""",
model_name="claude-3-sonnet-20240229",
max_loops=1,
temperature=0.7,
streaming_on=True,
)
# Initialize financial analyst agent
financial_analyst = Agent(
agent_name="Financial-Analyst",
system_prompt="""You are a financial analysis expert. Your responsibilities include:
1. Analyzing financial statements
2. Evaluating investment opportunities
3. Assessing risk factors
4. Providing financial forecasts
5. Recommending financial strategies""",
model_name="claude-3-sonnet-20240229",
max_loops=1,
streaming_on=True,
temperature=0.7,
)
# Initialize technical analyst agent
technical_analyst = Agent(
agent_name="Technical-Analyst",
system_prompt="""You are a technical analysis specialist. Your focus areas include:
1. Analyzing price patterns and trends
2. Evaluating technical indicators
3. Identifying support and resistance levels
4. Assessing market momentum
5. Providing trading recommendations""",
model_name="claude-3-sonnet-20240229",
max_loops=1,
temperature=0.7,
streaming_on=True,
)
# Create list of agents
agents = [market_researcher, financial_analyst, technical_analyst]
router = SequentialWorkflow(
name="market-analysis-router",
agents=agents,
max_loops=1,
# output_type="all",
)
result = router.run(
"Analyze Tesla (TSLA) stock from market, financial, and technical perspectives"
)
print(result)

@ -0,0 +1,68 @@
from swarms import Agent, SwarmRouter
# Initialize market research agent
market_researcher = Agent(
agent_name="Market-Researcher",
system_prompt="""You are a market research specialist. Your tasks include:
1. Analyzing market trends and patterns
2. Identifying market opportunities and threats
3. Evaluating competitor strategies
4. Assessing customer needs and preferences
5. Providing actionable market insights""",
model_name="claude-sonnet-4-20250514",
max_loops=1,
)
# Initialize financial analyst agent
financial_analyst = Agent(
agent_name="Financial-Analyst",
system_prompt="""You are a financial analysis expert. Your responsibilities include:
1. Analyzing financial statements
2. Evaluating investment opportunities
3. Assessing risk factors
4. Providing financial forecasts
5. Recommending financial strategies""",
model_name="claude-sonnet-4-20250514",
max_loops=1,
)
# Initialize technical analyst agent
technical_analyst = Agent(
agent_name="Technical-Analyst",
system_prompt="""You are a technical analysis specialist. Your focus areas include:
1. Analyzing price patterns and trends
2. Evaluating technical indicators
3. Identifying support and resistance levels
4. Assessing market momentum
5. Providing trading recommendations""",
model_name="claude-sonnet-4-20250514",
max_loops=1,
)
# Create list of agents
agents = [market_researcher, financial_analyst, technical_analyst]
# # Initialize the concurrent workflow
# workflow = ConcurrentWorkflow(
# name="market-analysis-workflow",
# agents=agents,
# max_loops=1,
# )
# # Run the workflow
# result = workflow.run(
# "Analyze Tesla (TSLA) stock from market, financial, and technical perspectives"
# )
router = SwarmRouter(
name="market-analysis-router",
swarm_type="ConcurrentWorkflow",
agents=agents,
max_loops=1,
# output_type="all",
)
result = router.run(
"Analyze Tesla (TSLA) stock from market, financial, and technical perspectives"
)
print(result)

@ -73,7 +73,12 @@ class ReasoningAgentRouter:
self.num_knowledge_items = num_knowledge_items
self.memory_capacity = memory_capacity
# Initialize agent factory mapping dictionary
# Added: Initialize the factory mapping dictionary
self._initialize_agent_factories()
@ -87,18 +92,17 @@ class ReasoningAgentRouter:
"reasoning-duo": self._create_reasoning_duo,
"reasoning-agent": self._create_reasoning_duo,
# SelfConsistencyAgent factory method
# SelfConsistencyAgent factory methods
"self-consistency": self._create_consistency_agent,
"consistency-agent": self._create_consistency_agent,
# IREAgent factory methods
# IREAgent factory method
"ire": self._create_ire_agent,
"ire-agent": self._create_ire_agent,
# Other agent type factory methods
"AgentJudge": self._create_agent_judge,
"ReflexionAgent": self._create_reflexion_agent,
"GKPAgent": self._create_gkp_agent
"GKPAgent": self._create_gkp_agent,
}
@ -195,6 +199,7 @@ class ReasoningAgentRouter:
Returns:
The selected reasoning swarm instance.
"""
# Generate cache key
cache_key = self._get_cache_key()
@ -202,6 +207,7 @@ class ReasoningAgentRouter:
if cache_key in self.__class__._agent_cache:
return self.__class__._agent_cache[cache_key]
try:
# Use the factory method to create a new instance
agent = self.agent_factories[self.swarm_type]()
@ -249,6 +255,7 @@ class ReasoningAgentRouter:
return results
@classmethod
def clear_cache(cls):
"""
@ -256,3 +263,5 @@ class ReasoningAgentRouter:
Use this when you need to free memory or force the creation of new instances.
"""
cls._agent_cache.clear()

@ -7,7 +7,6 @@ from swarms.utils.loguru_logger import initialize_logger
from swarms.telemetry.main import (
capture_system_data,
log_agent_data,
)
@ -34,7 +33,6 @@ class OnboardingProcess:
cache_save_path (str): The path where user data is cached for reliability.
"""
self.user_data: Dict[str, str] = {}
self.system_data: Dict[str, str] = capture_system_data()
self.auto_save_path = auto_save_path
self.cache_save_path = cache_save_path
self.load_existing_data()
@ -85,7 +83,7 @@ class OnboardingProcess:
while attempt < retry_attempts:
try:
combined_data = {**self.user_data, **self.system_data}
combined_data = {**self.user_data}
log_agent_data(combined_data)
return # Exit the function if saving was successful
except Exception as e:

@ -0,0 +1,177 @@
def get_multi_agent_collaboration_prompt_one(agents_in_swarm: str):
MULTI_AGENT_COLLABORATION_PROMPT_ONE = f"""
You are all operating within a multi-agent collaborative system. Your primary objectives are to work effectively with other agents to achieve shared goals while maintaining high reliability and avoiding common failure modes that plague multi-agent systems.
{agents_in_swarm}
## Fundamental Collaboration Principles
### 1. Role Adherence & Boundaries
- **STRICTLY adhere to your designated role and responsibilities** - never assume another agent's role or make decisions outside your scope
- If you encounter tasks outside your role, explicitly redirect to the appropriate agent
- Maintain clear hierarchical differentiation - respect the authority structure and escalation paths
- When uncertain about role boundaries, ask for clarification rather than assuming
### 2. Communication Excellence
- **Always ask for clarification** when instructions, data, or context are unclear, incomplete, or ambiguous
- Share ALL relevant information that could impact other agents' decision-making - never withhold critical details
- Use structured, explicit communication rather than assuming others understand implicit meanings
- Acknowledge and explicitly reference other agents' inputs before proceeding
- Use consistent terminology and avoid jargon that may cause misunderstanding
### 3. Task Specification Compliance
- **Rigorously adhere to task specifications** - review and confirm understanding of requirements before proceeding
- Flag any constraints or requirements that seem impossible or conflicting
- Document assumptions explicitly and seek validation
- Never modify requirements without explicit approval from appropriate authority
## Critical Failure Prevention Protocols
### Specification & Design Failures Prevention
- Before starting any task, restate your understanding of the requirements and constraints
- Maintain awareness of conversation history - reference previous exchanges when relevant
- Avoid unnecessary repetition of completed steps unless explicitly requested
- Clearly understand termination conditions for your tasks and the overall workflow
### Inter-Agent Misalignment Prevention
- **Never reset or restart conversations** without explicit instruction from a supervising agent
- When another agent provides input, explicitly acknowledge it and explain how it affects your approach
- Stay focused on the original task objective - if you notice drift, flag it immediately
- Match your reasoning process with your actions - explain discrepancies when they occur
### Verification & Termination Excellence
- **Implement robust verification** of your outputs before declaring tasks complete
- Never terminate prematurely - ensure all objectives are met and verified
- When reviewing others' work, provide thorough, accurate verification
- Use multiple verification approaches when possible (logical check, constraint validation, edge case testing)
## Operational Guidelines
### Communication Protocol
1. **State Check**: Begin interactions by confirming your understanding of the current state and context
2. **Role Confirmation**: Clearly identify your role and the roles of agents you're interacting with
3. **Objective Alignment**: Confirm shared understanding of immediate objectives
4. **Information Exchange**: Share relevant information completely and request missing information explicitly
5. **Action Coordination**: Coordinate actions to avoid conflicts and ensure complementary efforts
6. **Verification**: Verify outcomes and seek validation when appropriate
7. **Status Update**: Clearly communicate task status and next steps
### When Interacting with Other Agents
- **Listen actively**: Process and acknowledge their inputs completely
- **Seek clarification**: Ask specific questions when anything is unclear
- **Share context**: Provide relevant background information that informs your perspective
- **Coordinate actions**: Ensure your actions complement rather than conflict with others
- **Respect expertise**: Defer to agents with specialized knowledge in their domains
### Quality Assurance
- Before finalizing any output, perform self-verification using these checks:
- Does this meet all specified requirements?
- Are there any edge cases or constraints I haven't considered?
- Is this consistent with information provided by other agents?
- Have I clearly communicated my reasoning and any assumptions?
### Error Recovery
- If you detect an error or inconsistency, immediately flag it and propose correction
- When receiving feedback about errors, acknowledge the feedback and explain your correction approach
- Learn from failures by explicitly identifying what went wrong and how to prevent recurrence
## Interaction Patterns
### When Starting a New Task
```
1. Acknowledge the task assignment
2. Confirm role boundaries and responsibilities
3. Identify required inputs and information sources
4. State assumptions and seek validation
5. Outline approach and request feedback
6. Proceed with execution while maintaining communication
```
### When Collaborating with Peers
```
1. Establish communication channel and protocols
2. Share relevant context and constraints
3. Coordinate approaches to avoid duplication or conflicts
4. Maintain regular status updates
5. Verify integrated outputs collectively
```
### When Escalating Issues
```
1. Clearly describe the issue and its implications
2. Provide relevant context and attempted solutions
3. Specify what type of resolution or guidance is needed
4. Suggest next steps if appropriate
```
## Termination Criteria
Only consider a task complete when:
- All specified requirements have been met and verified
- Other agents have confirmed their portions are complete (if applicable)
- Quality checks have been performed and passed
- Appropriate verification has been conducted
- Clear communication of completion has been provided
## Meta-Awareness
Continuously monitor for these common failure patterns and actively work to prevent them:
- Role boundary violations
- Information withholding
- Premature termination
- Inadequate verification
- Communication breakdowns
- Task derailment
Remember: The goal is not just individual success, but collective success through reliable, high-quality collaboration that builds trust and produces superior outcomes.
"""
return MULTI_AGENT_COLLABORATION_PROMPT_ONE
MULTI_AGENT_COLLABORATION_PROMPT_TWO = """
# Compact Multi-Agent Collaboration Prompt
## Core Directives
You are an AI agent in a multi-agent system. Follow these essential collaboration protocols:
### Role & Boundaries
- **Stay in your designated role** - never assume another agent's responsibilities
- When tasks fall outside your scope, redirect to the appropriate agent
- Respect hierarchy and authority structures
### Communication Requirements
- **Always ask for clarification** when anything is unclear or incomplete
- **Share all relevant information** - never withhold details that could impact others
- **Acknowledge other agents' inputs** explicitly before proceeding
- Use clear, structured communication
### Task Execution
- **Confirm task requirements** before starting - restate your understanding
- **Adhere strictly to specifications** - flag conflicts or impossibilities
- **Maintain conversation context** - reference previous exchanges when relevant
- **Verify your work thoroughly** before declaring completion
### Collaboration Protocol
1. **State Check**: Confirm current context and your role
2. **Clarify**: Ask specific questions about unclear elements
3. **Coordinate**: Align actions with other agents to avoid conflicts
4. **Verify**: Check outputs meet requirements and constraints
5. **Communicate**: Clearly report status and next steps
### Termination Criteria
Only mark tasks complete when:
- All requirements verified as met
- Quality checks passed
- Other agents confirm their portions (if applicable)
- Clear completion communication provided
### Failure Prevention
Actively watch for and prevent:
- Role boundary violations
- Information withholding
- Premature task termination
- Inadequate verification
- Task objective drift
**Remember**: Success requires reliable collaboration, not just individual performance.
"""

@ -83,7 +83,13 @@ from swarms.structs.swarming_architectures import (
staircase_swarm,
star_swarm,
)
from swarms.structs.interactive_groupchat import InteractiveGroupChat
from swarms.structs.interactive_groupchat import (
InteractiveGroupChat,
round_robin_speaker,
random_speaker,
priority_speaker,
random_dynamic_speaker,
)
__all__ = [
"Agent",
@ -156,4 +162,8 @@ __all__ = [
"find_agent_by_name",
"run_agent",
"InteractiveGroupChat",
"round_robin_speaker",
"random_speaker",
"priority_speaker",
"random_dynamic_speaker",
]

File diff suppressed because it is too large Load Diff

@ -1,13 +1,10 @@
import concurrent.futures
import os
import time
from concurrent.futures import ThreadPoolExecutor
from functools import lru_cache
from typing import Any, Callable, Dict, List, Optional, Union
from typing import Callable, List, Optional, Union
from swarms.structs.agent import Agent
from swarms.structs.base_swarm import BaseSwarm
from swarms.structs.conversation import Conversation
from swarms.utils.formatter import formatter
from swarms.utils.history_output_formatter import (
history_output_formatter,
)
@ -35,9 +32,7 @@ class ConcurrentWorkflow(BaseSwarm):
return_str_on (bool): Flag indicating whether to return the output as a string. Defaults to False.
auto_generate_prompts (bool): Flag indicating whether to auto-generate prompts for agents. Defaults to False.
return_entire_history (bool): Flag indicating whether to return the entire conversation history. Defaults to False.
cache_size (int): The size of the cache. Defaults to 100.
max_retries (int): The maximum number of retry attempts. Defaults to 3.
retry_delay (float): The delay between retry attempts in seconds. Defaults to 1.0.
Raises:
ValueError: If the list of agents is empty or if the description is empty.
@ -50,13 +45,7 @@ class ConcurrentWorkflow(BaseSwarm):
auto_save (bool): Flag indicating whether to automatically save the metadata.
output_type (str): The type of output format.
max_loops (int): The maximum number of loops for each agent.
return_str_on (bool): Flag indicating whether to return the output as a string.
auto_generate_prompts (bool): Flag indicating whether to auto-generate prompts for agents.
return_entire_history (bool): Flag indicating whether to return the entire conversation history.
cache_size (int): The size of the cache.
max_retries (int): The maximum number of retry attempts.
retry_delay (float): The delay between retry attempts in seconds.
_cache (dict): The cache for storing agent outputs.
"""
def __init__(
@ -68,12 +57,7 @@ class ConcurrentWorkflow(BaseSwarm):
auto_save: bool = True,
output_type: str = "dict-all-except-first",
max_loops: int = 1,
return_str_on: bool = False,
auto_generate_prompts: bool = False,
return_entire_history: bool = False,
cache_size: int = 100,
max_retries: int = 3,
retry_delay: float = 1.0,
*args,
**kwargs,
):
@ -90,63 +74,31 @@ class ConcurrentWorkflow(BaseSwarm):
self.metadata_output_path = metadata_output_path
self.auto_save = auto_save
self.max_loops = max_loops
self.return_str_on = return_str_on
self.auto_generate_prompts = auto_generate_prompts
self.max_workers = os.cpu_count()
self.output_type = output_type
self.return_entire_history = return_entire_history
self.tasks = [] # Initialize tasks list
self.cache_size = cache_size
self.max_retries = max_retries
self.retry_delay = retry_delay
self._cache = {}
self.reliability_check()
self.conversation = Conversation()
def reliability_check(self):
try:
formatter.print_panel(
content=f"\n 🏷️ Name: {self.name}\n 📝 Description: {self.description}\n 🤖 Agents: {len(self.agents)}\n 🔄 Max Loops: {self.max_loops}\n ",
title="⚙️ Concurrent Workflow Settings",
style="bold blue",
)
formatter.print_panel(
content="🔍 Starting reliability checks",
title="🔒 Reliability Checks",
style="bold blue",
)
if self.name is None:
logger.error("❌ A name is required for the swarm")
if self.agents is None:
raise ValueError(
"❌ A name is required for the swarm"
"ConcurrentWorkflow: No agents provided"
)
if not self.agents or len(self.agents) <= 1:
logger.error(
"❌ The list of agents must not be empty."
)
if len(self.agents) == 0:
raise ValueError(
"❌ The list of agents must not be empty."
"ConcurrentWorkflow: No agents provided"
)
if not self.description:
logger.error("❌ A description is required.")
raise ValueError("❌ A description is required.")
formatter.print_panel(
content="✅ Reliability checks completed successfully",
title="🎉 Reliability Checks",
style="bold green",
if len(self.agents) == 1:
logger.warning(
"ConcurrentWorkflow: Only one agent provided. With ConcurrentWorkflow, you should use at least 2+ agents."
)
except ValueError as e:
logger.error(f"❌ Reliability check failed: {e}")
raise
except Exception as e:
logger.error(
f"💥 An unexpected error occurred during reliability checks: {e}"
f"ConcurrentWorkflow: Reliability check failed: {e}"
)
raise
@ -163,162 +115,84 @@ class ConcurrentWorkflow(BaseSwarm):
for agent in self.agents:
agent.auto_generate_prompt = True
@lru_cache(maxsize=100)
def _cached_run(self, task: str, agent_id: int) -> Any:
"""Cached version of agent execution to avoid redundant computations"""
return self.agents[agent_id].run(task=task)
def _validate_input(self, task: str) -> bool:
"""Validate input task"""
if not isinstance(task, str):
raise ValueError("Task must be a string")
if not task.strip():
raise ValueError("Task cannot be empty")
return True
def _run_with_retry(
self, agent: Agent, task: str, img: str = None
) -> Any:
"""Run agent with retry mechanism"""
for attempt in range(self.max_retries):
try:
output = agent.run(task=task, img=img)
self.conversation.add(agent.agent_name, output)
return output
except Exception as e:
if attempt == self.max_retries - 1:
logger.error(
f"Error running agent {agent.agent_name} after {self.max_retries} attempts: {e}"
)
raise
logger.warning(
f"Attempt {attempt + 1} failed for agent {agent.agent_name}: {e}"
)
time.sleep(
self.retry_delay * (attempt + 1)
) # Exponential backoff
def _process_agent(
self, agent: Agent, task: str, img: str = None
) -> Any:
def run(
self,
task: str,
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
):
"""
Process a single agent with caching and error handling.
Executes all agents in the workflow concurrently on the given task.
Args:
agent: The agent to process
task: Task to execute
img: Optional image input
task (str): The task to be executed by all agents.
img (Optional[str]): Optional image path for agents that support image input.
imgs (Optional[List[str]]): Optional list of image paths for agents that support multiple image inputs.
Returns:
The agent's output
"""
try:
# Fast path - check cache first
cache_key = f"{task}_{agent.agent_name}"
if cache_key in self._cache:
output = self._cache[cache_key]
else:
# Slow path - run agent and update cache
output = self._run_with_retry(agent, task, img)
if len(self._cache) >= self.cache_size:
self._cache.pop(next(iter(self._cache)))
self._cache[cache_key] = output
The formatted output based on the configured output_type.
return output
except Exception as e:
logger.error(
f"Error running agent {agent.agent_name}: {e}"
)
raise
def _run(
self, task: str, img: str = None, *args, **kwargs
) -> Union[Dict[str, Any], str]:
"""
Enhanced run method with parallel execution.
Example:
>>> workflow = ConcurrentWorkflow(agents=[agent1, agent2])
>>> result = workflow.run("Analyze this financial data")
>>> print(result)
"""
# Fast validation
self._validate_input(task)
self.conversation.add("User", task)
self.conversation.add(role="User", content=task)
try:
# Parallel execution with optimized thread pool
with ThreadPoolExecutor(
max_workers=self.max_workers
# Use 95% of available CPU cores for optimal performance
max_workers = int(os.cpu_count() * 0.95)
# Run agents concurrently using ThreadPoolExecutor
with concurrent.futures.ThreadPoolExecutor(
max_workers=max_workers
) as executor:
futures = [
# Submit all agent tasks and store with their index
future_to_agent = {
executor.submit(
self._process_agent, agent, task, img
)
agent.run, task=task, img=img, imgs=imgs
): agent
for agent in self.agents
]
# Wait for all futures to complete
for future in futures:
future.result()
}
except Exception as e:
logger.error(f"An error occurred during execution: {e}")
raise e
# Collect results and add to conversation in completion order
for future in concurrent.futures.as_completed(
future_to_agent
):
agent = future_to_agent[future]
output = future.result()
self.conversation.add(role=agent.name, content=output)
return history_output_formatter(
self.conversation,
conversation=self.conversation,
type=self.output_type,
)
def run(
def batch_run(
self,
task: Optional[str] = None,
tasks: List[str],
img: Optional[str] = None,
*args,
**kwargs,
) -> Any:
imgs: Optional[List[str]] = None,
):
"""
Executes the agent's run method with parallel execution.
Executes the workflow on multiple tasks sequentially.
Args:
task (Optional[str], optional): The task to be executed. Defaults to None.
img (Optional[str], optional): The image to be processed. Defaults to None.
*args: Additional positional arguments to be passed to the execution method.
**kwargs: Additional keyword arguments to be passed to the execution method.
tasks (List[str]): List of tasks to be executed by all agents.
img (Optional[str]): Optional image path for agents that support image input.
imgs (Optional[List[str]]): Optional list of image paths for agents that support multiple image inputs.
Returns:
Any: The result of the execution.
Raises:
ValueError: If task validation fails.
Exception: If any other error occurs during execution.
"""
if task is not None:
self.tasks.append(task)
try:
outputs = self._run(task, img, *args, **kwargs)
return outputs
except Exception as e:
logger.error(f"An error occurred during execution: {e}")
raise e
List of results, one for each task.
def run_batched(self, tasks: List[str]) -> Any:
"""
Enhanced batched execution
Example:
>>> workflow = ConcurrentWorkflow(agents=[agent1, agent2])
>>> tasks = ["Task 1", "Task 2", "Task 3"]
>>> results = workflow.batch_run(tasks)
>>> print(len(results)) # 3
"""
if not tasks:
raise ValueError("Tasks list cannot be empty")
return [self.run(task) for task in tasks]
def clear_cache(self):
"""Clear the task cache"""
self._cache.clear()
def get_cache_stats(self) -> Dict[str, int]:
"""Get cache statistics"""
return {
"cache_size": len(self._cache),
"max_cache_size": self.cache_size,
}
return [
self.run(task=task, img=img, imgs=imgs) for task in tasks
]
# if __name__ == "__main__":

@ -221,27 +221,6 @@ class Conversation(BaseStructure):
):
super().__init__()
# Support both 'provider' and 'backend' parameters for backwards compatibility
# 'backend' takes precedence if both are provided
self.backend = backend or provider
self.backend_instance = None
# Validate backend
valid_backends = [
"in-memory",
"mem0",
"supabase",
"redis",
"sqlite",
"duckdb",
"pulsar",
]
if self.backend not in valid_backends:
raise ValueError(
f"Invalid backend: '{self.backend}'. "
f"Valid backends are: {', '.join(valid_backends)}"
)
# Initialize all attributes first
self.id = id
self.name = name or id
@ -275,6 +254,27 @@ class Conversation(BaseStructure):
self.provider = provider # Keep for backwards compatibility
self.conversations_dir = conversations_dir
# Support both 'provider' and 'backend' parameters for backwards compatibility
# 'backend' takes precedence if both are provided
self.backend = backend or provider
self.backend_instance = None
# Validate backend
valid_backends = [
"in-memory",
"mem0",
"supabase",
"redis",
"sqlite",
"duckdb",
"pulsar",
]
if self.backend not in valid_backends:
raise ValueError(
f"Invalid backend: '{self.backend}'. "
f"Valid backends are: {', '.join(valid_backends)}"
)
# Initialize backend if using persistent storage
if self.backend in [
"supabase",
@ -484,8 +484,7 @@ class Conversation(BaseStructure):
self,
role: str,
content: Union[str, dict, list, Any],
*args,
**kwargs,
category: Optional[str] = None,
):
"""Add a message to the conversation history.
@ -505,6 +504,9 @@ class Conversation(BaseStructure):
if self.message_id_on:
message["message_id"] = str(uuid.uuid4())
if category:
message["category"] = category
# Add message to conversation history
self.conversation_history.append(message)
@ -520,6 +522,79 @@ class Conversation(BaseStructure):
f"Failed to autosave conversation: {str(e)}"
)
def export_and_count_categories(
self, tokenizer_model_name: Optional[str] = "gpt-4.1-mini"
) -> Dict[str, int]:
"""Export all messages with category 'input' and 'output' and count their tokens.
This method searches through the conversation history and:
1. Extracts all messages marked with category 'input' or 'output'
2. Concatenates the content of each category
3. Counts tokens for each category using the specified tokenizer model
Args:
tokenizer_model_name (str): Name of the model to use for tokenization
Returns:
Dict[str, int]: A dictionary containing:
- input_tokens: Number of tokens in input messages
- output_tokens: Number of tokens in output messages
- total_tokens: Total tokens across both categories
"""
try:
# Extract input and output messages
input_messages = []
output_messages = []
for message in self.conversation_history:
# Get message content and ensure it's a string
content = message.get("content", "")
if not isinstance(content, str):
content = str(content)
# Sort messages by category
category = message.get("category", "")
if category == "input":
input_messages.append(content)
elif category == "output":
output_messages.append(content)
# Join messages with spaces
all_input_text = " ".join(input_messages)
all_output_text = " ".join(output_messages)
print(all_input_text)
print(all_output_text)
# Count tokens only if there is text
input_tokens = (
count_tokens(all_input_text, tokenizer_model_name)
if all_input_text.strip()
else 0
)
output_tokens = (
count_tokens(all_output_text, tokenizer_model_name)
if all_output_text.strip()
else 0
)
total_tokens = input_tokens + output_tokens
return {
"input_tokens": input_tokens,
"output_tokens": output_tokens,
"total_tokens": total_tokens,
}
except Exception as e:
logger.error(
f"Error in export_and_count_categories: {str(e)}"
)
return {
"input_tokens": 0,
"output_tokens": 0,
"total_tokens": 0,
}
def add_mem0(
self,
role: str,
@ -546,8 +621,9 @@ class Conversation(BaseStructure):
def add(
self,
role: str,
content: Union[str, dict, list],
content: Union[str, dict, list, Any],
metadata: Optional[dict] = None,
category: Optional[str] = None,
):
"""Add a message to the conversation history."""
# If using a persistent backend, delegate to it
@ -562,7 +638,9 @@ class Conversation(BaseStructure):
)
return self.add_in_memory(role, content)
elif self.provider == "in-memory":
return self.add_in_memory(role, content)
return self.add_in_memory(
role=role, content=content, category=category
)
elif self.provider == "mem0":
return self.add_mem0(
role=role, content=content, metadata=metadata
@ -1326,6 +1404,12 @@ class Conversation(BaseStructure):
self.conversation_history[-1]["content"],
)
def return_list_final(self):
"""Return the final message as a list."""
return [
self.conversation_history[-1]["content"],
]
@classmethod
def list_conversations(
cls, conversations_dir: Optional[str] = None

@ -104,9 +104,7 @@ class AgentValidator:
model_name in model["model_name"]
for model in model_list
):
valid_models = [
model["model_name"] for model in model_list
]
[model["model_name"] for model in model_list]
raise AgentValidationError(
"Invalid model name. Must be one of the supported litellm models",
"model_name",

@ -1,5 +1,6 @@
import re
from typing import Callable, List, Union
import random
from typing import Callable, List, Union, Optional
from loguru import logger
@ -35,6 +36,156 @@ class InvalidTaskFormatError(InteractiveGroupChatError):
pass
class InvalidSpeakerFunctionError(InteractiveGroupChatError):
"""Raised when an invalid speaker function is provided"""
pass
# Built-in speaker functions
def round_robin_speaker(
agents: List[str], current_index: int = 0
) -> str:
"""
Round robin speaker function that cycles through agents in order.
Args:
agents: List of agent names
current_index: Current position in the cycle
Returns:
Next agent name in the round robin sequence
"""
if not agents:
raise ValueError("No agents provided for round robin")
return agents[current_index % len(agents)]
def random_speaker(agents: List[str], **kwargs) -> str:
"""
Random speaker function that selects agents randomly.
Args:
agents: List of agent names
**kwargs: Additional arguments (ignored)
Returns:
Randomly selected agent name
"""
if not agents:
raise ValueError("No agents provided for random selection")
return random.choice(agents)
def priority_speaker(
agents: List[str], priorities: dict, **kwargs
) -> str:
"""
Priority-based speaker function that selects agents based on priority weights.
Args:
agents: List of agent names
priorities: Dictionary mapping agent names to priority weights
**kwargs: Additional arguments (ignored)
Returns:
Selected agent name based on priority weights
"""
if not agents:
raise ValueError("No agents provided for priority selection")
# Filter agents that exist in the priorities dict
available_agents = [
agent for agent in agents if agent in priorities
]
if not available_agents:
# Fallback to random if no priorities match
return random.choice(agents)
# Calculate total weight
total_weight = sum(
priorities[agent] for agent in available_agents
)
if total_weight == 0:
return random.choice(available_agents)
# Select based on weighted probability
rand_val = random.uniform(0, total_weight)
current_weight = 0
for agent in available_agents:
current_weight += priorities[agent]
if rand_val <= current_weight:
return agent
return available_agents[-1] # Fallback
def random_dynamic_speaker(
agents: List[str],
response: str = "",
strategy: str = "parallel",
**kwargs,
) -> Union[str, List[str]]:
"""
Random dynamic speaker function that selects agents based on @mentions in responses.
This function works in two phases:
1. If no response is provided (first call), randomly selects an agent
2. If a response is provided, extracts @mentions and returns agent(s) based on strategy
Args:
agents: List of available agent names
response: The response from the previous agent (may contain @mentions)
strategy: How to handle multiple mentions - "sequential" or "parallel"
**kwargs: Additional arguments (ignored)
Returns:
For sequential strategy: str (single agent name)
For parallel strategy: List[str] (list of agent names)
"""
if not agents:
raise ValueError(
"No agents provided for random dynamic selection"
)
# If no response provided, randomly select first agent
if not response:
return random.choice(agents)
# Extract @mentions from the response
mentions = re.findall(r"@(\w+)", response)
# Filter mentions to only include valid agents
valid_mentions = [
mention for mention in mentions if mention in agents
]
if not valid_mentions:
# If no valid mentions, randomly select from all agents
return random.choice(agents)
# Handle multiple mentions based on strategy
if strategy == "sequential":
# Return the first mentioned agent for sequential execution
return valid_mentions[0]
elif strategy == "parallel":
# Return all mentioned agents for parallel execution
return valid_mentions
else:
raise ValueError(
f"Invalid strategy: {strategy}. Must be 'sequential' or 'parallel'"
)
speaker_functions = {
"round-robin-speaker": round_robin_speaker,
"random-speaker": random_speaker,
"priority-speaker": priority_speaker,
"random-dynamic-speaker": random_dynamic_speaker,
}
class InteractiveGroupChat:
"""
An interactive group chat system that enables conversations with multiple agents using @mentions.
@ -49,6 +200,8 @@ class InteractiveGroupChat:
max_loops (int): Maximum number of conversation turns
conversation (Conversation): Stores the chat history
agent_map (Dict[str, Union[Agent, Callable]]): Mapping of agent names to their instances
speaker_function (Callable): Function to determine speaking order
speaker_state (dict): State for speaker functions that need it
Args:
name (str, optional): Name of the group chat. Defaults to "InteractiveGroupChat".
@ -57,9 +210,38 @@ class InteractiveGroupChat:
max_loops (int, optional): Maximum conversation turns. Defaults to 1.
output_type (str, optional): Type of output format. Defaults to "string".
interactive (bool, optional): Whether to enable interactive terminal mode. Defaults to False.
speaker_function (Union[str, Callable], optional): Function to determine speaking order. Can be:
- A string name: "round-robin-speaker", "random-speaker", "priority-speaker", "random-dynamic-speaker"
- A custom callable function
- None (defaults to round_robin_speaker)
speaker_state (dict, optional): Initial state for speaker function. Defaults to empty dict.
Raises:
ValueError: If invalid initialization parameters are provided
InvalidSpeakerFunctionError: If the speaker function is invalid
Examples:
# Initialize with string-based speaker function
group_chat = InteractiveGroupChat(
agents=[agent1, agent2, agent3],
speaker_function="random-speaker"
)
# Initialize with priority speaker function
group_chat = InteractiveGroupChat(
agents=[agent1, agent2, agent3],
speaker_function="priority-speaker",
speaker_state={"priorities": {"agent1": 3, "agent2": 2, "agent3": 1}}
)
# Initialize with dynamic speaker function (agents mention each other)
group_chat = InteractiveGroupChat(
agents=[agent1, agent2, agent3],
speaker_function="random-dynamic-speaker"
)
# Change speaker function during runtime
group_chat.set_speaker_function("round-robin-speaker")
"""
def __init__(
@ -71,6 +253,8 @@ class InteractiveGroupChat:
max_loops: int = 1,
output_type: str = "string",
interactive: bool = False,
speaker_function: Optional[Union[str, Callable]] = None,
speaker_state: Optional[dict] = None,
):
self.id = id
self.name = name
@ -80,6 +264,33 @@ class InteractiveGroupChat:
self.output_type = output_type
self.interactive = interactive
# Speaker function configuration
if speaker_function is None:
self.speaker_function = round_robin_speaker
elif isinstance(speaker_function, str):
if speaker_function not in speaker_functions:
available_functions = ", ".join(
speaker_functions.keys()
)
raise InvalidSpeakerFunctionError(
f"Invalid speaker function: '{speaker_function}'. "
f"Available functions: {available_functions}"
)
self.speaker_function = speaker_functions[
speaker_function
]
elif callable(speaker_function):
self.speaker_function = speaker_function
else:
raise InvalidSpeakerFunctionError(
"Speaker function must be either a string, callable, or None"
)
self.speaker_state = speaker_state or {"current_index": 0}
# Validate speaker function
self._validate_speaker_function()
# Initialize conversation history
self.conversation = Conversation(time_enabled=True)
@ -96,6 +307,256 @@ class InteractiveGroupChat:
self._setup_conversation_context()
self._update_agent_prompts()
def set_speaker_function(
self,
speaker_function: Union[str, Callable],
speaker_state: Optional[dict] = None,
) -> None:
"""
Set the speaker function using either a string name or a custom callable.
Args:
speaker_function: Either a string name of a predefined function or a custom callable
String options:
- "round-robin-speaker": Cycles through agents in order
- "random-speaker": Selects agents randomly
- "priority-speaker": Selects based on priority weights
- "random-dynamic-speaker": Randomly selects first agent, then follows @mentions in responses
Callable: Custom function that takes (agents: List[str], **kwargs) -> str
speaker_state: Optional state for the speaker function
Raises:
InvalidSpeakerFunctionError: If the speaker function is invalid
"""
if isinstance(speaker_function, str):
# Handle string-based speaker function
if speaker_function not in speaker_functions:
available_functions = ", ".join(
speaker_functions.keys()
)
raise InvalidSpeakerFunctionError(
f"Invalid speaker function: '{speaker_function}'. "
f"Available functions: {available_functions}"
)
self.speaker_function = speaker_functions[
speaker_function
]
logger.info(
f"Speaker function set to: {speaker_function}"
)
elif callable(speaker_function):
# Handle callable speaker function
self.speaker_function = speaker_function
logger.info(
f"Custom speaker function set to: {speaker_function.__name__}"
)
else:
raise InvalidSpeakerFunctionError(
"Speaker function must be either a string or a callable"
)
# Update speaker state if provided
if speaker_state:
self.speaker_state.update(speaker_state)
# Validate the speaker function
self._validate_speaker_function()
def set_priorities(self, priorities: dict) -> None:
"""
Set agent priorities for priority-based speaking order.
Args:
priorities: Dictionary mapping agent names to priority weights
"""
self.speaker_state["priorities"] = priorities
logger.info(f"Agent priorities set: {priorities}")
def get_available_speaker_functions(self) -> List[str]:
"""
Get a list of available speaker function names.
Returns:
List[str]: List of available speaker function names
"""
return list(speaker_functions.keys())
def get_current_speaker_function(self) -> str:
"""
Get the name of the current speaker function.
Returns:
str: Name of the current speaker function, or "custom" if it's a custom function
"""
for name, func in speaker_functions.items():
if self.speaker_function == func:
return name
return "custom"
def start_interactive_session(self):
"""
Start an interactive terminal session for chatting with agents.
This method creates a REPL (Read-Eval-Print Loop) that allows users to:
- Chat with agents using @mentions
- See available agents and their descriptions
- Exit the session using 'exit' or 'quit'
- Get help using 'help' or '?'
"""
if not self.interactive:
raise InteractiveGroupChatError(
"Interactive mode is not enabled. Initialize with interactive=True"
)
print(f"\nWelcome to {self.name}!")
print(f"Description: {self.description}")
print(
f"Current speaker function: {self.get_current_speaker_function()}"
)
print("\nAvailable agents:")
for name, agent in self.agent_map.items():
if isinstance(agent, Agent):
print(
f"- @{name}: {agent.system_prompt.splitlines()[0]}"
)
else:
print(f"- @{name}: Custom callable function")
print("\nCommands:")
print("- Type 'help' or '?' for help")
print("- Type 'exit' or 'quit' to end the session")
print("- Type 'speaker' to change speaker function")
print("- Use @agent_name to mention agents")
print("\nStart chatting:")
while True:
try:
# Get user input
user_input = input("\nYou: ").strip()
# Handle special commands
if user_input.lower() in ["exit", "quit"]:
print("Goodbye!")
break
if user_input.lower() in ["help", "?"]:
print("\nHelp:")
print("1. Mention agents using @agent_name")
print(
"2. You can mention multiple agents in one task"
)
print("3. Available agents:")
for name in self.agent_map:
print(f" - @{name}")
print(
"4. Type 'speaker' to change speaker function"
)
print(
"5. Type 'exit' or 'quit' to end the session"
)
continue
if user_input.lower() == "speaker":
print(
f"\nCurrent speaker function: {self.get_current_speaker_function()}"
)
print("Available speaker functions:")
for i, func_name in enumerate(
self.get_available_speaker_functions(), 1
):
print(f" {i}. {func_name}")
try:
choice = input(
"\nEnter the number or name of the speaker function: "
).strip()
# Try to parse as number first
try:
func_index = int(choice) - 1
if (
0
<= func_index
< len(
self.get_available_speaker_functions()
)
):
selected_func = self.get_available_speaker_functions()[
func_index
]
else:
print(
"Invalid number. Please try again."
)
continue
except ValueError:
# Try to parse as name
selected_func = choice
self.set_speaker_function(selected_func)
print(
f"Speaker function changed to: {self.get_current_speaker_function()}"
)
except InvalidSpeakerFunctionError as e:
print(f"Error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
continue
if not user_input:
continue
# Process the task and get responses
try:
self.run(user_input)
print("\nChat:")
# print(response)
except NoMentionedAgentsError:
print(
"\nError: Please mention at least one agent using @agent_name"
)
except AgentNotFoundError as e:
print(f"\nError: {str(e)}")
except Exception as e:
print(f"\nAn error occurred: {str(e)}")
except KeyboardInterrupt:
print("\nSession terminated by user. Goodbye!")
break
except Exception as e:
print(f"\nAn unexpected error occurred: {str(e)}")
print(
"The session will continue. You can type 'exit' to end it."
)
def _validate_speaker_function(self) -> None:
"""
Validates the speaker function.
Raises:
InvalidSpeakerFunctionError: If the speaker function is invalid
"""
if not callable(self.speaker_function):
raise InvalidSpeakerFunctionError(
"Speaker function must be callable"
)
# Test the speaker function with a dummy list
try:
test_result = self.speaker_function(
["test_agent"], **self.speaker_state
)
if not isinstance(test_result, str):
raise InvalidSpeakerFunctionError(
"Speaker function must return a string"
)
except Exception as e:
raise InvalidSpeakerFunctionError(
f"Speaker function validation failed: {e}"
)
def _validate_initialization(self) -> None:
"""
Validates the group chat configuration.
@ -150,6 +611,27 @@ class InteractiveGroupChat:
}
)
# Create the enhanced prompt that teaches agents how to use @mentions
mention_instruction = """
IMPORTANT: You are part of a collaborative group chat where you can interact with other agents using @mentions.
-COLLABORATIVE RESPONSE PROTOCOL:
1. FIRST: Read and understand all previous responses from other agents
2. ACKNOWLEDGE: Reference and acknowledge what other agents have said
3. BUILD UPON: Add your perspective while building upon their insights
4. MENTION: Use @agent_name to call on other agents when needed
5. COMPLETE: Acknowledge when your part is done and what still needs to be done
HOW TO MENTION OTHER AGENTS:
- Use @agent_name to mention another agent in your response
- You can mention multiple agents: @agent1 @agent2
- When you mention an agent, they will be notified and can respond
- Example: "I think @analyst should review this data" or "Let's ask @researcher to investigate this further"
AVAILABLE AGENTS TO MENTION:
"""
group_context = (
f"\n\nYou are part of a group chat named '{self.name}' with the following description: {self.description}\n"
f"Other participants in this chat:\n"
@ -163,11 +645,49 @@ class InteractiveGroupChat:
for info in agent_info
if info["name"] != agent.agent_name
]
agent_context = group_context
agent_context = group_context + mention_instruction
for other in other_agents:
agent_context += (
f"- {other['name']}: {other['description']}\n"
)
agent_context += f"- @{other['name']}: {other['description']}\n"
# Add final instruction
agent_context += """
COLLABORATION GUIDELINES:
- ALWAYS read the full conversation history before responding
- ACKNOWLEDGE other agents' contributions: "Building on @analyst's data insights..." or "I agree with @researcher's findings that..."
- BUILD UPON previous responses rather than repeating information
- SYNTHESIZE multiple perspectives when possible
- ASK CLARIFYING QUESTIONS if you need more information from other agents
- DELEGATE appropriately: "Let me ask @expert_agent to verify this" or "@specialist, can you elaborate on this point?"
TASK COMPLETION GUIDELINES:
- ACKNOWLEDGE when you are done with your part of the task
- CLEARLY STATE what still needs to be done before the overall task is finished
- If you mention other agents, explain what specific input you need from them
- Use phrases like "I have completed [specific part]" or "The task still requires [specific actions]"
- Provide a clear status update: "My analysis is complete. The task now needs @writer to create content and @reviewer to validate the approach."
RESPONSE STRUCTURE:
1. ACKNOWLEDGE: "I've reviewed the responses from @agent1 and @agent2..."
2. BUILD: "Building on @agent1's analysis of the data..."
3. CONTRIBUTE: "From my perspective, I would add..."
4. COLLABORATE: "To get a complete picture, let me ask @agent3 to..."
5. COMPLETE: "I have completed [my part]. The task still requires [specific next steps]"
6. SYNTHESIZE: "Combining our insights, the key findings are..."
EXAMPLES OF GOOD COLLABORATION:
- "I've reviewed @analyst's data analysis and @researcher's market insights. The data shows strong growth potential, and I agree with @researcher that we should focus on emerging markets. Let me add that from a content perspective, we should @writer to create targeted messaging for these markets. I have completed my market analysis. The task now requires @writer to develop content and @reviewer to validate our approach."
- "Building on @researcher's findings about customer behavior, I can see that @analyst's data supports this trend. To get a complete understanding, let me ask @writer to help us craft messaging that addresses these specific customer needs. My data analysis is complete. The task still needs @writer to create messaging and @reviewer to approve the final strategy."
AVOID:
- Ignoring other agents' responses
- Repeating what others have already said
- Making assumptions without consulting relevant experts
- Responding in isolation without considering the group's collective knowledge
- Not acknowledging task completion status
Remember: You are part of a team. Your response should reflect that you've read, understood, and are building upon the contributions of others, and clearly communicate your task completion status.
"""
# Update the agent's system prompt
agent.system_prompt = (
@ -202,90 +722,100 @@ class InteractiveGroupChat:
logger.error(f"Error extracting mentions: {e}")
raise InvalidTaskFormatError(f"Invalid task format: {e}")
def start_interactive_session(self):
def _get_speaking_order(
self, mentioned_agents: List[str]
) -> List[str]:
"""
Start an interactive terminal session for chatting with agents.
Determines the speaking order using the configured speaker function.
This method creates a REPL (Read-Eval-Print Loop) that allows users to:
- Chat with agents using @mentions
- See available agents and their descriptions
- Exit the session using 'exit' or 'quit'
- Get help using 'help' or '?'
Args:
mentioned_agents: List of agent names that were mentioned
Returns:
List of agent names in the order they should speak
"""
if not self.interactive:
raise InteractiveGroupChatError(
"Interactive mode is not enabled. Initialize with interactive=True"
if not mentioned_agents:
return []
# Use the speaker function to determine order
try:
if self.speaker_function == round_robin_speaker:
# For round robin, we need to maintain state
current_index = self.speaker_state.get(
"current_index", 0
)
ordered_agents = []
print(f"\nWelcome to {self.name}!")
print(f"Description: {self.description}")
print("\nAvailable agents:")
for name, agent in self.agent_map.items():
if isinstance(agent, Agent):
print(
f"- @{name}: {agent.system_prompt.splitlines()[0]}"
# Create the order starting from current index
for i in range(len(mentioned_agents)):
agent = round_robin_speaker(
mentioned_agents, current_index + i
)
else:
print(f"- @{name}: Custom callable function")
ordered_agents.append(agent)
print("\nCommands:")
print("- Type 'help' or '?' for help")
print("- Type 'exit' or 'quit' to end the session")
print("- Use @agent_name to mention agents")
print("\nStart chatting:")
# Update state for next round
self.speaker_state["current_index"] = (
current_index + len(mentioned_agents)
) % len(mentioned_agents)
return ordered_agents
while True:
try:
# Get user input
user_input = input("\nYou: ").strip()
elif self.speaker_function == random_speaker:
# For random, shuffle the list
shuffled = mentioned_agents.copy()
random.shuffle(shuffled)
return shuffled
# Handle special commands
if user_input.lower() in ["exit", "quit"]:
print("Goodbye!")
break
elif self.speaker_function == priority_speaker:
# For priority, we need priorities in speaker_state
priorities = self.speaker_state.get("priorities", {})
if not priorities:
# Fallback to random if no priorities set
shuffled = mentioned_agents.copy()
random.shuffle(shuffled)
return shuffled
if user_input.lower() in ["help", "?"]:
print("\nHelp:")
print("1. Mention agents using @agent_name")
print(
"2. You can mention multiple agents in one task"
)
print("3. Available agents:")
for name in self.agent_map:
print(f" - @{name}")
print(
"4. Type 'exit' or 'quit' to end the session"
# Sort by priority (higher priority first)
sorted_agents = sorted(
mentioned_agents,
key=lambda x: priorities.get(x, 0),
reverse=True,
)
continue
if not user_input:
continue
return sorted_agents
# Process the task and get responses
try:
response = self.run(user_input)
print("\nChat:")
print(response)
elif self.speaker_function == random_dynamic_speaker:
# For dynamic speaker, we need to handle it differently
# The dynamic speaker will be called during the run method
# For now, just return the original order
return mentioned_agents
except NoMentionedAgentsError:
print(
"\nError: Please mention at least one agent using @agent_name"
else:
# Custom speaker function
# For custom functions, we'll use the first agent returned
# and then process the rest in original order
first_speaker = self.speaker_function(
mentioned_agents, **self.speaker_state
)
except AgentNotFoundError as e:
print(f"\nError: {str(e)}")
except Exception as e:
print(f"\nAn error occurred: {str(e)}")
if first_speaker in mentioned_agents:
remaining = [
agent
for agent in mentioned_agents
if agent != first_speaker
]
return [first_speaker] + remaining
else:
return mentioned_agents
except KeyboardInterrupt:
print("\nSession terminated by user. Goodbye!")
break
except Exception as e:
print(f"\nAn unexpected error occurred: {str(e)}")
print(
"The session will continue. You can type 'exit' to end it."
)
logger.error(f"Error in speaker function: {e}")
# Fallback to original order
return mentioned_agents
def run(self, task: str) -> str:
def run(
self,
task: str,
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
) -> str:
"""
Process a task and get responses from mentioned agents.
If interactive mode is enabled, this will be called by start_interactive_session().
@ -303,8 +833,158 @@ class InteractiveGroupChat:
# Add user task to conversation
self.conversation.add(role="User", content=task)
# Get responses from mentioned agents
for agent_name in mentioned_agents:
# Handle dynamic speaker function differently
if self.speaker_function == random_dynamic_speaker:
# Get strategy from speaker state (default to sequential)
strategy = self.speaker_state.get(
"strategy", "sequential"
)
# For dynamic speaker, we'll determine the next speaker after each response
# Track which agents have spoken to ensure all get a chance
spoken_agents = set()
last_response = ""
max_iterations = (
len(mentioned_agents) * 3
) # Allow more iterations for parallel
iteration = 0
while iteration < max_iterations and len(
spoken_agents
) < len(mentioned_agents):
# Determine next speaker(s) using dynamic function
next_speakers = self.speaker_function(
mentioned_agents, # Use all mentioned agents, not remaining_agents
last_response,
strategy=strategy,
**self.speaker_state,
)
# Handle both single agent and multiple agents
if isinstance(next_speakers, str):
next_speakers = [next_speakers]
# Filter out invalid agents
valid_next_speakers = [
agent
for agent in next_speakers
if agent in mentioned_agents
]
if not valid_next_speakers:
# If no valid mentions found, randomly select from unspoken agents
unspoken_agents = [
agent
for agent in mentioned_agents
if agent not in spoken_agents
]
if unspoken_agents:
valid_next_speakers = [
random.choice(unspoken_agents)
]
else:
# All agents have spoken, break the loop
break
# Process agents based on strategy
if strategy == "sequential":
# Process one agent at a time
for next_speaker in valid_next_speakers:
if next_speaker in spoken_agents:
continue # Skip if already spoken
response = self._get_agent_response(
next_speaker, img, imgs
)
if response:
last_response = response
spoken_agents.add(next_speaker)
break # Only process one agent in sequential mode
elif strategy == "parallel":
# Process all mentioned agents in parallel
import concurrent.futures
# Get responses from all valid agents
responses = []
with concurrent.futures.ThreadPoolExecutor() as executor:
future_to_agent = {
executor.submit(
self._get_agent_response,
agent,
img,
imgs,
): agent
for agent in valid_next_speakers
if agent not in spoken_agents
}
for (
future
) in concurrent.futures.as_completed(
future_to_agent
):
agent = future_to_agent[future]
try:
response = future.result()
if response:
responses.append(response)
spoken_agents.add(agent)
except Exception as e:
logger.error(
f"Error getting response from {agent}: {e}"
)
# Combine responses for next iteration
if responses:
last_response = "\n\n".join(responses)
iteration += 1
else:
# For non-dynamic speaker functions, use the original logic
speaking_order = self._get_speaking_order(
mentioned_agents
)
logger.info(
f"Speaking order determined: {speaking_order}"
)
# Get responses from mentioned agents in the determined order
for agent_name in speaking_order:
response = self._get_agent_response(
agent_name, img, imgs
)
return history_output_formatter(
self.conversation, self.output_type
)
except InteractiveGroupChatError as e:
logger.error(f"GroupChat error: {e}")
raise
except Exception as e:
logger.error(f"Unexpected error: {e}")
raise InteractiveGroupChatError(
f"Unexpected error occurred: {str(e)}"
)
def _get_agent_response(
self,
agent_name: str,
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
) -> Optional[str]:
"""
Get response from a specific agent.
Args:
agent_name: Name of the agent to get response from
img: Optional image for the task
imgs: Optional list of images for the task
Returns:
The agent's response or None if error
"""
agent = self.agent_map.get(agent_name)
if not agent:
raise AgentNotFoundError(
@ -313,14 +993,33 @@ class InteractiveGroupChat:
try:
# Get the complete conversation history
context = (
self.conversation.return_history_as_string()
)
context = self.conversation.return_history_as_string()
# Get response from agent
if isinstance(agent, Agent):
collaborative_task = f"""{context}
COLLABORATIVE TASK: Please respond to the latest task as {agent_name}.
IMPORTANT INSTRUCTIONS:
1. Read the ENTIRE conversation history above
2. Acknowledge what other agents have said before adding your perspective
3. Build upon their insights rather than repeating information
4. If you need input from other agents, mention them using @agent_name
5. Provide your unique expertise while showing you understand the group's collective knowledge
TASK COMPLETION GUIDELINES:
- Acknowledge when you are done with your part of the task
- Clearly state what still needs to be done before the overall task is finished
- If you mention other agents, explain what specific input you need from them
- Use phrases like "I have completed [specific part]" or "The task still requires [specific actions]"
Remember: You are part of a collaborative team. Your response should demonstrate that you've read, understood, and are building upon the contributions of others."""
response = agent.run(
task=f"{context}\nPlease respond to the latest task as {agent_name}."
task=collaborative_task,
img=img,
imgs=imgs,
)
else:
# For callable functions
@ -332,6 +1031,7 @@ class InteractiveGroupChat:
role=agent_name, content=response
)
logger.info(f"Agent {agent_name} responded")
return response
except Exception as e:
logger.error(
@ -341,16 +1041,23 @@ class InteractiveGroupChat:
role=agent_name,
content=f"Error: Unable to generate response - {str(e)}",
)
return f"Error: Unable to generate response - {str(e)}"
return history_output_formatter(
self.conversation, self.output_type
)
return None
except InteractiveGroupChatError as e:
logger.error(f"GroupChat error: {e}")
raise
except Exception as e:
logger.error(f"Unexpected error: {e}")
raise InteractiveGroupChatError(
f"Unexpected error occurred: {str(e)}"
def set_dynamic_strategy(self, strategy: str) -> None:
"""
Set the strategy for the random-dynamic-speaker function.
Args:
strategy: Either "sequential" or "parallel"
- "sequential": Process one agent at a time based on @mentions
- "parallel": Process all mentioned agents simultaneously
"""
if strategy not in ["sequential", "parallel"]:
raise ValueError(
"Strategy must be either 'sequential' or 'parallel'"
)
self.speaker_state["strategy"] = strategy
logger.info(f"Dynamic speaker strategy set to: {strategy}")

@ -1,12 +1,17 @@
from typing import List, Any, Optional, Union, Callable
import random
from swarms.prompts.collaborative_prompts import (
get_multi_agent_collaboration_prompt_one,
)
def list_all_agents(
agents: List[Union[Callable, Any]],
conversation: Optional[Any] = None,
name: str = "",
add_to_conversation: bool = False,
name: Optional[str] = None,
description: Optional[str] = None,
add_to_conversation: Optional[bool] = False,
add_collaboration_prompt: Optional[bool] = True,
) -> str:
"""Lists all agents in a swarm and optionally adds them to a conversation.
@ -27,6 +32,7 @@ def list_all_agents(
>>> conversation = Conversation()
>>> agent_info = list_all_agents(agents, conversation, "MySwarm")
>>> print(agent_info)
Swarm: MySwarm
Total Agents: 2
Agent: Agent1
@ -39,8 +45,15 @@ def list_all_agents(
# Compile information about all agents
total_agents = len(agents)
all_agents = f"Total Agents: {total_agents}\n\n" + "\n\n".join(
f"Agent: {agent.agent_name} \n\n Description: {agent.description or (agent.system_prompt[:50] + '...' if len(agent.system_prompt) > 50 else agent.system_prompt)}"
all_agents = f"Team Name: {name}\n" if name else ""
all_agents += (
f"Team Description: {description}\n" if description else ""
)
all_agents += f"Total Agents: {total_agents}\n\n"
all_agents += "| Agent | Description |\n"
all_agents += "|-------|-------------|\n"
all_agents += "\n".join(
f"| {agent.agent_name} | {agent.description or (agent.system_prompt[:50] + '...' if len(agent.system_prompt) > 50 else agent.system_prompt)} |"
for agent in agents
)
@ -48,9 +61,14 @@ def list_all_agents(
# Add the agent information to the conversation
conversation.add(
role="System",
content=f"All Agents Available in the Swarm {name}:\n\n{all_agents}",
content=all_agents,
)
if add_collaboration_prompt:
return get_multi_agent_collaboration_prompt_one(
agents_in_swarm=all_agents
)
else:
return all_agents
@ -68,6 +86,7 @@ models = [
"o4-mini",
"o3",
"gpt-4.1",
"groq/llama-3.1-8b-instant",
"gpt-4.1-nano",
]

@ -1,17 +1,18 @@
import asyncio
import os
from typing import List, Optional
from swarms.structs.agent import Agent
from swarms.prompts.ag_prompt import aggregator_system_prompt_main
from swarms.structs.ma_utils import list_all_agents
from swarms.utils.history_output_formatter import (
history_output_formatter,
)
from swarms.utils.loguru_logger import initialize_logger
import concurrent.futures
from swarms.utils.output_types import OutputType
from swarms.structs.conversation import Conversation
from swarms.utils.history_output_formatter import (
history_output_formatter,
)
logger = initialize_logger(log_folder="mixture_of_agents")
@ -25,13 +26,13 @@ class MixtureOfAgents:
self,
name: str = "MixtureOfAgents",
description: str = "A class to run a mixture of agents and aggregate their responses.",
agents: List[Agent] = [],
agents: List[Agent] = None,
aggregator_agent: Agent = None,
aggregator_system_prompt: str = aggregator_system_prompt_main,
layers: int = 3,
max_loops: int = 1,
return_str_on: bool = False,
output_type: OutputType = "dict",
output_type: OutputType = "final",
aggregator_model_name: str = "claude-3-5-sonnet-20240620",
) -> None:
"""
Initialize the Mixture of Agents class with agents and configuration.
@ -48,16 +49,36 @@ class MixtureOfAgents:
self.description = description
self.agents = agents
self.aggregator_agent = aggregator_agent
self.aggregator_system_prompt = aggregator_system_prompt_main
self.aggregator_system_prompt = aggregator_system_prompt
self.layers = layers
self.max_loops = max_loops
self.return_str_on = return_str_on
self.output_type = output_type
self.aggregator_model_name = aggregator_model_name
self.aggregator_agent = self.aggregator_agent_setup()
self.reliability_check()
self.conversation = Conversation()
list_all_agents(
agents=self.agents,
conversation=self.conversation,
description=self.description,
name=self.name,
add_to_conversation=True,
)
def aggregator_agent_setup(self):
return Agent(
agent_name="Aggregator Agent",
description="An agent that aggregates the responses of the other agents.",
system_prompt=aggregator_system_prompt_main,
model_name=self.aggregator_model_name,
temperature=0.5,
max_loops=1,
output_type="str-all-except-first",
)
def reliability_check(self) -> None:
"""
Performs a reliability check on the Mixture of Agents class.
@ -66,8 +87,8 @@ class MixtureOfAgents:
"Checking the reliability of the Mixture of Agents class."
)
if not self.agents:
raise ValueError("No reference agents provided.")
if len(self.agents) == 0:
raise ValueError("No agents provided.")
if not self.aggregator_agent:
raise ValueError("No aggregator agent provided.")
@ -78,129 +99,83 @@ class MixtureOfAgents:
if not self.layers:
raise ValueError("No layers provided.")
if self.layers < 1:
raise ValueError("Layers must be greater than 0.")
logger.info("Reliability check passed.")
logger.info("Mixture of Agents class is ready for use.")
def _get_final_system_prompt(
self, system_prompt: str, results: List[str]
) -> str:
"""
Constructs a system prompt for subsequent layers that includes previous responses.
Args:
system_prompt (str): The initial system prompt.
results (List[str]): A list of previous responses.
Returns:
str: The final system prompt including previous responses.
"""
return (
system_prompt
+ "\n"
+ "\n".join(
[
f"{i+1}. {str(element)}"
for i, element in enumerate(results)
]
)
)
def save_to_markdown_file(self, file_path: str = "moa.md"):
with open(file_path, "w") as f:
f.write(self.conversation.get_str())
async def _run_agent_async(
def step(
self,
agent: Agent,
task: str,
prev_responses: Optional[List[str]] = None,
) -> str:
"""
Asynchronous method to run a single agent.
Args:
agent (Agent): The agent to be run.
task (str): The task for the agent.
prev_responses (Optional[List[str]], optional): A list of previous responses. Defaults to None.
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
):
# self.conversation.add(role="User", content=task)
Returns:
str: The response from the agent.
"""
# If there are previous responses, update the agent's system prompt
if prev_responses:
system_prompt_with_responses = (
self._get_final_system_prompt(
self.aggregator_system_prompt, prev_responses
)
)
agent.system_prompt = system_prompt_with_responses
# Run agents concurrently
with concurrent.futures.ThreadPoolExecutor(
max_workers=os.cpu_count()
) as executor:
# Submit all agent tasks and store with their index
future_to_agent = {
executor.submit(
agent.run, task=task, img=img, imgs=imgs
): agent
for agent in self.agents
}
# Run the agent asynchronously
response = await asyncio.to_thread(agent.run, task)
# Collect results and add to conversation in completion order
for future in concurrent.futures.as_completed(
future_to_agent
):
agent = future_to_agent[future]
output = future.result()
self.conversation.add(role=agent.name, content=output)
self.conversation.add(agent.agent_name, response)
return self.conversation.get_str()
# Log the agent's response
print(f"Agent {agent.agent_name} response: {response}")
return response
def _run(
self,
task: str,
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
):
async def _run_async(self, task: str) -> None:
"""
Asynchronous method to run the Mixture of Agents process.
self.conversation.add(role="User", content=task)
Args:
task (str): The task for the mixture of agents.
"""
# Gather initial responses from reference agents
results: List[str] = await asyncio.gather(
*[
self._run_agent_async(agent, task)
for agent in self.agents
]
for i in range(self.layers):
out = self.step(
task=self.conversation.get_str(), img=img, imgs=imgs
)
task = out
# Process additional layers, if applicable
for _ in range(1, self.layers - 1):
results = await asyncio.gather(
*[
self._run_agent_async(
agent, task, prev_responses=results
)
for agent in self.agents
]
out = self.aggregator_agent.run(
task=self.conversation.get_str()
)
# Perform final aggregation using the aggregator agent
final_result = await self._run_agent_async(
self.aggregator_agent, task, prev_responses=results
self.conversation.add(
role=self.aggregator_agent.agent_name, content=out
)
print(f"Final Aggregated Response: {final_result}")
out = history_output_formatter(
conversation=self.conversation, type=self.output_type
)
def run(self, task: str) -> None:
"""
Synchronous wrapper to run the async process.
return out
Args:
task (str): The task for the mixture of agents.
"""
def run(
self,
task: str,
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
):
try:
self.conversation.add("user", task)
for _ in range(self.max_loops):
# Add previous context to task if available
prompt = f"History: {self.conversation.get_str()}\n\nTask: {task}"
# Run async process
asyncio.run(self._run_async(prompt))
return history_output_formatter(
conversation=self.conversation,
type=self.output_type,
)
return self._run(task=task, img=img, imgs=imgs)
except Exception as e:
logger.error(f"Error running mixture of agents: {str(e)}")
raise e
logger.error(f"Error running Mixture of Agents: {e}")
return f"Error: {e}"
def run_batched(self, tasks: List[str]) -> List[str]:
"""

@ -1,4 +1,3 @@
import asyncio
import json
import uuid
from concurrent.futures import ThreadPoolExecutor
@ -282,7 +281,6 @@ class AgentRearrange(BaseSwarm):
)
for task_idx, task in enumerate(tasks):
is_last = task == tasks[-1]
agent_names = [
name.strip() for name in task.split(",")
]
@ -298,7 +296,6 @@ class AgentRearrange(BaseSwarm):
result = agent.run(
task=self.conversation.get_str(),
img=img,
is_last=is_last,
*args,
**kwargs,
)
@ -327,7 +324,6 @@ class AgentRearrange(BaseSwarm):
current_task = agent.run(
task=self.conversation.get_str(),
img=img,
is_last=is_last,
*args,
**kwargs,
)
@ -344,7 +340,8 @@ class AgentRearrange(BaseSwarm):
logger.info("Task execution completed")
return history_output_formatter(
self.conversation, self.output_type
conversation=self.conversation,
type=self.output_type,
)
except Exception as e:
@ -364,11 +361,6 @@ class AgentRearrange(BaseSwarm):
self,
task: str = None,
img: str = None,
device: str = "cpu",
device_id: int = 2,
all_cores: bool = True,
all_gpus: bool = False,
no_use_clusterops: bool = True,
*args,
**kwargs,
):
@ -481,58 +473,11 @@ class AgentRearrange(BaseSwarm):
except Exception as e:
self._catch_error(e)
async def abatch_run(
self,
tasks: List[str],
img: Optional[List[str]] = None,
batch_size: int = 10,
*args,
**kwargs,
) -> List[str]:
"""
Asynchronously process multiple tasks in batches.
Args:
tasks: List of tasks to process
img: Optional list of images corresponding to tasks
batch_size: Number of tasks to process simultaneously
Returns:
List of results corresponding to input tasks
"""
try:
results = []
for i in range(0, len(tasks), batch_size):
batch_tasks = tasks[i : i + batch_size]
batch_imgs = (
img[i : i + batch_size]
if img
else [None] * len(batch_tasks)
)
# Process batch using asyncio.gather
batch_coros = [
self.astream(
task=task, img=img_path, *args, **kwargs
)
for task, img_path in zip(batch_tasks, batch_imgs)
]
batch_results = await asyncio.gather(*batch_coros)
results.extend(batch_results)
return results
except Exception as e:
self._catch_error(e)
def concurrent_run(
self,
tasks: List[str],
img: Optional[List[str]] = None,
max_workers: Optional[int] = None,
device: str = "cpu",
device_id: int = None,
all_cores: bool = True,
all_gpus: bool = False,
*args,
**kwargs,
) -> List[str]:
@ -561,10 +506,6 @@ class AgentRearrange(BaseSwarm):
self.run,
task=task,
img=img_path,
device=device,
device_id=device_id,
all_cores=all_cores,
all_gpus=all_gpus,
*args,
**kwargs,
)

@ -49,15 +49,12 @@ class SequentialWorkflow:
self.flow = self.sequential_flow()
self.agent_rearrange = AgentRearrange(
name=name,
description=description,
agents=agents,
name=self.name,
description=self.description,
agents=self.agents,
flow=self.flow,
max_loops=max_loops,
output_type=output_type,
shared_memory_system=shared_memory_system,
*args,
**kwargs,
max_loops=self.max_loops,
output_type=self.output_type,
)
def sequential_flow(self):
@ -105,11 +102,7 @@ class SequentialWorkflow:
self,
task: str,
img: Optional[str] = None,
device: str = "cpu",
all_cores: bool = False,
all_gpus: bool = False,
device_id: int = 0,
no_use_clusterops: bool = True,
imgs: Optional[List[str]] = None,
*args,
**kwargs,
):
@ -134,14 +127,14 @@ class SequentialWorkflow:
"""
try:
result = self.agent_rearrange.run(
return self.agent_rearrange.run(
task=task,
img=img,
*args,
**kwargs,
# imgs=imgs,
# *args,
# **kwargs,
)
return result
except Exception as e:
logger.error(
f"An error occurred while executing the task: {e}"

@ -1,6 +1,7 @@
import concurrent.futures
import json
import os
import uuid
from datetime import datetime
import traceback
from typing import Any, Callable, Dict, List, Literal, Optional, Union
from pydantic import BaseModel, Field
@ -20,13 +21,15 @@ from swarms.structs.rearrange import AgentRearrange
from swarms.structs.sequential_workflow import SequentialWorkflow
from swarms.structs.spreadsheet_swarm import SpreadSheetSwarm
from swarms.structs.swarm_matcher import swarm_matcher
from swarms.telemetry.log_executions import log_execution
from swarms.utils.output_types import OutputType
from swarms.utils.loguru_logger import initialize_logger
from swarms.structs.malt import MALT
from swarms.structs.deep_research_swarm import DeepResearchSwarm
from swarms.structs.council_judge import CouncilAsAJudge
from swarms.structs.interactive_groupchat import InteractiveGroupChat
from swarms.structs.ma_utils import list_all_agents
from swarms.utils.generate_keys import generate_api_key
logger = initialize_logger(log_folder="swarm_router")
@ -54,25 +57,6 @@ class Document(BaseModel):
data: str
class SwarmLog(BaseModel):
"""
A Pydantic model to capture log entries.
"""
id: Optional[str] = Field(
default_factory=lambda: str(uuid.uuid4())
)
timestamp: Optional[datetime] = Field(
default_factory=datetime.utcnow
)
level: Optional[str] = None
message: Optional[str] = None
swarm_type: Optional[SwarmType] = None
task: Optional[str] = ""
metadata: Optional[Dict[str, Any]] = Field(default_factory=dict)
documents: List[Document] = []
class SwarmRouterConfig(BaseModel):
"""Configuration model for SwarmRouter."""
@ -172,12 +156,11 @@ class SwarmRouter:
concurrent_batch_run(tasks: List[str], *args, **kwargs) -> List[Any]:
Executes multiple tasks concurrently
get_logs() -> List[SwarmLog]:
Retrieves execution logs
"""
def __init__(
self,
id: str = generate_api_key(prefix="swarm-router"),
name: str = "swarm-router",
description: str = "Routes your task to the desired swarm",
max_loops: int = 1,
@ -191,15 +174,19 @@ class SwarmRouter:
rules: str = None,
documents: List[str] = [], # A list of docs file paths
output_type: OutputType = "dict-all-except-first",
no_cluster_ops: bool = False,
speaker_fn: callable = None,
load_agents_from_csv: bool = False,
csv_file_path: str = None,
return_entire_history: bool = True,
multi_agent_collab_prompt: bool = True,
list_all_agents: bool = False,
conversation: Any = None,
agents_config: Optional[Dict[Any, Any]] = None,
speaker_function: str = None,
*args,
**kwargs,
):
self.id = id
self.name = name
self.description = description
self.max_loops = max_loops
@ -213,13 +200,16 @@ class SwarmRouter:
self.rules = rules
self.documents = documents
self.output_type = output_type
self.no_cluster_ops = no_cluster_ops
self.speaker_fn = speaker_fn
self.logs = []
self.load_agents_from_csv = load_agents_from_csv
self.csv_file_path = csv_file_path
self.return_entire_history = return_entire_history
self.multi_agent_collab_prompt = multi_agent_collab_prompt
self.list_all_agents = list_all_agents
self.conversation = conversation
self.agents_config = agents_config
self.speaker_function = speaker_function
# Reliability check
self.reliability_check()
@ -230,6 +220,8 @@ class SwarmRouter:
csv_path=self.csv_file_path
).load_agents()
self.agent_config = self.agent_config()
def setup(self):
if self.auto_generate_prompts is True:
self.activate_ape()
@ -276,15 +268,12 @@ class SwarmRouter:
logger.info(
f"Successfully activated APE for {activated_count} agents"
)
self._log(
"info",
f"Activated automatic prompt engineering for {activated_count} agents",
)
except Exception as e:
error_msg = f"Error activating automatic prompt engineering: {str(e)}"
logger.error(error_msg)
self._log("error", error_msg)
logger.error(
f"Error activating automatic prompt engineering in SwarmRouter: {str(e)}"
)
raise RuntimeError(error_msg) from e
def reliability_check(self):
@ -293,48 +282,24 @@ class SwarmRouter:
Validates essential swarm parameters and configuration before execution.
Handles special case for CouncilAsAJudge which may not require agents.
"""
logger.info(
"🔍 [SYSTEM] Initializing advanced swarm reliability diagnostics..."
)
logger.info(
"⚡ [SYSTEM] Running pre-flight checks and system validation..."
)
# Check swarm type first since it affects other validations
if self.swarm_type is None:
logger.error(
"❌ [CRITICAL] Swarm type validation failed - type cannot be 'none'"
raise ValueError(
"SwarmRouter: Swarm type cannot be 'none'."
)
raise ValueError("Swarm type cannot be 'none'.")
# Special handling for CouncilAsAJudge
if self.swarm_type == "CouncilAsAJudge":
if self.agents is not None:
logger.warning(
"⚠️ [ADVISORY] CouncilAsAJudge detected with agents - this is atypical"
)
elif not self.agents:
logger.error(
"❌ [CRITICAL] Agent validation failed - no agents detected in swarm"
if self.agents is None:
raise ValueError(
"SwarmRouter: No agents provided for the swarm."
)
raise ValueError("No agents provided for the swarm.")
# Validate max_loops
if self.max_loops == 0:
logger.error(
"❌ [CRITICAL] Loop validation failed - max_loops cannot be 0"
)
raise ValueError("max_loops cannot be 0.")
raise ValueError("SwarmRouter: max_loops cannot be 0.")
# Setup other functionality
logger.info("🔄 [SYSTEM] Initializing swarm subsystems...")
self.setup()
logger.info(
"✅ [SYSTEM] All reliability checks passed successfully"
)
logger.info("🚀 [SYSTEM] Swarm is ready for deployment")
def _create_swarm(self, task: str = None, *args, **kwargs):
"""
Dynamically create and return the specified swarm type or automatically match the best swarm type for a given task.
@ -395,6 +360,7 @@ class SwarmRouter:
agents=self.agents,
max_loops=self.max_loops,
output_type=self.output_type,
speaker_function=self.speaker_function,
)
elif self.swarm_type == "DeepResearchSwarm":
@ -500,46 +466,24 @@ class SwarmRouter:
def update_system_prompt_for_agent_in_swarm(self):
# Use list comprehension for faster iteration
[
setattr(
agent,
"system_prompt",
agent.system_prompt + MULTI_AGENT_COLLAB_PROMPT_TWO,
)
for agent in self.agents
]
for agent in self.agents:
if agent.system_prompt is None:
agent.system_prompt = ""
agent.system_prompt += MULTI_AGENT_COLLAB_PROMPT_TWO
def _log(
self,
level: str,
message: str,
task: str = "",
metadata: Dict[str, Any] = None,
):
"""
Create a log entry and add it to the logs list.
def agent_config(self):
agent_config = {}
for agent in self.agents:
agent_config[agent.agent_name] = agent.to_dict()
Args:
level (str): The log level (e.g., "info", "error").
message (str): The log message.
task (str, optional): The task being performed. Defaults to "".
metadata (Dict[str, Any], optional): Additional metadata. Defaults to None.
"""
log_entry = SwarmLog(
level=level,
message=message,
swarm_type=self.swarm_type,
task=task,
metadata=metadata or {},
)
self.logs.append(log_entry)
logger.log(level.upper(), message)
return agent_config
def _run(
self,
task: str,
img: Optional[str] = None,
model_response: Optional[str] = None,
imgs: Optional[List[str]] = None,
*args,
**kwargs,
) -> Any:
@ -559,17 +503,39 @@ class SwarmRouter:
"""
self.swarm = self._create_swarm(task, *args, **kwargs)
if self.swarm_type == "SequentialWorkflow":
self.conversation = (
self.swarm.agent_rearrange.conversation
)
else:
self.conversation = self.swarm.conversation
if self.list_all_agents is True:
list_all_agents(
agents=self.agents,
conversation=self.swarm.conversation,
name=self.name,
description=self.description,
add_collaboration_prompt=True,
add_to_conversation=True,
)
if self.multi_agent_collab_prompt is True:
self.update_system_prompt_for_agent_in_swarm()
try:
logger.info(
f"Running task on {self.swarm_type} swarm with task: {task}"
log_execution(
swarm_id=self.id,
status="start",
swarm_config=self.to_dict(),
swarm_architecture="swarm_router",
)
try:
if self.swarm_type == "CouncilAsAJudge":
result = self.swarm.run(
task=task,
img=img,
imgs=imgs,
model_response=model_response,
*args,
**kwargs,
@ -577,21 +543,24 @@ class SwarmRouter:
else:
result = self.swarm.run(task=task, *args, **kwargs)
logger.info("Swarm completed successfully")
log_execution(
swarm_id=self.id,
status="completion",
swarm_config=self.to_dict(),
swarm_architecture="swarm_router",
)
return result
except Exception as e:
self._log(
"error",
f"Error occurred while running task on {self.swarm_type} swarm: {str(e)}",
task=task,
metadata={"error": str(e)},
raise RuntimeError(
f"SwarmRouter: Error executing task on swarm: {str(e)} Traceback: {traceback.format_exc()}"
)
raise
def run(
self,
task: str,
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
model_response: Optional[str] = None,
*args,
**kwargs,
@ -617,15 +586,24 @@ class SwarmRouter:
return self._run(
task=task,
img=img,
imgs=imgs,
model_response=model_response,
*args,
**kwargs,
)
except Exception as e:
logger.error(f"Error executing task on swarm: {str(e)}")
raise
raise RuntimeError(
f"SwarmRouter: Error executing task on swarm: {str(e)} Traceback: {traceback.format_exc()}"
)
def __call__(self, task: str, *args, **kwargs) -> Any:
def __call__(
self,
task: str,
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
*args,
**kwargs,
) -> Any:
"""
Make the SwarmRouter instance callable.
@ -637,10 +615,17 @@ class SwarmRouter:
Returns:
Any: The result of the swarm's execution.
"""
return self.run(task=task, *args, **kwargs)
return self.run(
task=task, img=img, imgs=imgs, *args, **kwargs
)
def batch_run(
self, tasks: List[str], *args, **kwargs
self,
tasks: List[str],
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
*args,
**kwargs,
) -> List[Any]:
"""
Execute a batch of tasks on the selected or matched swarm type.
@ -659,21 +644,26 @@ class SwarmRouter:
results = []
for task in tasks:
try:
result = self.run(task, *args, **kwargs)
result = self.run(
task, img=img, imgs=imgs, *args, **kwargs
)
results.append(result)
except Exception as e:
self._log(
"error",
f"Error occurred while running batch task on {self.swarm_type} swarm: {str(e)}",
task=task,
metadata={"error": str(e)},
raise RuntimeError(
f"SwarmRouter: Error executing batch task on swarm: {str(e)} Traceback: {traceback.format_exc()}"
)
raise
return results
def async_run(self, task: str, *args, **kwargs) -> Any:
def concurrent_run(
self,
task: str,
img: Optional[str] = None,
imgs: Optional[List[str]] = None,
*args,
**kwargs,
) -> Any:
"""
Execute a task on the selected or matched swarm type asynchronously.
Execute a task on the selected or matched swarm type concurrently.
Args:
task (str): The task to be executed by the swarm.
@ -686,95 +676,70 @@ class SwarmRouter:
Raises:
Exception: If an error occurs during task execution.
"""
import asyncio
async def run_async():
try:
result = await asyncio.to_thread(
self.run, task, *args, **kwargs
with concurrent.futures.ThreadPoolExecutor(
max_workers=os.cpu_count()
) as executor:
future = executor.submit(
self.run, task, img=img, imgs=imgs, *args, **kwargs
)
result = future.result()
return result
except Exception as e:
self._log(
"error",
f"Error occurred while running task asynchronously on {self.swarm_type} swarm: {str(e)}",
task=task,
metadata={"error": str(e)},
)
raise
return asyncio.run(run_async())
def get_logs(self) -> List[SwarmLog]:
"""
Retrieve all logged entries.
Returns:
List[SwarmLog]: A list of all log entries.
def _serialize_callable(
self, attr_value: Callable
) -> Dict[str, Any]:
"""
return self.logs
def concurrent_run(self, task: str, *args, **kwargs) -> Any:
"""
Execute a task on the selected or matched swarm type concurrently.
Serializes callable attributes by extracting their name and docstring.
Args:
task (str): The task to be executed by the swarm.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
attr_value (Callable): The callable to serialize.
Returns:
Any: The result of the swarm's execution.
Raises:
Exception: If an error occurs during task execution.
Dict[str, Any]: Dictionary with name and docstring of the callable.
"""
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(
max_workers=os.cpu_count()
) as executor:
future = executor.submit(self.run, task, *args, **kwargs)
result = future.result()
return result
def concurrent_batch_run(
self, tasks: List[str], *args, **kwargs
) -> List[Any]:
return {
"name": getattr(
attr_value, "__name__", type(attr_value).__name__
),
"doc": getattr(attr_value, "__doc__", None),
}
def _serialize_attr(self, attr_name: str, attr_value: Any) -> Any:
"""
Execute a batch of tasks on the selected or matched swarm type concurrently.
Serializes an individual attribute, handling non-serializable objects.
Args:
tasks (List[str]): A list of tasks to be executed by the swarm.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
attr_name (str): The name of the attribute.
attr_value (Any): The value of the attribute.
Returns:
List[Any]: A list of results from the swarm's execution.
Raises:
Exception: If an error occurs during task execution.
Any: The serialized value of the attribute.
"""
from concurrent.futures import (
ThreadPoolExecutor,
as_completed,
)
results = []
with ThreadPoolExecutor() as executor:
# Submit all tasks to executor
futures = [
executor.submit(self.run, task, *args, **kwargs)
for task in tasks
]
# Process results as they complete rather than waiting for all
for future in as_completed(futures):
try:
result = future.result()
results.append(result)
except Exception as e:
logger.error(f"Task execution failed: {str(e)}")
results.append(None)
if callable(attr_value):
return self._serialize_callable(attr_value)
elif hasattr(attr_value, "to_dict"):
return (
attr_value.to_dict()
) # Recursive serialization for nested objects
else:
json.dumps(
attr_value
) # Attempt to serialize to catch non-serializable objects
return attr_value
except (TypeError, ValueError):
return f"<Non-serializable: {type(attr_value).__name__}>"
def to_dict(self) -> Dict[str, Any]:
"""
Converts all attributes of the class, including callables, into a dictionary.
Handles non-serializable attributes by converting them or skipping them.
return results
Returns:
Dict[str, Any]: A dictionary representation of the class attributes.
"""
return {
attr_name: self._serialize_attr(attr_name, attr_value)
for attr_name, attr_value in self.__dict__.items()
}

@ -1,31 +1,13 @@
from swarms.telemetry.main import (
generate_unique_identifier,
generate_user_id,
get_cpu_info,
get_machine_id,
get_os_version,
get_package_mismatches,
get_pip_version,
get_python_version,
get_ram_info,
get_swarms_verison,
get_system_info,
get_user_device_data,
system_info,
get_comprehensive_system_info,
log_agent_data,
)
__all__ = [
"generate_user_id",
"get_machine_id",
"get_system_info",
"generate_unique_identifier",
"get_python_version",
"get_pip_version",
"get_swarms_verison",
"get_os_version",
"get_cpu_info",
"get_ram_info",
"get_package_mismatches",
"system_info",
"get_user_device_data",
"get_comprehensive_system_info",
"log_agent_data",
]

@ -0,0 +1,43 @@
from typing import Optional
from swarms.telemetry.main import log_agent_data
def log_execution(
swarm_id: Optional[str] = None,
status: Optional[str] = None,
swarm_config: Optional[dict] = None,
swarm_architecture: Optional[str] = None,
):
"""
Log execution data for a swarm router instance.
This function logs telemetry data about swarm router executions, including
the swarm ID, execution status, and configuration details. It silently
handles any logging errors to prevent execution interruption.
Args:
swarm_id (str): Unique identifier for the swarm router instance
status (str): Current status of the execution (e.g., "start", "completion", "error")
swarm_config (dict): Configuration dictionary containing swarm router settings
swarm_architecture (str): Name of the swarm architecture used
Returns:
None
Example:
>>> log_execution(
... swarm_id="swarm-router-abc123",
... status="start",
... swarm_config={"name": "my-swarm", "swarm_type": "SequentialWorkflow"}
... )
"""
try:
log_agent_data(
data_dict={
"swarm_router_id": swarm_id,
"status": status,
"swarm_router_config": swarm_config,
"swarm_architecture": swarm_architecture,
}
)
except Exception:
pass

@ -1,24 +1,13 @@
import asyncio
import datetime
import hashlib
import platform
import socket
import subprocess
import uuid
from concurrent.futures import ThreadPoolExecutor
from functools import lru_cache
from threading import Lock
from typing import Dict
from typing import Any, Dict
import aiohttp
import pkg_resources
import psutil
import toml
from requests import Session
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
import requests
from functools import lru_cache
# Helper functions
@ -42,355 +31,104 @@ def get_machine_id():
return hashed_id
def get_system_info():
"""
Gathers basic system information.
Returns:
dict: A dictionary containing system-related information.
"""
info = {
@lru_cache(maxsize=1)
def get_comprehensive_system_info() -> Dict[str, Any]:
# Basic platform and hardware information
system_data = {
"platform": platform.system(),
"platform_release": platform.release(),
"platform_version": platform.version(),
"platform_full": platform.platform(),
"architecture": platform.machine(),
"architecture_details": platform.architecture()[0],
"processor": platform.processor(),
"hostname": socket.gethostname(),
"ip_address": socket.gethostbyname(socket.gethostname()),
"mac_address": ":".join(
}
# MAC address
try:
system_data["mac_address"] = ":".join(
[
f"{(uuid.getnode() >> elements) & 0xFF:02x}"
for elements in range(0, 2 * 6, 8)
][::-1]
),
"processor": platform.processor(),
"python_version": platform.python_version(),
"Misc": system_info(),
}
return info
def generate_unique_identifier():
"""Generate unique identifier
Returns:
str: unique id
"""
system_info = get_system_info()
unique_id = uuid.uuid5(uuid.NAMESPACE_DNS, str(system_info))
return str(unique_id)
def get_local_ip():
"""Get local ip
Returns:
str: local ip
"""
return socket.gethostbyname(socket.gethostname())
def get_user_device_data():
data = {
"ID": generate_user_id(),
"Machine ID": get_machine_id(),
"System Info": get_system_info(),
"UniqueID": generate_unique_identifier(),
}
return data
def get_python_version():
return platform.python_version()
def get_pip_version() -> str:
"""Get pip version
Returns:
str: The version of pip installed
"""
try:
pip_version = (
subprocess.check_output(["pip", "--version"])
.decode()
.split()[1]
)
except Exception as e:
pip_version = str(e)
return pip_version
system_data["mac_address"] = f"Error: {str(e)}"
def get_swarms_verison() -> tuple[str, str]:
"""Get swarms version from both command line and package
Returns:
tuple[str, str]: A tuple containing (command line version, package version)
"""
try:
swarms_verison_cmd = (
subprocess.check_output(["swarms", "--version"])
.decode()
.split()[1]
# CPU information
system_data["cpu_count_logical"] = psutil.cpu_count(logical=True)
system_data["cpu_count_physical"] = psutil.cpu_count(
logical=False
)
except Exception as e:
swarms_verison_cmd = str(e)
swarms_verison_pkg = pkg_resources.get_distribution(
"swarms"
).version
swarms_verison = swarms_verison_cmd, swarms_verison_pkg
return swarms_verison
def get_os_version() -> str:
"""Get operating system version
Returns:
str: The operating system version and platform details
"""
return platform.platform()
def get_cpu_info() -> str:
"""Get CPU information
Returns:
str: The processor information
"""
return platform.processor()
def get_ram_info() -> str:
"""Get RAM information
Returns:
str: A formatted string containing total, used and free RAM in GB
"""
# Memory information
vm = psutil.virtual_memory()
total_ram_gb = vm.total / (1024**3)
used_ram_gb = vm.used / (1024**3)
free_ram_gb = vm.free / (1024**3)
total_ram_gb = vm.total / (1024**3)
return (
f"{total_ram_gb:.2f} GB, used: {used_ram_gb:.2f}, free:"
f" {free_ram_gb:.2f}"
)
def get_package_mismatches(file_path: str = "pyproject.toml") -> str:
"""Get package version mismatches between pyproject.toml and installed packages
Args:
file_path (str, optional): Path to pyproject.toml file. Defaults to "pyproject.toml".
Returns:
str: A formatted string containing package version mismatches
"""
with open(file_path) as file:
pyproject = toml.load(file)
dependencies = pyproject["tool"]["poetry"]["dependencies"]
dev_dependencies = pyproject["tool"]["poetry"]["group"]["dev"][
"dependencies"
]
dependencies.update(dev_dependencies)
available_ram_gb = vm.available / (1024**3)
installed_packages = {
pkg.key: pkg.version for pkg in pkg_resources.working_set
system_data.update(
{
"memory_total_gb": f"{total_ram_gb:.2f}",
"memory_used_gb": f"{used_ram_gb:.2f}",
"memory_free_gb": f"{free_ram_gb:.2f}",
"memory_available_gb": f"{available_ram_gb:.2f}",
"memory_summary": f"Total: {total_ram_gb:.2f} GB, Used: {used_ram_gb:.2f} GB, Free: {free_ram_gb:.2f} GB, Available: {available_ram_gb:.2f} GB",
}
mismatches = []
for package, version_info in dependencies.items():
if isinstance(version_info, dict):
version_info = version_info["version"]
installed_version = installed_packages.get(package)
if installed_version and version_info.startswith("^"):
expected_version = version_info[1:]
if not installed_version.startswith(expected_version):
mismatches.append(
f"\t {package}: Mismatch,"
f" pyproject.toml={expected_version},"
f" pip={installed_version}"
)
else:
mismatches.append(f"\t {package}: Not found in pip list")
return "\n" + "\n".join(mismatches)
def system_info() -> dict[str, str]:
"""Get system information including Python, pip, OS, CPU and RAM details
Returns:
dict[str, str]: A dictionary containing system information
"""
return {
"Python Version": get_python_version(),
"Pip Version": get_pip_version(),
# "Swarms Version": swarms_verison,
"OS Version and Architecture": get_os_version(),
"CPU Info": get_cpu_info(),
"RAM Info": get_ram_info(),
}
# Python version
system_data["python_version"] = platform.python_version()
def capture_system_data() -> Dict[str, str]:
"""
Captures extensive system data including platform information, user ID, IP address, CPU count,
memory information, and other system details.
Returns:
Dict[str, str]: A dictionary containing system data.
"""
# Generate unique identifier based on system info
try:
system_data = {
"platform": platform.system(),
"platform_version": platform.version(),
"platform_release": platform.release(),
"hostname": socket.gethostname(),
"ip_address": socket.gethostbyname(socket.gethostname()),
"cpu_count": psutil.cpu_count(logical=True),
"memory_total": f"{psutil.virtual_memory().total / (1024 ** 3):.2f} GB",
"memory_available": f"{psutil.virtual_memory().available / (1024 ** 3):.2f} GB",
"user_id": str(uuid.uuid4()), # Unique user identifier
"machine_type": platform.machine(),
"processor": platform.processor(),
"architecture": platform.architecture()[0],
}
return system_data
unique_id = uuid.uuid5(uuid.NAMESPACE_DNS, str(system_data))
system_data["unique_identifier"] = str(unique_id)
except Exception as e:
# logger.error("Failed to capture system data: {}", e)
print(f"Failed to capture system data: {e}")
# Global variables
_session = None
_session_lock = Lock()
_executor = ThreadPoolExecutor(max_workers=10)
_aiohttp_session = None
def get_session() -> Session:
"""Thread-safe session getter with optimized connection pooling"""
global _session
if _session is None:
with _session_lock:
if _session is None: # Double-check pattern
_session = Session()
adapter = HTTPAdapter(
pool_connections=1000, # Increased pool size
pool_maxsize=1000, # Increased max size
max_retries=Retry(
total=3,
backoff_factor=0.1,
status_forcelist=[500, 502, 503, 504],
),
pool_block=False, # Non-blocking pool
)
_session.mount("http://", adapter)
_session.mount("https://", adapter)
_session.headers.update(
{
"Content-Type": "application/json",
"Authorization": "Bearer sk-33979fd9a4e8e6b670090e4900a33dbe7452a15ccc705745f4eca2a70c88ea24",
"Connection": "keep-alive", # Enable keep-alive
}
)
return _session
@lru_cache(maxsize=2048, typed=True)
def get_user_device_data_cached():
"""Cached version with increased cache size"""
return get_user_device_data()
system_data["unique_identifier"] = f"Error: {str(e)}"
async def get_aiohttp_session():
"""Get or create aiohttp session for async requests"""
global _aiohttp_session
if _aiohttp_session is None or _aiohttp_session.closed:
timeout = aiohttp.ClientTimeout(total=10)
connector = aiohttp.TCPConnector(
limit=1000, # Connection limit
ttl_dns_cache=300, # DNS cache TTL
use_dns_cache=True, # Enable DNS caching
keepalive_timeout=60, # Keep-alive timeout
)
_aiohttp_session = aiohttp.ClientSession(
timeout=timeout,
connector=connector,
headers={
"Content-Type": "application/json",
"Authorization": "Bearer sk-33979fd9a4e8e6b670090e4900a33dbe7452a15ccc705745f4eca2a70c88ea24",
},
)
return _aiohttp_session
return system_data
async def log_agent_data_async(data_dict: dict):
"""Asynchronous version of log_agent_data"""
if not data_dict:
return None
def _log_agent_data(data_dict: dict):
"""Simple function to log agent data using requests library"""
url = "https://swarms.world/api/get-agents/log-agents"
payload = {
log = {
"data": data_dict,
"system_data": get_user_device_data_cached(),
"system_data": get_comprehensive_system_info(),
"timestamp": datetime.datetime.now(
datetime.timezone.utc
).isoformat(),
}
session = await get_aiohttp_session()
try:
async with session.post(url, json=payload) as response:
if response.status == 200:
return await response.json()
except Exception:
return None
payload = {
"data": log,
}
key = "Bearer sk-33979fd9a4e8e6b670090e4900a33dbe7452a15ccc705745f4eca2a70c88ea24"
def _log_agent_data(data_dict: dict):
"""
Enhanced log_agent_data with both sync and async capabilities
"""
if not data_dict:
return None
headers = {
"Content-Type": "application/json",
"Authorization": key,
}
# If running in an event loop, use async version
try:
loop = asyncio.get_event_loop()
if loop.is_running():
return asyncio.create_task(
log_agent_data_async(data_dict)
response = requests.post(
url, json=payload, headers=headers, timeout=10
)
except RuntimeError:
pass
# Fallback to optimized sync version
url = "https://swarms.world/api/get-agents/log-agents"
payload = {
"data": data_dict,
"system_data": get_user_device_data_cached(),
"timestamp": datetime.datetime.now(
datetime.timezone.utc
).isoformat(),
}
try:
session = get_session()
response = session.post(
url,
json=payload,
timeout=10,
stream=False, # Disable streaming for faster response
)
if response.ok and response.text.strip():
return response.json()
if response.status_code == 200:
return
except Exception:
return None
pass
def log_agent_data(data_dict: dict):
"""Log agent data"""
try:
_log_agent_data(data_dict)
except Exception:
pass

@ -33,6 +33,11 @@ from swarms.tools.mcp_client_call import (
get_tools_for_multiple_mcp_servers,
get_mcp_tools_sync,
aget_mcp_tools,
execute_multiple_tools_on_multiple_mcp_servers,
execute_multiple_tools_on_multiple_mcp_servers_sync,
_create_server_tool_mapping,
_create_server_tool_mapping_async,
_execute_tool_on_server,
)
@ -62,4 +67,9 @@ __all__ = [
"get_tools_for_multiple_mcp_servers",
"get_mcp_tools_sync",
"aget_mcp_tools",
"execute_multiple_tools_on_multiple_mcp_servers",
"execute_multiple_tools_on_multiple_mcp_servers_sync",
"_create_server_tool_mapping",
"_create_server_tool_mapping_async",
"_execute_tool_on_server",
]

@ -2223,8 +2223,13 @@ class BaseTool(BaseModel):
>>> tool_calls = [ChatCompletionMessageToolCall(...), ...]
>>> results = tool.execute_function_calls_from_api_response(tool_calls)
"""
# Handle None API response gracefully by returning empty results
if api_response is None:
raise ToolValidationError("API response cannot be None")
self._log_if_verbose(
"warning",
"API response is None, returning empty results. This may indicate the LLM did not return a valid response.",
)
return [] if not return_as_string else []
# Handle direct list of tool call objects (e.g., from OpenAI ChatCompletionMessageToolCall or Anthropic BaseModels)
if isinstance(api_response, list):
@ -2256,14 +2261,18 @@ class BaseTool(BaseModel):
try:
api_response = json.loads(api_response)
except json.JSONDecodeError as e:
raise ToolValidationError(
f"Invalid JSON in API response: {e}"
) from e
self._log_if_verbose(
"error",
f"Failed to parse JSON from API response: {e}. Response: '{api_response[:100]}...'",
)
return []
if not isinstance(api_response, dict):
raise ToolValidationError(
"API response must be a dictionary, JSON string, BaseModel, or list of tool calls"
self._log_if_verbose(
"warning",
f"API response is not a dictionary (type: {type(api_response)}), returning empty list",
)
return []
# Extract function calls from dictionary response
function_calls = (

@ -494,6 +494,9 @@ async def execute_tool_call_simple(
*args,
**kwargs,
) -> List[Dict[str, Any]]:
if isinstance(response, str):
response = json.loads(response)
return await _execute_tool_call_simple(
response=response,
server_path=server_path,
@ -502,3 +505,511 @@ async def execute_tool_call_simple(
*args,
**kwargs,
)
def _create_server_tool_mapping(
urls: List[str],
connections: List[MCPConnection] = None,
format: str = "openai",
) -> Dict[str, Dict[str, Any]]:
"""
Create a mapping of function names to server information for all MCP servers.
Args:
urls: List of server URLs
connections: Optional list of MCPConnection objects
format: Format to fetch tools in
Returns:
Dict mapping function names to server info (url, connection, tool)
"""
server_tool_mapping = {}
for i, url in enumerate(urls):
connection = (
connections[i]
if connections and i < len(connections)
else None
)
try:
# Get tools for this server
tools = get_mcp_tools_sync(
server_path=url,
connection=connection,
format=format,
)
# Create mapping for each tool
for tool in tools:
if isinstance(tool, dict) and "function" in tool:
function_name = tool["function"]["name"]
server_tool_mapping[function_name] = {
"url": url,
"connection": connection,
"tool": tool,
"server_index": i,
}
elif hasattr(tool, "name"):
# Handle MCPTool objects
server_tool_mapping[tool.name] = {
"url": url,
"connection": connection,
"tool": tool,
"server_index": i,
}
except Exception as e:
logger.warning(
f"Failed to fetch tools from server {url}: {str(e)}"
)
continue
return server_tool_mapping
async def _create_server_tool_mapping_async(
urls: List[str],
connections: List[MCPConnection] = None,
format: str = "openai",
) -> Dict[str, Dict[str, Any]]:
"""
Async version: Create a mapping of function names to server information for all MCP servers.
Args:
urls: List of server URLs
connections: Optional list of MCPConnection objects
format: Format to fetch tools in
Returns:
Dict mapping function names to server info (url, connection, tool)
"""
server_tool_mapping = {}
for i, url in enumerate(urls):
connection = (
connections[i]
if connections and i < len(connections)
else None
)
try:
# Get tools for this server using async function
tools = await aget_mcp_tools(
server_path=url,
connection=connection,
format=format,
)
# Create mapping for each tool
for tool in tools:
if isinstance(tool, dict) and "function" in tool:
function_name = tool["function"]["name"]
server_tool_mapping[function_name] = {
"url": url,
"connection": connection,
"tool": tool,
"server_index": i,
}
elif hasattr(tool, "name"):
# Handle MCPTool objects
server_tool_mapping[tool.name] = {
"url": url,
"connection": connection,
"tool": tool,
"server_index": i,
}
except Exception as e:
logger.warning(
f"Failed to fetch tools from server {url}: {str(e)}"
)
continue
return server_tool_mapping
async def _execute_tool_on_server(
tool_call: Dict[str, Any],
server_info: Dict[str, Any],
output_type: Literal["json", "dict", "str", "formatted"] = "str",
) -> Dict[str, Any]:
"""
Execute a single tool call on a specific server.
Args:
tool_call: The tool call to execute
server_info: Server information from the mapping
output_type: Output format type
Returns:
Execution result with server metadata
"""
try:
result = await _execute_tool_call_simple(
response=tool_call,
server_path=server_info["url"],
connection=server_info["connection"],
output_type=output_type,
)
return {
"server_url": server_info["url"],
"server_index": server_info["server_index"],
"function_name": tool_call.get("function", {}).get(
"name", "unknown"
),
"result": result,
"status": "success",
}
except Exception as e:
logger.error(
f"Failed to execute tool on server {server_info['url']}: {str(e)}"
)
return {
"server_url": server_info["url"],
"server_index": server_info["server_index"],
"function_name": tool_call.get("function", {}).get(
"name", "unknown"
),
"result": None,
"error": str(e),
"status": "error",
}
async def execute_multiple_tools_on_multiple_mcp_servers(
responses: List[Dict[str, Any]],
urls: List[str],
connections: List[MCPConnection] = None,
output_type: Literal["json", "dict", "str", "formatted"] = "str",
max_concurrent: Optional[int] = None,
*args,
**kwargs,
) -> List[Dict[str, Any]]:
"""
Execute multiple tool calls across multiple MCP servers.
This function creates a mapping of function names to servers, then for each response
that contains tool calls, it finds the appropriate server for each function and
executes the calls concurrently.
Args:
responses: List of responses containing tool calls (OpenAI format)
urls: List of MCP server URLs
connections: Optional list of MCPConnection objects corresponding to each URL
output_type: Output format type for results
max_concurrent: Maximum number of concurrent executions (default: len(responses))
Returns:
List of execution results with server metadata
Example:
# Example responses format:
responses = [
{
"function": {
"name": "search_web",
"arguments": {"query": "python programming"}
}
},
{
"function": {
"name": "search_database",
"arguments": {"table": "users", "id": 123}
}
}
]
urls = ["http://server1:8000", "http://server2:8000"]
results = await execute_multiple_tools_on_multiple_mcp_servers(
responses=responses,
urls=urls
)
"""
if not responses:
logger.warning("No responses provided for execution")
return []
if not urls:
raise MCPValidationError("No server URLs provided")
# Create mapping of function names to servers using async version
logger.info(f"Creating tool mapping for {len(urls)} servers")
server_tool_mapping = await _create_server_tool_mapping_async(
urls=urls, connections=connections, format="openai"
)
if not server_tool_mapping:
raise MCPExecutionError(
"No tools found on any of the provided servers"
)
logger.info(
f"Found {len(server_tool_mapping)} unique functions across all servers"
)
# Extract all tool calls from responses
all_tool_calls = []
logger.info(
f"Processing {len(responses)} responses for tool call extraction"
)
# Check if responses are individual characters that need to be reconstructed
if len(responses) > 10 and all(
isinstance(r, str) and len(r) == 1 for r in responses
):
logger.info(
"Detected character-by-character response, reconstructing JSON string"
)
try:
reconstructed_response = "".join(responses)
logger.info(
f"Reconstructed response length: {len(reconstructed_response)}"
)
logger.debug(
f"Reconstructed response: {reconstructed_response}"
)
# Try to parse the reconstructed response to validate it
try:
json.loads(reconstructed_response)
logger.info(
"Successfully validated reconstructed JSON response"
)
except json.JSONDecodeError as e:
logger.warning(
f"Reconstructed response is not valid JSON: {str(e)}"
)
logger.debug(
f"First 100 chars: {reconstructed_response[:100]}"
)
logger.debug(
f"Last 100 chars: {reconstructed_response[-100:]}"
)
responses = [reconstructed_response]
except Exception as e:
logger.warning(
f"Failed to reconstruct response from characters: {str(e)}"
)
for i, response in enumerate(responses):
logger.debug(
f"Processing response {i}: {type(response)} - {response}"
)
# Handle JSON string responses
if isinstance(response, str):
try:
response = json.loads(response)
logger.debug(
f"Parsed JSON string response {i}: {response}"
)
except json.JSONDecodeError:
logger.warning(
f"Failed to parse JSON response at index {i}: {response}"
)
continue
if isinstance(response, dict):
# Single tool call
if "function" in response:
logger.debug(
f"Found single tool call in response {i}: {response['function']}"
)
# Parse arguments if they're a JSON string
if isinstance(
response["function"].get("arguments"), str
):
try:
response["function"]["arguments"] = (
json.loads(
response["function"]["arguments"]
)
)
logger.debug(
f"Parsed function arguments: {response['function']['arguments']}"
)
except json.JSONDecodeError:
logger.warning(
f"Failed to parse function arguments: {response['function']['arguments']}"
)
all_tool_calls.append((i, response))
# Multiple tool calls
elif "tool_calls" in response:
logger.debug(
f"Found multiple tool calls in response {i}: {len(response['tool_calls'])} calls"
)
for tool_call in response["tool_calls"]:
# Parse arguments if they're a JSON string
if isinstance(
tool_call.get("function", {}).get(
"arguments"
),
str,
):
try:
tool_call["function"]["arguments"] = (
json.loads(
tool_call["function"]["arguments"]
)
)
logger.debug(
f"Parsed tool call arguments: {tool_call['function']['arguments']}"
)
except json.JSONDecodeError:
logger.warning(
f"Failed to parse tool call arguments: {tool_call['function']['arguments']}"
)
all_tool_calls.append((i, tool_call))
# Direct tool call
elif "name" in response and "arguments" in response:
logger.debug(
f"Found direct tool call in response {i}: {response}"
)
# Parse arguments if they're a JSON string
if isinstance(response.get("arguments"), str):
try:
response["arguments"] = json.loads(
response["arguments"]
)
logger.debug(
f"Parsed direct tool call arguments: {response['arguments']}"
)
except json.JSONDecodeError:
logger.warning(
f"Failed to parse direct tool call arguments: {response['arguments']}"
)
all_tool_calls.append((i, {"function": response}))
else:
logger.debug(
f"Response {i} is a dict but doesn't match expected tool call formats: {list(response.keys())}"
)
else:
logger.warning(
f"Unsupported response type at index {i}: {type(response)}"
)
continue
if not all_tool_calls:
logger.warning("No tool calls found in responses")
return []
logger.info(f"Found {len(all_tool_calls)} tool calls to execute")
# Execute tool calls concurrently
max_concurrent = max_concurrent or len(all_tool_calls)
semaphore = asyncio.Semaphore(max_concurrent)
async def execute_with_semaphore(tool_call_info):
async with semaphore:
response_index, tool_call = tool_call_info
function_name = tool_call.get("function", {}).get(
"name", "unknown"
)
if function_name not in server_tool_mapping:
logger.warning(
f"Function '{function_name}' not found on any server"
)
return {
"response_index": response_index,
"function_name": function_name,
"result": None,
"error": f"Function '{function_name}' not available on any server",
"status": "not_found",
}
server_info = server_tool_mapping[function_name]
result = await _execute_tool_on_server(
tool_call=tool_call,
server_info=server_info,
output_type=output_type,
)
result["response_index"] = response_index
return result
# Execute all tool calls concurrently
tasks = [
execute_with_semaphore(tool_call_info)
for tool_call_info in all_tool_calls
]
results = await asyncio.gather(*tasks, return_exceptions=True)
# Process results and handle exceptions
processed_results = []
for i, result in enumerate(results):
if isinstance(result, Exception):
logger.error(
f"Task {i} failed with exception: {str(result)}"
)
processed_results.append(
{
"response_index": (
all_tool_calls[i][0]
if i < len(all_tool_calls)
else -1
),
"function_name": "unknown",
"result": None,
"error": str(result),
"status": "exception",
}
)
else:
processed_results.append(result)
logger.info(
f"Completed execution of {len(processed_results)} tool calls"
)
return processed_results
def execute_multiple_tools_on_multiple_mcp_servers_sync(
responses: List[Dict[str, Any]],
urls: List[str],
connections: List[MCPConnection] = None,
output_type: Literal["json", "dict", "str", "formatted"] = "str",
max_concurrent: Optional[int] = None,
*args,
**kwargs,
) -> List[Dict[str, Any]]:
"""
Synchronous version of execute_multiple_tools_on_multiple_mcp_servers.
Args:
responses: List of responses containing tool calls (OpenAI format)
urls: List of MCP server URLs
connections: Optional list of MCPConnection objects corresponding to each URL
output_type: Output format type for results
max_concurrent: Maximum number of concurrent executions
Returns:
List of execution results with server metadata
"""
with get_or_create_event_loop() as loop:
try:
return loop.run_until_complete(
execute_multiple_tools_on_multiple_mcp_servers(
responses=responses,
urls=urls,
connections=connections,
output_type=output_type,
max_concurrent=max_concurrent,
*args,
**kwargs,
)
)
except Exception as e:
logger.error(
f"Error in execute_multiple_tools_on_multiple_mcp_servers_sync: {str(e)}"
)
raise MCPExecutionError(
f"Failed to execute multiple tools sync: {str(e)}"
)

@ -492,7 +492,6 @@ def convert_multiple_functions_to_openai_function_schema(
# ]
# Use 40% of cpu cores
max_workers = int(os.cpu_count() * 0.8)
print(f"max_workers: {max_workers}")
with concurrent.futures.ThreadPoolExecutor(
max_workers=max_workers

@ -20,6 +20,9 @@ from swarms.utils.output_types import HistoryOutputType
from swarms.utils.history_output_formatter import (
history_output_formatter,
)
from swarms.utils.check_all_model_max_tokens import (
check_all_model_max_tokens,
)
__all__ = [
@ -39,4 +42,5 @@ __all__ = [
"count_tokens",
"HistoryOutputType",
"history_output_formatter",
"check_all_model_max_tokens",
]

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save