ActiveModel::Validations::AI
Lets you validate a model attribute with against a written policy, like this:
class Platform::Application < ApplicationRecord
validates_compliance_of :name, policy: "must not use uppercase letters"
# Or:
validates :name, compliance: { policy: "must not use uppercase letters" }
end
You can also use this with ActiveModel::Validations
including models:
class Vibe
include ActiveModel::Validations
attr_accessor :value
validates_compliance_of :value, policy: "must be 10/10"
end
The policy and attribute value is sent to OpenAI with our prompt to be validated with GPT. AI generally isn't near zero-cost like other APIs, so this can be expensive to run. Use with some caution.
Configuration
We're using ruby-openai
as the OpenAI client under the hood, see the documentation here for how to configure the access token.
https://github.com/alexrudall/ruby-openai#with-config
Note: if you're using Bullet Train, fill in
ENV["OPENAI_ACCESS_TOKEN"]
and OpenAI will already be configured for you.
Setting request timeout
For validations we know we're not waiting for a complicated chat response, so we timeout requests after 3 seconds instead of ruby-openai
's 120 second default. If you need to, you can change the timeout with this:
ActiveModel::Validations::AI.request_timeout = 5 # seconds
Handling errors
We capture any Faraday::Error
exceptions and report them. If we're in a Rails app we report via Rails.error
otherwise it's via ActiveSupport::ErrorReporter.new
. This gives you access to report to your existing error reporting service automatically.
You can also change the error reporter if need be:
ActiveModel::Validations::AI.error_reporter = ActiveSupport::ErrorReporter.new
Testing
We've tested that our requests to OpenAI work so you don't have to pay that cost in your apps tests.
Add this in your test/test_helper.rb
or spec/spec_helper.rb
:
ActiveModel::Validations::AI.stub_with valid: true
Which prevent any requests from firing and assumes that any validation from this gem is valid.
Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add active_model-validations-ai
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install active_model-validations-ai
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/kaspth/active_model-validations-ai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the ActiveModel::Validations::AI project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.