Building a Retrieval-Augmented Generation (RAG) Chatbot

Published on 25-08-2025


πŸ”Ž What is a RAG Chatbot?

RAG stands for Retrieval-Augmented Generation. It combines:

Instead of relying only on pre-trained knowledge, the bot dynamically looks into your dataset first, then crafts an answer based on that context.

βš™οΈ Tech Stack

πŸ› οΈ Implementation Steps

  1. Read BotData.csv and parse the ChunkContentDto.Content field
  2. Pre-process numeric/date fields for analytics queries
  3. Create embeddings with SentenceTransformer and store them in a FAISS index
  4. Detect if the user’s question is numeric (sum, average, etc.) or text-based
  5. For numeric queries β†’ compute directly and optionally format using the LLM
  6. For text queries β†’ retrieve relevant chunks & ask the LLM for a concise answer
  7. Serve a very light Flask HTML interface for asking questions

πŸ“Έ UI Previews

Chatbot Main Page

Chatbot Main Page

Query Input & Retrieval

Query Input & Retrieval

Answer Display & Context

Answer Display & Context

πŸ“œ Key Code Snippet

# Build FAISS index
data_embeddings = model.encode(blocks, convert_to_numpy=True)
dimension = data_embeddings.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(data_embeddings)

# Retrieve context
def get_context(query, top_k=20, max_chars=12000):
    query_embedding = model.encode([query], convert_to_numpy=True)
    distances, indices = index.search(query_embedding, top_k)
    return "\n\n".join([blocks[i] for i in indices[0]])[:max_chars]

# Flask route
@app.route("/", methods=["GET", "POST"])
def home():
    ...
      

Full code available on request or in the project files.

πŸ’‘ Lessons Learned

πŸš€ Conclusion

This project demonstrates how a lightweight RAG workflow can turn a simple CSV into a question-answering assistant. In the future, I plan to add authentication, better UI, and support for file uploads.


← Back to Portfolio