-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervised_delegation.py
More file actions
68 lines (59 loc) · 2.53 KB
/
Copy pathsupervised_delegation.py
File metadata and controls
68 lines (59 loc) · 2.53 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
from typing import TypedDict
from langgraph.graph import StateGraph, END
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-haiku-4-5-20251001")
class SupervisionState(TypedDict):
task: str
subtasks: list[str]
worker_a_result: str
worker_b_result: str
final_review: str
def supervisor_split(state: SupervisionState) -> SupervisionState:
task = state["task"]
response = llm.invoke(
f"Split this task into exactly 2 subtasks (one line each, no numbering):\n{task}"
)
lines = [l.strip() for l in response.content.strip().split("\n") if l.strip()]
subtasks = lines[:2] if len(lines) >= 2 else [task, task]
print(f"[Supervisor] Subtasks: {subtasks}")
return {**state, "subtasks": subtasks}
def worker_a(state: SupervisionState) -> SupervisionState:
subtask = state["subtasks"][0]
result = llm.invoke(f"Complete this subtask in 1-2 sentences:\n{subtask}")
print(f"[Worker A] {result.content.strip()}")
return {**state, "worker_a_result": result.content.strip()}
def worker_b(state: SupervisionState) -> SupervisionState:
subtask = state["subtasks"][1]
result = llm.invoke(f"Complete this subtask in 1-2 sentences:\n{subtask}")
print(f"[Worker B] {result.content.strip()}")
return {**state, "worker_b_result": result.content.strip()}
def supervisor_review(state: SupervisionState) -> SupervisionState:
combined = f"Result A: {state['worker_a_result']}\nResult B: {state['worker_b_result']}"
review = llm.invoke(f"Review and synthesize these two results in 2 sentences:\n{combined}")
print(f"[Supervisor Review] {review.content.strip()}")
return {**state, "final_review": review.content.strip()}
def build_graph():
g = StateGraph(SupervisionState)
g.add_node("supervisor_split", supervisor_split)
g.add_node("worker_a", worker_a)
g.add_node("worker_b", worker_b)
g.add_node("supervisor_review", supervisor_review)
g.set_entry_point("supervisor_split")
g.add_edge("supervisor_split", "worker_a")
g.add_edge("worker_a", "worker_b")
g.add_edge("worker_b", "supervisor_review")
g.add_edge("supervisor_review", END)
return g.compile()
def main():
graph = build_graph()
initial = {
"task": "Write a short report on the benefits of microservices architecture",
"subtasks": [],
"worker_a_result": "",
"worker_b_result": "",
"final_review": "",
}
result = graph.invoke(initial)
print(f"\n[Final] {result['final_review']}")
if __name__ == "__main__":
main()