|
| 1 | +""" |
| 2 | +GraphWorkflow Token Streaming Example |
| 3 | +
|
| 4 | +Demonstrates real token-by-token streaming from multiple agents. |
| 5 | +You'll see tokens appear in the terminal as each agent generates |
| 6 | +them, with color-coded labels showing which agent is "speaking". |
| 7 | +
|
| 8 | +Architecture: |
| 9 | + Coordinator (Layer 0) |
| 10 | + -> Market-Analyst (Layer 1, parallel) |
| 11 | + -> Tech-Analyst (Layer 1, parallel) |
| 12 | + -> Risk-Analyst (Layer 1, parallel) |
| 13 | + -> Synthesizer (Layer 2) |
| 14 | +""" |
| 15 | + |
| 16 | +import sys |
| 17 | +import threading |
| 18 | +import time |
| 19 | + |
| 20 | +from swarms.structs.agent import Agent |
| 21 | +from swarms.structs.graph_workflow import GraphWorkflow |
| 22 | + |
| 23 | +# ANSI colors for each agent |
| 24 | +COLORS = { |
| 25 | + "Coordinator": "\033[96m", # cyan |
| 26 | + "Market-Analyst": "\033[93m", # yellow |
| 27 | + "Tech-Analyst": "\033[92m", # green |
| 28 | + "Risk-Analyst": "\033[91m", # red |
| 29 | + "Synthesizer": "\033[95m", # magenta |
| 30 | +} |
| 31 | +RESET = "\033[0m" |
| 32 | +BOLD = "\033[1m" |
| 33 | + |
| 34 | +# Lock to avoid garbled output from parallel agents |
| 35 | +print_lock = threading.Lock() |
| 36 | + |
| 37 | + |
| 38 | +def create_agent(name: str, description: str) -> Agent: |
| 39 | + return Agent( |
| 40 | + agent_name=name, |
| 41 | + agent_description=description, |
| 42 | + system_prompt=f"You are {name}. {description} Keep your response to 2-3 sentences.", |
| 43 | + model_name="gpt-5.4", |
| 44 | + max_loops=1, |
| 45 | + verbose=False, |
| 46 | + print_on=False, |
| 47 | + streaming_on=True, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + # -- Build agents -- |
| 53 | + coordinator = create_agent( |
| 54 | + "Coordinator", |
| 55 | + "You coordinate analysis tasks. Briefly outline what each team member should focus on.", |
| 56 | + ) |
| 57 | + market_analyst = create_agent( |
| 58 | + "Market-Analyst", |
| 59 | + "You analyse market trends and competitive landscape.", |
| 60 | + ) |
| 61 | + tech_analyst = create_agent( |
| 62 | + "Tech-Analyst", |
| 63 | + "You evaluate technical feasibility and architecture.", |
| 64 | + ) |
| 65 | + risk_analyst = create_agent( |
| 66 | + "Risk-Analyst", |
| 67 | + "You identify risks and propose mitigations.", |
| 68 | + ) |
| 69 | + synthesizer = create_agent( |
| 70 | + "Synthesizer", |
| 71 | + "You synthesize inputs from multiple analysts into a concise executive summary.", |
| 72 | + ) |
| 73 | + |
| 74 | + # -- Build workflow -- |
| 75 | + workflow = GraphWorkflow(name="Streaming-Demo") |
| 76 | + for agent in [coordinator, market_analyst, tech_analyst, risk_analyst, synthesizer]: |
| 77 | + workflow.add_node(agent) |
| 78 | + |
| 79 | + workflow.add_edges_from_source( |
| 80 | + "Coordinator", |
| 81 | + ["Market-Analyst", "Tech-Analyst", "Risk-Analyst"], |
| 82 | + ) |
| 83 | + workflow.add_edges_to_target( |
| 84 | + ["Market-Analyst", "Tech-Analyst", "Risk-Analyst"], |
| 85 | + "Synthesizer", |
| 86 | + ) |
| 87 | + |
| 88 | + # -- Token-by-token streaming callback -- |
| 89 | + # Track which agents have printed their header |
| 90 | + active_agents = {} |
| 91 | + |
| 92 | + def on_token(node_id: str, token: str) -> None: |
| 93 | + color = COLORS.get(node_id, "") |
| 94 | + with print_lock: |
| 95 | + if node_id not in active_agents: |
| 96 | + active_agents[node_id] = True |
| 97 | + sys.stdout.write(f"\n{color}{BOLD}[{node_id}]{RESET}{color} ") |
| 98 | + sys.stdout.write(f"{color}{token}{RESET}") |
| 99 | + sys.stdout.flush() |
| 100 | + |
| 101 | + def on_complete(node_id: str, output) -> None: |
| 102 | + with print_lock: |
| 103 | + sys.stdout.write("\n") |
| 104 | + sys.stdout.flush() |
| 105 | + # Clear so next run of same agent gets a new header |
| 106 | + active_agents.pop(node_id, None) |
| 107 | + |
| 108 | + # -- Run -- |
| 109 | + task = "Evaluate the feasibility of launching an AI-powered personal finance assistant." |
| 110 | + |
| 111 | + print(f"{BOLD}{'=' * 60}") |
| 112 | + print(" GraphWorkflow Token Streaming Demo") |
| 113 | + print(f"{'=' * 60}{RESET}") |
| 114 | + print(f"\n Task: {task}\n") |
| 115 | + |
| 116 | + start = time.time() |
| 117 | + result = workflow.run( |
| 118 | + task, |
| 119 | + streaming_callback=on_token, |
| 120 | + on_node_complete=on_complete, |
| 121 | + ) |
| 122 | + elapsed = time.time() - start |
| 123 | + |
| 124 | + print(f"\n{BOLD}{'=' * 60}") |
| 125 | + print(f" Done in {elapsed:.1f}s | Agents: {list(result.keys())}") |
| 126 | + print(f"{'=' * 60}{RESET}") |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + main() |
0 commit comments