ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
16 lines (15 loc) • 613 B
JavaScript
export async function summarizeRecursively({ summarize, split, join = (texts) => texts.join("\n\n"), text, }, options) {
const chunks = await split({ text });
const summarizedTexts = await Promise.all(chunks.map((chunk) => summarize({ text: chunk }, options)));
if (summarizedTexts.length === 1) {
return summarizedTexts[0];
}
// recursive mapping: will split joined results as needed to stay
// within the allowed size limit of the splitter.
return summarizeRecursively({
text: join(summarizedTexts),
summarize,
split,
join,
}, options);
}