Published on

From BERT Theory to Real-World RAG

Authors
  • avatar
    Name
    Laksmita Widya Astuti
    Twitter

From BERT Theory to Real-World RAG: My Experience Building Ready-to-Production Chatbots

The BERT vs GPT Confusion (And Why It Matters)

While generative AI tools like ChatGPT have dominated headlines over the past few years (creating everything from viral AI art to controversial Totoro-like images), BERT remains one of the most widely used language models for core NLP tasks like text classification, sentiment analysis, and question answering (QA).

So why is GPT more famous?

The answer lies in their design purposes:

  • GPT is generative, interactive, and user-facing (chatbots, content creation)
  • BERT is an encoder-only model designed for backend analysis, classification, and understanding

GPT's ability to create human-like text directly appeals to consumers, while BERT is designed to analyze, understand, classify, and extract information from existing text. Think of it this way: GPT is the "creative writer," while BERT is the "mind reader."

The Technical Difference

Both GPT and BERT are based on the Transformer architecture, but their training objectives are fundamentally different:

  • GPT: Trained to predict the next word (left-to-right, autoregressive)
  • BERT: Trained to predict masked words using bidirectional context

This means:

  • BERT's main strength: Understanding and representation
  • GPT's main strength: Generation and completion

The Frozen Knowledge Problem

Here's a critical limitation both models share: after pretraining, their knowledge is frozen. They only "know" what was in their training data up to a certain cutoff date.

So how do we give them domain-specific, up-to-date knowledge? That's where a Knowledge Base (KB) comes in.

BERT's Hidden Superpower: Embeddings

BERT transforms text input into numerical semantic encodings that accurately represent words and sentences. This makes it perfect as an embedding model in modern AI systems.

When used with a knowledge base, BERT-based embeddings:

  • Enhance query understanding
  • Reduce context irrelevancy
  • Improve the accuracy of search results

RAG: Where BERT and GPT Finally Work Together

This is where Retrieval-Augmented Generation (RAG) comes in. It combines the strengths of both models.

Classic RAG Pipeline

User Question
Embedding Model (BERT-like model)
Vector Database (Knowledge Base)
Retrieved Documents
GPT (Generator)
Final Answer

My Real-World Implementation: Production Chatbot with AWS

When I built a production chatbot, I used a similar RAG pipeline but with AWS services. Here's what the architecture looked like:

User Question
Amazon Titan Embed V2 (Embedding Model)
OpenSearch / Bedrock Knowledge Base (Vector Store)
Retrieved Documents (FAQ + URL sources)
Amazon Nova Pro (Generator via Bedrock Agent)
Final AnswerWebSocketCustomer Results

This architecture delivered:

  • >75% accuracy across different data sources
  • Support for multiple document types (web crawl, PDFs stored in S3)
  • Real-time responses via WebSocket
  • Scalable, production-ready infrastructure

Why This Matters: Connecting Theory to Practice

When I was writing my BERT article (Part 2), I focused on the theoretical foundations. How transformers work, what masked language modeling does, why contextual embeddings matter. But the real "aha moment" came when I saw BERT working in production as part of a RAG system.

Key insights from building this:

  1. BERT isn't obsolete. It's just doing different work than GPT. In RAG systems, BERT-based embedding models are essential for understanding user queries and finding relevant documents.

  2. The frozen knowledge problem is solved by architecture, not just bigger models. RAG lets us combine the understanding power of BERT with the generation power of GPT, plus a dynamic knowledge base that can be updated anytime.

  3. Domain-specific performance comes from good embeddings. The quality of your embedding model (like Titan Embed V2) directly impacts retrieval accuracy, which then determines how good your final generated answer will be.

Lessons Learned

Building this chatbot taught me that:

  • Understanding > Generation for retrieval: BERT-like models outperform GPT-like models when you need to match user queries to documents
  • Vector databases are crucial: The knowledge base isn't just storage, it's where semantic search happens
  • Multi-source data works: Combining web crawl data with structured PDFs gave better coverage than either alone
  • Accuracy depends on the full pipeline: A great generator can't fix poor retrieval, and great retrieval is wasted on a bad generator

From Research to Production

This experience changed how I think about my sentiment analysis research (which also used BERT). The same principles apply:

  • Contextual embeddings help BERT understand code-mixed Indonesian-English text
  • Subword tokenization handles informal spelling and unknown words
  • Bidirectional context captures meaning better than left-to-right models

But now I see these aren't just academic features. They're the foundation for real systems that millions of users interact with daily.

The journey from understanding transformers in research papers to deploying them in production taught me that the best AI systems don't choose between BERT and GPT. They use both, each doing what it does best.