liveBook · Manning

Retrieval-augmented generation

As explained in Build Python Web Apps with Streamlit

Retrieval-augmented generation is a technique that retrieves relevant external information and supplies it to a language model when generating a response.

A customer-support chatbot may need to answer questions about products and policies that a pretrained language model does not know. Retrieval-augmented generation (RAG) supplies relevant information from a knowledge base when the chatbot answers, rather than relying on the model’s pretrained knowledge alone. section 10.0 section 10.7

Why not put every support article in every prompt? A model’s context window limits how much it can receive, very large prompts can degrade performance, and sending the whole knowledge base on each call increases token costs. RAG selects the parts needed for the current question. section 10.5.1

The basic flow is:

Support articles as a knowledge base

Text files describe products and fountain-pen maintenance. An ingestion script loads the files, splits them into overlapping chunks, and stores the chunks with embeddings in a Pinecone index. Similarity search can then return chunks related to a customer’s question. API keys and index settings are kept in Streamlit secrets. section 10.5.2

splitter = RecursiveCharacterTextSplitter(
  chunk_size=1000,
  chunk_overlap=200
)
texts = splitter.split_documents(documents)

The splitter uses chunks of 1,000 characters with 200 characters of overlap before the documents are stored. section 10.5.2

In the chatbot workflow, a retrieval step searches using the conversation’s message contents. An augmentation step joins the returned document contents and inserts them into the system message. The prompt also tells the chatbot to refer the customer to a human representative if it cannot answer confidently from the supplied information. section 10.5.2

@staticmethod
def augment_node(state: AgentState):
  docs = state["retrieved_docs"]
  docs_content_list = [doc.page_content for doc in docs]
  content = "\n".join(docs_content_list)
  new_text = SYS_MSG_AUGMENTATION.replace("{docs_content}", content)
  return {"sys_msg_text": BASE_SYS_MSG + "\n\n" + new_text}

This augmentation step turns retrieved document contents into text for the system message. section 10.5.2

How Build Python Web Apps with Streamlit covers it

How deep it goes
Works through knowledge-base ingestion, similarity search, and prompt augmentation, using existing embedding and vector-store tools rather than implementing them from scratch.
Examples
Uses product and fountain-pen maintenance articles as searchable context for customer questions.
What it assumes
Assumes a Python chatbot and API credentials while walking through vector-store setup and article ingestion.
What you can do after
Apply it via Pinecone, LangChain, and LangGraph to retrieve support-article chunks and include them in chatbot answers.
How it connects
Connects context windows, embeddings, vector databases, similarity search, system messages, and LangGraph workflow nodes.

Also explained in

Compare how 32 books explain retrieval-augmented generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Python Web Apps with Streamlit. The section links point to the parts of the book it draws on.