- how to add Ruby on DSPy to a Rails app and install DSPy;
- how to give DSPy a model and your key;
- how to make a first prediction in the console.
Before you start
You need:- Ruby 3.3.1 or later, and Rails 8.1. The tutorial uses Ruby 4.0.10 and Rails 8.1.4.1.
- macOS or Linux.
- An OpenAI API key.
1. Create the app
Start with a new app and a scaffold for tickets, each with a subject and a body:2. Add Ruby on DSPy
Until Ruby on DSPy is on RubyGems, Bundler installs it from GitHub, pinned to the commit this tutorial was written with. Add it at the end of theGemfile, after a blank line, so the file ends like this:
Gemfile
3. Install DSPy
The gem installs its own Python and DSPy, apart from any Python you have. Install them, then check the install:doctor’s six lines should start with ok.
4. The model and your key
Tell DSPy which model to call, and give it your key. DSPy reads the key fromOPENAI_API_KEY. If it’s set in your shell, there’s nothing to add. If not, keep it in the app’s credentials: run bin/rails credentials:edit and add:
config/initializers/dspy_rb.rb. It sets the model every call uses, and hands DSPy the key from credentials when there is one:
config/initializers/dspy_rb.rb
5. A first prediction
With a model in place, try a first task: summing up a ticket. The signature is the string"ticket -> summary".
Open the console and give it a ticket:
6. Check your work
system-test job as well. The scaffold writes no system tests, so that job fails. Delete it, and the blank line above it, from .github/workflows/ci.yml.
If something goes wrong
Comparing with the tag
Thechapter-1 tag holds the app at the end of this chapter. To compare yours with it:
--stat to see each change. Expect these to differ, since every app makes its own:
config/credentials.yml.enc- the migrations’ timestamps
- the
version:line indb/schema.rb - the
branches:line in.github/workflows/ci.yml, if git’s default branch isn’tmain - your platform and Bundler version, in
Gemfile.lock
Gemfile.lock, also shows in the diff.
Next
Triage
Label each new ticket with a category and an urgent flag, and show them on its page.