Building a Python Document Question Answering System: A Step-by-Step Guide

In the age of information, the ability to quickly access and extract relevant data from extensive documents can be a game-changer for businesses and developers alike. Imagine having a system that allows users to ask questions about a company’s policy documents and receive accurate answers without sifting through pages of text. This blog post will walk you through building a simple yet effective Document Question Answering (Q&A) system in Python using the Gemini API. By the end of this tutorial, you will understand how to implement this system and explore its capabilities.

Introduction to Document Q&A Systems

A Document Q&A system is designed to enable users to query a document and receive precise answers. This is especially useful for large organizations that maintain extensive policy manuals, legal documents, or any form of text that may require frequent reference. With advancements in AI and natural language processing, we can leverage APIs that facilitate this functionality, making it easier to obtain insights quickly and efficiently.

Creating a Sample Document

This snippet demonstrates how to create and write a sample text document, which is essential for setting up a context for the Q&A functionality.

📚 Recommended Python Learning Resources

Level up your Python skills with these hand-picked resources:

Vibe Coding Blueprint | No-Code Low-Code Guide

Vibe Coding Blueprint | No-Code Low-Code Guide

Click for details
View Details →

Complete Gemini API Guide – 42 Python Scripts, 70+ Page PDF & Cheat Sheet – Digital Download

Complete Gemini API Guide – 42 Python Scripts, 70+ Page PDF & Cheat Sheet – Digital Download

Click for details
View Details →

AI Thinking Workbook

AI Thinking Workbook

Click for details
View Details →

ACT Test (American College Testing) Prep Flashcards Bundle: Vocabulary, Math, Grammar, and Science

ACT Test (American College Testing) Prep Flashcards Bundle: Vocabulary, Math, Grammar, and Science

Click for details
View Details →

