Documentation
¶
Overview ¶
Package extract is the parse stage: one extractor per media type, each turning an item's bytes into units, such as a thread's post and comments or a book's pages. A registry keyed by media type picks the extractor, so an item parses the same whatever source sent it.
An extractor registers by joining builtin. One written in Go sets Extract. One whose library is not Go is a Program from the worker image, run through isolatedprocess. Each output is keyed by item, extractor and version, so a changed extractor takes a new version and its outputs write beside the old. A media type holds one primary extractor, which landing queues, and may hold secondary ones, whose parse another stage's write queues and writes beside the primary's.
Index ¶
Constants ¶
const DoclingPins = "e919138066e6"
DoclingPins is the first 12 hex digits of the SHA-256 of every pin in release/python-requirements.txt, then every model pin in release/download-docling-models, one a line. Each shapes Docling's markdown, and a test fails until this equals them.
const DoclingTimeout = 3 * time.Hour
DoclingTimeout bounds one run of pdf-docling. A 1,245-page PDF, the longest that sections selected when this was measured, took 34 minutes under `/usr/bin/time -v pdf-docling < <the PDF>` on a busy desk of 16 CPUs, 1.6 s a page. A node with half those CPUs takes about twice that, and the bound leaves more than twice that again.
const DoclingVersion = "2.153.0"
DoclingVersion is the Docling that release/python-requirements.txt pins, and that the worker image installs for pdf-docling.
const MediaTypePDF = "application/pdf"
MediaTypePDF is a PDF's media type, as a folder connector lands one.
const ProgramDir = "/usr/local/libexec/ingestion/extractors"
ProgramDir is where the worker image installs each extractor program.
const ProgramTimeout = 30 * time.Minute
ProgramTimeout is a program extractor's Timeout unless its registration sets another.
const PyMuPDFVersion = "1.28.2"
PyMuPDFVersion is the PyMuPDF that release/python-requirements.txt pins, and that the worker image installs for the PDF programs.
Variables ¶
var PDFDocling = func() Extractor { e := Program("docling", "1+docling-"+DoclingVersion+"-"+DoclingPins, MediaTypePDF, ProgramDir+"/pdf-docling") e.Positional, e.Secondary, e.Timeout, e.SlotLimit = true, true, DoclingTimeout, 1 return e }()
PDFDocling reads a PDF with Docling's standard pipeline, one unit per PDF page, so unit n is PDF page n, a blank page included. A unit's text is the page's markdown, with each code block fenced and each table's rows kept, and its label is the page's printed label, as PDFText reads it. Docling drops every formula, so it reads only the books whose profile sections.FontShares.CodeHeavyMathLight picks.
It is secondary, so landing never queues it. The write that ends such a book's structure task queues it, and its parse writes beside PDFText's. A worker runs one at a time unless [worker.slot_limits] says otherwise. Its version names Docling's and DoclingPins, so a parse by other libraries or weights writes beside the old one. A change to the program's own reading raises the leading number.
var PDFText = func() Extractor { e := Program("pdf-text", "1+pymupdf-"+PyMuPDFVersion, MediaTypePDF, ProgramDir+"/pdf-text") e.Positional = true return e }()
PDFText reads a PDF's text with PyMuPDF, one unit per PDF page, so unit n is PDF page n, a blank page included. A unit's label is the page's printed label where the PDF declares page labels, and null where it declares none. Its version names PyMuPDF's, so a library upgrade writes its pages beside the old ones.
var ThreadText = Extractor{Name: "thread-text", Version: "1", MediaType: redditexport.MediaTypeSave, Extract: threadText}
ThreadText parses a Reddit save: its post, then each comment of its thread depth first, in the order Reddit sorted them. A comment that its author deleted or a moderator removed is left out, and its replies are kept. Each unit's label is its author.
Functions ¶
func Capability ¶
Capability is the capability of e's parse tasks, so a worker claims only the tasks of extractors it runs.
Types ¶
type Extractor ¶
type Extractor struct {
// Name names the extractor in each output it writes, such as thread-text.
Name string
// Version is written beside each output, so a changed extractor takes a
// new one.
Version string
MediaType string
// Program is the absolute path of the extractor's program, and empty for
// one written in Go. A worker runs it only where the program is installed.
Program string
// Extract reads body and answers its units in order.
Extract func(ctx context.Context, body io.Reader) ([]Unit, error)
// Timeout bounds one run of Extract, and none where it is zero. A run
// past it fails its task, so a program that hangs on one item's bytes
// frees its worker slot and the item waits in review.
Timeout time.Duration
// Positional keeps a unit with no text in its place, so unit n is the
// artifact's own part n, such as a book's PDF page n. Otherwise such a
// unit is dropped and the rest are numbered again.
Positional bool
// Secondary marks an extractor that landing never queues and a parse
// replay never picks, because another stage's write queues its parse, as
// the structure's queues PDFDocling's.
Secondary bool
// SlotLimit caps the extractor's parses that one worker runs at once,
// where [worker.slot_limits] names no limit for it. Zero leaves them
// uncapped. A program whose run holds gigabytes sets it, so the worker's
// memory holds its runs.
SlotLimit int
}
Extractor turns the bytes of one media type into a parse's units.
func Program ¶
Program is an extractor that runs the program at path with args, such as one whose library is Python. The program reads the item's bytes on stdin, and writes one JSON line per unit on stdout, in order:
{"label": "Chapter 1", "text": "It was a bright cold day…"}
label may be null or left out, and any other field is refused. A run that exits non-zero fails its task, with the end of its stderr kept, and so does a run past ProgramTimeout. The program runs through isolatedprocess, so it holds none of the worker's credentials.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry picks the primary extractor for an item by its media type, and holds every extractor that a worker runs.
func Builtin ¶
func Builtin() *Registry
Builtin is the registry of every extractor that ingestion holds.
func NewRegistry ¶
NewRegistry registers extractors. It refuses one with no name, version, media type or way to run, two of one name, and two primary ones of one media type.
func (*Registry) For ¶
For is the primary extractor that reads mediaType, and false where none does.
func (*Registry) Handlers ¶
Handlers is a worker handler for each extractor of r that runs here: each written in Go, and each whose program is installed. A worker claims only these extractors' tasks.
func (*Registry) QueueParse ¶
QueueParse queues a parse task for item where an extractor reads its media type. Landing calls it in the write that adds the item, so every item lands with its parse queued.
func (*Registry) Replayable ¶
func (r *Registry) Replayable() server.Replayable
Replayable is the parse stage as a replay runs it again: a task for each item whose media type an extractor reads.