Developing a Chatbot Using the RAG Architecture for Document-Based Query Resolution

Developing a Chatbot Using the RAG Architecture for Document-Based Query Resolution

Custom Chatbot AI Use case by Techenhance

Problem Statement

The ability to accurately answer user questions based on specific documents is a challenging yet crucial task in various fields, including customer service, education, and professional services. Traditional chatbots often struggle to provide precise answers when the required information is embedded within complex documents. The problem we aim to solve is:

“Develop a chatbot using the RAG architecture to answer user questions based on uploaded documents while also incorporating chat history.”

Solution Description

To address this problem, we utilize our AI development services to propose a solution that leverages the Retrieval-Augmented Generation (RAG) architecture. Our solution consists of several key components: preprocessing uploaded documents, storing their embeddings in a vector database, performing similarity searches, generating answers using OpenAI’s GPT-3.5-turbo, and integrating chat history for contextually relevant responses. The workflow we have used is popular amongst the top chatbot companies.

  1. User-initiated conversations via file uploads or URLs:
    • Users can initiate conversations by uploading documents or providing URLs.
  2. Accepted file types:
    • URL, .pdf, .doc, .docx, .txt, .md, .html, .htm, .pptx
  3. Preprocessing:
    • Text data extraction, chunking, embedding, and storing the embeddings in Astra DB.
  4. Cosine similarity search:
    • Search against stored document embeddings to find relevant information.
  5. OpenAI (GPT-3.5-turbo) for answer generation:
    • Utilize the powerful language model to generate accurate answers.
  6. Integration of chat history:
    • Incorporate previous interactions to provide contextually aware responses.
  7. Tech Stack:
    • OpenAI, AstraDB, RAG

Detailed Solution

User-Initiated Conversations via File Uploads or URLs

The chatbot begins by allowing users to upload documents or provide URLs. This flexibility ensures that users can easily input the sources of information they need the chatbot to reference. The supported file types include various common formats to accommodate a wide range of documents.

Accepted File Types

The system accepts the following file types:

  • URLs
  • .pdf
  • .doc
  • .docx
  • .txt
  • .md
  • .html
  • .htm
  • .pptx

This variety ensures that the chatbot can handle different types of documents commonly used in professional and educational settings.


Step 1: Text Data Extraction

The first step in preprocessing is extracting text data from the uploaded documents. Each file type has a specific method for text extraction:

  • PDF: Using libraries like PyPDF2 or pdfplumber.
  • DOC/DOCX:Using python-docx.
  • TXT/MD:Reading the file directly.
  • HTML/HTM: Using BeautifulSoup to parse and extract text.
  • PPTX: Using python-pptx.

Step 2: Text Chunking

Once the text is extracted, it is chunked into manageable pieces. This is necessary because documents can be lengthy, and processing them as a whole can be inefficient. Chunking helps in breaking down the text into smaller, coherent segments that can be individually analyzed and embedded.

Step 3: Text Embedding

Each chunk of text is then converted into embeddings using a pre-trained model. Embeddings are dense vector representations of text that capture its semantic meaning. These embeddings are generated using models such as BERT, Roberta, or Sentence Transformers.

Step 4: Storing Embeddings in Astra DB

The extracted embeddings are stored in Astra DB, a highly scalable and performant vector database. Astra DB supports vector data types and provides efficient storage and retrieval capabilities.


This case study presents a comprehensive solution for developing a chatbot using the RAG architecture to answer user questions based on uploaded documents while incorporating chat history. By leveraging advanced technologies such as OpenAI’s GPT-3.5-turbo and Astra DB, the chatbot can efficiently process and retrieve relevant information, generating accurate and contextually aware responses.