DEV Community

Cover image for From PDF to Knowledge Base: Building an AI-Powered Glossary Assistant for Intelligent Q&A

From PDF to Knowledge Base: Building an AI-Powered Glossary Assistant for Intelligent Q&A

“The technology you use impresses no one. The experience you create with it is everything.” - Sean Gerety

Key Takeaways

  • Why is a Knowledge Base useful for PDF-based AI applications?
  • How are PDFs converted into searchable knowledge?
  • How does semantic search improve document Q&A?
  • Why is an AI assistant needed on top of the Knowledge Base?
  • How does the glossary handle terminology-based questions?
  • How do the Knowledge Base, glossary, retrieval system, and LLM work together?

Table of Content(Index)

  1. Introduction 1.1 From PDF to Knowledge 1.2 Problems with Traditional PDF Search 1.3 Intelligent Document Interaction
  2. Knowledge Base for PDFs 2.1 What is a Knowledge Base? 2.2 Building a Knowledge Base 2.3 Benefits of a Document-Based Knowledge Base
  3. AI-Powered Q&A Assistant 3.1 Why an AI Assistant is Needed 3.2 Natural Language Q&A 3.3 Conversation and Ambiguity Handling
  4. Building the Knowledge Base 4.1 Text Extraction and Structure Detection 4.2 Chunking 4.3 Embeddings and FAISS 4.4 Semantic Retrieval and Sources
  5. Role of the Glossary 5.1 What is a Glossary? 5.2 Automatic Glossary Extraction 5.3 Glossary vs Knowledge Base
  6. Putting Everything Together 6.1 LLM and Context 6.2 End-to-End Workflow 6.3 User Interface
  7. FAQs
  8. Interesting Facts
  9. Conclusion

1. Introduction

1.1 From PDF to Knowledge
PDFs are mainly designed for storing and displaying documents. They are convenient for humans, but their content is not automatically organized in a form that an AI system can efficiently search.

A Knowledge Base (KB) transforms document content into a structured and searchable representation that an AI application can retrieve when answering questions.

The goal is therefore not simply to store PDFs, but to transform their content into usable knowledge.

1.2 Problems with Traditional PDF Search

Traditional PDF search has several limitations.

Keyword Mismatch

Keyword-based search mainly looks for matching words. A search for:
"repair ruptured plumbing"

may not return content describing:
"fixing a leaking pipe"

even though the two expressions have a similar meaning.

Semantic search addresses this by representing text as embeddings and comparing their meaning. Sentence Transformers, for example, supports embeddings and semantic search across text.

Poor Machine Structure

PDFs are designed around visual layouts. During extraction, issues can occur with:

  • Multicolumn text
  • Headers and footers
  • Reading order
  • Tables
  • Text inside images

PDF extraction tools such as pdfplumber provide text and layout-related extraction capabilities, but the quality still depends on how the original PDF was created.

Information Spread Across Multiple Files

Important information may be distributed across several PDFs. Users have to search each document separately and combine the results themselves.

A Knowledge Base brings these documents into a single searchable layer.

Scanned Documents

Scanned PDFs may contain images instead of machine-readable text. Such documents require OCR before their content can be processed as text. pdfplumber's documentation also notes that it works best with machine-generated PDFs rather than scanned documents.

1.3 Intelligent Document Interaction

Instead of manually searching through hundreds of pages, users can ask questions directly.

For example:

"What are the termination liabilities in Section 4?"

The system retrieves relevant information and provides it to an LLM for answer generation.

A typical RAG-style document workflow is:

PDF → Extract → Chunk → Embeddings → Vector Search → Context → LLM → Answer

The key idea is:

Instead of searching only for exact words, search for meaning and context.

2. Knowledge Base for PDFs

The Knowledge Base is the foundation of the assistant. Before the AI can answer questions, information from PDFs must be extracted, organized, indexed, and made searchable.

Instead of treating a PDF as one large file, the system divides its content into smaller meaningful units called chunks.

2.1 What is a Knowledge Base?

A Knowledge Base is a centralized collection of information that an application can retrieve and use to answer questions.

In this project:

