Customer Support Chatbot Code Explanation
Overview
This guide explains the uploaded chatbot source code section by section. The program is a
Retrieval-Augmented Generation (RAG) chatbot. It loads text files from a knowledge base, splits
them into chunks, indexes them with TF-IDF, retrieves the most relevant chunks using cosine
similarity, sends the retrieved context to an LLM, logs conversations, and escalates uncertain
queries to a human agent.
Imports
The code imports os, re, glob, json and datetime for filesystem, text processing, file discovery,
logging and timestamps. dataclasses define RetrievedChunk and ChatResponse. sklearn provides
TfidfVectorizer and cosine_similarity for retrieval. dotenv loads environment variables. The
uploaded version originally uses [Link] inside call_llm(), though you later discussed
replacing it with Ollama.
Configuration
Constants define the knowledge-base folder, log folder, chunk size, overlap, number of retrieved
chunks (TOP_K), and a confidence threshold. Greeting, thanks and escalation trigger phrases are
also configured.
Knowledge Base
load_documents() scans the knowledge_base folder for .txt files and loads each document into
memory.
Chunking
chunk_text() breaks long documents into overlapping pieces so information is not lost at chunk
boundaries and retrieval becomes more accurate.
Normalization
normalize_query() trims whitespace, converts text to lowercase and compresses repeated spaces
before searching.
Retriever
Retriever builds a TF-IDF matrix over all chunks. When a query arrives it vectorizes the query,
computes cosine similarity, sorts results and returns the highest-scoring chunks together with their
source filenames.
LLM
call_llm() builds a prompt containing the retrieved context and the user's question. The prompt
instructs the model to answer only from the supplied context and say it lacks information otherwise.
Logging
Every conversation is written to [Link] with timestamp, confidence, answer and sources.
Escalated conversations are additionally written to [Link].
PolicyChatbot
The main class detects greetings, thanks and explicit escalation requests using simple rules.
Otherwise it retrieves relevant chunks, checks the confidence score, calls the LLM when
appropriate, and escalates if retrieval confidence is low or the model says it lacks sufficient
information.
Program Flow
Startup → Load documents → Chunk documents → Build TF-IDF index → User asks a question →
Normalize query → Retrieve top chunks → Check confidence → Generate answer with context →
Log response → Escalate if needed.
Notes
This explanation is based on the uploaded source code, including its RAG pipeline, TF-IDF
retriever, logging, and PolicyChatbot flow.