HyperAI

Swarm Multi-agent Framework

Swarm is an experimental multi-agent framework developed by OpenAI in 2024 that aims to simplify the construction, orchestration, and deployment of multi-agent systems. Swarm focuses on making agent collaboration and execution lightweight, highly controllable, and easy to test.

At its core, Swarm lies in two primitive abstractions:Agents and handoffs.Agents contain commands and tools, and can choose to hand off the conversation to another agent at any time. This design allows for flexible task transfer between agents, adapting to different scenarios and needs. Swarm is suitable for situations where there are a large number of independent functions and commands that are difficult to encode into a single prompt word.

Swarm Framework Installation

The installation of the Swarm framework is very simple and can be installed directly through the pip command:

bash
pip install git+ssh://git@github.com/openai/swarm.git

When using Swarm, you can define agents and specify their behavior. For example, the following code defines two agents, and the user's instruction is to talk to agent B:

from swarm import Swarm, Agent
client = Swarm()
def transfer_to_agent_b():
    return agent_b
agent_a = Agent(
    name="Agent A",
    instructions="You are a helpful agent.",
    functions=[transfer_to_agent_b],
)
agent_b = Agent(
    name="Agent B",
    instructions="Only speak in Haikus.",
)
response = client.run(
    agent=agent_a,
    messages=[{"role": "user", "content": "I want to talk to agent B."}],
)
print(response.messages[-1]["content"])

The final output message will be Agent B's answer in the form of Haikus' poem.