-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy path11_dynamic_selector.py
More file actions
69 lines (57 loc) · 2.17 KB
/
Copy path11_dynamic_selector.py
File metadata and controls
69 lines (57 loc) · 2.17 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
"""11 · Dynamic control flow — route between agents with a Selector (if / while).
A `flow.Selector` is an LLM-backed router: given the current session and a set
of self-describing workflows, it returns which one to run next, or a no-op
`flow.EmptyWorkflow` when the task is done. Control flow stays plain Python — you
write the `if` / `while`; the Selector only makes the routing decision.
Run:
python example/11_dynamic_selector.py
"""
from __future__ import annotations
from _shared import provider_from_env
from rath import flow
from rath.session import Session
def main() -> None:
provider = provider_from_env()
selector = flow.Selector(provider)
billing = flow.Agent(
"You handle billing questions. Be brief.",
provider,
description="Billing, invoices, refunds, payment methods",
)
tech = flow.Agent(
"You solve technical problems. Be brief.",
provider,
description="Installation, errors, configuration, troubleshooting",
)
wrapup = flow.Agent(
"You write a one-line closing summary.",
provider,
description="Wrap up and produce a final summary",
)
# --- if: route to at most one branch, once ---
print("--- if: single branch ---")
session = Session.from_user_message("My last invoice was charged twice.").to(
"local"
)
chosen = selector.forward(session, billing, tech)
if not isinstance(chosen, flow.EmptyWorkflow):
print("routed to:", chosen.description)
session = chosen(session)
print(session.text())
# --- while: keep routing until the Selector returns an EmptyWorkflow (done) ---
print("--- while: loop until done ---")
session = Session.from_user_message(
"I got an error installing, then I want a summary."
).to("local")
rounds = 0
while not isinstance(
nxt := selector.forward(session, tech, billing, wrapup), flow.EmptyWorkflow
):
rounds += 1
print(f"round {rounds} -> {nxt.description}")
session = nxt(session)
if rounds >= 4: # safety bound for the demo
break
print("final:", session.text())
if __name__ == "__main__":
main()