Incremental engine for long horizon agents π Star if you like it!
Search the text and the pictures inside your PDFs.
Split each PDF page into text and images, embed each with its own model, and store both in Qdrant.
One question searches the passages and the pictures β incrementally, in plain async Python.
A PDF holds two kinds of content: prose and pictures. A text model cannot see a diagram, and an image model reads prose badly. This example gives each element type its own model:
- Text: SentenceTransformers
all-MiniLM-L6-v2(384-dim), into thePdfElementsTextcollection. - Images: CLIP
openai/clip-vit-large-patch14(ViT-L/14, 768-dim), into thePdfElementsImagecollection.
Every point carries its file name and page number, so a hit takes you back to the page.
How it works
Read in main.py:
localfs.walk_dirlists the PDFs insource_files/.coco.mount_eachrunsprocess_fileonce per file.extract_pdf_elementsreads each page withpypdf: the page text, plus every image of 16 px or more on a side, made into a thumbnail of at most 512Γ512.RecursiveSplittercuts the page text into chunks (600 characters, 100 overlap).process_chunkembeds each chunk and declares one point in the text collection.process_imageembeds each image with CLIP and declares one point in the image collection. The thumbnail goes in the payload (base64), so a UI can show the hit.qdrant.mount_collection_targetcreates both collections. The text collection takes its vector size from the embedder; the image collection takes it from the CLIP config.
You declare the points. CocoIndex inserts, updates and deletes them to match the PDFs.
Prerequisites
Python 3.11 or later.
A Qdrant server. Start one with Docker (HTTP on
6333, gRPC on6334):docker run -d -p 6333:6333 -p 6334:6334 qdrant/qdrantThe example connects over gRPC to
http://localhost:6334/. SetQDRANT_URLin.envto use another server.About 2 GB of disk for the models. The first run downloads them from Hugging Face. A GPU is optional; CLIP runs on the CPU too, more slowly.
Run it
1. Configure and install:
cp .env.example .env
pip install -e .
2. Get sample PDFs. The script downloads four board game rulebooks into source_files/:
./fetch_manuals.sh
You can also put your own PDFs in source_files/.
3. Build the index:
cocoindex update main # catch-up: scan, sync, exit
cocoindex update -L main # live: keep watching source_files/ for changes
4. Query. One question goes to both collections. The text collection gets a MiniLM vector; the image collection gets a CLIP text vector, so words find pictures:
python main.py "how do players trade goods?"
The output lists the top 5 text passages and the top 5 images, each with its score, file and page. You can also browse the points in the Qdrant dashboard.
What a re-run does
Run cocoindex update main again, and CocoIndex does only the work that changed:
- No change:
process_fileis memoized (memo=True). An unchanged PDF is skipped: no parse, no embedding, no write to Qdrant. - A PDF changes: only that file is processed again.
process_imageis memoized too, so an image that did not change is not sent to CLIP again. Points for text or images that are gone are deleted. - A PDF is deleted: all its points are deleted from both collections.
- The text model changes:
EMBEDDERis a context key withdetect_change=True, so all text is embedded again.clip_embed_imagenamesCLIP_MODELindeps, so a CLIP model change embeds all images again.
Want the pictures in your PDFs to be searchable too? Give CocoIndex a star β β it helps a lot.
Docs Β· Discord Β· See all examples β