Skip to main content

agrag.chunking.extras.NeuralChunker

Bases: _ExtraSpanChunker

Cuts where a token classification model predicts a topic break.

Needs the chunk-neural extra. The first use downloads the model.

Attributes:

  • model (str | None) – The Hugging Face model id. None uses chonkie's default model.
  • device_map (str) – The device for the model, for example "cpu" or "auto".
  • min_characters_per_chunk (int) – The smallest chunk the splitter keeps apart.

Functions:

  • chunk – Split a document into chunks.
  • fingerprint – Return the hash of settings(), 16 hex characters.
  • model_copy – Copy the chunker, validating any changed setting.
  • model_post_init – Compute the fingerprint only, so the extra is not needed to build.
  • settings – Return the strategy name and every setting as JSON-safe data.
  • spans – Return the character spans this strategy cuts text into.

chunk​

chunk(document:Document) -> list[Chunk]

Split a document into chunks.

Every chunk has non-empty text, indexes run from 0 without gaps, and a chunk with text provenance has text equal to document.text at its offsets.

Parameters:

  • document (Document) – The document to split.

Returns:

  • list[Chunk] – The chunks, in document order, each with chunker and chunker_hash set. A strategy that sets chunker itself keeps its value.

Raises:

  • ChunkingError – The strategy returned chunks that break the contract.

device_map​

device_map: str = 'cpu'

fingerprint​

fingerprint() -> str

Return the hash of settings(), 16 hex characters.

min_characters_per_chunk​

min_characters_per_chunk: int = Field(default=10, gt=0)

model​

model: str | None = None

model_config​

model_config = ConfigDict(frozen=True, extra='forbid')

model_copy​

model_copy(*, update:Mapping[str, Any] | None = None, deep:bool = False) -> Self

Copy the chunker, validating any changed setting.

A plain copy keeps the fingerprint and the splitter of the original. A copy with changes is built again from its configuration.

model_post_init​

model_post_init(context:Any) -> None

Compute the fingerprint only, so the extra is not needed to build.

settings​

settings() -> dict[str, Any]

Return the strategy name and every setting as JSON-safe data.

A setting that is itself a chunker appears as that chunker's settings.

spans​

spans(text:str) -> list[tuple[int, int]]

Return the character spans this strategy cuts text into.

Raises:

strategy​

strategy: str

The strategy name, "neural".