IRB-AI
is an experimental project that explores various ways to enhance users' IRB experience through AI (currently using ChatGPT
).
Add this gem to your Gemfile
:
gem "irb-ai", group: [:development, :test]
Ensure you have your OpenAI API key stored in ENV["OPENAI_API_KEY"]
.
At the moment, this gem only provides the explain
command:
explain <expression> as <context expression>
explains the execution of<expression>
by observing<context expression>
- For instance,
explain Post.find_the_closest as Post
will tracePost
's method calls to collect runtime information.
- For instance,
explain <expression>
is equivalent toexplain <expression> as self
Including <context expression>
is advised because this project relies on ruby/tracer
's
ObjectTracer
, which only collects traces around the target object.
The more detailed CallTracer
is not used because the current ChatGPT models
have a relatively low token limit (gpt-3.5-turbo
allows only 4000
tokens). Thus, using it can easily exceed that limit and
result in the request being rejected.
You can specify the model through IRB::AI.model = "<my model>"
. The default is gpt-3.5-turbo
.
Given this script:
require "bundler/inline"
gemfile do
gem "irb-ai"
gem "activesupport"
end
require "irb/ai"
require "active_support"
require "active_support/core_ext/object/blank"
old_secret = SecureRandom.base64(24)
old_encryptor = ActiveSupport::MessageEncryptor.new old_secret
new_secret = SecureRandom.base64(24)
new_encryptor = ActiveSupport::MessageEncryptor.new new_secret
new_encryptor.rotate old_secret
msg_from_old_encryptor = old_encryptor.encrypt_and_sign("test")
# Run `explain new_encryptor.decrypt_and_verify(msg_from_old_encryptor) as new_encryptor`
binding.irb
If you run the following command from the breakpoint:
explain new_encryptor.decrypt_and_verify(msg_from_old_encryptor) as new_encryptor
It will respond with answers like this as rendered markdown output:
This is an analysis of the program's behaviour when running the expression `new_encryptor.decrypt_and_verify(msg_from_old_encryptor)`
### Code Summary
From the given information, there is no program source code provided.
### Execution Summary
The `decrypt_and_verify` method was called on an instance of `ActiveSupport::MessageEncryptor` with a message received from an old encryptor. The method returned the decrypted message "test".
### Execution Details
1. The `decrypt_and_verify` method was called on an instance of `ActiveSupport::MessageEncryptor` with a message received from an old encryptor.
2. The `decrypt_and_verify` method called the `verifier` method on the same instance of `ActiveSupport::MessageEncryptor`.
3. An `ActiveSupport::MessageVerifier::InvalidSignature` exception was raised at line 178 in `message_verifier.rb`, indicating that the message could not be authenticated with the given signature (which was generated by the old encryptor).
4. The exception was caught by `run_rotations` in `rotator.rb`.
5. `run_rotations` calls `decrypt_and_verify` again, this time with a different key used by the new encryptor.
6. `decrypt_and_verify` called `verifier` again, but this time with the new key used by the new encryptor.
7. The `verifier` method verified the signature of the message using the new key, and found it to be valid.
8. The `decrypt_and_verify` method decrypted the message using the new key, which was used to encrypt the message before it was signed.
9. The decrypted message "test" was returned from the `decrypt_and_verify` method.
Thus the program successfully decrypted and authenticated the message received from the old encryptor.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test-unit
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/st0012/irb-ai. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the IRB-AI
project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the code of conduct.