The summerizeArticle
function is an asynchronous function that summarizes an article in two ways: a detailed summary and a single sentence summary. It creates an LLM session, sends prompts for both types of summaries, and returns the results as an array.
npm run import -- "summarize llm article"
// TODO: prompt llm, in one sentence summarize this article. in a whole paragraph summarize this article.
async function summerizeArticle(article, funny) {
let {llmPrompt} = await importer.import("create llm session")
if(!funny) {
funny = ''
}
let q1 = 'Summerize this article in great detail ' + funny + ':\n' + article.substr(0, 4096) + '\nOnly return the summary and nothing else, no explanations.'
console.log('User: ' + q1)
let a1 = await llmPrompt(q1)
console.log('AI: ' + a1)
let q2 = 'Summerize this article in a single sentence ' + funny + ':\n' + article.substr(0, 4096) + '\nOnly return the summary and nothing else, no explanations.'
console.log('User: ' + q2)
let a2 = await llmPrompt(q2)
console.log('AI: ' + a2)
return [a1, a2]
}
module.exports = summerizeArticle
/**
* Summarize an article using a large language model.
*
* @param {string} article - The article to be summarized.
* @param {boolean} [funny=false] - Whether the summary should be funny or not.
* @returns {Promise<[string, string]>} A promise that resolves to an array of two strings, the detailed summary and the one-sentence summary.
*/
async function summarizeArticle(article, funny = false) {
try {
// Import the LLM prompt and create a session.
const { llmPrompt } = await importModule('create-llm-session');
// Define the prompts for the detailed and one-sentence summaries.
const prompts = [
{
prompt: `Summarize this article in great detail ${funny? 'funnily' : ''}: ${article.substring(0, 4096)}`,
responseLength: 'full',
},
{
prompt: `Summarize this article in a single sentence ${funny? 'funnily' : ''}: ${article.substring(0, 4096)}`,
responseLength: 'one-sentence',
},
];
// Log the user prompts.
console.log('User prompts:');
prompts.forEach((prompt, index) => {
console.log(`${index + 1}. ${prompt.prompt}`);
});
// Get the LLM responses.
const responses = await Promise.all(prompts.map(async (prompt) => {
return await llmPrompt(prompt.prompt);
}));
// Log the LLM responses.
console.log('LLM responses:');
responses.forEach((response, index) => {
console.log(`${index + 1}. ${response}`);
});
// Return the detailed and one-sentence summaries.
return responses;
} catch (error) {
// Handle any errors that occur during execution.
console.error('Error:', error);
throw error;
}
}
module.exports = summarizeArticle;
// Helper function to import a module.
function importModule(moduleName) {
try {
return import(moduleName);
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
throw new Error(`Module '${moduleName}' not found.`);
} else {
throw error;
}
}
}
summerizeArticle
is an asynchronous function that summarizes an article in two ways:
article
: The article to be summarized, with a maximum of 4096 characters.funny
: An optional parameter that can be used to make the summary humorous.The function returns an array containing two summaries:
a1
: The detailed summary.a2
: The single sentence summary.create llm session
module.The function is exported as a module using module.exports = summerizeArticle
.