Back

OpenAI Swarm Example

Published onOctober 24, 2024
116Views
4Minutes Read
OpenAI Swarm Example

Introduction

In this blog post, we will walk through an example of using the OpenAI Swarm framework to create a multi-agent system. This system will include agents for fetching weather data and stock prices. We'll use the OpenWeatherMap API for weather data and the yfinance library for stock prices. Let's dive into the code step by step.

Prerequisites

First, make sure you have the necessary libraries installed. You can install them using pip:
Next, set your API keys as environment variables:

Import Libraries

Start by importing the necessary libraries:

Initialize Swarm Client

Initialize the Swarm client:

Load API Key

Load the OpenWeatherMap API key from the environment variable:

Fetch Weather Data

Define a function to fetch real weather data:

Fetch Stock Price

Define a function to fetch stock prices using yfinance:

Transfer Functions

Define functions to transfer control between agents:

Define Agents

Define the manager agent, weather agent, and stock price agent:

Run the Manager Agent

Run the manager agent to handle user queries:

Conclusion

This example demonstrates how to create a multi-agent system using the OpenAI Swarm framework. The manager agent directs user queries to the appropriate assistant agents, which then fetch and return the requested information. This setup allows for a modular and scalable approach to handling various types of user requests.
Tags:
#Python

Comments (0)

No comments yet. Be the first to comment!