Skip to content

Turn PDFs, Word files and slides into Markdown with docread, index them with index, and answer questions from them with an Ailoy agent that searches them itself.

Retrieval-augmented generation (RAG) answers a question from documents: it finds the passages that bear on the question, and a model answers from them. Here a sandbox does the finding and an agent the answering:

  • docread parses documents precisely, into text that keeps their structure.
  • index keeps that text searchable, and finds the passages that bear on a question.
  • An Ailoy agent, given the sandbox, searches with index and answers from what it finds.

docread and index run on any image, without the network.

Put the documents in

Keep the documents and the store in a volume, so the store outlives the sandbox that builds it and the next one searches it without building it again.

from pathlib import Path
from virtx import SandboxVolume
volume = await client.create_volume("manuals")
await volume.create_dir_all("raw")
for path in Path("manuals").glob("*.pdf"):
await volume.write_file(f"raw/{path.name}", path.read_bytes())
sandbox = await client.create_sandbox(
"alpine:latest",
abins=["docread", "index"],
volumes=[SandboxVolume(volume, "/data")],
network=False,
)

Parse and index them

docread turns each document into Markdown, and index builds a searchable store from it.

cd /data
docread raw --to md --output md
index init manuals.db
index ingest manuals.db md

docread reads PDF, Word, PowerPoint, Excel, HTML, EPUB, HWP and more. A scanned document, an image of text, is not read.

Search them

index search answers the passages that best match a query, best first.

cd /data
index search manuals.db -n 5 --text warranty coverage

Each names the document and the lines it comes from, with the passage under it:

Output
12.4 md/warranty.md:41-58 Limited warranty
## What is covered
The product is covered against defects in materials and workmanship ...

It matches words, not meanings: a passage that says the same in other words is not found.

Answer from them

An Ailoy agent given the sandbox learns index and docread from it by itself. It searches the store, tries other words when it finds too little, and answers from the passages. Tell it where the store is, and to cite the passages its answer comes from.

from ailoy import AgentBuilder
agent = await (
AgentBuilder("anthropic/claude-sonnet-5-5")
.instruction(
"The user's documents are indexed in /data/manuals.db; run `index` from /data. "
"Answer from what they say, and cite each passage by the path and lines a search answers."
)
.sandbox(sandbox)
.build()
)
async for output in agent.run("What does the warranty cover?"):
message = output["message"]
if message["role"] == "assistant":
for part in message["contents"]:
if part["type"] == "text":
print(part["text"])

It prints the agent’s answer, with the passages it comes from:

Output
The warranty covers defects in materials and workmanship for two years from the date of
purchase (md/warranty.md:41-58). It does not cover damage from accidents, misuse or
unauthorised repairs, nor normal wear of the battery (md/warranty.md:59-74).
To make a claim, contact support with the serial number and proof of purchase; a covered
product is repaired or replaced at no charge (md/support.md:12-25).

The model runs outside the sandbox, so the sandbox needs no network for it. Ailoy’s README says which models and backends it runs, and how to install it for each language.

Keep it up to date

index sync brings the store up to date with what was added, changed or removed. A document taken out of raw needs its .md taken out too.

cd /data
docread raw --to md --output md
index sync manuals.db md