📘 Introduction

Retrieval-Augmented Generation, usually called RAG, is one of the most practical ways to make an AI app answer questions from your own documents instead of relying only on its general training.

In this tutorial, you will build a beginner-friendly RAG chatbot with LangChain, Python, FAISS, and OpenAI models. We will load a small local knowledge base, split it into chunks, turn those chunks into embeddings, store them in a vector index, and then answer questions with retrieved context.

🎯
By the end, you will have a working local project that can answer questions from your own text file instead of guessing from general web knowledge.

💡 What are we implementing?

We are building a simple document question-answering workflow. A user asks a question, the app retrieves the most relevant chunks from a local file, and the chat model uses that context to generate a grounded answer.

Question -> Embedding search in FAISS -> Relevant chunks -> Chat model answer

This pattern matters because it is one of the most common starting points for internal knowledge bots, course assistants, FAQ tools, and document search applications.

✅ Prerequisites

☑️ Python installed
☑️ Basic Python knowledge
☑️ An OpenAI API key
☑️ A terminal or command prompt

⚙️1️⃣ Create a project folder

Create a new local folder for the tutorial project:

mkdir langchain-rag-chatbot
cd langchain-rag-chatbot

🐍2️⃣ Create a virtual environment

Create and activate a virtual environment:

python -m venv .venv
source .venv/bin/activate

On Windows PowerShell, activate it with:

.venv\Scripts\Activate.ps1

📦3️⃣ Install libraries

Install the LangChain packages, the OpenAI integration, and the local FAISS vector store dependency:

pip install langchain langchain-community langchain-openai langchain-text-splitters faiss-cpu

🔐4️⃣ Set your API key

Store your OpenAI API key as an environment variable. Replace the placeholder with your real key and never hardcode secrets in your script.

export OPENAI_API_KEY="your_api_key_here"

On Windows PowerShell, use:

$env:OPENAI_API_KEY="your_api_key_here"
🔐
The OpenAI and LangChain SDKs can read the API key from the environment, so you do not need to paste it directly into your Python file.

📝5️⃣ Create a small knowledge base file

For a beginner-friendly example, we will use a local text file as the knowledge base. Create a file named knowledge_base.txt with this sample content:

LangChain is a framework for building applications with large language models.

A RAG system improves answers by retrieving relevant context before the model responds.

FAISS is a library for similarity search over vectors. It helps us find the document chunks that are closest to a user's question.

Embeddings turn text into numeric vectors so similar text ends up close together in vector space.

A retriever is the component that fetches the most relevant chunks from the vector store.

knowledge_base.txt

This small file is enough to show the complete RAG workflow before you move on to PDFs, web pages, or larger document collections.

🔎6️⃣ Preview how LangChain splits documents

Now create a file named preview_chunks.py. This script loads the text file and splits it into smaller chunks that can later be embedded and retrieved.

from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter

loader = TextLoader("knowledge_base.txt")
documents = loader.load()

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=120,
    chunk_overlap=30,
)
chunks = text_splitter.split_documents(documents)

print(f"Loaded documents: {len(documents)}")
print(f"Created chunks: {len(chunks)}")
print("\nFirst chunk:\n")
print(chunks[0].page_content)

preview_chunks.py

Run the script:

python preview_chunks.py

You should see that one source document becomes multiple chunks. That chunking step is important because vector search works better on smaller, focused pieces of text.

Loaded documents: 1
Created chunks: 5

First chunk:

LangChain is a framework for building applications with large language models.
🎓
Want the full project? In the Academy section, we turn these chunks into embeddings, store them in FAISS, and build a working question-answering chatbot step by step.

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In