-
-
Notifications
You must be signed in to change notification settings - Fork 984
Expand file tree
/
Copy path02_fan_in.py
More file actions
92 lines (75 loc) · 2.33 KB
/
Copy path02_fan_in.py
File metadata and controls
92 lines (75 loc) · 2.33 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
r"""
Topology 2 — Fan-in / many → one (summarize)
============================================
Three independent analysts run in parallel on the same question, then a
synthesizer agent merges their views into one recommendation.
Bull, Bear, Quant -> Synthesizer
\____parallel____/
\___ summarize ___/
"""
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,
)
bull = _agent(
"Bull",
"You are a bullish equity analyst. Give two concise reasons the asset "
"will outperform over the next 12 months. No hedging.",
)
bear = _agent(
"Bear",
"You are a bearish equity analyst. Give two concise reasons the asset "
"will underperform over the next 12 months. No hedging.",
)
quant = _agent(
"Quant",
"You are a quantitative analyst. Give two concise data-driven signals "
"(valuation multiples, momentum, etc.) about the asset.",
)
synth = _agent(
"Synthesizer",
"You are a CIO. Given the prior bull, bear, and quant analyses, produce "
"(a) a single sentence recommendation (Buy / Hold / Sell) and (b) one "
"sentence explaining the key driver.",
)
pipeline = AgentRearrange(
name="fan-in",
agents=[bull, bear, quant, synth],
flow="Bull, Bear, Quant -> Synthesizer",
max_loops=1,
output_type="dict",
autosave=False,
)
TASK = "Evaluate NVIDIA (NVDA) stock for a 12-month horizon."
def main() -> None:
print("=" * 72)
print(f"FAN-IN | flow: {pipeline.flow}")
print("=" * 72)
print(f"Task: {TASK}\n")
t0 = time.perf_counter()
messages = pipeline.run(TASK)
print(f"Completed in {time.perf_counter() - t0:.2f}s\n")
latest = {}
for msg in messages:
role = msg.get("role")
if role in {"Bull", "Bear", "Quant", "Synthesizer"}:
latest[role] = msg.get("content", "")
for name in ["Bull", "Bear", "Quant", "Synthesizer"]:
out = latest.get(name)
if not out:
continue
print("-" * 72)
print(f"[{name}]")
print("-" * 72)
print(str(out).strip())
print()
if __name__ == "__main__":
main()