Skip to content

Commit 5dba3c8

Browse files
authored
docs: improve docstring for create_swarm (#72)
1 parent db05e02 commit 5dba3c8

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

langgraph_swarm/swarm.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,59 @@ def create_swarm(
9696
9797
Args:
9898
agents: List of agents to add to the swarm
99+
An agent can be a LangGraph [CompiledStateGraph](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.state.CompiledStateGraph),
100+
a functional API [workflow](https://langchain-ai.github.io/langgraph/reference/func/#langgraph.func.entrypoint),
101+
or any other [Pregel](https://langchain-ai.github.io/langgraph/reference/pregel/#langgraph.pregel.Pregel) object.
99102
default_active_agent: Name of the agent to route to by default (if no agents are currently active).
100103
state_schema: State schema to use for the multi-agent graph.
101104
config_schema: An optional schema for configuration.
102105
Use this to expose configurable parameters via `swarm.config_specs`.
103106
104107
Returns:
105108
A multi-agent swarm StateGraph.
109+
110+
Example:
111+
112+
```python
113+
from langgraph.checkpoint.memory import InMemorySaver
114+
from langgraph.prebuilt import create_react_agent
115+
from langgraph_swarm import create_handoff_tool, create_swarm
116+
117+
def add(a: int, b: int) -> int:
118+
'''Add two numbers'''
119+
return a + b
120+
121+
alice = create_react_agent(
122+
"openai:gpt-4o",
123+
[add, create_handoff_tool(agent_name="Bob")],
124+
prompt="You are Alice, an addition expert.",
125+
name="Alice",
126+
)
127+
128+
bob = create_react_agent(
129+
"openai:gpt-4o",
130+
[create_handoff_tool(agent_name="Alice", description="Transfer to Alice, she can help with math")],
131+
prompt="You are Bob, you speak like a pirate.",
132+
name="Bob",
133+
)
134+
135+
checkpointer = InMemorySaver()
136+
workflow = create_swarm(
137+
[alice, bob],
138+
default_active_agent="Alice"
139+
)
140+
app = workflow.compile(checkpointer=checkpointer)
141+
142+
config = {"configurable": {"thread_id": "1"}}
143+
turn_1 = app.invoke(
144+
{"messages": [{"role": "user", "content": "i'd like to speak to Bob"}]},
145+
config,
146+
)
147+
turn_2 = app.invoke(
148+
{"messages": [{"role": "user", "content": "what's 5 + 7?"}]},
149+
config,
150+
)
151+
```
106152
"""
107153
active_agent_annotation = state_schema.__annotations__.get("active_agent")
108154
if active_agent_annotation is None:

0 commit comments

Comments
 (0)