PDF → Text → Chunks → Embeddings → FAISS

Each chunk represents a small piece of document knowledge. Its embedding allows the system to find it when a user's question is semantically related.

In simple terms:
A PDF is the source of information.
A Knowledge Base is the searchable representation of that information.

2.2 Building a Knowledge Base

Suppose the system receives several PDFs:

  • Learning.pdf
  • Constructors.pdf
  • Encapsulation.pdf
  • Inheritance.pdf

Instead of searching each document independently, the project processes them through the same pipeline:
PDFs → Text Extraction → Structure Detection → Chunking → Embeddings → FAISS

The chunks and their metadata are combined:

all_chunks.extend(get_chunks)
all_metadata.extend(get_metadata)

Embeddings are then generated for the combined collection:

chunk_embeddings = embedding_chunks(all_chunks)
stored_chunk_embeddings = store_embeddings(chunk_embeddings)

The result is a single searchable knowledge layer containing information from multiple PDFs.

2.3 Benefits of a Document-Based Knowledge Base

1. Centralized Knowledge

Multiple PDFs can be processed into one searchable system.
Multiple PDFs → Processing → Unified Knowledge Base

2. Faster Retrieval

Instead of processing an entire document for every question, the system searches indexed chunks and retrieves only the most relevant content.

3. Semantic Understanding

Embeddings allow the system to retrieve related content even when the question and document use different wording. Semantic search works by embedding the query and corpus into a shared vector space and finding nearby representations.

4. Context Preservation

Each chunk can retain metadata such as:

  • PDF filename
  • Page number
  • Heading

For example:
Inheritance.pdf → Page 4 → Types of Inheritance → Relevant Chunk

5. Reusable AI Foundation
Once created, the Knowledge Base can support:

  • Question answering
  • Document assistants
  • Summarization
  • Information extraction
  • Intelligent search

3. AI-Powered Q&A Assistant

The Knowledge Base solves the problem of storing and retrieving information, but users still need a simple way to interact with it.
Users should not need to understand embeddings, vector indexes, or similarity calculations. They should simply be able to ask a question.

3.1 Why an AI Assistant is Needed
Manual document search requires users to:

  1. Find the correct document.
  2. Open it.
  3. Search for keywords.
  4. Read surrounding content.
  5. Compare information across documents.
  6. Form the answer themselves.

This becomes inefficient as documents become larger or more numerous.
Instead of manually searching:

"What is encapsulation and why is it useful?"

the user can ask the assistant directly. The assistant handles the retrieval process in the background.

“Any sufficiently advanced technology is indistinguishable from magic.” - Arthur C. Clarke

3.2 Natural Language Q&A

The user can ask questions in normal language.
For example:

User: What is inheritance in Python?

The question is converted into an embedding and used to retrieve relevant document chunks.

Question → Relevant Context → LLM → Natural-Language Answer

The retrieved context is then passed to the LLM.

This creates an important separation:
Retrieval finds the knowledge.
Generation explains the knowledge.

Sentence Transformers supports embedding generation and semantic search, making this type of retrieval possible.

3.3 Conversation and Ambiguity Handling

A useful assistant should understand follow-up questions.

User: What is inheritance?
Assistant: Inheritance allows one class to derive properties and behavior from another.
User: Explain it with an example.

The second question depends on the first. The assistant therefore uses conversation history:

Current Question + Previous Conversation → LLM → Context-Aware Answer

The assistant should also avoid guessing when a question is ambiguous.

For example:
What is inheritance?

If multiple contexts are available:

  1. Python inheritance
  2. Java inheritance
  3. C++ inheritance

the assistant can ask the user to clarify instead of selecting one without sufficient evidence.

“Knowledge is power. Information is liberating.” - Kofi Annan

4. Building the Knowledge Base

The Knowledge Base converts raw PDF content into searchable vector representations.

PDF → Text Extraction → Structure Detection → Chunking → Embeddings → FAISS

4.1 Text Extraction and Structure Detection

The first step is to extract text from each PDF page.
The project uses pdfplumber:

with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
page_text = page.extract_text()

