Skip to main content
After reading this guide, you will know:
  • what DSPy means by a module and a program;
  • how to write a program as a DSPyRb::Module;
  • how to run a ticket’s whole job with one call;
  • how to test a program on its own.
In chapters 3 and 4, the console tried each predictor on its own, never the whole job: the only way to run both is to save a ticket. In this chapter, the two become one program, which you can run on its own. It returns every output in one prediction, and Ticket makes a single call to it.

1. A program that runs both predictors

A DSPy module is a building block for programs that use language models, and modules can be composed into bigger modules, called programs. Triage’s ChainOfThought and extraction’s Predict are DSPy’s built-in modules, so the two can be composed into one program. In Ruby, a program is a DSPyRb::Module, DSPy’s dspy.Module: it holds its predictors in instance variables and calls them in forward. Create app/programs/ticket_desk.rb:
app/programs/ticket_desk.rb
Its predictors are chapter 4’s, unchanged, so triage still writes out its reasoning. In Ruby on DSPy, forward must return a prediction, so it builds one with DSPyRb::Prediction[...], naming each output it takes from the two predictors. You run a program with call, as you do a predictor: call runs forward and returns its prediction. app/programs is a new folder, so restart any running server or console, as in chapter 2.

2. Try it in the console

Run the program on its own, on a new ticket:
One call to the program gives all five outputs, though it still makes two model calls, one for each predictor.

3. Run the program for each new ticket

Let the program do each new ticket’s work. Replace app/models/ticket.rb with:
app/models/ticket.rb
The steps now live in the program, so process calls it and saves each output in one update!. Nothing changes on the ticket’s page, and chapter 4’s tests still pass.

4. Test the program on its own

The program exists so that a ticket’s whole job runs without saving a ticket. Its own test checks that. Three test files now need the same scripted model, so it becomes a helper they all share. In test/test_helper.rb, add scripted_model under Rails’ comment for helpers, so the file reads:
test/test_helper.rb
Create test/programs/ticket_desk_test.rb, which runs the program with no ticket at all:
test/programs/ticket_desk_test.rb
The ticket tests now use the helper too. Replace test/models/ticket_test.rb with:
test/models/ticket_test.rb
And replace test/integration/ticket_submission_test.rb with:
test/integration/ticket_submission_test.rb

5. Check your work

Both should pass, with no offenses and 7 tests.

If something goes wrong

If you see a NameError naming TicketDesk, the server or console started before app/programs existed. Restart it.

Comparing with the tag

The chapter-5 tag holds the app at the end of this chapter. To compare yours with it:
Expect the same files to differ as in chapter 1.

Next

What a ticket cost

Total each ticket’s model calls, tokens and cost, and show them on its page.