liveBook · Manning

Chapter 9: Creating an agentic RAG-based chatbot

What you will learn in chapter 9 of Building LLM Applications with DSPy

The introduction explains how retrieval, tool-using agents, and conversation history help DSPy chatbots answer questions and carry out requests.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains how retrieval, tool-using agents, and conversation history help DSPy chatbots answer questions and carry out requests.

Key terms: retrieval-augmented generation, RAG, knowledge base, contextual information, agentic systems, agents, LMs, tools, history of discussions, memory

9.1 The WixQA dataset

The section introduces WixQA’s expert-written answers and knowledge base articles as the data for a RAG-based chatbot.

Key terms: WixQA, queries, golden documents, golden answers, wixqa_expertwritten, wix_kb_ corpus, article IDs, knowledge base

9.2 Creating a DSPy RAG application

Introduces a DSPy RAG application that retrieves relevant WixQA articles by vector similarity and supplies their content to an LM to answer a question.

Key terms: RAG, retrieval, generation, custom module, vector search, embedding, article titles, vector store, context, vector DB

9.2.1 Embedding the article titles

The section shows how to use embeddings to retrieve relevant Wix knowledge-base article titles and then locate the corresponding article text.

Key terms: SentenceTransformer, bge-small-en-v1.5, DSPy retriever, Embeddings, corpus, k, vector store, Faiss, in memory, WixQA

9.2.2 The baseline RAG module

The section explains how a baseline DSPy RAG module retrieves relevant knowledge-base articles and uses an LM to answer a question.

Key terms: RAG module, forward(), retrieve step, generation step, k most similar articles, custom modules, DSPy program, evaluate, optimize

9.2.3 The factuality evaluation metric

This section introduces a factuality judge for evaluating RAG answers against WixQA ground-truth answers and checks it on two known examples.

Key terms: ground truth answer, golden answers, LLM-as-a-judge, factuality, prediction, ground truth, completeness, judge, baseline RAG module

9.2.4 Calculating the overall accuracy of the baseline

The section evaluates a baseline RAG program with a judge, compares two task LMs, and shows how to decide whether disappointing answers call for retrieval or answer-generation improvements.

Key terms: judge, metric function, forward(), task LM, example, prediction, retriever, LM, prompt, RAG program

9.2.5 Multihop RAG

This section explains how RAG can move beyond one retrieval step by searching iteratively, including a multistep module that uses a critic to decide when to stop.

Key terms: baseline RAG, multihop workflow, retriever, LM, multistep workflow, search terms, critic, maximum number of steps, RAG module, evaluated

9.2.6 HyDE

Explains how HyDE improves RAG retrieval by generating hypothetical matching titles or document content, then contrasts RAG with fine-tuning a stable knowledge base into an LM.

Key terms: Hypothetical Document Embedding (HyDE), retriever, hypothetical query strings, fine-tune, model’s weights, local knowledge base, context information

9.3 Agents and ReAct

Introduces DSPy’s ReAct agent as a reasoning-and-action loop that uses Python tools and their results to complete tasks.

Key terms: ReAct, reasoning, acting, subtasks, ReAct module, tools, Python functions, observation, long-running loop, always-ready assistant

9.3.1 Creating a ReAct calculator

This section uses a four-operation DSPy calculator to show how ReAct selects and executes tools, records their results, and produces a final answer.

Key terms: ReAct, signature, tools, max_iters, reasoning–acting loop, tool executions, observations, final response, trajectory, finish

9.3.2 Agentic RAG

Explains how a ReAct-based RAG system chooses and repeatedly uses retrieval tools, illustrates that process with WixQA, and weighs its accuracy against its LM-call cost.

Key terms: agentic RAG, ReAct, tools, multistep process, WixQA, retrieve_article_titles_and_ids(), read_article(), SupportBackend, ReAct-based approach, single-step model

9.3.3 Agents using DSPy’s Python interpreter

This section explains why a ReAct agent might use Python execution and how DSPy’s PythonInterpreter serves as that tool.

Key terms: Python code, complex analytic solution, simulation, ReAct agent, PythonInterpreter, Deno, Pyodide

9.4 Maintaining memory

This section explains how passing conversation history to a DSPy chatbot helps it answer follow-up questions, and what a production memory implementation must account for.

Key terms: memory, history of previous interactions, DSPy’s History class, history, prompts, general knowledge, relevant items in history, separate histories per user, bloat

9.5 Self-correction with DSPy Refine

This section shows how DSPy Refine can improve an inconsistent chatbot by trying responses against a quality check, while warning that repeated attempts may rerun tools.

Key terms: Refine, reward function, LLM-as-a-judge, ReAct submodule, threshold, N, ReAct, tools, cache

9.6 Connecting agents to MCP

This section explains why agents need a standard way to use external capabilities and introduces MCP’s server-and-client approach to tool discovery and invocation.

Key terms: Python functions, API, SDK, tool, Model Context Protocol (MCP), MCP server, MCP client, tools, input schemas, database

9.6.1 MCP protocol intro

Introduces the MCP host, client, and server roles and the two connection types needed to understand how an application reaches MCP services.

Key terms: host, MCP client, MCP server, stdio, standard input/output, HTTP

9.6.2 Exposing the Wix knowledge base through an MCP server

The section shows how to expose WixQA knowledge-base search and article reading through a FastMCP server for other AI applications.

Key terms: MCP server, search_wix_kb, read_wix_kb_article, FastMCP, @mcp.tool(), tool definition, docstring, type hints, knowledge base backend, WixQA articles

9.6.3 Connecting a DSPy agent to an MCP server

This section explains how a DSPy ReAct agent connects to an MCP server, discovers and uses its tools, and calls them asynchronously.

Key terms: StdioServerParameters, initialize(), list_tools(), MCP session, dspy.Tool.from_mcp_tool(), dspy.Tool, client session, observation, MCP host, MCP server

9.7 Summary

Concepts explained in this chapter

← Chapter 8: Summarization and more effective metric functions

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.