text.chunk
cuts a long text into pieces, and text.render_template writes a message from a template and the values wired to it.
text.chunk
Splits the text of a message into chunks of a fixed number of characters. Consecutive chunks overlap, so the text at one
edge appears in both chunks. The chunks are the input a database.vector.ingest takes.
A chunk starts
char_length - overlap_chars characters after the one before it, so with the defaults the first chunk
holds characters 0 to 1023 and the second starts at character 992. Characters are counted, not words or tokens, and a
chunk can start or end in the middle of a word. Each chunk is a new message with the role of the input message, one
text block and a new attributes.id. The step posts a component.output event, Initial text (<n> char long) was split into <m> chunks.
Edge cases
- A text of at most
char_length - overlap_charscharacters is one chunk, and an empty text is no chunks. - A longer text can end with a chunk that lies wholly inside the one before it: with the defaults, a text of 1000 characters gives two chunks, the second the last 8 characters, which the first already holds.
- An
overlap_charsequal tochar_lengthmakes the step fail, with no event. - An
overlap_charsabovechar_lengthsplits nothing: the step takessuccesswith an emptychunks.
text.render_template
Renders a Jinja template into one message. The template is a message, and every
other data input the component holds is a variable the template reads. The inputs are not fixed: add as many as the
template needs, each a data input with its own route, the custom route in the table.
Input modes
A variable is always one of the component’s data inputs other thantemplate and input_mode. An input holding no
value is not a variable. What the template sees depends on input_mode:
In
data mode a message is an object with role, content and attributes, so {{ question.content[0].text }}
reads its first block, and a messages input is a list a {% for %} loop walks. A text input in value mode is
{"type": "text", "text": "..."}, which is why .text is needed there.
Beside the inputs, the template always sees five variables: blob, session, definition, execution and
attributes, the last holding current_time and a fresh id. They are the same ones a
logic.code program is given, and an input with one of those names is
replaced by the variable.
When a render fails
The step takesfailure in each case, and no content is written:
- An
input_modethat is notvalueordataposts"input_mode" is not supported. - A template that uses an attribute the renderer blocks posts
text.render_template "<name>": the template used a blocked attribute and was not rendered.The message does not repeat the template. - Any other error in the template, posts the error’s trace.
Example: a shipment notice for Corvel Freight
Corvel Freight sends a notice for each booking. Atext.render_template in data mode has a customer input that
holds Northwind Foods and a lines input wired from a data.messages. The template is:
content output is a message with the template’s role holding the finished text, ready for a genai.chat or a
thread.
Example: chunk a handbook
Atext.chunk reads the handbook from a data.message and writes the chunks to a data.messages, with a chunk length
of 800 characters and an overlap of 80. The connections follow the shape of a data connection: text reads the source
and chunks writes the target.
Split handbook and follow its success route are left out.
