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
- Import
Sourcefromtriplum.datatype. - Set
collection_idto theidof the collection the source belongs to. - Set
originto something that identifies the text. Here it is a file path; a URL or another identifier works too. It is always a string. - Set
textto 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_idis optional and defaults toNone.- It is not part of the
id. Two sources equal incollection_id,originandtextbut with different external ids have the sameid, so they conflict: a store refuses the second. - A store also refuses a source whose
external_idanother 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¶
Sourceis 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 typeextra_forbidden. - Sources are immutable: assigning to a field raises a validation error. To change the text,
create a new
Source. textmay be empty; chunking then produces no chunks.- The
idis a deterministic UUIDv8 derived fromcollection_id,originandtext. Creating the same source again gives the sameid; changing any of the three gives a different one, and changing the text back gives the originalidagain. IDs repeat across program runs only when the same collectionidis reused: a newCollection(name=...)has a freshid. Supplying anidthat does not match these fields fails validation. Chunks refer back to their source throughsource_id, which is thatid. originandtextenter theidexactly 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 threeidfields plusexternal_id, recomputing on each call. See Fingerprints for storage and cache behavior.originis 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.