AIFA RAG
A RAG system over 18,000 Italian drug leaflets, and a measured answer to where it should run: an M4 Mac, cloud CPUs or an H100, provisioned as code on Chameleon Cloud.
Contents
Overview
AIFA RAG answers questions about Italian medicines from the documents the Italian Medicines Agency (AIFA) publishes: the patient leaflets (Foglietti Illustrativi) and the summaries of product characteristics (Riassunti delle Caratteristiche del Prodotto). I built it because I wanted a fast way to read drug information from a validated source, instead of piecing it together from forums.
The pipeline downloads about 25,000 PDFs, converts 18,000 of them to Markdown, splits them into 805,000 section-aware chunks and indexes them in a vector database, next to a SQLite copy of AIFA's drug registry for the questions that have an exact answer.
The second half of the project is a benchmark. Once the system worked, I measured where each phase should actually run, on a Mac, on cloud CPUs or on an H100, and what each choice costs in time and money.
The problem
The first full run worked, but on a GPU VM that I had reserved and configured by hand, with one configuration, no cost figures and no way to repeat it exactly. Two questions stayed open:
- What would it cost to run it again? In euros on a commercial cloud, and in hours of waiting.
- Is one GPU box for everything a waste? The pipeline has phases bound by the network, by the CPU and by the GPU, and a single VM pays GPU prices for all of them.
How it works
The system is a hybrid RAG. Questions about tabular data, such as which company makes a drug or what its ATC code is, are exact lookups in SQLite. Only the free text of the leaflets goes through retrieval.
- Download: asynchronous, at most 6 requests at a time to avoid overloading AIFA's servers, with every outcome logged so the gaps in the corpus are auditable.
- Conversion: PyMuPDF4LLM turns each PDF into Markdown and sends scanned pages to OCR, one process per CPU core.
- Indexing: chunks follow the standard numbered sections of the leaflets, carry the drug name and section in their embedding, and are stored in ChromaDB with bge-m3 vectors.
- Querying: 30 candidates from vector search, a cross-encoder reranker keeps the best 6, and an LLM answers only from those, citing the drug it took each fact from.
The details of each step are in the build log.
Running it where it makes sense
To answer the cost question I turned the VM into code. A Makefile reserves the hardware through Chameleon's Blazar service, Terraform creates the security group, network port, instance and public IP, and one more command ships the exact commit and a fixed sample of 972 documents to the machine. A reachable VM went from about 7 minutes of clicking to 71 seconds, and teardown releases everything in about 20 seconds, including the public IP that the manual procedure had left behind.
On top of that I ran the same instrumented benchmark on four machines: an M4 Mac mini, an 8 and a 16 vCPU cloud VM, and an H100 VM with 48 vCPUs. Every run records per-phase timings and CPU and GPU usage, and a small cost model prices the results with on-demand list prices from Azure, AWS, GCP, Lambda and RunPod.
Results
- Embedding: the H100 is about 30 times faster than the M4's CPU and 51 to 88 times faster than the cloud CPU VMs. The Mac's integrated GPU is slower than its CPU.
- Conversion: scales almost linearly on cloud VMs up to 16 workers, while on the Mac it stops at 2 workers.
- A single GPU VM wastes the GPU: it sat idle for 54% of the full run, during download and conversion. Moving those phases to a cheap CPU VM cuts the estimated cost by about 45%.
- The provider matters more than the architecture: conversion plus embedding of the full corpus costs €11.77 on the cheapest H100 provider and €33.10 on the most expensive.
- The cloud sells time, not savings: the Mac is cheaper at every corpus size, but it would need about 3.7 days for the full corpus against 3.4 hours on the H100.
The full benchmark, with the charts, the code and where my assumptions broke, is in the write-up: What it actually costs to run a RAG pipeline.
Engineering decisions
- Lease outside Terraform: Terraform's OpenStack provider doesn't cover Chameleon's reservation service, so the lease goes through the OpenStack CLI and Terraform only receives its id.
- Code as a git bundle: the VM receives the current commit over SSH, so it needs no GitHub credentials and runs exactly the code recorded next to each result.
- A fixed sample, checked against reality: dozens of comparable runs on 972 documents, validated with one full run that showed the sample underestimates real times by 25 to 36%.
What's next
A prebuilt VM image to cut the four minutes of environment setup, an actual run of the two-VM configuration instead of the estimate, and measuring the query side on the cloud, where the GPU would have to stay on all day.
Tech stack
Python · PyMuPDF4LLM and Tesseract (PDF to Markdown, OCR) · bge-m3 and bge-reranker-v2-m3 · ChromaDB · SQLite · Terraform · OpenStack CLI and Blazar on Chameleon Cloud · GNU Make · pandas and matplotlib (benchmark and cost model).