Skip to main content
After reading this guide, you will know:
  • 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.
A support agent answers most tickets from the same few help center articles. In this chapter, the model drafts that answer when a support agent asks, for them to check and send.

1. A reply from the ticket alone

Start simple: one predictor, with a string signature like chapter 1’s.
The model doesn’t know the shop’s policies, so it guesses: “1-2 business days to review”, where the help center says 5 business days. The reply needs the help center’s articles.

2. Articles for the help center

Generate a model for the articles:
Add a few articles to the end of db/seeds.rb, so the file reads:
db/seeds.rb
Then load them:
The app could search first and hand the model what it finds. Replace app/models/article.rb with:
app/models/article.rb
A LIKE is enough for a few articles. A large help center needs full-text search. Try it with two words:
“order” finds most of the help center, and “charged” finds the one article that answers. The right word depends on the ticket, and a search that finds nothing needs another try. Sending every article instead pays for all of them on every suggestion. So let the model choose what to search. A tool is a function the model can ask DSPy to run. In Ruby on DSPy, it’s a DSPyRb::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
The model knows it as 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:
A predictor makes one call. It can’t search, read the results, then answer.

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, like Predict and ChainOfThought. It works in turns:
  • Each turn, the model asks for tool calls, and DSPy runs them.
  • DSPy adds a submit tool, which the model calls with the signature’s outputs when it’s done.
  • max_iters caps the turns, not the tool calls. If the turns run out, or the model stops calling tools, DSPy asks for submit once 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.
Unlike step 1’s, this reply says what the refunds article says, and cites it. The model chose “charged” itself, the word step 2 showed matters.
  • termination_reason is "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.
A turn’s "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. Create app/programs/reply_writer.rb:
app/programs/reply_writer.rb
Run it on the same ticket:
DSPy’s cache answered both calls, as in chapter 6, so the program gave step 5’s reply at once, for nothing.

7. Keep each suggestion with its ticket

A suggestion belongs to a ticket, with the articles it cites. Generate a model for them:
Replace app/models/suggested_reply.rb with:
app/models/suggested_reply.rb
Replace app/models/ticket.rb with:
app/models/ticket.rb
A ticket’s model calls, tokens and cost still cover only its triage and details.

8. The button

A support agent asks for a suggestion from the ticket’s page. Nest a route for it in the tickets’ resources, so config/routes.rb reads:
config/routes.rb
Create app/controllers/suggested_replies_controller.rb:
app/controllers/suggested_replies_controller.rb
The button and the suggestions go on the ticket’s page, not in _ticket, which the list of tickets shows too. Replace app/views/tickets/show.html.erb with:
app/views/tickets/show.html.erb
Create 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 items
Click “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 on DummyLM, 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
The tests also need articles, tickets and a suggestion to start from. Replace the generated test/fixtures/articles.yml with:
test/fixtures/articles.yml
Each article has a fixed id, so the scripted answers can cite it. Replace test/fixtures/tickets.yml with:
test/fixtures/tickets.yml
Replace the generated test/fixtures/suggested_replies.yml with:
test/fixtures/suggested_replies.yml
The tests now use two answer files, so 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 generated test/models/article_test.rb with:
test/models/article_test.rb
Create test/tools/search_articles_test.rb:
test/tools/search_articles_test.rb
Create test/programs/reply_writer_test.rb:
test/programs/reply_writer_test.rb
Replace the generated test/models/suggested_reply_test.rb with:
test/models/suggested_reply_test.rb
Replace test/models/ticket_test.rb with:
test/models/ticket_test.rb
Then test the button as a support agent uses it. Replace test/integration/ticket_submission_test.rb with:
test/integration/ticket_submission_test.rb

12. Check your work

Both should pass, with no offenses and 18 tests.

If something goes wrong

If you see a NameError naming SearchArticles, the server or console started before app/tools existed. Restart it.If no suggestion ever has a “Based on:” line, check that bin/rails db:seed ran.If a suggestion fails, Rails shows its error page and saves no suggestion. The page names the error: DSPyRb::LMNotConfiguredError means no key reached DSPy, as in chapter 1, step 4.

Comparing with the tag

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

Another reply

Get a fresh suggestion past DSPy’s cache, with a rollout ID on a copy of the model.