- 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.
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’sChainOfThought 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
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:3. Run the program for each new ticket
Let the program do each new ticket’s work. Replaceapp/models/ticket.rb with:
app/models/ticket.rb
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. Intest/test_helper.rb, add scripted_model under Rails’ comment for helpers, so the file reads:
test/test_helper.rb
test/programs/ticket_desk_test.rb, which runs the program with no ticket at all:
test/programs/ticket_desk_test.rb
test/models/ticket_test.rb with:
test/models/ticket_test.rb
test/integration/ticket_submission_test.rb with:
test/integration/ticket_submission_test.rb
5. Check your work
If something goes wrong
Comparing with the tag
Thechapter-5 tag holds the app at the end of this chapter. To compare yours with it:
Next
What a ticket cost
Total each ticket’s model calls, tokens and cost, and show them on its page.