- 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.
1. Past the cache
The first fix to reach for is Installation’scache: 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:
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:4. A rollout ID for each suggestion
Replaceapp/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_suggestioncopiesDSPyRb.lm, notconfig.lm, so in a test it copies the test’sDummyLM, 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. Replaceapp/helpers/tickets_helper.rb with:
app/helpers/tickets_helper.rb
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
ADummyLM 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:
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
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
test/helpers/tickets_helper_test.rb:
test/helpers/tickets_helper_test.rb
8. Check your work
If something goes wrong
Comparing with the tag
Thechapter-8 tag holds the app at the end of this chapter. To compare yours with it:
Reading DSPy’s docs
DSPy’s docs go deeper into signatures, predictors, reasoning, programs and agents. Their code is Python, and its names map to Ruby like this:dspy.LM(...)isDSPyRb::LM[...]dspy.configure(lm=...)isDSPyRb.configure do |config| config.lm = ... enddspy.context(lm=...)isDSPyRb.context(lm: ...) do ... enddspy.settings.lmisDSPyRb.lmlm.copy(rollout_id=..., temperature=...)islm.copy(rollout_id: ..., temperature: ...)lm.kwargs["rollout_id"]islm.kwargs[:rollout_id]dspy.SignatureisDSPyRb::Signature: its docstring isinstructions, anddspy.InputField()anddspy.OutputField(desc=...)areinputandoutput ..., desc:dspy.PredictisDSPyRb::Predictdspy.ChainOfThoughtisDSPyRb::ChainOfThoughtrationale_field_type=strisreasoning: :writtendspy.Module, the class your programs subclass, isDSPyRb::Moduledspy.PredictionisDSPyRb::Predictiondspy.ReAct(signature, tools=[...], max_iters=...)isDSPyRb::ReAct[signature, tools: [...], max_iters: ...]- a DSPy tool, a Python function with a docstring, is a
DSPyRb::Toolsubclass: its docstring isdesc, each parameter anarg, and its bodycall result.history.messagesisresult.history["messages"]dspy.utils.DummyLMisDSPyRb::DummyLM