liveBook · Manning

Chapter 10: RAG and agentic apps with LangGraph and Streamlit

What you will learn in chapter 10 of Build Python Web Apps with Streamlit

This chapter introduces building a Streamlit customer-service chatbot that uses LangGraph, private data retrieval, and real-world actions to provide business value.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces building a Streamlit customer-service chatbot that uses LangGraph, private data retrieval, and real-world actions to provide business value.

Key terms: pre-trained model, external data sources, domain-specific queries, business systems, personalized assistance, customer service chatbot, company data, track orders, cancel orders, human agent

10.1 Nibby: A customer service bot

This section introduces the fictional customer-support problem that motivates building Nibby, a Streamlit-based support bot.

Key terms: Nibby, Streamlit support bot, customer support

10.1.1 Stating the concept and requirements

Defines Nibby’s customer-support role, required capabilities, deliberate scope limits, and the learning focus of its toy service actions.

Key terms: Nibby, customer support chatbot, custom knowledge base, frontline agent, human customer support agent, tracking an order, canceling an order, toy logic, real-world actions, out of scope

10.1.2 Visualizing the user experience

This subsection sketches Nibby as a familiar chat interface where user messages produce AI replies in an accumulating conversation view.

Key terms: text box, AI response, conversation view, chatbot

10.1.3 Brainstorming the implementation

This section outlines Nibby’s backend design for grounding customer-support answers in private knowledge and letting the bot execute real-world actions through tools.

Key terms: private knowledge store, vector database, context, LLM, tools, tool call, conversation view, backend, private knowledge

10.1.4 Installing dependencies

10.2 Creating a basic chatbot

This section introduces LangGraph and LangChain as libraries for reducing the complexity of sophisticated AI applications beyond simple OpenAI API integrations.

Key terms: LangGraph, LangChain, AI-driven applications, OpenAI API, Retrieval-Augmented Generation, RAG, agent-based workflows

10.2.1 Introduction to LangGraph and LangChain

This section introduces LangChain and LangGraph as Python libraries for managing the multi-step, stateful, tool-using workflows needed in modern LLM applications.

Key terms: LLM, prompt, multi-step workflows, external tools, conversation context, LangChain, LangGraph, prompt management, memory, tool integration

10.2.2 Graphs, nodes, edges, and state

This section explains how LangGraph models an application as a directed graph whose nodes transform shared state from an initial input to a final output.

Key terms: graph, vertices, nodes, edges, graph theory, LangGraph, state, graph state, node, st.session_state

10.2.3 A one-node LLM graph

This section builds Nibby’s first single-turn chatbot by implementing a one-node LangGraph LLM graph, wrapping it in backend classes, and connecting it to a Streamlit chat interface.

Key terms: StateGraph, messages, assistant_node, START, END, invoke, TypedDict, MyGraphState, MessagesState, AnyMessage

10.3 Multi-turn conversations

This section introduces the limitation of Nibby’s single-turn chatbot and frames the work needed to support remembered multi-turn conversations.

Key terms: multi-turn conversations, conversation history, frontend

10.3.1 Adding memory to the graph

This section adds in-memory conversational memory to Nibby’s LangGraph so repeated invocations in one Streamlit session retain prior messages.

Key terms: state, graph invocation, multi-turn conversations, checkpointer, MemorySaver, compile, graph state, thread ID, self.config, configurable

10.3.2 Displaying the conversation history

This section exposes a LangGraph conversation's stored messages through the backend and renders every message in the Streamlit chat interface.

Key terms: SupportAgentGraph, get_conversation, get_history, self.graph.get_state(self.config), messages, conversation thread, bot.get_history(), st.chat_message, HumanMessage, AIMessage

10.4 Restricting the bot to customer support

This section motivates restricting Nibby from a general chatbot to a strictly customer-support-focused bot so frivolous LLM requests do not create unnecessary costs.

Key terms: general chatbot, customer support bot, Nibby, cloud-based LLM, LLM tokens, frivolous request, strictly business

10.4.1 Creating a base prompt

This subsection defines a reusable base system prompt that tells Nibby its customer-support role for Note n’ Nib and makes it refuse unrelated requests.

Key terms: BASE_SYS_MSG, base prompt, system message, Note n’ Nib, Nibby, refuse, unrelated requests, customer support, prompts.py, Listing 10.6

10.4.2 Inserting a base context node in the graph

This subsection adds a base-context node to Nibby’s LangGraph so a system prompt is stored in graph state and sent before conversation messages, keeping the bot focused on customer support.

Key terms: base_context_node, base_context, START, assistant, BASE_SYS_MSG, StateGraph, assistant_node, SystemMessage, sys_msg_text, messages

10.5 Retrieval Augmented Generation

This section motivates Retrieval Augmented Generation by showing why Nibby needs private, custom knowledge rather than relying only on an LLM’s public pre-training.

Key terms: pre-trained, publicly available information, private information, GPT-5.1, hallucinated, hallucination, Note n’ Nib, InkStream, RoyalQuill, augment

10.5.1 What is Retrieval Augmented Generation?

This section explains why Retrieval Augmented Generation selectively retrieves relevant custom knowledge for an LLM and introduces embeddings and vector databases as the retrieval mechanism.

Key terms: Retrieval Augmented Generation, RAG, knowledge base, context, retrieve, augment, context window length, tokens, information overload, custom knowledge base

10.5.2 Implementing RAG in our app

This section implements RAG for Nibby by ingesting support articles into Pinecone and adding retrieval and prompt augmentation nodes to its LangGraph workflow.

Key terms: Pinecone, index, vector database, embeddings, text-embedding-3-small, dimension, VectorStore, ingest_folder, DirectoryLoader, TextLoader

10.6 Turning the bot into an agent

This section explains why Nibby must become an agent to handle customer-specific requests that require accessing company systems or taking actions, not merely providing information.

Key terms: agent, customer support knowledge base, static textual articles, agents, AI applications, interact with the real world, retrieve the right information, take the appropriate action

10.6.1 What are agents?

This section distinguishes action-taking AI agents from informational chatbots and introduces ReAct as a framework for dynamic reasoning and action.

Key terms: AI agents, agentic apps, traditional AI chatbots, reason, plan, external systems, tool use, APIs, database queries, workflows

10.6.2 The ReAct framework

This section explains how the ReAct framework makes an AI agent alternate between reasoning and actions until it can give the user a final response.

Key terms: ReAct framework, Reason, Act, Respond, Nibby, Note n’ Nib, order management system, order status

10.6.3 Creating tools an agent can use

This section supplies Nibby with mock order data and four well-described callable tools so an agent can identify customers, inspect orders, and cancel them.

Key terms: tool, ReAct AI agent, function, API, retrieve_user_id, get_order_id, get_order_status, cancel_order, user ID, order ID

10.6.4 Making our graph agentic

This section turns Nibby into an agent by wiring LangGraph tool execution, conditional routing, and internal-message filtering into its conversation graph.

Key terms: bind_tools, AIMessage, tool_calls, docstrings, type hints, ToolNode, tools_condition, conditional edge, assistant, tools

10.7 Summary

This summary recaps how Nibby combines Streamlit chat UI, LangGraph workflows and memory, RAG knowledge retrieval, and agent tools to create an advanced customer-service bot.

Where this is used later

Concepts explained in this chapter

← Chapter 9: Creating an AI-powered application Chapter 11: Testing Streamlit apps →

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.