Using OpenAI Swarm in Python: A Step-by-Step Guide

Black Friday: Enjoy a 25% discount on the starter kits. Use code BLACKFRIDAY2024 at checkout.

LaunchFast Logo LaunchFast

Using OpenAI Swarm in Python: A Step-by-Step Guide

Rishi Raj Jain
Using OpenAI Swarm in Python

In this tutorial, you will learn how to set up and use OpenAI Swarm in Python to create a multi-agent system. The tutorial will cover environment setup, and basic usage of the OpenAI Swarm framework.

Sponsored AICardify - AI-generated greeting cards

Generate AI Powered Greeting Cards For Every Occasion

But what can I do with OpenAI Swarm?

With Firecrawl and OpenAI Swarm, a multi-agent system could crawl the internet and answer the pricing of LaunchFa.st starter kits in JSON format 💥

Prerequisites

You’ll need the following:

Table Of Contents

Set up a new virtual environment

Terminal window
# Create a new directory for your project
mkdir openai-swarm-tutorial
# Navigate to the project directory
cd openai-swarm-tutorial
# Create a new virtual environment
python -m venv venv
# Activate the virtual environment
## On Windows:
venv\Scripts\activate
## On macOS and Linux:
source venv/bin/activate

Define dependencies

Create a requirements.txt file in the project directory with the following dependencies:

Terminal window
git+https://github.com/openai/swarm.git
python-dotenv

Install the dependencies

Install the dependencies using pip:

Terminal window
pip install -r requirements.txt

Define environment variables

Create a .env file in the project directory with the following:

Terminal window
OPENAI_API_KEY="your_api_key_here"

Replace your_api_key_here with your actual OpenAI API key.

Create a Python script to use OpenAI Swarm

Create the main script app.py with the following code:

app.py
from dotenv import load_dotenv
from swarm import Agent, Swarm
# Load environment variables
load_dotenv()
# Initialize the Swarm client
client = Swarm()
def transfer_to_agent_b():
return agent_b
# Define Agent A
agent_a = Agent(
name="Agent A",
instructions="You are a helpful agent.",
functions=[transfer_to_agent_b],
)
# Define Agent B
agent_b = Agent(
name="Agent B",
instructions="Only speak in Haikus.",
)
# Run the Swarm with Agent A
response = client.run(
agent=agent_a,
messages=[{"role": "user", "content": "I want to talk to agent B."}],
)
# Print the last message from the response
print(response.messages[-1]["content"])

This script does the following:

  • Imports necessary modules and loads the OPENAI_API_KEY environment variable.
  • Initializes the OpenAI Swarm client.
  • Defines a transfer_to_agent_b function to switch to Agent B.
  • Creates two agents: Agent A and Agent B with specific instructions.
  • Runs the Swarm with Agent A and a user message.
  • Prints the last message from the response.

Run the Script

Execute the script:

Terminal window
python app.py

This will process the initial message with Agent A, potentially transfer to Agent B, and display the result.

Conclusion

You have now learned how to set up and use OpenAI Swarm to create a basic multi-agent system. This example demonstrates agent definition, inter-agent communication, and basic Swarm execution. To expand on this, consider adding more agents, implementing complex transfer logic, or exploring advanced Swarm features like memory or tool use.

If you have any questions or comments, feel free to reach out to me on Twitter.

Learn More Authenticating users in Astro with Better Auth: A Step-by-Step Guide
Authenticating users in Astro with Better Auth: A Step-by-Step Guide November 24, 2024
Astro vs Next.js: Choosing the Right Framework in 2024
Astro vs Next.js: Choosing the Right Framework in 2024 October 30, 2024
6 Essential Features Every Web Starter Kit Should Include
6 Essential Features Every Web Starter Kit Should Include October 26, 2024