- why one predictor can’t answer from the help center;
- what DSPy’s ReAct agent is, and how to give it a
DSPyRb::Tool; - how to read what an agent did, in its history;
- how to test an agent on scripted turns.
1. A reply from the ticket alone
Start simple: one predictor, with a string signature like chapter 1’s.2. Articles for the help center
Generate a model for the articles:db/seeds.rb, so the file reads:
db/seeds.rb
app/models/article.rb with:
app/models/article.rb
LIKE is enough for a few articles. A large help center needs full-text search.
Try it with two words:
3. A tool for the search
A tool is a function the model can ask DSPy to run. In Ruby on DSPy, it’s aDSPyRb::Tool subclass: desc tells the model what it does, each arg declares a typed argument, and call does the work.
Create app/tools/search_articles.rb:
app/tools/search_articles.rb
search_articles, from the class’s name. Each article comes back with its id, so the model can cite it.
app/tools is a new folder, so restart any running server or console. Then try the tool:
4. DSPy’s ReAct agent
Deciding what to search, searching again, and knowing when to answer is the open-ended tool use DSPy gives to an agent: a module in which the model asks for tools, turn by turn, until it can answer. “Agent” means DSPy’s agent; “support agent” means the person. DSPy’s ReAct is an agent, and one of DSPy’s built-in modules, likePredict and ChainOfThought. It works in turns:
- Each turn, the model asks for tool calls, and DSPy runs them.
- DSPy adds a
submittool, which the model calls with the signature’s outputs when it’s done. max_iterscaps the turns, not the tool calls. If the turns run out, or the model stops calling tools, DSPy asks forsubmitonce more.
DSPyRb::ReAct[signature, tools: [...], max_iters: ...] makes one in Ruby.
The agent’s outputs are the reply and the articles it rests on. Create app/signatures/suggest_reply.rb:
app/signatures/suggest_reply.rb
article_ids keeps the articles apart from the text the customer reads, so the app can show them.
5. Try the agent in the console
Ask the agent about step 1’s ticket, with five turns at most: room for a few searches and a reply, and a cap on what one suggestion can cost.termination_reasonis"submit"when the model submitted, and"forced_submit"when DSPy had to ask for it.history["messages"]holds a message for each turn: the model’s thought, the tool calls it asked for, and their results.
"tool_calls" is DSPy’s ToolCalls, which keeps its list under "tool_calls" too, so the key appears twice.
The app never reads the history; it’s for you, when a reply looks wrong.
6. A program that holds the agent
The app and its tests need the same agent, so its signature, tool and turn cap live in one place: a program, like chapter 5’s desk. Createapp/programs/reply_writer.rb:
app/programs/reply_writer.rb
7. Keep each suggestion with its ticket
A suggestion belongs to a ticket, with the articles it cites. Generate a model for them:app/models/suggested_reply.rb with:
app/models/suggested_reply.rb
app/models/ticket.rb with:
app/models/ticket.rb
8. The button
A support agent asks for a suggestion from the ticket’s page. Nest a route for it in the tickets’ resources, soconfig/routes.rb reads:
config/routes.rb
app/controllers/suggested_replies_controller.rb:
app/controllers/suggested_replies_controller.rb
_ticket, which the list of tickets shows too. Replace app/views/tickets/show.html.erb with:
app/views/tickets/show.html.erb
app/views/suggested_replies/_suggested_reply.html.erb:
app/views/suggested_replies/_suggested_reply.html.erb
9. See it in the browser
Start the server, open one of chapter 6’s “Missing item” tickets, and click “Suggest a reply”. The page shows a suggestion such as:We are sorry to hear that your order 6012 arrived without the charging cable. Please let us know the missing item (in this case, the charging cable), and we will send it to you within 2 business days. Based on: Missing itemsClick “Suggest a reply” again, and the same reply appears a second time, at once: DSPy answered it from its cache. A support agent who wanted a different suggestion gets the first one again.
10. Scripted turns for the tests
In the tests, the agent runs onDummyLM, which takes one scripted answer for each turn: the model’s next_thought, and the tool_calls it asks for, with the doubled key from step 5. Create test/fixtures/files/reply_answers.yml:
test/fixtures/files/reply_answers.yml
test/fixtures/articles.yml with:
test/fixtures/articles.yml
test/fixtures/tickets.yml with:
test/fixtures/tickets.yml
test/fixtures/suggested_replies.yml with:
test/fixtures/suggested_replies.yml
scripted_model takes the file’s name. In test/test_helper.rb, change it so the file reads:
test/test_helper.rb
11. Test the new pieces
Test the search, the tool and the program each on its own, so a failing test points at the piece that broke. Replace the generatedtest/models/article_test.rb with:
test/models/article_test.rb
test/tools/search_articles_test.rb:
test/tools/search_articles_test.rb
test/programs/reply_writer_test.rb:
test/programs/reply_writer_test.rb
test/models/suggested_reply_test.rb with:
test/models/suggested_reply_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
12. Check your work
If something goes wrong
Comparing with the tag
Thechapter-7 tag holds the app at the end of this chapter. To compare yours with it:
Next
Another reply
Get a fresh suggestion past DSPy’s cache, with a rollout ID on a copy of the model.