top of page
Writer's pictureRevanth Reddy Tondapu

Exploring the Potential of AI Multi-Agent Frameworks with Swarm



AI is evolving rapidly, offering new ways to automate and streamline complex tasks through innovative frameworks. Recently, a new multi-agent framework called Swarm has been introduced, focusing on enabling multiple AI agents to work together in a coordinated manner. This approach is lightweight, highly controllable, and easily testable, making it accessible even for beginners. In this blog post, we'll delve into the capabilities of Swarm, illustrating its functionality with practical examples and explaining how it differs from other multi-agent frameworks.


What are AI Agents?

AI agents are independent systems that collaborate to complete complex tasks. Instead of relying on multiple people to execute a task, AI agents can work autonomously, interacting with each other to achieve the desired outcome. These agents can be integrated with various open-source models, allowing them to run locally without incurring additional costs.


Introducing Swarm

Swarm is a framework designed to facilitate the coordination of multiple AI agents, making it easier to automate tasks across different domains. The framework is built with simplicity in mind, ensuring that even those new to AI can create and manage agents effectively.


Key Features of Swarm

  • Lightweight and Controllable: Swarm is designed to be efficient and easy to manage, offering a streamlined approach to agent coordination.

  • Easily Testable: The framework provides tools to test and refine agent interactions, ensuring reliable performance.

  • Customizable: Users can tailor agents to their specific needs, integrating various tools to enhance functionality.


Building AI Agents with Swarm

Let's explore how you can create AI agents using Swarm with a practical example involving a manager agent, a weather agent, and a stock price agent.


Example Use Case

Imagine you want to automate the process of retrieving weather and stock price information. Using Swarm, you can create:

  1. Manager Agent: Directs user queries to the appropriate agent.

  2. Weather Agent: Fetches weather data for a specified location.

  3. Stock Price Agent: Retrieves stock price information for a given company.


Step-by-Step Guide

  1. Install Necessary Packages: Begin by installing the Swarm package and any additional libraries required for your tools (e.g., Yahoo Finance for stock prices).

pip install git+https://github.com/openai/swarm.git yfinance
  1. Set Up API Keys: Export your API keys for the services you'll use, such as OpenWeather for weather data.

export OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxx
export OPENWEATHER_API_KEY=xxxxxxxxxxxxxxxx
  1. Create Agent Tools: Develop functions to serve as tools for your agents. For example, a function to get weather data:

# Function to fetch real weather data
def get_weather(location):
  1. Define Agents: Use Swarm to create and configure your agents, assigning them the necessary tools and instructions.

import os
import requests
import yfinance as yf
from swarm import Swarm, Agent

# Initialize Swarm client
client = Swarm()

# Load OpenWeatherMap API key from environment variable
API_KEY = os.getenv('OPENWEATHER_API_KEY')
if not API_KEY:
    raise ValueError("OPENWEATHER_API_KEY environment variable not set")

BASE_URL = "http://api.openweathermap.org/data/2.5/weather"

# Function to fetch real weather data
def get_weather(location):
    print(f"Running weather function for {location}...")
    
    params = {
        "q": location,
        "appid": API_KEY,
        "units": "metric"  # Change to 'imperial' for Fahrenheit
    }
    response = requests.get(BASE_URL, params=params)
    data = response.json()
    
    if response.status_code == 200:
        temperature = data['main']['temp']
        weather_description = data['weather'][0]['description']
        city_name = data['name']
        return f"The weather in {city_name} is {temperature}°C with {weather_description}."
    else:
        return f"Could not get the weather for {location}. Please try again."

# Function to fetch stock price using yfinance
def get_stock_price(ticker):
    print(f"Running stock price function for {ticker}...")
    stock = yf.Ticker(ticker)
    stock_info = stock.history(period="1d")
    if not stock_info.empty:
        latest_price = stock_info['Close'].iloc[-1]
        return f"The latest stock price for {ticker} is {latest_price}."
    else:
        return f"Could not retrieve stock price for {ticker}."

# Function to transfer from manager agent to weather agent
def transfer_to_weather_assistant():
    print("Transferring to Weather Assistant...")
    return weather_agent

# Function to transfer from manager agent to stock price agent
def transfer_to_stockprice_assistant():
    print("Transferring to Stock Price Assistant...")
    return stockprice_agent

# manager Agent
manager_agent = Agent(
    name="manager Assistant",
    instructions="You help users by directing them to the right assistant.",
    functions=[transfer_to_weather_assistant, transfer_to_stockprice_assistant],
)

# Weather Agent
weather_agent = Agent(
    name="Weather Assistant",
    instructions="You provide weather information for a given location using the provided tool",
    functions=[get_weather],
)

# Stock Price Agent
stockprice_agent = Agent(
    name="Stock Price Assistant",
    instructions="You provide the latest stock price for a given ticker symbol using the yfinance library.",
    functions=[get_stock_price],
)

print("Running manager Assistant for Weather...")
response = client.run(
    agent=manager_agent,
    messages=[{"role": "user", "content": "What's the weather in New York?"}],
)
print(response.messages[-1]["content"])

# Example: User query handled by manager agent to get stock price
print("\nRunning manager Assistant for Stock Price...")
response = client.run(
    agent=manager_agent,
    messages=[{"role": "user", "content": "Get me the stock price of AAPL."}],
)

print(response.messages[-1]["content"])
  1. Run the Agents: Execute your application to see the agents in action, responding to queries and coordinating tasks.

python app.py

Customizing Your Agents

Swarm allows you to add various tools to your agents, such as financial data APIs or custom utilities. This flexibility enables you to automate a wide range of tasks beyond weather and stock prices, adapting the framework to your specific needs.


Comparing Swarm to Other Frameworks

Swarm offers a unique approach to multi-agent coordination, distinct from other frameworks like Crew AI and AutoGen. While these established frameworks provide extensive features and maturity, Swarm's transparency and customization potential make it an attractive option for those seeking a more hands-on experience.


Key Differences

  • Transparency: Swarm provides clear visibility into each step of the agent interactions, allowing for detailed customization and understanding.

  • Direct API Use: Unlike some frameworks that rely on intermediary libraries, Swarm interacts directly with APIs, ensuring efficiency and simplicity.


Conclusion

Swarm represents a promising advancement in AI multi-agent frameworks, offering a user-friendly and customizable solution for automating complex tasks. Whether you're a beginner or an experienced developer, Swarm provides the tools and flexibility to create powerful AI agents tailored to your needs. As the framework continues to evolve, it may soon rival more established alternatives, offering even more capabilities for those looking to harness the power of AI in their workflows.

1 view0 comments

Comments


bottom of page