liveBook · Manning

Chapter 7: Q&A chatbots with LangChain and LangSmith

What you will learn in chapter 7 of AI Agents and Applications

Introduces the chapter’s planned coverage of building RAG chatbots with LangChain and tracing their execution with LangSmith.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces the chapter’s planned coverage of building RAG chatbots with LangChain and tracing their execution with LangSmith.

7.1 LangChain object model for Q&A chatbots

This section explains how LangChain’s abstractions and ready-made components support Q&A applications, then introduces the two stages discussed next.

Key terms: abstractions, data loaders, vector stores, LLMs, ready-to-use implementations, splitting source text, retrieving relevant context, generating prompts, managing the context window

7.1.1 Content ingestion (indexing) stage

This section explains the LangChain components and sequence used to ingest content into a vector store for Q&A.

Key terms: Loader, Document, DocumentTransformer, TextSplitter, Embeddings, VectorStore, vector stores, embedding model, precompute the embeddings

7.1.2 Q&A (retrieval and generation) stage

This section explains the LangChain components and sequence that retrieve relevant document chunks and use them to answer a question.

Key terms: Retriever, VectorStore, Embeddings, text chunks, Q&A orchestrator, PromptTemplate, LanguageModel, context

7.2 Vector store content ingestion

This section establishes that documents must be stored before they can be queried and sets up the environment for ingesting them.

Key terms: vector database, querying your documents, virtual environment, Jupyter Notebook, OpenAI API key

7.2.1 Splitting and storing the documents

This section shows how to make documents searchable by splitting them into chunks and storing their embeddings in Chroma, while noting that Wikipedia loading may bring in extra articles.

Key terms: chunks, overlap, embeddings, Chroma vector database, WikipediaLoader, Wikipedia hyperlinks, related articles

7.2.2 Removing duplication

This section removes duplication in content-ingestion code by extracting shared work into a function called for each loader.

Key terms: duplication, function, loader

7.2.3 Ingesting multiple documents from a folder

This section presents two ways to ingest a folder of documents into a vector store: selecting a loader for each file or using DirectoryLoader.

Key terms: loader factory, get_loader(), split_and_import(), vector store, DirectoryLoader, folder path, glob pattern, Unstructured, langchain-unstructured

7.3 Q&A across stored documents

The section explains why retrieving information across stored documents is a useful check on content ingestion and chunking.

Key terms: vector store, ingestion, chunking strategy

7.3.1 Querying the vector store directly

A direct vector-store query shows which chunks are retrieved for a question spanning sources, before a LangChain chain produces a formulated answer.

Key terms: vector store, chunks, metadata, LangChain chain

7.3.2 Asking a question through a LangChain chain

This subsection explains how a LangChain RAG chain handles prompt setup and presents two ways to obtain its prompt from the LangChain Hub.

Key terms: RAG chain, prompt template, vector store, LangChain Hub, prompt instance

7.3.3 Completing the RAG chain setup

This section completes a RAG chain by connecting retrieved context and the user’s question to a prompt that a chat model uses to generate an answer.

Key terms: Retriever, Question feeder, Chat model, context parameter, pass-through component, Runnable interface, dictionary, LangChain chain

7.3.4 Follow-up question

A vague follow-up question reveals that the chatbot cannot resolve references to an earlier exchange because it does not retain conversation history.

Key terms: stateless, memory, conversation flow, follow-up question

7.4 Chatbot memory of message history

This section explains why remembering earlier questions and responses makes a chatbot useful for follow-up questions.

Key terms: message history, previous questions and responses, context

7.4.1 Amending the prompt

This subsection adapts a RAG prompt to include chat history and explains the message format and inputs the revised prompt uses.

Key terms: PromptTemplate.from_template, ChatPromptTemplate.from_messages, message history, chat message, role, message text, chat history, chat_history, question, retrieved_context

7.4.2 Updating the chat message history

This section shows how to keep user questions and LLM responses in LangChain chat history so they can be supplied with the next question.

Key terms: ChatMessageHistory, chat_history_memory, human, ai, system, chat_history, messages property, prompt

7.4.3 Feeding the chat history to the RAG chain

This section connects updated message history to the RAG chain’s prompt using LCEL.

Key terms: message history, RAG chain, LangChain Expression Language (LCEL), chat_history_messages

7.4.4 Putting everything together

This section brings the memory-enabled RAG chain together and tests whether chat history lets it answer a contextual follow-up question.

Key terms: chat history, LCEL, retriever, prompt, memory, model, follow-up question, memoryless chatbot

7.5 Tracing execution with LangSmith

The section explains why LangSmith is useful, how to enable tracing for the Q&A chatbot notebook, and how to inspect its execution in the dashboard.

Key terms: LangSmith, tracing, evaluators, monitoring, LangSmith API key, Personal Access Token, environment variables, Jupyter Notebook, project, trace

7.6 Summary

This section summarizes how LangChain assembles RAG chatbots, preserves conversation context, and uses LangSmith to inspect execution.

Where this is used later

Concepts explained in this chapter

← Chapter 6: RAG fundamentals with ChromaDB Chapter 8: Advanced indexing →

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.