- how a description tells the model what an output means;
- how to run a predictor when a ticket is created, and save what it returns;
- how to test model calls without calling a model.
1. A signature for triage
Chapter 1’s signature,"ticket -> summary", let the model answer in any words it liked. Triage needs more than that. The category has to come from a fixed list the app can rely on, urgent has to be a real true or false, and the model needs to know what the team counts as urgent.
For that, you write the signature as a class, like the Introduction’s, and give urgent a description.
Create app/signatures/triage_ticket.rb:
app/signatures/triage_ticket.rb
desc: gives an output a description, which tells the model what it means. That’s where the team’s idea of urgent goes.
As in the Introduction, a reply that doesn’t fit the types raises an error: DSPyRb::AdapterParseError.
app/signatures is a new folder, so restart any running server or console for Rails to load it.
2. Try it in the console
Before wiring the signature into the app, try it on a sample ticket:to_h shows all of a prediction’s outputs at once, which suits the console. The app reads each one by name instead, as in result.category.
3. Columns for the results
Give tickets a column for each output, with the same name, so each assignment reads plainly, as incategory: result.category:
4. Triage each new ticket
Run the predictor whenever a ticket is created. Replaceapp/models/ticket.rb with:
app/models/ticket.rb
process runs the ticket through each step, then saves it once. For now triage is the only step; chapter 3 adds a second.
The model call runs inline, in the request, so creating a ticket now waits for the model.
5. Show the results
Show the category on the ticket’s page, and mark the ticket when it’s urgent. Replaceapp/views/tickets/_ticket.html.erb with:
app/views/tickets/_ticket.html.erb
6. See it in the browser
Start the server, and create a ticket at http://localhost:3000/tickets/new. Try the subject “Where is my parcel?” and the body “My order 3310 was due on Monday and has not arrived. I need it for a wedding on Saturday.” The ticket’s page shows the category the model chose,shipping for this one, with an “Urgent” mark.
7. Tests without a real model
Creating a ticket now calls the model, but tests shouldn’t depend on one: a real model costs money and needs your key, and no test can know its answer in advance. So the tests useDSPyRb::DummyLM, DSPy’s own stand-in model. It returns the answers you give it, one per call, in order, and never calls a provider.
A test hands its DummyLM to the code it tests with DSPyRb.context(lm: ...), which runs its block with that model in place of config.lm.
So that no test reaches the real model by mistake, even with your key set, take the default model away in tests. Add these lines after require "rails/test_help" in test/test_helper.rb, so it reads:
test/test_helper.rb
DSPyRb.context now raises DSPyRb::Error (“no LM”) at once, and sends nothing.
8. Test a new ticket
A new ticket should arrive with the category and urgent flag the model gave it. To test that,DummyLM needs an answer to give, and the next step’s test uses the same one, so keep it in a fixture file. Create test/fixtures/files/ticket_answers.yml:
test/fixtures/files/ticket_answers.yml
test/models/ticket_test.rb with:
test/models/ticket_test.rb
9. Test what a support agent sees
TheTicket test checks what triage saves. This test checks what a support agent sees: it logs a ticket through the app and reads its page. Create test/integration/ticket_submission_test.rb:
test/integration/ticket_submission_test.rb
DSPyRb.context, so it fails, and its six other tests cover only code the scaffold generated. Delete it:
10. Run the tests on any machine
DummyLM runs inside DSPy, so a new machine, or GitHub’s, needs DSPy installed before the tests can run. Add the install to bin/setup, after its bundle install line:
bin/setup
test job in .github/workflows/ci.yml, before “Run tests”:
.github/workflows/ci.yml
11. Check your work
If something goes wrong
Comparing with the tag
Thechapter-2 tag holds the app at the end of this chapter. To compare yours with it:
Next
Extraction
Pull the order numbers and products out of each ticket, as lists.