|
20 | 20 | class MixtureOfAgents: |
21 | 21 | """Run a layered Mixture-of-Agents workflow. |
22 | 22 |
|
23 | | - ``MixtureOfAgents`` sends the task and accumulated conversation |
24 | | - context to each worker agent concurrently for each configured layer. |
25 | | - After all layers complete, the aggregator agent receives the full |
26 | | - conversation and produces the final synthesized answer. |
| 23 | + ``MixtureOfAgents`` runs workers in parallel across multiple layers, |
| 24 | + then synthesises their outputs with an aggregator agent. |
| 25 | +
|
| 26 | + Worker context per layer: |
| 27 | + - Layer 0: each worker receives only the original task. |
| 28 | + - Layer 1+: each worker receives the original task plus the |
| 29 | + concatenated outputs from the previous layer. |
| 30 | +
|
| 31 | + The aggregator always receives the full conversation transcript. |
27 | 32 |
|
28 | 33 | Args: |
29 | 34 | id: Optional identifier accepted for API compatibility. |
@@ -158,8 +163,8 @@ def step( |
158 | 163 | """Run one worker layer concurrently. |
159 | 164 |
|
160 | 165 | Args: |
161 | | - task: Task or accumulated conversation context to send to each |
162 | | - worker agent. |
| 166 | + task: On layer 0 this is the raw user task. On later layers it |
| 167 | + is ``"Original task: …\\n\\nPrevious layer synthesis:\\n…"``. |
163 | 168 | img: Optional image path, URL, or encoded image payload passed |
164 | 169 | through to each worker agent. |
165 | 170 |
|
@@ -192,20 +197,31 @@ def _run( |
192 | 197 |
|
193 | 198 | self.conversation.add(role="User", content=task) |
194 | 199 |
|
195 | | - full_context = self.conversation.get_str() |
| 200 | + # Workers receive only the original task on the first layer, and |
| 201 | + # task + previous-layer synthesis on subsequent layers. This avoids |
| 202 | + # re-sending the full growing transcript to every worker on every layer. |
| 203 | + worker_input = task |
| 204 | + prev_layer_output: Optional[str] = None |
196 | 205 |
|
197 | 206 | for i in range(self.layers): |
198 | | - # Pass the full context/history string to the step method |
199 | | - step_output = self.step(task=full_context, img=img) |
| 207 | + if prev_layer_output is not None: |
| 208 | + worker_input = ( |
| 209 | + f"Original task: {task}\n\n" |
| 210 | + f"Previous layer synthesis:\n{prev_layer_output}" |
| 211 | + ) |
| 212 | + |
| 213 | + step_output = self.step(task=worker_input, img=img) |
200 | 214 |
|
201 | | - # Log each agent's output with full context awareness |
202 | 215 | for agent_name, agent_output in step_output.items(): |
203 | 216 | self.conversation.add( |
204 | 217 | role=agent_name, content=agent_output |
205 | 218 | ) |
206 | 219 |
|
207 | | - # Update the full_context with the latest conversation history |
208 | | - full_context = self.conversation.get_str() |
| 220 | + # Summarise the layer as the concatenation of worker outputs so |
| 221 | + # the next layer has a compact view of what was produced. |
| 222 | + prev_layer_output = "\n\n".join( |
| 223 | + f"{name}: {out}" for name, out in step_output.items() |
| 224 | + ) |
209 | 225 |
|
210 | 226 | aggregator_output = self.aggregator_agent.run( |
211 | 227 | task=self.conversation.get_str() |
|
0 commit comments