When it fires
In every sync of the knowledge base, for each chunk of each post, twice: first to tell whether the chunk changed since the last sync (a hash of the returned text and the chunk’s index data), then, when it did or the sync is forced, right before the embedding is requested from the AI provider. Return the same text for the same input, or every sync embeds every chunk again.
The returned text decides what the vector means, so what the knowledge search finds. It is not stored: the snippet shown under answers and the text the AI reads keep the chunk’s own text. To add text that is also shown and read, use kimaai_embedding_extra_text.
When the callback starts returning another text, the changed chunks are embedded again on their next sync.
Parameters
| Name | Type | Description |
|---|---|---|
text | string | The chunk’s text; it starts with a “Context:” line naming its heading or the post’s title. |
post | WP_Post | The post the chunk belongs to. |
chunk | array | The chunk, with text, chunk_index, heading_path, anchor, token_count, start_char and end_char. |
clean | array | The post’s prepared text, with lang, text, title and html. |
Returns
string: The text to embed.
Example
add_filter(
'kimaai_embedding_chunk_text',
function (string $text, WP_Post $post): string {
if ($post->post_type !== 'product') {
return $text;
}
// Questions name the brand, the product texts rarely do: each chunk of a product is found by its brand too.
$brand = (string) get_post_meta($post->ID, 'brand', true);
return $brand !== '' ? 'Brand: ' . $brand . "\n" . $text : $text;
},
10,
2
);
Related
kimaai_embedding_extra_text: adds text to the post, stored and shown with its chunks.kimaai_embedding_vector_metadata: stores data with each chunk.