A lightweight AI agent framework
Warning
To support common agentic AI workflow, the API will be changed at any time until the design is completed.
Install the gem and add to the application's Gemfile by executing:
bundle add autofluxIf bundler is not being used to manage dependencies, install the gem by executing:
gem install autofluxTip
You may only need an agent to your workflow. The autoflux workflow is designed to multiple agents or multiple states.
Autoflux provides a default state machine for a chat workflow.
stateDiagram-v2
[*] --> Start
Start --> Command
Command --> Agent
Command --> Stop
Agent --> Command
Stop --> [*]
To start a new workflow use Autoflux::Workflow:
workflow = Autoflux::Workflow.new(
agent: agent,
io: io,
)
workflow.runWhen the io is EOF, the workflow will stop.
The agent is an object have #name and #call methods.
require 'autoflux/openai'
agent = Autoflux::OpenAI::Agent.new(
name: "chat",
model: "gpt-4o-mini"
)The workflow will pass itself as context to the agent.
class MyAgent
attr_reader :name
def initialize(name:)
@name = name
end
def call(params, workflow:)
workflow.io.write("Hello, #{params[:name]}!")
end
endWorkflow never knows the how the agent works and which tool is used.
The agent can switch by workflow if the workflow knows it. You can use it in the agent's tool to switch the agent.
workflow = Autoflux::Workflow.new(
agent: agent1, # if not given the first agent in `agents` will be used
agents: [agent1, agent2],
io: io,
)
workflow.switch_agent("agent2")The IO is an adapter to let the workflow interact with the user.
# :nodoc:
class ConsoleIO
def read
print 'User: '
gets.chomp
end
def write(message)
puts "Assistant: #{message}"
end
endThe default Autoflux::Stdio implement the minimal Standard I/O support.
require 'autoflux/stdio'
workflow = Autoflux::Workflow.new(
agent: agent,
io: Autoflux::Stdio.new(prompt: '> ')
)
workflow.runThe step is an object with #call method to define the behavior.
class MyCommand
def call(workflow:)
input = workflow.io.read
return Autoflux::Step::Stop.new if input.nil?
return Autoflux::Step::Stop.new if input == 'exit'
MyAgent.new(prompt: input)
end
endclass MyAgent
attr_reader :prompt
def initialize(prompt:)
@prompt = prompt
end
def call(workflow:)
res = workflow.agent.call(prompt, workflow: workflow)
workflow.io.write(res.to_s)
MyCommand.new
end
endYou can use it to design a state machine for the workflow.
workflow = Autoflux::Workflow.new(
agent: agent,
io: io,
step: MyCommand.new
)After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/elct9620/autoflux.
The gem is available as open source under the terms of the Apache License 2.0.