Repository navigation
Expand file tree
/
Copy pathworkflow.py
More file actions
148 lines (109 loc) · 5.47 KB
/
Copy pathworkflow.py
File metadata and controls
148 lines (109 loc) · 5.47 KB
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
"""L2a · Parallel fan-out + JoinNode (Pillar 1, part 1 of 2).
The first half of a graph workflow. Three fetches run IN PARALLEL (0 LLM), a
`JoinNode` bundles them into one typed payload, and a single strategy agent
writes the plan. There's no router yet — every run uses the same one agent.
L2b adds the deterministic router that picks a specialized agent.
START ─► fetch_weather ──┐
START ─► analyze_course ─┼─► JoinNode ─► strategy (1 agent)
START ─► pull_fitness ───┘
Run it:
python -m L2a_parallel_join.workflow # HOT
python -m L2a_parallel_join.workflow COLD
"""
from __future__ import annotations
import asyncio
import os
import sys
import time
from dotenv import load_dotenv
from google.adk import Agent, Event, Runner, Workflow
from google.adk.workflow import JoinNode, START
from google.adk.sessions import InMemorySessionService
from shared import BundledRunData, RaceStrategy, scenario, slow_mo
load_dotenv()
# [local-only]
# Fail fast with one readable line. Without this, a missing key surfaces ~290
# lines of ADK/asyncio traceback with the real cause on the very last line.
_vertex = os.getenv("GOOGLE_GENAI_USE_VERTEXAI", "").lower() in ("true", "1", "yes")
if not (os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY") or _vertex):
sys.exit(
"\u2717 No API key found.\n"
" cp .env.example .env then add GOOGLE_API_KEY=...\n"
" Get a free key at https://aistudio.google.com/apikey\n"
" (On GCP: export GOOGLE_GENAI_USE_VERTEXAI=True + GOOGLE_CLOUD_PROJECT instead.)"
)
# [/local-only]
MODEL = os.getenv("ADK_MODEL", "gemini-flash-latest") # AI Studio alias; on Vertex set ADK_MODEL=gemini-2.5-flash
if os.getenv("GOOGLE_GENAI_USE_VERTEXAI", "").lower() in ("true", "1", "yes") and "latest" in MODEL:
sys.exit(
f"\u2717 ADK_MODEL={MODEL!r} is an AI-Studio-only alias and 404s on Vertex AI.\n"
" export ADK_MODEL=gemini-2.5-flash (then re-run)"
)
# ─── Fetch nodes: parallel, zero LLM. Simulated latency so you SEE concurrency. ─
#
# Each fetch prints when it starts and finishes, relative to the start of the run.
# That overlap is the actual evidence for the parallel claim — all three start at
# ~0.0s and the fan-out ends when the SLOWEST one does, not when their durations
# add up. (Wall time for the whole run is a bad proxy: it also contains the
# strategy agent's LLM call, which is several seconds on its own.)
_T0 = 0.0
def _stamp(msg: str) -> None:
print(f" [t={time.perf_counter() - _T0:4.1f}s] {msg}")
async def _fetch(name: str, seconds: float, key: str):
_stamp(f"{name} started")
await asyncio.sleep(seconds * slow_mo())
_stamp(f"{name} finished")
return Event(output=scenario()[key].model_dump())
async def fetch_weather(node_input):
return await _fetch("fetch_weather", 1.5, "weather")
async def analyze_course(node_input):
return await _fetch("analyze_course", 2.0, "course")
async def pull_fitness(node_input):
return await _fetch("pull_fitness", 1.0, "fitness")
# ─── Join: bundle the three parallel results into one typed payload. ──────────
join_inputs = JoinNode(name="join_inputs") # ★ waits for ALL branches, bundles outputs keyed by function name
# ─── One strategy agent (no routing yet). ─────────────────────────────────────
strategy = Agent(
name="strategy", model=MODEL,
input_schema=BundledRunData, output_schema=RaceStrategy,
instruction="""You are a marathon coach. You receive BundledRunData (weather from
fetch_weather, course from analyze_course, fitness from pull_fitness). Produce a
RaceStrategy that ADAPTS to whatever the conditions are — hot, cold, or ideal.
Cite actual numbers (temps, mile numbers, the runner's pace). Each field 1-2
short sentences.""",
)
# ─── The workflow: fan-out, join, one agent. ──────────────────────────────────
root = Workflow(
name="l2a_parallel_join",
description="Parallel data gathering + a single strategy agent.",
edges=[
# ★ three edges from START — this IS the fan-out
(START, fetch_weather, join_inputs),
(START, analyze_course, join_inputs),
(START, pull_fitness, join_inputs),
(join_inputs, strategy),
],
)
# [harness]
def _event_text(event):
msg = getattr(event, "message", None)
if msg and getattr(msg, "parts", None):
chunks = [p.text for p in msg.parts if getattr(p, "text", None)]
if chunks:
return "".join(chunks)
out = getattr(event, "output", None)
return out if isinstance(out, str) else None
async def run(scenario_name="HOT"):
global _T0
os.environ["MARATHON_SCENARIO"] = scenario_name.upper()
print(f"=== L2a · scenario: {scenario_name.upper()} ===")
runner = Runner(node=root, app_name="l2a", session_service=InMemorySessionService(), auto_create_session=True)
t0 = _T0 = time.perf_counter()
async for event in runner.run_async(user_id="u1", session_id="s1", new_message=None):
t = _event_text(event)
if t:
print(f"\n🏁 RaceStrategy:\n{t}")
print(f"\n Total wall time: {time.perf_counter()-t0:.1f}s "
f"(fan-out + join + 1 LLM call — the fetch timestamps above are the parallel evidence)")
if __name__ == "__main__":
asyncio.run(run(sys.argv[1] if len(sys.argv) > 1 else "HOT"))