Skip to main content

Convert documents to Markdown to build a RAG solution

Context is key in building effective AI enabled solutions. The most popular way to extend the pretrained knowledge set of a Large Language Model is through RAG, or Retrieval-Augmented Generation. By augmenting LLMs with external data we ensure that outputs are not only coherent but also factually grounded and up-to-date. This makes it invaluable for applications like chatbots, personalized recommendations, content creation, and decision support systems.

What is MarkItDown?

For any RAG solution to function effectively, the quality and format of the input data are critical. This is where MarkItDown, a lightweight Python utility created by Microsoft, stands out. It specializes in converting various files into Markdown format, a token-efficient and LLM-friendly structure.

From the documentation:

MarkItDown is a lightweight Python utility for converting various files to Markdown for use with LLMs and related text analysis pipelines. To this end, it is most comparable to textract, but with a focus on preserving important document structure and content as Markdown (including: headings, lists, tables, links, etc.) While the output is often reasonably presentable and human-friendly, it is meant to be consumed by text analysis tools -- and may not be the best option for high-fidelity document conversions for human consumption.

At present, MarkItDown supports:

  • PDF
  • PowerPoint
  • Word
  • Excel
  • Images (EXIF metadata and OCR)
  • Audio (EXIF metadata and speech transcription)
  • HTML
  • Text-based formats (CSV, JSON, XML)
  • ZIP files (iterates over contents)
  • Youtube URLs
  • EPubs
  • ... and more!

Running MarkItDown

You can install it directly using pip:

pip install 'markitdown[all]~=0.1.0a1'

But as I like to keep my system clean, I’ll use docker instead:

  • First clone the repository:

git clone https://github.com/microsoft/markitdown.git

  • Now we can build the docker image using:

docker build -t markitdown:latest .

  • Once we have build our docker image, we can convert a file using the following command:

docker run --rm -i -v .:/docs markitdown:latest /docs/input.pdf > output.md

As you can see whe share our current folder with Docker; using this flag: -v .:/docs. This makes our current folder accessible as the /docs folder in the container.

Let’s give it a try with an example PDF of a CV

docker run --rm -i -v .:/docs markitdown:latest /docs/cvexample.pdf > cvoutput.md

This was the example PDF (found somewhere online) I used:

 

And here is the output I got back:

 

That’s a good starter…

In a next post I’ll look how we can also process images in our source documents. See you again tomorrow!

More information

microsoft/markitdown: Python tool for converting files and office documents to Markdown.

Popular posts from this blog

Kubernetes–Limit your environmental impact

Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

DevToys–A swiss army knife for developers

As a developer there are a lot of small tasks you need to do as part of your coding, debugging and testing activities.  DevToys is an offline windows app that tries to help you with these tasks. Instead of using different websites you get a fully offline experience offering help for a large list of tasks. Many tools are available. Here is the current list: Converters JSON <> YAML Timestamp Number Base Cron Parser Encoders / Decoders HTML URL Base64 Text & Image GZip JWT Decoder Formatters JSON SQL XML Generators Hash (MD5, SHA1, SHA256, SHA512) UUID 1 and 4 Lorem Ipsum Checksum Text Escape / Unescape Inspector & Case Converter Regex Tester Text Comparer XML Validator Markdown Preview Graphic Col...