Spaces:
Sleeping
Sleeping
File size: 7,940 Bytes
a1bf219 | 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 | """Agent-to-routing-policy matrix component for multi-provider configuration."""
from typing import Dict, List, Tuple
import gradio as gr
# List of agents in the system
AGENT_NAMES = [
"indicator_agent",
"pattern_agent",
"trend_agent",
"decision_agent",
"fundamentals_agent",
"sentiment_agent",
"news_agent",
"technical_analyst",
"risk_manager",
"portfolio_manager",
]
# Routing policy options
ROUTING_POLICIES = [
("Auto (Default)", "auto"),
("π Fastest Response", ":fastest"),
("π° Cheapest Cost", ":cheapest"),
("Groq", "groq"),
("Together AI", "together"),
("Replicate", "replicate"),
("Cerebras", "cerebras"),
("Fireworks", "fireworks"),
("DeepInfra", "deepinfra"),
("Llama 3.3 70B", "meta-llama/Llama-3.3-70B-Instruct"),
]
# Model tier options
MODEL_TIERS = [
("Fast (Lower Cost)", "fast"),
("Capable (Balanced)", "capable"),
("Advanced (Premium)", "vision"),
]
def create_agent_provider_matrix() -> Tuple[gr.Dataframe, Dict]:
"""
Create agent-to-routing-policy matrix UI component.
Returns:
Tuple of (dataframe component, agent_dropdowns dict)
"""
with gr.Accordion("π― Per-Agent Routing Configuration", open=False):
gr.Markdown("""
### Advanced Multi-Provider Configuration
Assign different routing policies to each agent for optimal cost/performance balance.
**Examples:**
- Fast agents (Indicator, Pattern) β `:cheapest` for cost savings
- Decision-critical agents (Decision, Portfolio Manager) β `:fastest` for best quality
- Research agents (News, Sentiment) β `groq` or specific providers
""")
# Create a dictionary to store dropdowns for each agent
agent_components = {}
with gr.Column():
# Header row
with gr.Row():
gr.Markdown("**Agent**", elem_classes=["matrix-header"])
gr.Markdown("**Routing Policy**", elem_classes=["matrix-header"])
gr.Markdown("**Model Tier**", elem_classes=["matrix-header"])
# Create a row for each agent
for agent_name in AGENT_NAMES:
with gr.Row():
# Agent name label
display_name = agent_name.replace("_", " ").title()
gr.Markdown(f"**{display_name}**")
# Routing policy dropdown
routing_dropdown = gr.Dropdown(
choices=ROUTING_POLICIES,
value="auto",
label="",
show_label=False,
container=False,
)
# Model tier dropdown
tier_dropdown = gr.Dropdown(
choices=MODEL_TIERS,
value="capable",
label="",
show_label=False,
container=False,
)
agent_components[agent_name] = {
"routing_policy": routing_dropdown,
"model_tier": tier_dropdown,
}
return agent_components
def get_agent_routing_config(agent_components: Dict) -> Dict[str, Dict[str, str]]:
"""
Extract current routing configuration from agent components.
Args:
agent_components: Dictionary of agent UI components
Returns:
Dictionary mapping agent names to their routing config
"""
config = {}
for agent_name, components in agent_components.items():
config[agent_name] = {
"routing_policy": components["routing_policy"].value,
"model_tier": components["model_tier"].value,
}
return config
def apply_routing_preset(preset: str, agent_components: Dict) -> List[gr.update]:
"""
Apply a routing preset to all agents.
Args:
preset: Preset name ("cost_optimized" or "performance_optimized")
agent_components: Dictionary of agent UI components
Returns:
List of Gradio updates for each dropdown
"""
updates = []
if preset == "cost_optimized":
# All agents use :cheapest routing
for agent_name in AGENT_NAMES:
updates.append(gr.update(value=":cheapest")) # routing policy
updates.append(gr.update(value="fast")) # model tier
elif preset == "performance_optimized":
# All agents use :fastest routing
for agent_name in AGENT_NAMES:
updates.append(gr.update(value=":fastest")) # routing policy
updates.append(gr.update(value="vision")) # model tier
elif preset == "balanced":
# Fast agents use :cheapest, decision agents use :fastest
fast_agents = ["indicator_agent", "pattern_agent", "trend_agent"]
decision_agents = ["decision_agent", "portfolio_manager", "risk_manager"]
for agent_name in AGENT_NAMES:
if agent_name in fast_agents:
updates.append(gr.update(value=":cheapest"))
updates.append(gr.update(value="fast"))
elif agent_name in decision_agents:
updates.append(gr.update(value=":fastest"))
updates.append(gr.update(value="vision"))
else:
updates.append(gr.update(value="auto"))
updates.append(gr.update(value="capable"))
else:
# Reset to defaults
for agent_name in AGENT_NAMES:
updates.append(gr.update(value="auto"))
updates.append(gr.update(value="capable"))
return updates
def format_routing_config_status(config: Dict[str, Dict[str, str]]) -> str:
"""
Format routing configuration status message.
Args:
config: Agent routing configuration dictionary
Returns:
Formatted status string
"""
if not config:
return "Using global routing policy for all agents"
# Count unique routing policies
policies = {}
for agent_name, agent_config in config.items():
policy = agent_config.get("routing_policy", "auto")
if policy not in policies:
policies[policy] = []
policies[policy].append(agent_name.replace("_", " ").title())
# Format status
status_lines = ["β
Per-Agent Routing Active:"]
for policy, agents in policies.items():
if policy.startswith(":"):
policy_display = policy.upper()
elif "/" in policy:
policy_display = policy.split("/")[-1]
else:
policy_display = policy.title()
status_lines.append(f" β’ {policy_display}: {len(agents)} agents")
return "\n".join(status_lines)
def export_routing_config(config: Dict[str, Dict[str, str]]) -> str:
"""
Export routing configuration as JSON string.
Args:
config: Agent routing configuration dictionary
Returns:
JSON string of configuration
"""
import json
return json.dumps(config, indent=2)
def import_routing_config(config_json: str) -> Dict[str, Dict[str, str]]:
"""
Import routing configuration from JSON string.
Args:
config_json: JSON string of configuration
Returns:
Parsed configuration dictionary
"""
import json
try:
config = json.loads(config_json)
# Validate structure
if not isinstance(config, dict):
raise ValueError("Configuration must be a dictionary")
for agent_name, agent_config in config.items():
if not isinstance(agent_config, dict):
raise ValueError(f"Agent config for {agent_name} must be a dictionary")
if "routing_policy" not in agent_config:
raise ValueError(f"Missing routing_policy for {agent_name}")
return config
except json.JSONDecodeError as e:
raise ValueError(f"Invalid JSON: {str(e)}")
|