Swarm (experimental, educational)
An educational framework exploring ergonomic, lightweight multi-agent orchestration.
[!WARNING]
Swarm is currently an experimental sample framework intended to explore ergonomic interfaces for multi-agent systems. It is not intended to be used in production, and therefore has no official support. (This also means we will not be reviewing PRs or issues!)The primary goal of Swarm is to showcase the handoff & routines patterns explored in the Orchestrating Agents: Handoffs & Routines cookbook. It is not meant as a standalone library, and is primarily for educational purposes.
Install
Requires Python 3.10+
or
Usage
Overview
Swarm focuses on making agent coordination and execution lightweight, highly controllable, and easily testable.
It accomplishes this through two primitive abstractions: Agent
s and handoffs. An Agent
encompasses instructions
and tools
, and can at any point choose to hand off a conversation to another Agent
.
These primitives are powerful enough to express rich dynamics between tools and networks of agents, allowing you to build scalable, real-world solutions while avoiding a steep learning curve.
[!NOTE]
Swarm Agents are not related to Assistants in the Assistants API. They are named similarly for convenience, but are otherwise completely unrelated. Swarm is entirely powered by the Chat Completions API and is hence stateless between calls.
Why Swarm
Swarm explores patterns that are lightweight, scalable, and highly customizable by design. Approaches similar to Swarm are best suited for situations dealing with a large number of independent capabilities and instructions that are difficult to encode into a single prompt.
The Assistants API is a great option for developers looking for fully-hosted threads and built in memory management and retrieval. However, Swarm is an educational resource for developers curious to learn about multi-agent orchestration. Swarm runs (almost) entirely on the client and, much like the Chat Completions API, does not store state between calls.
Examples
Check out /examples
for inspiration! Learn more about each one in its README.
basic
: Simple examples of fundamentals like setup, function calling, handoffs, and context variablestriage_agent
: Simple example of setting up a basic triage step to hand off to the right agentweather_agent
: Simple example of function callingairline
: A multi-agent setup for handling different customer service requests in an airline context.support_bot
: A customer service bot which includes a user interface agent and a help center agent with several toolspersonal_shopper
: A personal shopping agent that can help with making sales and refunding orders
Documentation
Running Swarm
Start by instantiating a Swarm client (which internally just instantiates an OpenAI
client).
client.run()
Swarm's run()
function is analogous to the chat.completions.create()
function in the Chat Completions API – it takes messages
and returns messages
and saves no state between calls. Importantly, however, it also handles Agent function execution, hand-offs, context variable references, and can take multiple turns before returning to the user.
At its core, Swarm's client.run()
implements the following loop:
- Get a completion from the current Agent
- Execute tool calls and append results
- Switch Agent if necessary
- Update context variables, if necessary
- If no new function calls, return
Arguments
Once client.run()
is finished (after potentially multiple calls to agents and tools) it will return a Response
containing all the relevant updated state. Specifically, the new messages
, the last Agent
to be called, and the most up-to-date context_variables
. You can pass these values (plus new user messages) in to your next execution of client.run()
to continue the interaction where it left off – much like chat.completions.create()
. (The run_demo_loop
function implements an example of a full execution loop in /swarm/repl/repl.py
.)
Response
Fields
Agents
An Agent
simply encapsulates a set of instructions
with a set of functions
(plus some additional settings below), and has the capability to hand off execution to another Agent
.
While it's tempting to personify an Agent
as "someone who does X", it can also be used to represent a very specific workflow or step defined by a set of instructions
and functions
(e.g. a set of steps, a complex retrieval, single step of data transformation, etc). This allows Agent
s to be composed into a network of "agents", "workflows", and "tasks", all represented by the same primitive.
Agent
Fields
Instructions
Agent
instructions
are directly converted into the system
prompt of a conversation (as the first message). Only the instructions
of the active Agent
will be present at any given time (e.g. if there is an Agent
handoff, the system
prompt will change, but the chat history will not.)
The instructions
can either be a regular str
, or a function that returns a str
. The function can optionally receive a context_variables
parameter, which will be populated by the context_variables
passed into client.run()
.
Functions
- Swarm
Agent
s can call python functions directly. - Function should usually return a
str
(values will be attempted to be cast as astr
). - If a function returns an
Agent
, execution will be transfered to thatAgent
. - If a function defines a
context_variables
parameter, it will be populated by thecontext_variables
passed intoclient.run()
.
- If an
Agent
function call has an error (missing function, wrong argument, error) an error response will be appended to the chat so theAgent
can recover gracefully. - If multiple functions are called by the
Agent
, they will be executed in that order.
Handoffs and Updating Context Variables
An Agent
can hand off to another Agent
by returning it in a function
.
It can also update the context_variables
by returning a more complete Result
object. This can also contain a value
and an agent
, in case you want a single function to return a value, update the agent, and update the context variables (or any subset of the three).
[!NOTE]
If anAgent
calls multiple functions to hand-off to anAgent
, only the last handoff function will be used.
Function Schemas
Swarm automatically converts functions into a JSON Schema that is passed into Chat Completions tools
.
- Docstrings are turned into the function
description
. - Parameters without default values are set to
required
. - Type hints are mapped to the parameter's
type
(and default tostring
). - Per-parameter descriptions are not explicitly supported, but should work similarly if just added in the docstring. (In the future docstring argument parsing may be added.)
Streaming
Uses the same events as Chat Completions API streaming. See process_and_print_streaming_response
in /swarm/repl/repl.py
as an example.
Two new event types have been added:
{"delim":"start"}
and{"delim":"start"}
, to signal each time anAgent
handles a single message (response or function call). This helps identify switches betweenAgent
s.{"response": Response}
will return aResponse
object at the end of a stream with the aggregated (complete) response, for convenience.
Evaluations
Evaluations are crucial to any project, and we encourage developers to bring their own eval suites to test the performance of their swarms. For reference, we have some examples for how to eval swarm in the airline
, weather_agent
and triage_agent
quickstart examples. See the READMEs for more details.
Utils
Use the run_demo_loop
to test out your swarm! This will run a REPL on your command line. Supports streaming.
Core Contributors
- Ilan Bigio - ibigio
- James Hills - jhills20
- Shyamal Anadkat - shyamal-anadkat
- Charu Jaiswal - charuj
- Colin Jarvis - colin-openai
- Katia Gil Guzman - katia-openai
No reviews found!
No comments found for this product. Be the first to comment!