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:
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
end_to_end_rag_samples/ sits next to the script.python end_to_end_rag_demo.py
If it fails
- Missing file: end_to_end_rag_samples/html_notes.txt — unzip
end_to_end_rag_demo.zipso the sample folder sits besideend_to_end_rag_demo.py. - AuthenticationError — check
OPENAI_API_KEYandload_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.