Skip to main content
Version: 0.0.5 (stable)

agrag.loaders.chat.ChatLoader

Bases: ProseLoader

Reads a file of chat messages as one document with a section for each message.

Each message is a JSON object with a role and a content string, and an optional id. A .jsonl or .ndjson file has one message per line. A .json file holds an array of messages. The document text is one [role] content block per message, separated by a blank line. Each message is a section at depth 1 with the role as its heading and the message id as its source_id.

The loader is not registered for any extension, because .jsonl belongs to the record loader. Pass loader=ChatLoader() with a single-file source.

Attributes:

  • extensions – The .jsonl, .ndjson and .json extensions.

Functions:

  • is_available – Return whether the package that the loader's extra installs is present.
  • load – Yield one prose Document that holds every message.

extensions​

extensions = frozenset({'.jsonl', '.ndjson', '.json'})

extra​

extra: str | None = None

extra_module​

extra_module: str | None = None

family​

family = DocumentFamily.PROSE

is_available​

is_available() -> bool

Return whether the package that the loader's extra installs is present.

A loader with no extra is always available. The check looks the module up without importing it, so an installed package that fails to import still counts.

load​

load(source:SourceRef, stream:BinaryIO, opts:ReadOptions, *, start_at:int = 0) -> Iterator[Document]

Yield one prose Document that holds every message.

Parameters:

  • source (SourceRef) – The source to read.
  • stream (BinaryIO) – The open binary stream for the source.
  • opts (ReadOptions) – The read options.
  • start_at (int) – Ignored by prose loaders.

Yields:

  • Document – One Document, or none when the source has no messages.

Raises:

  • MalformedRecordError – A message is not a JSON object with a non-empty string role and a string content, an id repeats an earlier one, or the source is not valid JSON of the expected shape.

mime_types​

mime_types: frozenset[str] = frozenset()