Skip to main content

Beyond Ctrl+F - Use LLM's For PDF Analysis


PDFs are everywhere, seemingly indestructible, and present in our daily lives at all thinkable and unthinkable positions. We've all got mountains of them, and even companies shouting about "digital transformation" haven't managed to escape their clutches. Now, I'm a product guy, not a document management guru. But I started thinking: if PDFs are omnipresent in our existence, why not throw some cutting-edge AI at the problem? Maybe Large Language Models (LLMs) and Retrieval Augmented Generation (RAG) could be the answer.

Don't get me wrong, PDF search indexes like Solr exist, but they're basically glorified Ctrl+F. They point you to the right file, but don't actually help you understand what's in it. And sure, Microsoft Fabric's got some fancy PDF Q&A stuff, but it's a complex beast with a hefty price tag.

That's why I decided to experiment with LLMs and RAG. My idea? An intelligent knowledge base built on top of our existing PDFs. Imagine asking a question and getting a precise answer, with the relevant document sections highlighted, without having to sift through pages of dense text.

Why RAG? Contextual Search

Retrieval Augmented Generation (RAG) is a fancy way of saying that your LLM gets a little help from its friends. Instead of just relying on its internal knowledge, RAG taps into a separate "retrieval" step that finds relevant information from an external source (like your PDF collection). This means you get answers grounded in your specific documents, not just general knowledge scraped from the web.

Why is this such a big deal? Because context matters! Think about those times when you needed to:

  • Trace a Transaction: You've got a bank account number, but it appears in multiple financial reports. RAG can pinpoint the exact documents where the number is relevant to a specific transaction.
  • Summarize Research: You've got a stack of whitepapers on a new technology. RAG can distill the key findings and compare them across documents, saving you hours of reading.
  • Answer Complex Questions: You want to know your company's stance on a specific policy issue. RAG can scan through internal documents and provide a nuanced, contextualized answer.

RAG isn't just about finding keywords, it's about understanding the meaning and relationships within your documents.

DIY LLM-Powered Search

So I sat down and read the documentation of langchain, transformers, vector search and tinkered a python script together which works for me, but also can be the basis for much more advanced tools or apps - docai (see on GitHub).

There are two python scripts, one uses the Huggingface's model registry online, the other one looks for a local model and tries to find it at HuggingFace to download the necessary definitions. I've split both into 3 parts: check the setup, prep the setup, execute:

  1. Checks Your Setup: Makes sure you've got the right tools for the job. It verifies your Python version (3.9 or higher) and installs any missing libraries needed for LLM magic (like langchain, transformers, etc.).

  2. Loads the PDF: Uses a library called PyPDFLoader to grab all the text from your PDF file.

  3. Breaks It Down: The text gets chopped into smaller chunks using RecursiveCharacterTextSplitter. Think of it like cutting up a giant pizza into manageable slices.

  4. Builds a Knowledge Base: These text chunks are embedded (converted into numerical representations that capture meaning) using a pre-trained SentenceTransformer model and stored in a FAISS vector store. This is the real-time super-efficient search index.

  5. Asks & Answers: When you ask a question, the script performs a similarity search in the vector store to find the most relevant chunks of text. Then, it feeds those chunks and your question into a language model from Hugging Face (you get to choose which one!). The model generates an answer based on the context it's been given.

Why Do I Like It?

This simple script harnesses the power of two cutting-edge technologies:

  • Large Language Models (LLMs): These models are trained on massive amounts of text data, so they're great at understanding language, summarizing information, and generating answers.
  • Retrieval Augmented Generation (RAG): This combines the power of LLMs with information retrieval. By searching your PDF collection for relevant context, RAG gives your LLM the information it needs to give you accurate, targeted answers.


Using LLMs and RAG to layer the content of your files into a contextual search engine can revolutionize how you utilize your PDF archives. This allows you to build a comprehensive knowledge system based on historical information, accessible and beneficial to everyone in your organization. The LLM/SLM translates natural language queries into vector-semantic searches, providing relevant answers grounded in the data you provide, effectively reducing hallucinations and improving accuracy. This approach is straightforward and focuses on delivering practical results rather than getting bogged down in complex technology.


Popular posts from this blog

Deal with corrupted messages in Apache Kafka

Under some strange circumstances it can happen that a message in a Kafka topic is corrupted. This happens often by using 3rd party frameworks together with Kafka. Additionally, Kafka < 0.9 has no lock at at the consumer read level, but has a lock on Log.write(). This can cause a rare race condition, as described in KAKFA-2477 [1]. Probably a log entry looks like: ERROR Error processing message, stopping consumer: ($) kafka.message.InvalidMessageException: Message is corrupt (stored crc = xxxxxxxxxx, computed crc = yyyyyyyyyy Kafka-Tools Kafka stores the offset of every consumer in Zookeeper. To read out the offsets, Kafka provides handy tools [2]. But also can be used, at least to display the consumer and the stored offsets. First we need to find the consumer for a topic (> Kafka 0.9): bin/ --zookeeper management01:2181 --describe --group test Prior to Kafka 0.9 the only possibility to get this inform

Hive query shows ERROR "too many counters"

A hive job face the odd " Too many counters:"  like Ended Job = job_xxxxxx with exception 'org.apache.hadoop.mapreduce.counters.LimitExceededException(Too many counters: 201 max=200)' FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.MapRedTask Intercepting System.exit(1) These happens when operators are used in queries ( Hive Operators ). Hive creates 4 counters per operator, max upto 1000, plus a few additional counters like file read/write, partitions and tables. Hence the number of counter required is going to be dependent upon the query.  To avoid such exception, configure " mapreduce.job.counters.max " in mapreduce-site.xml to a value above 1000. Hive will fail when he is hitting the 1k counts, but other MR jobs not. A number around 1120 should be a good choice. Using " EXPLAIN EXTENDED " and " grep -ri operators | wc -l " print out the used numbers of operators. Use this value to tweak the MR s

GPT & GenAI for Startup Storytelling

OpenAI and Bard   are the most used GenAI tools today; the first one has a massive Microsoft investment, and the other one is an experiment from Google. But did you know that you can also use them to optimize and hack your startup?  For startups, creating pitch scripts, sales emails, and elevator pitches with generative AI (GenAI) can help you not only save time but also validate your marketing and wording. Curious? Here are a few prompt hacks for startups to create,improve, and validate buyer personas, your startup's mission/vision statements, and unique selling proposition (USP) definitions. First Step: Introduce yourself and your startup Introduce yourself, your startup, your website, your idea, your position, and in a few words what you are doing to the chatbot: Prompt : I'm NAME and our startup NAME, with website URL, is doing WHATEVER. With PRODUCT NAME, we aim to change or disrupt INDUSTRY. Bard is able to pull information from your website. I'm not sure if ChatGPT