Copy
Ask AI
"""
String Selector
===============
Demonstrates returning a step name string from a router selector.
"""
from typing import List, Union
from agno.agent.agent import Agent
from agno.models.openai import OpenAIChat
from agno.workflow.router import Router
from agno.workflow.step import Step
from agno.workflow.types import StepInput
from agno.workflow.workflow import Workflow
# ---------------------------------------------------------------------------
# Create Agents
# ---------------------------------------------------------------------------
tech_expert = Agent(
name="tech_expert",
model=OpenAIChat(id="gpt-4o-mini"),
instructions="You are a tech expert. Provide technical analysis.",
)
biz_expert = Agent(
name="biz_expert",
model=OpenAIChat(id="gpt-4o-mini"),
instructions="You are a business expert. Provide business insights.",
)
generalist = Agent(
name="generalist",
model=OpenAIChat(id="gpt-4o-mini"),
instructions="You are a generalist. Provide general information.",
)
# ---------------------------------------------------------------------------
# Define Steps
# ---------------------------------------------------------------------------
tech_step = Step(name="Tech Research", agent=tech_expert)
business_step = Step(name="Business Research", agent=biz_expert)
general_step = Step(name="General Research", agent=generalist)
# ---------------------------------------------------------------------------
# Define Router Selector
# ---------------------------------------------------------------------------
def route_by_topic(step_input: StepInput) -> Union[str, Step, List[Step]]:
topic = step_input.input.lower()
if "tech" in topic or "ai" in topic or "software" in topic:
return "Tech Research"
if "business" in topic or "market" in topic or "finance" in topic:
return "Business Research"
return "General Research"
# ---------------------------------------------------------------------------
# Create Workflow
# ---------------------------------------------------------------------------
workflow = Workflow(
name="Expert Routing (String Selector)",
steps=[
Router(
name="Topic Router",
selector=route_by_topic,
choices=[tech_step, business_step, general_step],
),
],
)
# ---------------------------------------------------------------------------
# Run Workflow
# ---------------------------------------------------------------------------
if __name__ == "__main__":
workflow.print_response("Tell me about AI trends", stream=True)
Run the Example
Copy
Ask AI
# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/04_workflows/05_conditional_branching
# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate
python string_selector.py