Skip to content

Llm

pipelines.extraction.pdf.llm

Summary

LLM splitter variant of the pdf family (impl-layer, bank-agnostic).


LLMSplitterPort is a faithful port of TransactionSplitterLLMAdaptor (domain/extraction/adaptors/txn_blocks.py) using SyncOpenRouterPool. It swaps into the pdf chain exactly where ImplSplitterPort sits, so the rest of the tree (raw_lines → txn_blocks → txn_dicts → raw_expense) is unchanged.

LLMPdfExtractionPipeline re-pins only the txn_blocks.splitter slot.

Classes

LLMPdfExtractionPipeline

Bases: ImplPDFExtractionPipeline

PDF root with the LLM splitter swapped into the txn-blocks stage.

Attributes:

Name Type Description
txn_blocks LLMTxnBlocksPort

Txn-blocks stage with LLM splitter.

Functions
run
run(source)

Run the pdf extraction chain end-to-end.

Parameters:

Name Type Description Default
source

Raw extraction source.

required

Returns:

Type Description

List of raw expense records.

LLMSplitterPort

LLMSplitterPort(pool=None, **kwargs)

Bases: SplitterPort

LLM-backed splitter that groups lines into transactions via prompts.

Attributes:

Name Type Description
DATE_RE Pattern[str]

Compiled regex matching leading DD-MM-YYYY dates.

number ImplNumberPort

Number extraction leaf.

date ImplDatePort

Date parsing leaf.

GROUP_PROMPT str

Prompt grouping indices into transactions.

RESOLVE_PROMPT str

Prompt resolving cross-batch conflicts.

MAX_CONFLICT_LOOPS int

Max conflict resolution loops.

BATCH_MERGE int

Date lines per batch.

Initialize the splitter with an LLM pool.

Parameters:

Name Type Description Default
pool

LLM pool; defaults to a new SyncOpenRouterPool.

None
**kwargs

Passed through to the base class.

{}
Functions
clean_raw_lines staticmethod
clean_raw_lines(raw_lines: List[str]) -> List[str]

Clean raw lines: strip trailing noise and drop Page lines.

Parameters:

Name Type Description Default
raw_lines List[str]

Raw OCR lines.

required

Returns:

Type Description
List[str]

List[str]: Cleaned lines.

split_blocks
1
2
3
4
5
6
7
split_blocks(
    lines,
    *,
    has_header=False,
    min_numbers=3,
    detect_date_ranges=False
)

Group lines into transaction blocks via the LLM.

Parameters:

Name Type Description Default
lines

Raw OCR lines.

required
has_header

Whether a header precedes the rows.

False
min_numbers

Minimum numbers for a block.

3
detect_date_ranges

Detect date-range lines.

False

Returns:

Type Description

List of transaction block texts.

strip_headers staticmethod
strip_headers(raw_lines: List[str]) -> List[str]

Drop header lines preceding the first date line.

Parameters:

Name Type Description Default
raw_lines List[str]

Raw OCR lines.

required

Returns:

Type Description
List[str]

List[str]: Lines with headers removed.

LLMTxnBlocksPort

Bases: ImplTxnBlocksPort

Txn-blocks stage re-pinning the splitter to the LLM variant.

Attributes:

Name Type Description
splitter LLMSplitterPort

LLM-backed splitter.

Functions
extract
extract(raw_lines)

Extract txn blocks keyed by opening balance.

Parameters:

Name Type Description Default
raw_lines

Raw OCR text lines.

required

Returns:

Type Description

Dict[float, List[str]]: Opening balance → block texts.