AutoGen Integration¶
Semantic speaker selection and group chat routing with Microsoft AutoGen.
Installation¶
Overview¶
AutoGen orchestrates multi-agent conversations through group chats. StrataRouter replaces AutoGen's default speaker selection with semantic routing — choosing the right agent based on the meaning of the conversation, not just round-robin or manual rules.
Quick Start¶
import autogen
from stratarouter import Router, Route
from stratarouter.integrations.autogen import StrataRouterSpeakerSelector
# Define your AutoGen agents
billing_agent = autogen.AssistantAgent(
name="BillingAgent",
system_message="You specialize in billing, invoices, and payments.",
llm_config={"model": "gpt-4o"}
)
support_agent = autogen.AssistantAgent(
name="SupportAgent",
system_message="You specialize in technical support and bug resolution.",
llm_config={"model": "gpt-4o"}
)
user_proxy = autogen.UserProxyAgent(
name="UserProxy",
human_input_mode="NEVER",
code_execution_config=False
)
# Build router
router = Router(dimension=384)
billing = Route("BillingAgent")
billing.examples = ["invoice", "payment failed", "refund request"]
billing.keywords = ["invoice", "payment", "refund", "billing"]
router.add_route(billing)
support = Route("SupportAgent")
support.examples = ["app crash", "login error", "bug report"]
support.keywords = ["crash", "error", "bug", "broken"]
router.add_route(support)
router.build_index()
# Create semantic speaker selector
selector = StrataRouterSpeakerSelector(router)
# Use in GroupChat
groupchat = autogen.GroupChat(
agents=[user_proxy, billing_agent, support_agent],
messages=[],
max_round=5,
speaker_selection_method=selector.select_speaker
)
manager = autogen.GroupChatManager(groupchat=groupchat, llm_config={"model": "gpt-4o"})
# Start conversation
user_proxy.initiate_chat(manager, message="My invoice for last month is missing.")
Custom Speaker Selection¶
Implement fine-grained control over agent selection:
class StrataRouterSpeakerSelector:
def __init__(self, router: Router, fallback_agent: str = None):
self.router = router
self.fallback = fallback_agent
def select_speaker(
self,
last_speaker: autogen.Agent,
groupchat: autogen.GroupChat
) -> autogen.Agent:
# Get last message
last_message = groupchat.messages[-1]["content"]
# Route semantically
embedding = embed(last_message)
result = self.router.route(last_message, embedding)
if result.confidence < 0.5 and self.fallback:
# Low confidence — use fallback
target_name = self.fallback
else:
target_name = result.route_id
# Find the agent by name
for agent in groupchat.agents:
if agent.name == target_name:
return agent
# Default to first non-user agent
return groupchat.agents[1]
Conversation-Aware Routing¶
Route based on the full conversation context, not just the last message:
def context_aware_select(last_speaker, groupchat):
# Build context from recent messages
recent = groupchat.messages[-3:]
context = " ".join(m["content"] for m in recent)
# Route on full context
embedding = embed(context)
result = router.route(context, embedding)
# Find target agent
for agent in groupchat.agents:
if agent.name == result.route_id:
return agent
groupchat = autogen.GroupChat(
agents=[user_proxy, billing_agent, support_agent],
messages=[],
max_round=10,
speaker_selection_method=context_aware_select
)