@@ -70,57 +70,56 @@ def add_active_agent_router(
7070 StateGraph with the router added.
7171
7272 Example:
73+ ```python
74+ from langgraph.checkpoint.memory import InMemorySaver
75+ from langgraph.prebuilt import create_react_agent
76+ from langgraph.graph import StateGraph
77+ from langgraph_swarm import SwarmState, create_handoff_tool, add_active_agent_router
78+
79+ def add(a: int, b: int) -> int:
80+ '''Add two numbers'''
81+ return a + b
82+
83+ alice = create_react_agent(
84+ "openai:gpt-4o",
85+ [add, create_handoff_tool(agent_name="Bob")],
86+ prompt="You are Alice, an addition expert.",
87+ name="Alice",
88+ )
7389
74- ```python
75- from langgraph.checkpoint.memory import InMemorySaver
76- from langgraph.prebuilt import create_react_agent
77- from langgraph.graph import StateGraph
78- from langgraph_swarm import SwarmState, create_handoff_tool, add_active_agent_router
79-
80- def add(a: int, b: int) -> int:
81- '''Add two numbers'''
82- return a + b
83-
84- alice = create_react_agent(
85- "openai:gpt-4o",
86- [add, create_handoff_tool(agent_name="Bob")],
87- prompt="You are Alice, an addition expert.",
88- name="Alice",
89- )
90-
91- bob = create_react_agent(
92- "openai:gpt-4o",
93- [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
94- prompt="You are Bob, you speak like a pirate.",
95- name="Bob",
96- )
90+ bob = create_react_agent(
91+ "openai:gpt-4o",
92+ [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
93+ prompt="You are Bob, you speak like a pirate.",
94+ name="Bob",
95+ )
9796
98- checkpointer = InMemorySaver()
99- workflow = (
100- StateGraph(SwarmState)
101- .add_node(alice, destinations=("Bob",))
102- .add_node(bob, destinations=("Alice",))
103- )
104- # this is the router that enables us to keep track of the last active agent
105- workflow = add_active_agent_router(
106- builder=workflow,
107- route_to=["Alice", "Bob"],
108- default_active_agent="Alice",
109- )
97+ checkpointer = InMemorySaver()
98+ workflow = (
99+ StateGraph(SwarmState)
100+ .add_node(alice, destinations=("Bob",))
101+ .add_node(bob, destinations=("Alice",))
102+ )
103+ # this is the router that enables us to keep track of the last active agent
104+ workflow = add_active_agent_router(
105+ builder=workflow,
106+ route_to=["Alice", "Bob"],
107+ default_active_agent="Alice",
108+ )
110109
111- # compile the workflow
112- app = workflow.compile(checkpointer=checkpointer)
110+ # compile the workflow
111+ app = workflow.compile(checkpointer=checkpointer)
113112
114- config = {"configurable": {"thread_id": "1"}}
115- turn_1 = app.invoke(
116- {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
117- config,
118- )
119- turn_2 = app.invoke(
120- {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
121- config,
122- )
123- ```
113+ config = {"configurable": {"thread_id": "1"}}
114+ turn_1 = app.invoke(
115+ {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
116+ config,
117+ )
118+ turn_2 = app.invoke(
119+ {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
120+ config,
121+ )
122+ ```
124123 """
125124 channels = builder .schemas [builder .schema ]
126125 if "active_agent" not in channels :
@@ -161,47 +160,46 @@ def create_swarm(
161160 A multi-agent swarm StateGraph.
162161
163162 Example:
163+ ```python
164+ from langgraph.checkpoint.memory import InMemorySaver
165+ from langgraph.prebuilt import create_react_agent
166+ from langgraph_swarm import create_handoff_tool, create_swarm
167+
168+ def add(a: int, b: int) -> int:
169+ '''Add two numbers'''
170+ return a + b
171+
172+ alice = create_react_agent(
173+ "openai:gpt-4o",
174+ [add, create_handoff_tool(agent_name="Bob")],
175+ prompt="You are Alice, an addition expert.",
176+ name="Alice",
177+ )
164178
165- ```python
166- from langgraph.checkpoint.memory import InMemorySaver
167- from langgraph.prebuilt import create_react_agent
168- from langgraph_swarm import create_handoff_tool, create_swarm
169-
170- def add(a: int, b: int) -> int:
171- '''Add two numbers'''
172- return a + b
173-
174- alice = create_react_agent(
175- "openai:gpt-4o",
176- [add, create_handoff_tool(agent_name="Bob")],
177- prompt="You are Alice, an addition expert.",
178- name="Alice",
179- )
180-
181- bob = create_react_agent(
182- "openai:gpt-4o",
183- [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
184- prompt="You are Bob, you speak like a pirate.",
185- name="Bob",
186- )
179+ bob = create_react_agent(
180+ "openai:gpt-4o",
181+ [create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
182+ prompt="You are Bob, you speak like a pirate.",
183+ name="Bob",
184+ )
187185
188- checkpointer = InMemorySaver()
189- workflow = create_swarm(
190- [alice, bob],
191- default_active_agent="Alice"
192- )
193- app = workflow.compile(checkpointer=checkpointer)
186+ checkpointer = InMemorySaver()
187+ workflow = create_swarm(
188+ [alice, bob],
189+ default_active_agent="Alice"
190+ )
191+ app = workflow.compile(checkpointer=checkpointer)
194192
195- config = {"configurable": {"thread_id": "1"}}
196- turn_1 = app.invoke(
197- {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
198- config,
199- )
200- turn_2 = app.invoke(
201- {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
202- config,
203- )
204- ```
193+ config = {"configurable": {"thread_id": "1"}}
194+ turn_1 = app.invoke(
195+ {"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
196+ config,
197+ )
198+ turn_2 = app.invoke(
199+ {"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
200+ config,
201+ )
202+ ```
205203 """
206204 active_agent_annotation = state_schema .__annotations__ .get ("active_agent" )
207205 if active_agent_annotation is None :
0 commit comments