Course navigation
Embeddings, Vector Stores & RAGLesson 10 of 11

End-to-End RAG Pipeline

One script wires together every step from this module — load a text file, split it, embed the chunks, store them in Chroma, call a retriever, then run the same LCEL chain from Retrieval-Augmented Generation (RAG). The demo prints a line after each stage.

Before you run

Activate the venv from Project Setup. Install the splitter and Chroma packages:

pip install langchain-text-splitters chromadb

Keep a valid OPENAI_API_KEY in .env from OpenAI Account Setup.

Six steps, one file:

LoaderTextLoader → Document list
Text SplitterRecursiveCharacterTextSplitter → chunks
EmbeddingsOpenAIEmbeddings → float lists
Vector StoreChroma.from_documents → end_to_end_rag_chroma_db/
Retrieveras_retriever → top k chunks
Chainprompt → llm → parser → print string
Steps 1–4 match earlier lessons in this module. Steps 5–6 match Retrieval-Augmented Generation (RAG). The script prints a header after each stage.

Load a file

TextLoader reads end_to_end_rag_samples/html_notes.txt into one Document.

from langchain_community.document_loaders import TextLoader

docs = TextLoader("end_to_end_rag_samples/html_notes.txt", encoding="utf-8").load()

print(len(docs), "document(s)")

Split into chunks

RecursiveCharacterTextSplitter breaks the file into smaller pieces before embedding — chunk_size=150, chunk_overlap=30.

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(
    chunk_size=150,
    chunk_overlap=30,
)

chunks = splitter.split_documents(docs)

print(len(chunks), "chunks")

Embed and store

OpenAIEmbeddings turns each chunk into a float list. Chroma.from_documents writes them to end_to_end_rag_chroma_db/.

from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings

embeddings = OpenAIEmbeddings(model="text-embedding-3-small")

vectorstore = Chroma.from_documents(
    documents=chunks,
    embedding=embeddings,
    persist_directory="end_to_end_rag_chroma_db",
)

Retriever and chain

Same code as Retrieval-Augmented Generation (RAG): as_retriever, format_docs, then an LCEL pipe with RunnablePassthrough, ChatPromptTemplate, and StrOutputParser.

retriever = vectorstore.as_retriever(search_kwargs={"k": 2})

docs = retriever.invoke("How do I make a link on a page?")

for doc in docs:
    print(doc.page_content)
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough

def format_docs(docs):
    return "\n\n".join(doc.page_content for doc in docs)

prompt = ChatPromptTemplate.from_template(
    "Answer in one short sentence using only the context below.\n\n"
    "Context:\n{context}\n\n"
    "Question: {question}"
)

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

rag_chain = (
    {"context": retriever | format_docs, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

answer = rag_chain.invoke("How do I make a link on a page?")
print(answer)

Run the demo

Download the ZIP so end_to_end_rag_samples/ sits next to the script, then run:

end_to_end_rag_demo.py

Includes end_to_end_rag_samples/html_notes.txt — creates end_to_end_rag_chroma_db/ on each run

Unzip so end_to_end_rag_samples/ sits next to the script.
end_to_end_rag_demo.py
"""end_to_end_rag_demo.py"""
# loader → splitter → embed → Chroma → retriever → chain
docs = TextLoader(...).load()
python end_to_end_rag_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python end_to_end_rag_demo.py
=== Loader ===
1 document, 735 characters
=== Text Splitter ===
7 chunks
=== Vector Store ===
Saved to end_to_end_rag_chroma_db
=== Retriever ===
[0] The <a> tag creates a hyperlink. Use the href attribute…
[1] The <title> tag sets the page title shown in the browser tab…
=== Chain output ===
Q: How do I make a link on a page?
Printed: Use the <a> tag with an href attribute to create a hyperlink.
The printed line should mention the <a> tag. Wording may vary slightly if you rerun the script.

If it fails

  • Missing file: end_to_end_rag_samples/html_notes.txt — unzip end_to_end_rag_demo.zip so the sample folder sits beside end_to_end_rag_demo.py.
  • AuthenticationError — check OPENAI_API_KEY and load_dotenv().
  • ModuleNotFoundError: chromadb — run pip install chromadb.
  • ModuleNotFoundError: langchain_text_splitters — run pip install langchain-text-splitters.

More detail: LangChain retrieval docs.

What's Next

Wrap the same pipeline in a Streamlit chat window.