The extracted text is stored along with page information:
page_text_list.append(page_text)

pdfplumber supports page-level text extraction and can also work with layout-related information such as character positions and font attributes.

The basic transformation is:

PDF → Machine-Readable Text

Structure Detection

Extracted text alone may lose useful document structure. Headings provide additional context.

For example:

Chapter 2
Inheritance

Inheritance allows one class to...
The project uses font-size information to identify headings.

It calculates the median body font size:

body_font_size = np.median(all_sizes)

and defines a threshold:

heading_threshold = body_font_size + 1.5

A sufficiently large and relatively short line can then be treated as a heading.

The resulting chunk can retain:

Heading: Inheritance
Text: Inheritance allows one class...

This makes the retrieved information more meaningful.

4.2 Chunking

Large documents should not be treated as one giant block of text. The content is divided into smaller chunks.

The project uses:

max_chunk_size = 1000

and:

overlap = 200

The overlap helps preserve context between neighboring chunks.

Chunk 1 → 1000 characters
200-character overlap
Chunk 2 → 1000 characters

The project also associates headings and metadata with chunks, providing additional context during retrieval.

4.3 Embeddings and FAISS
After chunking, each chunk is converted into a numerical representation called an embedding.

The project uses:

SentenceTransformer("all-MiniLM-L6-v2")

and:

embeddings = model.encode(chunks)

Sentence Transformers creates fixed-size vector representations that can be used for semantic search and similarity comparison.

Conceptually:

"Python inheritance" → [0.12, -0.34, 0.81, ...]

The exact numbers are not meaningful to humans. They allow text to be compared mathematically.

Question Embedding → Compare Similarity → Document Chunk Embeddings → Relevant Chunks

FAISS

Once embeddings are generated, they need to be stored for efficient search.
The project uses FAISS:

dimension = embeddings.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(

np.array(embeddings).astype("float32")
)

IndexFlatL2 performs nearest-neighbor search using L2 distance.
The simplified flow is:
Document Chunks → Embeddings → FAISS → Vector Search

FAISS provides indexing and efficient similarity-search functionality for vector data.

4.4 Semantic Retrieval and Sources

When a user asks a question, the question is converted into an embedding using the same model.

The system then searches FAISS:

User Question → Question Embedding → FAISS Search → Relevant Chunks

The project retrieves the top 3 relevant chunks:

distances, indices = index.search(
question_embedding,
k=3
)

These chunks become the context for the LLM.

Source Metadata
Each chunk also retains information such as:

  • File name
  • Page number
  • Heading

For example:

Inheritance.pdf
Page 4
Heading: Types of Inheritance

This allows users to verify the answer and return to the original document.

5. Role of the Glossary

Technical documents often contain specialized terminology. Even when the system retrieves the correct information, users may not understand the terms being used. This is where the glossary becomes important.

“Our industry does not respect tradition. It only respects innovation.” - Satya Nadella

5.1 What is a Glossary?

A glossary is a collection of important terms and their definitions.

For example:

Encapsulation
→ The practice of bundling data and methods within a class.
Inheritance
→ A mechanism through which one class derives properties and behavior from another.
Polymorphism
→ The ability of different objects to respond to the same interface in different ways.

The glossary provides quick access to definitions without requiring users to search through the complete document.

5.2 Automatic Glossary Extraction

Instead of manually creating the glossary, the project uses an LLM to identify important terminology from the extracted document content.

The system can identify:

  • Key terms
  • Abbreviations
  • Variables
  • Concepts
  • Tools
  • Definitions The expected structure is a dictionary:

{
"inheritance": "A mechanism through which one class derives properties and behavior from another.",
"encapsulation": "A technique of bundling data and methods within a class."
}

The terms are normalized:

return {
str(k).lower().strip(): str(v).strip()
for k, v in glossary_dict.items()
}

This makes lookup more consistent.

5.3 Glossary vs Knowledge Base

The glossary and Knowledge Base have different responsibilities.

For example:

**Glossary Question

"What is inheritance?"

Question → Glossary Lookup → Definition

Knowledge Base Question

"How is inheritance used in this application?"