Leonardo.Ai API Mastery: Python Automation Guide (PDF + Code + HTML

Leonardo.Ai API Mastery: Python Automation Guide (PDF + Code + HTML

Click for details
View Details →
def create_sample_document():
    """Create a sample text document."""
    doc_path = 'company_policy.txt'
    
    if Path(doc_path).exists():
        return doc_path
    
    content = """COMPANY REMOTE WORK POLICY

Effective Date: January 1, 2024

1. Eligibility
All full-time employees are eligible for remote work arrangements.
Part-time employees may request remote work on a case-by-case basis.
...
"""
    
    with open(doc_path, 'w') as f:
        f.write(content)
    
    print(f"[OK] Created sample document: {doc_path}")
    return doc_path

Prerequisites and Setup

To follow this tutorial, you should have:

Document Q&A Session

This snippet illustrates how to conduct an interactive Q&A session with a document, showcasing how to read document content and generate responses based on user-defined questions.

def document_qa_session(client, doc_path):
    """Interactive Q&A with document."""
    if not doc_path or not Path(doc_path).exists():
        print("[WARNING]  No document available")
        return
    
    print(f"\n[DOC] Loading document: {doc_path}")
    
    with open(doc_path, 'r') as f:
        doc_content = f.read()
    
    questions = [
        "What are the core working hours for remote employees?",
        "What equipment does the company provide?",
        ...
    ]
    
    print("\n[CHAT] Q&A Session:")
    print("=" * 60)
    
    for i, question in enumerate(questions, 1):
        print(f"\nQ{i}: {question}")
        
        prompt = f"""Based on this document:

{doc_content}

Question: {question}

Provide a concise answer based only on the document content."""
        
        try:
            response = client.models.generate_content(
                model='gemini-2.5-flash',
                contents=prompt
            )
            
            print(f"A: {response.text}")
            
        except Exception as e:
            print(f"[X] Error: {e}")
  • Intermediate knowledge of Python programming.
  • Familiarity with APIs and how to manage API keys.
  • A Google Cloud account to access the Gemini API.
  • Python installed on your machine, along with the necessary libraries. You can install the required library with the command pip install google-genai.

Ensure you have your GEMINI_API_KEY stored as an environment variable on your system. This is crucial for securely accessing the API without hardcoding sensitive information into your code.

Core Concepts Explanation

Before delving into the implementation, let’s discuss some core concepts that underpin our Document Q&A system:

Initializing the Client

This snippet demonstrates how to initialize the API client using an environment variable for the API key, which is a common practice for managing sensitive credentials securely.

def main():
    print("=" * 60)
    print("  GEMINI API - DOCUMENT Q&A")
    print("=" * 60)
    
    api_key = os.environ.get('GEMINI_API_KEY')
    if not api_key:
        print("\n[X] GEMINI_API_KEY not found")
        return
    
    client = genai.Client(api_key=api_key)
    print("\n[OK] Client initialized")

1. Document Creation

Creating a sample document is the first step in our implementation. This document will serve as the dataset from which the Q&A system will extract information. By writing a clear and informative document, we set the stage for effective querying.

2. Interactive Q&A Session

Once we have our document, the next step is to enable users to interact with it. This involves reading the document’s content and processing user-defined questions to generate meaningful responses.

3. API Client Initialization

To leverage the capabilities of the Gemini API, we need to initialize an API client. This process involves securely retrieving the API key and setting up a connection to the service, which allows us to send queries and receive responses.

4. Handling Questions and Prompts

Defining the questions we want to ask the system is essential. The quality of the responses we receive heavily depends on how well we frame our questions. This part of the implementation focuses on creating clear and concise prompts for the API.

Step-by-Step Implementation Walkthrough

Now that we have a solid understanding of the concepts, let’s walk through the implementation of our Document Q&A system step by step.

Handling Questions and Prompts

This snippet shows how to define a list of questions and format prompts for the API, emphasizing the importance of clear and specific queries in obtaining accurate responses.

questions = [
    "What are the core working hours for remote employees?",
    "What equipment does the company provide?",
    "How often are performance reviews conducted?",
    "What is the home office stipend amount?",
    "Who should I contact for questions about this policy?"
]

for i, question in enumerate(questions, 1):
    print(f"\nQ{i}: {question}")
    
    prompt = f"""Based on this document:

{doc_content}

Question: {question}

Provide a concise answer based only on the document content."""

Step 1: Create a Sample Document

First, we need a document that contains information that users will query. As discussed earlier, creating a sample document involves writing content that is structured and informative. In our implementation, we check if the document already exists to avoid overwriting any data.

Step 2: Initialize the API Client

Next, we will set up the Gemini API client. This involves retrieving the API key from the environment variables and ensuring that it is present. If the key is missing, it’s crucial to notify the user so that they can rectify the issue.

Step 3: Conduct an Interactive Q&A Session

With our document and API client ready, we can now create an interactive session. This part of the implementation reads the content of the document, allowing users to input their questions. The system then sends these questions to the Gemini API and retrieves the corresponding answers.

Step 4: Handling User Questions

Defining the questions is a critical aspect of our implementation. We will create a list of questions that are relevant to the content of our document. By formatting these questions correctly, we can enhance the accuracy of the responses generated by the API.

Advanced Features or Optimizations

Once the basic implementation is in place, you may want to consider adding advanced features:

Error Handling in API Calls

This snippet highlights the importance of error handling when making API calls, ensuring that the program can gracefully handle issues and provide feedback to the user.

try:
    response = client.models.generate_content(
        model='gemini-2.5-flash',
        contents=prompt
    )
    
    print(f"A: {response.text}")
    
except Exception as e:
    print(f"[X] Error: {e}")
  • Multiple Document Support: Enhance the system’s capabilities to handle multiple documents by allowing users to select which document they want to query.
  • Improved User Interface: Implement a graphical user interface (GUI) or a web-based interface for a more user-friendly experience.
  • Logging and Analytics: Track user queries and responses for analytical purposes, helping to improve the document’s content over time.

Practical Applications

The Document Q&A system can be applied in various scenarios:

  • Human Resources: Automate the retrieval of company policies and procedures, making it easier for employees to find information.
  • Legal: Quickly access legal documents and contracts to extract specific clauses or information.
  • Education: Students can ask questions about textbooks and reference materials, enhancing their learning experience.

Common Pitfalls and Solutions

While implementing the Document Q&A system, you may encounter some common pitfalls:

  • API Key Issues: Ensure that your API key is active and correctly set in your environment variables. If you receive errors related to authentication, double-check your setup.
  • Document Content Quality: The effectiveness of your Q&A system is directly related to the clarity and organization of the document content. Invest time in creating well-structured documents.
  • Question Clarity: Vague or poorly phrased questions can lead to inaccurate responses. Encourage users to ask clear and specific questions.

Conclusion and Next Steps

In this tutorial, we explored how to build a Python Document Question Answering system using the Gemini API. We discussed the core concepts, walked through the implementation, and highlighted potential advanced features. As you consider next steps, think about how you might expand this project. You could integrate it with a web application, enhance the user interface, or experiment with different document types.

With the knowledge gained from this tutorial, you are now equipped to create your own Document Q&A system. Embrace the opportunities that AI and automation present, and continue to explore the vast possibilities within Python programming!


About This Tutorial: This code tutorial is designed to help you learn Python programming through practical examples. Always test code in a development environment first and adapt it to your specific needs.

Want to accelerate your Python learning? Check out our premium Python resources including Flashcards, Cheat Sheets, Interivew preparation guides, Certification guides, and a range of tutorials on various technical areas.

Scroll to Top
WhatsApp Chat on WhatsApp