chapter nine

9 Creating an agentic RAG-based chatbot

 

This chapter covers

  • Using DSPy for Retrieval Augmented Generation
  • Agents and the ReAct methodology
  • Agentic RAG
  • DSPy’s support for memory and long-running conversations

So far, we’ve covered the most important ideas in DSPy and described how we can create, evaluate, and optimize DSPy-based applications. We’re now ready to create more sophisticated and realistic systems that you’ll likely need in research or work environments. This includes creating RAG-based applications, agents, and systems that can maintain conversations over long periods of time. DSPy supports all of these systems, as well as combining them to create powerful applications, including fully functional chatbots.

We’ll look first at RAG applications, which allow us to work with LMs so that they can respond to queries even when doing so requires information they didn’t see during their training. For example, a company chatbot can accept questions from customers about the company (related to its policies, products, or services), such as “Can I return a coffee maker I bought if it’s unused but the box is opened?” The chatbot may make one or more LM calls to answer these questions, but the LMs won’t, on their own, have any way to know the answer.

9.1 The WixQA Dataset

9.2 Creating a DSPy RAG application

9.2.1 Embedding the article titles

9.2.2 The baseline RAG module

9.2.3 The Factuality evaluation metric

9.2.4 Calculate the overall accuracy of the baseline

9.2.5 Multihop RAG

9.2.6 HYDE

9.3 Agents and ReAct

9.3.1 Creating a ReAct calculator

9.3.2 Agentic RAG

9.3.3 Agents using DSPy’s Python interpreter

9.4 Maintaining memory

9.5 Self-correction with DSPy Refine

9.6 Connecting agents to MCP

9.6.1 MCP Protocol Intro

9.6.2 Exposing the Wix knowledge base through MCP server

9.6.3 Connecting DSPy agent to MCP server

9.7 Summary