0.0
The project is in a healthy, maintained state
Swarm is a Ruby library that provides a simple interface for managing OpenAI chat completions with function calling support. It includes features like streaming responses, context management, and function execution.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
~> 13.0
~> 3.0

Runtime

~> 2.6
~> 7.0.0
 Project Readme

Swarm

Swarm is a Ruby library that simplifies the use of OpenAI API for chat completion and function calling.

This project was inspired by OpenAI's swarm repository.

Installation

Add this line to your Gemfile:

gem 'swarm'

And then execute:

$ bundle install

Or install it directly:

$ gem install swarm

Usage

Basic Usage

require 'swarm'

# Create an agent
agent = Swarm::Agent.new(
  name: "assistant",
  model: "gpt-4o-mini",
  instructions: "You are a helpful assistant.",
)

# Create a Swarm instance
swarm = Swarm::Swarm.new

# Execute chat
response = swarm.run(
  agent: agent,
  messages: [
    { role: "user", content: "Hello!" }
  ]
)

puts response.messages.last["content"]

Using Function Calling

def get_weather(location:)
  "The weather in #{location} is sunny"
end

agent = Swarm::Agent.new(
  name: "weather_bot",
  model: "gpt-4o-mini",
  instructions: "I am a weather information bot.",
  functions: [method(:get_weather)]
)

swarm = Swarm::Swarm.new

response = swarm.run(
  agent: agent,
  messages: [
    { role: "user", content: "What's the weather in Tokyo?" }
  ]
)

Using Streaming Response

swarm.run(
  agent: agent,
  messages: messages,
  stream: true
) do |chunk|
  print chunk.dig("delta", "content")
end

Environment Variables

  • OPENAI_API_KEY: Set your OpenAI API key.

Development

Bug reports and pull requests are welcome at https://github.com/kiyo-e/swarm.

License

This gem is available as open source under the terms of the MIT License.