|
| 1 | +# MALT (Multi-Agent Learning Task) Example |
| 2 | + |
| 3 | +MALT is a sophisticated multi-agent architecture designed for complex problem-solving tasks, particularly mathematical proofs and rigorous analysis. It uses a three-agent system: a creator, verifier, and refiner, working together to produce high-quality outputs through iterative refinement. |
| 4 | + |
| 5 | +## How It Works |
| 6 | + |
| 7 | +1. **Creator Agent**: Generates the initial solution or proof |
| 8 | +2. **Verifier Agents**: Multiple verifiers run concurrently to check the solution |
| 9 | +3. **Majority Voting**: Consensus is reached on verification results |
| 10 | +4. **Refiner Agents**: Multiple refiners improve the solution based on feedback |
| 11 | +5. **Iterative Process**: Can run multiple loops for continuous improvement |
| 12 | + |
| 13 | +This architecture is ideal for: |
| 14 | +- Mathematical proofs and theorem generation |
| 15 | +- Complex problem-solving requiring verification |
| 16 | +- Tasks needing rigorous validation |
| 17 | +- Academic and research applications |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Install the swarms package using pip: |
| 22 | + |
| 23 | +```bash |
| 24 | +pip install -U swarms |
| 25 | +``` |
| 26 | + |
| 27 | +## Basic Setup |
| 28 | + |
| 29 | +1. First, set up your environment variables: |
| 30 | + |
| 31 | +```python |
| 32 | +WORKSPACE_DIR="agent_workspace" |
| 33 | +OPENAI_API_KEY="your-api-key" |
| 34 | +``` |
| 35 | + |
| 36 | +## Step-by-Step Example |
| 37 | + |
| 38 | +### Step 1: Import Required Modules |
| 39 | + |
| 40 | +```python |
| 41 | +from swarms import Agent, MALT |
| 42 | +``` |
| 43 | + |
| 44 | +### Step 2: Create MALT with Preset Agents (Recommended) |
| 45 | + |
| 46 | +The easiest way is to use MALT's preset agents, which are optimized for mathematical proofs: |
| 47 | + |
| 48 | +```python |
| 49 | +malt = MALT( |
| 50 | + preset_agents=True, # Uses optimized proof creator, verifier, and refiner |
| 51 | + max_loops=1, |
| 52 | + return_dict=False, # Return as string |
| 53 | +) |
| 54 | +``` |
| 55 | + |
| 56 | +### Step 3: Run MALT on a Task |
| 57 | + |
| 58 | +```python |
| 59 | +task = "Prove that the sum of two even numbers is always even" |
| 60 | + |
| 61 | +result = malt.run(task=task) |
| 62 | + |
| 63 | +print(result) |
| 64 | +``` |
| 65 | + |
| 66 | +## Custom Agents Example |
| 67 | + |
| 68 | +### Step 1: Create Custom Agents |
| 69 | + |
| 70 | +```python |
| 71 | +# Creator Agent: Generates solutions |
| 72 | +creator = Agent( |
| 73 | + agent_name="Solution-Creator", |
| 74 | + system_prompt="""You are an expert problem solver. Generate comprehensive |
| 75 | + solutions with clear reasoning and step-by-step explanations.""", |
| 76 | + model_name="gpt-4o-mini", |
| 77 | + max_loops=1, |
| 78 | +) |
| 79 | + |
| 80 | +# Verifier Agent: Validates solutions |
| 81 | +verifier = Agent( |
| 82 | + agent_name="Solution-Verifier", |
| 83 | + system_prompt="""You are a rigorous validator. Check solutions for correctness, |
| 84 | + logical consistency, and completeness. Identify any errors or gaps.""", |
| 85 | + model_name="gpt-4o-mini", |
| 86 | + max_loops=1, |
| 87 | +) |
| 88 | + |
| 89 | +# Refiner Agent: Improves solutions |
| 90 | +refiner = Agent( |
| 91 | + agent_name="Solution-Refiner", |
| 92 | + system_prompt="""You are a solution refiner. Take verified feedback and improve |
| 93 | + the solution by addressing identified issues and enhancing clarity.""", |
| 94 | + model_name="gpt-4o-mini", |
| 95 | + max_loops=1, |
| 96 | +) |
| 97 | +``` |
| 98 | + |
| 99 | +### Step 2: Create MALT with Custom Agents |
| 100 | + |
| 101 | +```python |
| 102 | +malt = MALT( |
| 103 | + main_agent=creator, |
| 104 | + verifier_agent=verifier, |
| 105 | + refiner_agent=refiner, |
| 106 | + max_loops=1, |
| 107 | + return_dict=False, |
| 108 | +) |
| 109 | +``` |
| 110 | + |
| 111 | +### Step 3: Run the Task |
| 112 | + |
| 113 | +```python |
| 114 | +task = "Solve: If a train travels 120 km in 2 hours, what is its average speed? Show your work." |
| 115 | + |
| 116 | +result = malt.run(task=task) |
| 117 | +print(result) |
| 118 | +``` |
| 119 | + |
| 120 | +## Understanding the Process |
| 121 | + |
| 122 | +1. **Creation Phase**: The creator agent generates an initial solution |
| 123 | +2. **Verification Phase**: Three verifier agents run concurrently to check the solution |
| 124 | +3. **Voting Phase**: A majority voting agent synthesizes the verification results |
| 125 | +4. **Refinement Phase**: Three refiner agents improve the solution based on feedback |
| 126 | +5. **Output**: The refined solution is returned |
| 127 | + |
| 128 | +## Output Formats |
| 129 | + |
| 130 | +### String Format (Default) |
| 131 | + |
| 132 | +```python |
| 133 | +malt = MALT( |
| 134 | + preset_agents=True, |
| 135 | + return_list=False, |
| 136 | + return_dict=False, |
| 137 | +) |
| 138 | + |
| 139 | +result = malt.run(task="Your task here") |
| 140 | +# Returns: str |
| 141 | +``` |
| 142 | + |
| 143 | +### List Format |
| 144 | + |
| 145 | +```python |
| 146 | +malt = MALT( |
| 147 | + preset_agents=True, |
| 148 | + return_list=True, |
| 149 | + return_dict=False, |
| 150 | +) |
| 151 | + |
| 152 | +result = malt.run(task="Your task here") |
| 153 | +# Returns: list of messages |
| 154 | +``` |
| 155 | + |
| 156 | +### Dictionary Format |
| 157 | + |
| 158 | +```python |
| 159 | +malt = MALT( |
| 160 | + preset_agents=True, |
| 161 | + return_list=False, |
| 162 | + return_dict=True, |
| 163 | +) |
| 164 | + |
| 165 | +result = malt.run(task="Your task here") |
| 166 | +# Returns: dict of messages |
| 167 | +``` |
| 168 | + |
| 169 | +## Multiple Iterations |
| 170 | + |
| 171 | +You can run multiple loops for iterative improvement: |
| 172 | + |
| 173 | +```python |
| 174 | +malt = MALT( |
| 175 | + preset_agents=True, |
| 176 | + max_loops=3, # Run 3 iterations |
| 177 | + return_dict=False, |
| 178 | +) |
| 179 | + |
| 180 | +result = malt.run(task="Prove the Pythagorean theorem") |
| 181 | +``` |
| 182 | + |
| 183 | +## Batch Processing |
| 184 | + |
| 185 | +Process multiple tasks: |
| 186 | + |
| 187 | +```python |
| 188 | +tasks = [ |
| 189 | + "Prove that sqrt(2) is irrational", |
| 190 | + "Prove that the sum of angles in a triangle is 180 degrees", |
| 191 | + "Prove that 0.999... = 1", |
| 192 | +] |
| 193 | + |
| 194 | +results = malt.run_batched(tasks) |
| 195 | + |
| 196 | +print(results) |
| 197 | +``` |
| 198 | + |
| 199 | +## Use Cases |
| 200 | + |
| 201 | +### Mathematical Proofs |
| 202 | + |
| 203 | +```python |
| 204 | +malt = MALT(preset_agents=True, max_loops=1) |
| 205 | +result = malt.run("Prove that there are infinitely many prime numbers") |
| 206 | +``` |
| 207 | + |
| 208 | +### Problem Solving |
| 209 | + |
| 210 | +```python |
| 211 | +malt = MALT(preset_agents=True, max_loops=2) |
| 212 | +result = malt.run(""" |
| 213 | +Solve this optimization problem: |
| 214 | +Maximize f(x,y) = 2x + 3y subject to: |
| 215 | +- x + y <= 10 |
| 216 | +- 2x + y <= 16 |
| 217 | +- x >= 0, y >= 0 |
| 218 | +""") |
| 219 | +``` |
| 220 | + |
| 221 | +### Algorithm Verification |
| 222 | + |
| 223 | +```python |
| 224 | +malt = MALT(preset_agents=True, max_loops=1) |
| 225 | +result = malt.run(""" |
| 226 | +Verify the correctness of this algorithm: |
| 227 | +1. Sort the array |
| 228 | +2. Use binary search to find the target |
| 229 | +3. Return the index if found, -1 otherwise |
| 230 | +
|
| 231 | +Provide a proof of correctness. |
| 232 | +""") |
| 233 | +``` |
| 234 | + |
| 235 | +## Support and Community |
| 236 | + |
| 237 | +If you're facing issues or want to learn more, check out the following resources: |
| 238 | + |
| 239 | +| Platform | Link | Description | |
| 240 | +|----------|------|-------------| |
| 241 | +| 📚 Documentation | [docs.swarms.world](https://docs.swarms.world) | Official documentation and guides | |
| 242 | +| 💬 Discord | [Join Discord](https://discord.gg/EamjgSaEQf) | Live chat and community support | |
| 243 | +| 🐦 Twitter | [@swarms_corp](https://x.com/swarms_corp) | Latest news and announcements | |
| 244 | + |
0 commit comments