Question → Embedding → FAISS Search → Relevant Chunks → LLM → Answer**

The glossary is therefore not a replacement for the Knowledge Base. It is an additional layer designed to make terminology-focused questions faster and clearer.

6. Putting Everything Together

The final assistant combines the Knowledge Base, glossary, retrieval system, conversation history, and LLM into one workflow.

6.1 LLM and Context
The LLM is responsible for generating the final natural-language response.

The basic process is:

Question → Relevant Context → LLM → Natural-Language Answer

The important distinction is:

The vector index retrieves.
The LLM explains.

The retrieved context helps ground the generated answer in information from the uploaded documents.

For follow-up questions:

Current Question + Previous Conversation → LLM → Context-Aware Answer

“The advance of technology is based on making it fit in so that you don't really even think about it.” - Bill Gates

6.2 End-to-End Workflow

The complete system can be divided into two stages.

Stage 1: Knowledge Base Creation

PDF → Text Extraction → Structure Detection → Chunking → Embeddings → FAISS

The glossary is generated alongside the Knowledge Base:

PDF Text → Important Terms → Glossary

Stage 2: Question Answering

For a definition-based question:

Question → Glossary Lookup → Definition

For a broader question:

Question → Embedding → FAISS Search → Relevant Chunks → LLM → Answer

For a follow-up question:

Current Question + Previous Conversation → LLM → Context-Aware Answer

The overall experience is simple:

Upload PDF → Ask Question → Get Answer

6.3 User Interface

The project uses Streamlit to provide the user interface.
The user can:

  1. Upload PDF documents.
  2. Process the documents.
  3. Ask questions.
  4. View generated answers.
  5. Continue the conversation.
  6. View relevant sources.

From the user's perspective:

Upload PDF → Ask Question → Get Answer

Behind the scenes:

PDF → Extraction → Chunking → Embeddings → FAISS → Retrieval → LLM → Answer

The complexity remains in the backend while the user experience remains simple.

FAQs

1. What is a PDF Knowledge Base?
A PDF Knowledge Base converts information from PDFs into searchable chunks. These chunks are represented as embeddings and stored in a vector index such as FAISS.

2. Why not simply search the PDF?
Traditional search relies heavily on matching words. Semantic search can retrieve related information even when different wording is used.

3. What is the role of FAISS?
FAISS stores vector representations and performs similarity search to retrieve relevant chunks.

4. Why is a glossary needed?
The Knowledge Base handles broader questions, while the glossary provides quick answers to terminology-based questions such as "What is encapsulation?"

5. Can the assistant answer follow-up questions?
Yes. Conversation history allows the assistant to interpret questions based on previous messages.

6. What happens when a question is ambiguous?
The assistant can identify multiple relevant contexts and ask the user to clarify instead of making an unsupported assumption.

Interesting Facts

  • Semantic search focuses on meaning rather than only exact keywords -Semantic Search Documentation
  • The entire PDF does not need to be sent to the LLM. Relevant chunks are retrieved first - Sentence Transformers Documentation
  • This project uses 1,000-character chunks with 200-character overlap - FAISS Documentation
  • The glossary and Knowledge Base serve different purposes.
  • Source metadata can connect retrieved content back to its original PDF, page, and heading - pdfplumber Documentation
  • The same Knowledge Base can support multiple AI applications beyond Q&A.

“We are stuck with technology when what we really want is just stuff that works.” - Douglas Adams

Conclusion

The project demonstrates how static PDF documents can be transformed into an interactive source of knowledge.

By combining PDF extraction, structure detection, chunking, embeddings, FAISS-based retrieval, glossary lookup, conversation history, source tracking, and an LLM, users can interact with documents using natural language instead of manually searching through files.

The key idea is:

The Knowledge Base retrieves the right information, the glossary explains important terminology, and the AI assistant turns that information into a conversational answer.

The result is a document assistant that combines semantic search, contextual understanding, and glossary-based definitions into one simple question-answering experience.sea

About the Author: Tanisha is an AI Engineer at AddWeb Solution, working on AI/ML solutions, intelligent document processing, and AI-powered applications.

Top comments (0)