I’m getting this errore in a Code node
Node execution failed
This can happen for various reasons. Please try executing the node again. If the problem persists, you can try the following:
- Reduce the number of items processed at a time, by batching them using a loop node
- Upgrade your cloud plan to increase the available memory
It’s a script dividing text into chunks.
The script is this:
// Parametri configurabili
const maxChars = 1000; // lunghezza massima (in caratteri) per chunk
const overlapChars = 200; // caratteri di sovrapposizione tra chunk
// Estrai il testo dall'input
const text = $('Extract from File').first().json.text;
const chunks = [];
let start = 0;
const totalLength = text.length;
while (start < totalLength) {
// posizione massima teorica
let tentativeEnd = Math.min(start + maxChars, totalLength);
// Se non siamo alla fine, cerchiamo di non tagliare parole o frasi
if (tentativeEnd < totalLength) {
// cerca l'ultima fine frase (.
const endOfSentence = text.lastIndexOf(/[\.\!\?]\s/, tentativeEnd);
if (endOfSentence > start + overlapChars) {
tentativeEnd = endOfSentence + 1; // includi il carattere di punteggiatura
} else {
// altrimenti, cerca l'ultimo spazio
const lastSpace = text.lastIndexOf(' ', tentativeEnd);
if (lastSpace > start + overlapChars) {
tentativeEnd = lastSpace;
}
}
}
const chunkText = text.slice(start, tentativeEnd).trim();
chunks.push(chunkText);
// Calcola il nuovo start tenendo conto dell'overlap
start = tentativeEnd - overlapChars;
if (start < 0) start = 0;
}
// Restituisci ogni chunk come singolo item JSON
return chunks.map((chunk, index) => ({
json: {
chunk_id: index,
text: chunk,
}
}));
The text is 100k characters long.
Here’s the scenario:
Is it a memory problem?