> ## Documentation Index
> Fetch the complete documentation index at: https://ruby-on-dspy.ralak.co/llms.txt
> Use this file to discover all available pages before exploring further.

# A new Rails app

> Create Helpdesk, add Ruby on DSPy and make a first prediction in the console

After reading this guide, you will know:

* 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.

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 Ruby on DSPy and make a first prediction in the console.

If you haven't yet, read the [Introduction](https://ruby-on-dspy.ralak.co/index.md) 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](https://guides.rubyonrails.org/getting_started.html) guide.

## 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:

```bash theme={"theme":"one-dark-pro"}
rails new helpdesk
cd helpdesk
bin/rails generate scaffold Ticket subject:string body:text
bin/rails db:migrate
```

## 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 the `Gemfile`, after a blank line, so the file ends like this:

```ruby Gemfile theme={"theme":"one-dark-pro"}
group :test do
  # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
  gem "capybara"
  gem "selenium-webdriver"
end

gem "ruby-on-dspy", github: "samuelralak/ruby-on-dspy", ref: "92f5155"
```

Then install it:

```bash theme={"theme":"one-dark-pro"}
bundle install
```

## 3. Install DSPy

The gem installs its own Python and DSPy, apart from any Python you have. Install them, then check the install:

```bash theme={"theme":"one-dark-pro"}
bundle exec ruby-on-dspy setup
bundle exec ruby-on-dspy doctor
```

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:

```yaml theme={"theme":"one-dark-pro"}
openai_api_key: sk-...
```

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:

```ruby config/initializers/dspy_rb.rb theme={"theme":"one-dark-pro"}
DSPyRb.configure do |config|
  config.lm = DSPyRb::LM["openai/gpt-4.1-mini"]
  config.env = { "OPENAI_API_KEY" => Rails.application.credentials.openai_api_key }.compact
end
```

## 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:

```irb theme={"theme":"one-dark-pro"}
$ bin/rails console
Loading development environment (Rails 8.1.4.1)
helpdesk(dev)> ticket = "Hi, I was charged twice for order 4521. Please refund one of the charges."
=> "Hi, I was charged twice for order 4521. Please refund one of the charges."
helpdesk(dev)> DSPyRb::Predict["ticket -> summary"].call(ticket:).summary
=> "Customer reports being charged twice for order 4521 and requests a refund for one of the charges."
```

The first call takes a few seconds, while DSPy starts.

## 6. Check your work

```bash theme={"theme":"one-dark-pro"}
bin/rubocop
bin/rails test
```

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

> [!WARNING]
>
> * `DSPyRb::SetupError`: DSPy isn't installed for the gem you have now, which happens after you add or update it. Run `bundle exec ruby-on-dspy setup`.
> * `DSPyRb::LMNotConfiguredError`: no key reached DSPy. Its message reads:
>
>   ```text theme={"theme":"one-dark-pro"}
>   LMNotConfiguredError: [openai/gpt-4.1-mini] no API key (set OPENAI_API_KEY in your environment or in DSPyRb.config.env)
>   ```
>
>   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](https://github.com/samuelralak/dspy-tutorial/tree/chapter-1) holds the app at the end of this chapter. To compare yours with it:

```bash theme={"theme":"one-dark-pro"}
git remote add tutorial https://github.com/samuelralak/dspy-tutorial.git
git fetch tutorial --tags --force
git add --all
git diff --cached chapter-1 --stat
```

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

> [card] [Triage](https://ruby-on-dspy.ralak.co/helpdesk/triage.md)
>
> Label each new ticket with a category and an urgent flag, and show them on its page.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.