Thuta Learning
ProjectsAIadvanced

Mini Project: Document Chatbot – Part 1 (Setup & RAG Core)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Apply Mini Project: Document Chatbot – Part 1 (Setup & RAG Core) in a hands-on project
  • Write and run the code yourself
  • Build out an entire project step by step

Let's think this through for a moment

In the first stage of this project, we'll combine the Models, Document Loaders, Text Splitters, Embeddings, and Vector Store you learned about in the Basic and Intermediate chapters to build the core RAG pipeline of our Document Chatbot. The goal of the project is an assistant that loads a user's own document (say, a company FAQ or personal notes) and answers questions based on that document's content. In Part 1, we'll take things through loading the document, splitting it into small chunks, converting those chunks into embeddings and storing them in a vector store, and finally building a RetrievalQA chain and running a basic test query. This stage is the foundation for the whole project, so it's worth setting it up carefully. In Part 2 and Part 3, we'll build on this base by adding memory and agent tools.

Let's build it

Create a new project folder and install langchain, langchain-openai, langchain-community, and chromadb. Load a sample document with TextLoader (or PyPDFLoader), then split it into chunks with RecursiveCharacterTextSplitter (chunk_size=500, chunk_overlap=50). Embed it with OpenAIEmbeddings and store it in a Chroma vector store (with a persist_directory). Take vectorstore.as_retriever() as your retriever and build a RetrievalQA.from_chain_type with a ChatOpenAI model. Finally, run a sample question and check whether you get back an answer grounded in the document's data.

Code Example

python
import os
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_community.vectorstores import Chroma
from langchain.chains import RetrievalQA

os.environ["OPENAI_API_KEY"] = "your-api-key"

# 1. Load document
loader = TextLoader("notes.txt", encoding="utf-8")
documents = loader.load()

# 2. Split into chunks
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
chunks = splitter.split_documents(documents)

# 3. Embed + store in vector DB
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(
    chunks, embeddings, persist_directory="./chroma_db"
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

# 4. Build a basic RetrievalQA chain
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
qa_chain = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)

# 5. Test it
result = qa_chain.invoke({"query": "notes.txt ထဲမှာ ဘာအကြောင်းအရာတွေ ပါသလဲ?"})
print(result["result"])
You should see
An AI-generated answer based on the data in notes.txt appears in the terminal.

5-Minute Try-It

In 5 minutes, create a notes.txt file with 3-4 facts of your own, run the code above, and ask a question about one of the facts in the document.

A Quick Warning

Creating the vector store with a persist_directory means you don't need to re-embed the document on every run — this saves both API cost and time.

Easy traps

  • Forgetting to set the OPENAI_API_KEY environment variable, which triggers an authentication error
  • Setting chunk_size too large, which hurts retrieval accuracy and pulls in irrelevant context

Now Try It Yourself

In 5 minutes, create a notes.txt file with 3-4 facts of your own, run the code above, and ask a question about one of the facts in the document.

You'll know it worked when: An AI-generated answer based on the data in notes.txt appears in the terminal.

Mini Project: Document Chatbot – Part 1 (Setup & RAG Core) | Thuta Learning