-
-
Notifications
You must be signed in to change notification settings - Fork 984
Expand file tree
/
Copy path01_fan_out.py
More file actions
102 lines (85 loc) · 2.48 KB
/
Copy path01_fan_out.py
File metadata and controls
102 lines (85 loc) · 2.48 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
r"""
Topology 1 — Fan-out (one → many)
=================================
A single source agent produces a piece of content, then three downstream
agents fan out in parallel to localize it. No final join.
Source -> Translator_FR, Translator_ES, Translator_JP
\_____________parallel step_____________/
Each translator sees the source paragraph and produces an independent
localized version. Results are returned in dict form, one entry per agent.
"""
import time
from swarms import Agent, AgentRearrange
MODEL = "gpt-4o-mini"
def _agent(name: str, prompt: str) -> Agent:
return Agent(
agent_name=name,
system_prompt=prompt,
model_name=MODEL,
max_loops=1,
verbose=False,
persistent_memory=False,
)
source = _agent(
"Source",
"Write ONE concise English paragraph (≤60 words) introducing the "
"concept of multi-agent orchestration to a non-technical reader.",
)
fr = _agent(
"Translator_FR",
"Translate the most recent English paragraph into formal French. "
"Output only the translation.",
)
es = _agent(
"Translator_ES",
"Translate the most recent English paragraph into Latin-American Spanish. "
"Output only the translation.",
)
jp = _agent(
"Translator_JP",
"Translate the most recent English paragraph into natural Japanese (keigo). "
"Output only the translation.",
)
pipeline = AgentRearrange(
name="fan-out",
agents=[source, fr, es, jp],
flow="Source -> Translator_FR, Translator_ES, Translator_JP",
max_loops=1,
output_type="dict",
autosave=False,
)
def main() -> None:
print("=" * 72)
print(f"FAN-OUT | flow: {pipeline.flow}")
print("=" * 72)
t0 = time.perf_counter()
messages = pipeline.run(
"Introduce multi-agent orchestration in one paragraph."
)
print(f"\nCompleted in {time.perf_counter() - t0:.2f}s\n")
latest = {}
for msg in messages:
role = msg.get("role")
if role in {
"Source",
"Translator_FR",
"Translator_ES",
"Translator_JP",
}:
latest[role] = msg.get("content", "")
for name in [
"Source",
"Translator_FR",
"Translator_ES",
"Translator_JP",
]:
out = latest.get(name)
if not out:
continue
print("-" * 72)
print(f"[{name}]")
print("-" * 72)
print(str(out).strip())
print()
if __name__ == "__main__":
main()