Skip to content

Source

A Source is identified input text: the full text of one document, webpage or article, together with an origin that says where it came from. It is what chunking consumes and what every chunk points back to.

Upstream steps (reading files, extracting HTML, OCR) exist to produce sources. Everything after them works on plain text and never needs to know the original file format.

Example

from triplum.datatype import Collection, Source

collection = Collection(name="Research notes")
source = Source(
    collection_id=collection.id,
    origin="notes/returns.md",
    text="Returns are accepted within 30 days. Refunds take 5 days.",
)
print(source.origin, len(source.text))  # notes/returns.md 57
print(source.id.version)  # 8
  1. Import Source from triplum.datatype.
  2. Set collection_id to the id of the collection the source belongs to.
  3. Set origin to something that identifies the text. Here it is a file path; a URL or another identifier works too. It is always a string.
  4. Set text to the full text, already decoded. The source holds it in memory.

External id

When a dataset has its own key for each document, such as a benchmark's document id, set it as external_id:

from triplum.datatype import Collection, Source

collection = Collection(name="Articles")
source = Source(
    collection_id=collection.id,
    origin="https://example.org/articles/42",
    text="Returns are accepted within 30 days.",
    external_id="article-42",
)
print(source.external_id)  # article-42
  • external_id is optional and defaults to None.
  • It is not part of the id. Two sources equal in collection_id, origin and text but with different external ids have the same id, so they conflict: a store refuses the second.
  • A store also refuses a source whose external_id another source of the same collection already holds. Sources without one never conflict.
  • Two text revisions of one document therefore cannot both carry its external id.

What it guarantees

  • Source is a Pydantic model, so construction validates the field types. It has no storage behavior of its own.
  • Unknown field names are rejected, so a misspelt field fails at once instead of being lost: Source(..., externalid="x") raises a validation error of type extra_forbidden.
  • Sources are immutable: assigning to a field raises a validation error. To change the text, create a new Source.
  • text may be empty; chunking then produces no chunks.
  • The id is a deterministic UUIDv8 derived from collection_id, origin and text. Creating the same source again gives the same id; changing any of the three gives a different one, and changing the text back gives the original id again. IDs repeat across program runs only when the same collection id is reused: a new Collection(name=...) has a fresh id. Supplying an id that does not match these fields fails validation. Chunks refer back to their source through source_id, which is that id.
  • origin and text enter the id exactly as given: "notes/a.md" and "./notes/a.md" give different IDs. Normalize them before creating the source if they should match.
  • fingerprint() hashes the three id fields plus external_id, recomputing on each call. See Fingerprints for storage and cache behavior.
  • origin is for people and for matching back to the original document; it is not checked for uniqueness.

Reference

Source in the API reference.

Next: Chunk, an excerpt of a source.