-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathround-robin.py
More file actions
101 lines (80 loc) · 2.98 KB
/
Copy pathround-robin.py
File metadata and controls
101 lines (80 loc) · 2.98 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
#!/usr/bin/env python3
"""
Simple round-robin orchestration example.
Run in CLI mode (default):
python examples/orchestration/round-robin.py
Run with Web UI:
python examples/orchestration/round-robin.py --web
"""
import argparse
import asyncio
from picoagents import Agent
from picoagents.llm import OpenAIChatCompletionClient
from picoagents.orchestration import RoundRobinOrchestrator
from picoagents.termination import MaxMessageTermination, TextMentionTermination
def get_orchestrator():
"""Demonstrate round-robin conversation flow."""
client = OpenAIChatCompletionClient(model="gpt-4.1-mini")
# Create a haiku writer and critic working together
poet = Agent(
name="poet",
description="Haiku poet.",
instructions="""You are a haiku poet.""",
model_client=client,
)
critic = Agent(
name="critic",
description="Poetry critic who provides specific, constructive feedback on haikus.",
instructions="""You are a haiku critic. When you see a haiku, provide 2-3 specific,
actionable suggestions for improvement. Focus on imagery, syllable count, seasonal words,
or emotional impact. Be constructive and brief. If you are satisfied with the haiku and your comments addressed, respond with the word 'APPROVED'""",
model_client=client,
)
termination = MaxMessageTermination(max_messages=8) | TextMentionTermination(
text="APPROVED"
)
# Create orchestrator - poet writes first, then critic provides feedback
orchestrator = RoundRobinOrchestrator(
agents=[poet, critic], termination=termination, max_iterations=4
)
return orchestrator
orchestrator = get_orchestrator()
async def main():
task = "Write a haiku about cherry blossoms in spring"
print(f"🎯 Task: {task}")
print("🔄 Poet and Critic collaboration:\n")
# Run orchestration and show final conversation
stream = orchestrator.run_stream(task)
async for message in stream:
print(f"========\n{message}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Round-robin orchestration example with poet and critic agents"
)
parser.add_argument(
"--web",
action="store_true",
help="Launch Web UI instead of CLI mode",
)
parser.add_argument(
"--port",
type=int,
default=8070,
help="Port for Web UI (default: 8070)",
)
args = parser.parse_args()
if args.web:
# Import WebUI only if needed
from picoagents.webui import serve
print("🚀 Starting PicoAgents WebUI with round-robin orchestrator...")
print(f"\n📋 Poet & Critic Collaboration")
print(f" • Port: {args.port}")
print(f" • Try: 'Write a haiku about cherry blossoms in spring'\n")
serve(
entities=[orchestrator],
port=args.port,
auto_open=True,
)
else:
# Run in CLI mode
asyncio.run(main())