Vectors: Add only custom boundary chunking

77bee453abe1b325e96700684def04832f6948bd

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +16 -1Showing whitespace changes
public/scripts/extensions/vectors/index.js+10 -1
@@ -82,6 +82,7 @@ const settings = {
8282 chunk_size: 5000,
8383 chunk_count: 2,
8484 overlap_percent: 0,
85+ only_custom_boundary: false,
8586
8687 // For Data Bank
8788 size_threshold_db: 5,
@@ -571,7 +572,9 @@ async function vectorizeFile(fileText, fileName, collectionId, chunkSize, overla
571572 const delimiters = getChunkDelimiters();
572573 // Overlap should not be included in chunk size. It will be later compensated by overlapChunks
573574 chunkSize = overlapSize > 0 ? (chunkSize - overlapSize) : chunkSize;
574- const chunks = splitRecursive(fileText, chunkSize, delimiters).map((x, y, z) => overlapSize > 0 ? overlapChunks(x, y, z, overlapSize) : x);
575+ const chunks = settings.only_custom_boundary && settings.force_chunk_delimiter
576+ ? fileText.split(settings.force_chunk_delimiter)
577+ : splitRecursive(fileText, chunkSize, delimiters).map((x, y, z) => overlapSize > 0 ? overlapChunks(x, y, z, overlapSize) : x);
575578 console.debug(`Vectors: Split file ${fileName} into ${chunks.length} chunks with ${overlapPercent}% overlap`, chunks);
576579
577580 const items = chunks.map((chunk, index) => ({ hash: getStringHash(chunk), text: chunk, index: index }));
@@ -1555,6 +1558,12 @@ jQuery(async () => {
15551558 saveSettingsDebounced();
15561559 });
15571560
1561+ $('#vectors_only_custom_boundary').prop('checked', settings.only_custom_boundary).on('input', () => {
1562+ settings.only_custom_boundary = !!$('#vectors_only_custom_boundary').prop('checked');
1563+ Object.assign(extension_settings.vectors, settings);
1564+ saveSettingsDebounced();
1565+ });
1566+
15581567 $('#vectors_ollama_pull').on('click', (e) => {
15591568 const presetModel = extension_settings.vectors.ollama_model || '';
15601569 e.preventDefault();
public/scripts/extensions/vectors/settings.html+6 -0
@@ -186,6 +186,12 @@
186186 </label>
187187
188188 <div id="vectors_files_settings" class="marginTopBot5">
189+ <label class="checkbox_label" for="vectors_only_custom_boundary" title="Create a chunk for every custom Chunk boundary detected in the file text. Don't chunk on default boundaries.">
190+ <input id="vectors_only_custom_boundary" type="checkbox" class="checkbox">
191+ <span data-i18n="Only chunk on custom boundary">
192+ Only chunk on custom boundary
193+ </span>
194+ </label>
189195 <label class="checkbox_label" for="vectors_translate_files" title="This can help with retrieval accuracy if using embedding models that are trained on English data. Uses the selected API from Chat Translation extension settings.">
190196 <input id="vectors_translate_files" type="checkbox" class="checkbox">
191197 <span data-i18n="Translate files into English before processing">