-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path67_pattern_first_api.py
More file actions
76 lines (64 loc) · 2.44 KB
/
67_pattern_first_api.py
File metadata and controls
76 lines (64 loc) · 2.44 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
"""
Example 67: Pattern-First API
=============================
Demonstrates the thin coordination wrappers introduced after the
epistemic topology work:
1. `advise_topology(...)` for quick architecture guidance
2. `reviewer_gate(...)` for single-worker + reviewer execution
3. `specialist_swarm(...)` for centralized specialist coordination
"""
from __future__ import annotations
import sys
from operon_ai import advise_topology, reviewer_gate, specialist_swarm
def main() -> None:
print("=" * 68)
print("Pattern-First API")
print("=" * 68)
print("\n--- 1. Topology Advice ---")
seq = advise_topology(
task_shape="sequential",
tool_count=2,
subtask_count=3,
error_tolerance=0.02,
)
par = advise_topology(
task_shape="parallel",
tool_count=4,
subtask_count=3,
error_tolerance=0.1,
)
print(f"Sequential task -> {seq.recommended_pattern}: {seq.rationale}")
print(f"Parallel task -> {par.recommended_pattern}: {par.rationale}")
print("\n--- 2. Reviewer Gate ---")
gate = reviewer_gate(
executor=lambda prompt: f"EXECUTE: {prompt}",
reviewer=lambda prompt, candidate: "safe" in prompt.lower() and "EXECUTE" in candidate,
)
decision = gate.run("Deploy safe schema migration")
print(f"Allowed: {decision.allowed}")
print(f"Status: {decision.status}")
print(f"Output: {decision.output}")
print(f"Class: {gate.analysis.classification.topology_class.value}")
print("\n--- 3. Specialist Swarm ---")
swarm = specialist_swarm(
roles=["legal", "security", "finance"],
workers={
"legal": lambda task, role: f"{role}: contract risk is low",
"security": lambda task, role: f"{role}: controls look adequate",
"finance": lambda task, role: f"{role}: annual cost is acceptable",
},
aggregator=lambda task, outputs: " | ".join(outputs.values()),
)
result = swarm.run("Assess this vendor")
print(f"Outputs: {result.outputs}")
print(f"Aggregate: {result.aggregate}")
print(f"Class: {result.analysis.classification.topology_class.value}")
print(f"Amplifies: {result.analysis.error_bound.amplification_ratio:.2f}x")
if __name__ == "__main__":
try:
main()
if "--test" in sys.argv:
print("\n[OK] Pattern-first example completed.")
except Exception as exc:
print(f"\n[ERROR] {exc}")
raise