File size: 8,724 Bytes
9abace2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | """
Stage 10 + 11: AgentDispatch + MergeOutput
AgentDispatch:
Takes the sparse routing weights and fires the active experts.
Each expert is an async callable: (input, context) -> output.
Experts run concurrently (asyncio.gather).
Timeout per expert — stalled experts are dropped, not waited on.
MergeOutput:
Combines expert outputs using their routing weights.
Strategy options:
- weighted_concat: concatenate with weight annotation
- weighted_avg: average numeric fields by weight
- highest_weight: return only the top-weighted output
- ensemble_text: join text outputs with weight-proportional prominence
"""
from __future__ import annotations
import asyncio
from dataclasses import dataclass, field
from typing import Any, Callable, Awaitable
from .sparse import RoutingWeights
# ==========================================
# Expert type
# ==========================================
Expert = Callable[[str, dict[str, Any]], Awaitable[dict[str, Any]]]
# ==========================================
# Dispatch
# ==========================================
@dataclass
class ExpertResult:
expert_name: str
weight: float
output: dict[str, Any] | None
success: bool
error: str | None = None
latency_ms: float = 0.0
@dataclass
class DispatchResult:
expert_results: list[ExpertResult]
active_count: int
success_count: int
failed_experts: list[str]
routing_weights: RoutingWeights
merged_output: dict[str, Any]
class AgentDispatch:
"""
Stage 10: Fire active experts concurrently.
"""
def __init__(
self,
experts: dict[str, Expert],
timeout_ms: int = 30000,
merge_strategy: str = "weighted_concat"
):
self.experts = experts
self.timeout_s = timeout_ms / 1000.0
self.merge_strategy = merge_strategy
self._merger = MergeOutput()
async def dispatch(
self,
input_text: str,
context: dict[str, Any],
routing: RoutingWeights
) -> DispatchResult:
active = routing.active_experts
tasks = []
for expert_name in active:
weight = routing.weights.get(expert_name, 0.0)
if weight < 1e-6:
continue
expert_fn = self.experts.get(expert_name)
if expert_fn is None:
continue
task = self._run_expert(expert_name, weight, expert_fn, input_text, context)
tasks.append(task)
if not tasks:
return DispatchResult(
expert_results=[],
active_count=0,
success_count=0,
failed_experts=[],
routing_weights=routing,
merged_output={"error": "No active experts after dispatch"}
)
results: list[ExpertResult] = await asyncio.gather(*tasks)
failed = [r.expert_name for r in results if not r.success]
succeeded = [r for r in results if r.success]
merged = self._merger.merge(succeeded, self.merge_strategy)
return DispatchResult(
expert_results=results,
active_count=len(tasks),
success_count=len(succeeded),
failed_experts=failed,
routing_weights=routing,
merged_output=merged
)
async def _run_expert(
self,
name: str,
weight: float,
fn: Expert,
input_text: str,
context: dict[str, Any]
) -> ExpertResult:
import time
t0 = time.monotonic()
try:
output = await asyncio.wait_for(
fn(input_text, context),
timeout=self.timeout_s
)
latency = (time.monotonic() - t0) * 1000
return ExpertResult(
expert_name=name,
weight=weight,
output=output,
success=True,
latency_ms=latency
)
except asyncio.TimeoutError:
return ExpertResult(
expert_name=name,
weight=weight,
output=None,
success=False,
error=f"Timeout after {self.timeout_s}s"
)
except Exception as e:
return ExpertResult(
expert_name=name,
weight=weight,
output=None,
success=False,
error=str(e)
)
# ==========================================
# Merge Output
# ==========================================
class MergeOutput:
"""
Stage 11: Combine expert outputs using routing weights.
"""
def merge(
self,
results: list[ExpertResult],
strategy: str = "weighted_concat"
) -> dict[str, Any]:
if not results:
return {"output": None, "experts": []}
if strategy == "highest_weight":
return self._highest_weight(results)
elif strategy == "weighted_avg":
return self._weighted_avg(results)
elif strategy == "ensemble_text":
return self._ensemble_text(results)
else:
return self._weighted_concat(results)
def _weighted_concat(self, results: list[ExpertResult]) -> dict[str, Any]:
"""
Return all outputs annotated with their weights.
Agents downstream can decide how to use them.
"""
total_weight = sum(r.weight for r in results)
outputs = []
for r in sorted(results, key=lambda x: x.weight, reverse=True):
normalized_weight = r.weight / max(total_weight, 1e-6)
outputs.append({
"expert": r.expert_name,
"weight": normalized_weight,
"output": r.output,
"latency_ms": r.latency_ms
})
# Primary output = highest weighted expert's output
primary = results[0].output if results else {}
return {
"output": primary,
"expert_outputs": outputs,
"strategy": "weighted_concat",
"expert_count": len(results)
}
def _highest_weight(self, results: list[ExpertResult]) -> dict[str, Any]:
best = max(results, key=lambda r: r.weight)
return {
"output": best.output,
"expert": best.expert_name,
"weight": best.weight,
"strategy": "highest_weight"
}
def _weighted_avg(self, results: list[ExpertResult]) -> dict[str, Any]:
"""
Average numeric values across outputs, weighted by routing weight.
Non-numeric keys taken from highest-weight expert.
"""
total_weight = sum(r.weight for r in results) or 1.0
merged: dict[str, Any] = {}
# Collect all keys
all_keys: set[str] = set()
for r in results:
if r.output:
all_keys.update(r.output.keys())
for key in all_keys:
# Try weighted average of numeric values
numeric_vals = []
for r in results:
if r.output and key in r.output:
val = r.output[key]
if isinstance(val, (int, float)):
numeric_vals.append((val, r.weight))
if numeric_vals:
merged[key] = sum(v * w for v, w in numeric_vals) / total_weight
else:
# Non-numeric: take from highest-weight expert
best = max(results, key=lambda r: r.weight)
if best.output and key in best.output:
merged[key] = best.output[key]
merged["strategy"] = "weighted_avg"
return merged
def _ensemble_text(self, results: list[ExpertResult]) -> dict[str, Any]:
"""
Join text outputs in weight order.
Higher-weight experts appear first.
"""
sorted_results = sorted(results, key=lambda r: r.weight, reverse=True)
parts = []
for r in sorted_results:
if r.output:
text = r.output.get("text") or r.output.get("content") or str(r.output)
pct = int(r.weight * 100)
parts.append(f"[{r.expert_name} {pct}%]: {text}")
return {
"output": "\n\n".join(parts),
"strategy": "ensemble_text",
"expert_count": len(results)
}
|