Skip to main content
After reading this guide, you will know:
  • why this chapter keeps DSPy’s cache on;
  • what a rollout ID is, and how it gets a fresh reply past DSPy’s cache;
  • how to call a copy of the model, with other options, for one block of code;
  • why a double click would be billed twice, and how Turbo prevents it.
At the end of chapter 7, asking again for a reply to the same ticket brought back the first one, from DSPy’s cache. In this chapter, each new suggestion asks the model afresh.

1. Past the cache

The first fix to reach for is Installation’s cache: false. On the app’s model, it turns off the cache for every call, triage’s too. DSPy’s way to get a different answer and keep the cache on is a rollout ID, its rollout_id. A rollout ID is part of each call’s cache key: a new one reaches the model, and the same one again is answered from the cache. DSPy’s own BestOfN and Refine, which run a module several times and keep the best answer, use it this way. Temperature sets how much a model’s replies vary. A rollout ID changes the reply only when temperature is above 0, and the app’s model leaves temperature to the provider, so set temperature: 1.0 with it.

2. A copy of the model for one block

Every call uses the app’s one model, so a suggestion needs a copy of it with a rollout ID and a temperature, used only for its own calls. DSPyRb.lm is the model your calls use: the app’s model, or in a test, the test’s DummyLM. Its kwargs are its options, and nil means the provider’s default:
copy makes a new model with some options changed. The original keeps its own:
DSPyRb.context(lm: ...), which your tests have used since chapter 2, makes the calls in its block use another model. After the block, they use the app’s model again:
None of this calls the model. In DSPy, these are dspy.settings.lm, lm.copy and dspy.context.

3. Try a rollout ID

In the same console, run chapter 7’s program on its ticket, twice with the same rollout ID, then with a new one:
So each suggestion needs a new rollout ID.

4. A rollout ID for each suggestion

Replace app/models/ticket.rb with:
app/models/ticket.rb
  • Each suggestion asks with a random rollout ID, one of about two billion, so each click gets a fresh reply.
  • model_for_next_suggestion copies DSPyRb.lm, not config.lm, so in a test it copies the test’s DummyLM, as step 2 said, and the copy keeps its scripted answers.

5. The button asks for another

Once a ticket has a suggestion, its button should offer another. Replace app/helpers/tickets_helper.rb with:
app/helpers/tickets_helper.rb
A double click would ask for two fresh replies, both billed. Turbo disables a form’s button while it submits, so one support agent can’t send two by accident; chapter 7’s button already did that. The view adds data-turbo-submits-with, which shows “Suggesting a reply…” meanwhile. Replace app/views/tickets/show.html.erb with:
app/views/tickets/show.html.erb

6. See it in the browser

Start the server, and open the “Missing item” ticket you asked for a reply in chapter 7. Its button now reads “Suggest another reply”. Click it, and a fresh reply appears above the earlier ones, such as:
We are sorry to hear that your order 6012 arrived without the charging cable. Please confirm the missing item, and we will send it to you within 2 business days. Based on: Missing items

7. Test the fresh reply and the label

A DummyLM never caches, so a test can’t see a fresh reply. It checks the options each suggestion asks with instead. on_call: runs a listener after each model call in its block. A context inside another keeps the outer one’s listener, so the listener also runs for calls made with the inner one’s model. Try both on the test’s scripted answers, for nothing:
The agent made two calls, the outer listener ran after each, and both asked with the copy’s options. max_tokens: 1000 is a DummyLM’s own default. The test does the same: its context gives the DummyLM and the listener, and suggest_reply’s gives the copy. It checks that two suggestions ask with temperature 1.0 and different rollout IDs. Replace test/models/ticket_test.rb with:
test/models/ticket_test.rb
In model_options_during_next_suggestion, uniq.sole checks that every call of a suggestion asked with the same options. Replace test/integration/ticket_submission_test.rb with:
test/integration/ticket_submission_test.rb
Its “another reply” test starts from the ticket that already has a suggestion. The button’s label has a test of its own. Create test/helpers/tickets_helper_test.rb:
test/helpers/tickets_helper_test.rb

8. Check your work

Both should pass, with no offenses and 22 tests.

If something goes wrong

If “Suggest another reply” gives the same reply again, check that suggest_reply runs the program inside DSPyRb.context(lm: model_for_next_suggestion).

Comparing with the tag

The chapter-8 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

A ledger of model work

Record what each piece of a ticket’s model work cost, in a ledger whose entries never change.