Notebooks
O
OpenAI
Parse PDF Docs For RAG

Parse PDF Docs For RAG

chatgptopenaigpt-4examplesopenai-apiopenai-cookbook

Parsing PDF documents for RAG applications

This notebook shows how to leverage GPT-4o to turn rich PDF documents such as slide decks or exports from web pages into usable content for your RAG application.

This technique can be used if you have a lot of unstructured data containing valuable information that you want to be able to retrieve as part of your RAG pipeline.

For example, you could build a Knowledge Assistant that could answer user queries about your company or product based on information contained in PDF documents.

The example documents used in this notebook are located at data/example_pdfs. They are related to OpenAI's APIs and various techniques that can be used as part of LLM projects.

Data preparation

In this section, we will process our input data to prepare it for retrieval.

We will do this in 2 ways:

  1. Extracting text with pdfminer
  2. Converting the PDF pages to images to analyze them with GPT-4o

You can skip the 1st method if you want to only use the content inferred from the image analysis.

Setup

We need to install a few libraries to convert the PDF to images and extract the text (optional).

Note: You need to install poppler on your machine for the pdf2image library to work. You can follow the instructions to install it here.

[79]
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.
Note: you may need to restart the kernel to use updated packages.
[80]

File processing

[3]

Testing with an example

[4]
[8]
[77]
OutputOutputOutputOutputOutputOutputOutput

Image analysis with GPT-4o

After converting a PDF file to multiple images, we'll use GPT-4o to analyze the content based on the images.

[63]
[76]
[70]

Testing with an example

[81]
Output
[82]

Processing all documents

[83]
[84]

We will list all files in the example folder and process them by

  1. Extracting the text
  2. Converting the docs to images
  3. Analyzing pages with GPT-4o

Note: This takes about ~2 mins to run. Feel free to skip and load directly the result file (see below).

[85]
100%|██████████| 19/19 [00:20<00:00,  1.07s/it]
100%|██████████| 9/9 [00:15<00:00,  1.76s/it]
100%|██████████| 12/12 [00:12<00:00,  1.08s/it]
100%|██████████| 6/6 [00:07<00:00,  1.31s/it]
[86]
[87]

Embedding content

Before embedding the content, we will chunk it logically by page. For real-world scenarios, you could explore more advanced ways to chunk the content:

  • Cutting it into smaller pieces
  • Adding data - such as the slide title, deck title and/or the doc description - at the beginning of each piece of content. That way, each independent chunk can be in context

For the sake of brevity, we will use a very simple chunking strategy and rely on separators to split the text by page.

[88]
[90]
[91]
[92]
[93]
[94]
[95]
[97]
[98]

Retrieval-augmented generation

The last step of the process is to generate outputs in response to input queries, after retrieving content as context to reply.

[99]
[100]
[101]

Wrapping up

In this notebook, we have learned how to develop a basic RAG pipeline based on PDF documents. This includes:

  • How to parse pdf documents, taking slide decks and an export from an HTML page as examples, using a python library as well as GPT-4o to interpret the visuals
  • How to process the extracted content, clean it and chunk it into several pieces
  • How to embed the processed content using OpenAI embeddings
  • How to retrieve content that is relevant to an input query
  • How to use GPT-4o to generate an answer using the retrieved content as context

If you want to explore further, consider these optimisations:

  • Playing around with the prompts provided as examples
  • Chunking the content further and adding metadata as context to each chunk
  • Adding rule-based filtering on the retrieval results or re-ranking results to surface to most relevant content

You can apply the techniques covered in this notebook to multiple use cases, such as assistants that can access your proprietary data, customer service or FAQ bots that can read from your internal policies, or anything that requires leveraging rich documents that would be better understood as images.