Loading

Quipoin Menu

Learn • Practice • Grow

generative-ai / Project: Document Q&A with RAG
tutorial

Project: Document Q&A with RAG

In this project, you will build a Document Q&A system using RAG (Retrieval‑Augmented Generation). Users can upload a PDF or text document and ask questions about its content. The system will retrieve relevant parts and answer using an LLM.

RAG allows the LLM to answer questions based on your private documents, not just its training data.

What You Will Build

  • A simple web app (Gradio) where users upload a PDF or enter text.
  • Chunk the document, create embeddings, and store in a vector database (Chroma).
  • For a question, retrieve the most relevant chunks and ask the LLM to answer.

Prerequisites

  • Python installed.
  • OpenAI API key.
  • Basic Python knowledge.

Step 1: Install Libraries

pip install langchain chromadb gradio openai tiktoken pypdf

Step 2: Write the Code

Create a file doc_qna.py with the following:
import gradio as gr
from langchain.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import RetrievalQA
from langchain.chat_models import ChatOpenAI
import os

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

def process_document(file):
# Load PDF
loader = PyPDFLoader(file.name)
documents = loader.load()
# Split into chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
chunks = text_splitter.split_documents(documents)
# Create embeddings and vector store
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings)
return vectorstore

def answer_question(file, question):
if file is None:
return "Please upload a document first."
vectorstore = process_document(file)
# Create QA chain
llm = ChatOpenAI(model="gpt-3.5-turbo")
qa_chain = RetrievalQA.from_chain_type(llm, retriever=vectorstore.as_retriever())
answer = qa_chain.run(question)
return answer

# Gradio interface
iface = gr.Interface(
fn=answer_question,
inputs=[gr.File(label="Upload PDF"), gr.Textbox(label="Your Question")],
outputs="text",
title="Document Q&A with RAG",
description="Upload a PDF and ask any question about its content."
)

iface.launch()
Replace `your-api-key` with your actual OpenAI API key.

Step 3: Run the App

python doc_qna.py
Your browser will open a Gradio interface. Upload a PDF, type a question (e.g., "What is the main topic?"), and get an answer based on the document.

How It Works

  • The PDF is split into overlapping chunks (1000 characters each).
  • Each chunk is converted into an embedding (vector) using OpenAI’s embedding model.
  • Embeddings are stored in a Chroma vector database.
  • When you ask a question, it is also embedded, and the most similar chunks are retrieved.
  • The LLM receives the question + the retrieved chunks and generates an answer grounded in the document.


Two Minute Drill
  • Document Q&A uses RAG: retrieve relevant chunks, then generate answer.
  • LangChain simplifies building the pipeline.
  • Chroma is a lightweight vector database.
  • Gradio provides a simple web UI.

Need more clarification?

Drop us an email at career@quipoinfotech.com