Advanced guides

Roseflow on Rails

The roseflow-rails gem and engine provides a simple and convenient way to add Roseflow interactions in a Ruby on Rails application. This documentation will guide you through the process of adding the gem and the engine to your Rails application.

Prerequisites

  • Ruby on Rails application (Rails 6.x or newer)
  • Roseflow gem

Installation

Add the roseflow-rails gem to your Rails application's Gemfile:

gem "roseflow-rails"

Then, run bundle install to install the gem.

bundle install

Configuration

Specify Roseflow configuration in an initializer generated by the installer.

# config/initializers/roseflow.rb
Roseflow.configure do |config|
  ...
end

Usage

Define interactions in app/interactions.

HTTP APIs for your interactions

The engine provides you a simple way of providing access to your interactions.

Mount the roseflow-rails engine in your config/routes.rb

# config/routes.rb
Rails.application.routes.draw do
  mount Roseflow::Rails::Engine => "/roseflow"
end

Creating an Interaction with HTTP API

To create an interaction that can be accessed via the HTTP API provided by the engine, ensure that your interaction class extends relevant modules:

# app/interaction/my_interaction.rb
class MyInteraction
  extend Roseflow::Interaction
  extend Roseflow::Interaction::WithHttpApi

  api_resource_name "special_inference"

  def self.call(context)
    # Your interaction logic
  end
end

The engine generates a POST route for each interaction that extends Roseflow::Interaction::WithHttpApi. The route follows the format

/interactions/:api_resource_name

The route for MyInteraction would be /interactions/special_inference. If you mount the engine at /roseflow, then the full path to the endpoint would be /roseflow/interactions/special_inference.

Providing context in the endpoint call

The engine will look for context param hash in the request that it will supply to the interaction as the context. If no context param is found, the interaction is called with an empty context.

Previous
Adding Memory