Skip to main content
After reading this guide, you will know:
  • how to add dspy-rb 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.
In this tutorial you build Helpdesk, a Rails app for a support team, where a language model triages each new ticket, pulls out the orders and products it mentions, explains its triage, records what each ticket cost, and suggests replies from the help center when a support agent asks. In this chapter, you create the app, add dspy-rb and make a first prediction in the console. If you haven’t yet, read the Introduction first: it explains the model, signatures, predictors and predictions this tutorial uses. The tutorial assumes you know Ruby and Rails. If Rails is new to you, start with Rails’ own Getting Started guide.

Before you start

You need:
  • Ruby 3.2 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 dspy-rb

Until dspy-rb is on RubyGems, Bundler installs it from GitHub, pinned to the commit this tutorial was written with. Add it at the end of the Gemfile, after a blank line, so the file ends like this:
Gemfile
Then install it:

3. Install DSPy

The gem installs its own Python and DSPy, apart from any Python you have. Install them, then check the install:
The first run downloads Python 3.12 and DSPy 3.5.0, which takes about a minute. Each of 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 from OPENAI_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:
Then create 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:
The first call takes a few seconds, while DSPy starts.

6. Check your work

Both should pass, with no offenses and the scaffold’s 7 tests. Rails’ CI on GitHub runs these too, and a 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

  • DSPyRb::SetupError: DSPy isn’t installed for the gem you have now, which happens after you add or update it. Run bundle exec dspy-rb setup.
  • DSPyRb::LMNotConfiguredError: no key reached DSPy. Its message reads:
    Add the key as in step 4, then start a new console, since DSPy reads keys once, when it starts.
  • DSPyRb::LMAuthError: OpenAI refused the key. Check it.

Comparing with the tag

The chapter-1 tag holds the app at the end of this chapter. To compare yours with it:
Leave out --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 in db/schema.rb
  • the branches: line in .github/workflows/ci.yml, if git’s default branch isn’t main
  • your platform and Bundler version, in Gemfile.lock
A different Ruby or Rails version, or a newer 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.