scraping | test article extract | test article summarizer | Search

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.

Run example

npm run import -- "summarize llm article"

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

What the code could have been:

/**
 * 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;
    }
  }
}

Function Definition

summerizeArticle is an asynchronous function that summarizes an article in two ways:

  1. Detailed Summary: A detailed summary of the article in a few paragraphs.
  2. Single Sentence Summary: A brief summary of the article in a single sentence.

Parameters

Return Value

The function returns an array containing two summaries:

  1. a1: The detailed summary.
  2. a2: The single sentence summary.

Functionality

  1. Creates an LLM session using the create llm session module.
  2. Sends two prompts to the LLM session:
  3. Retrieves the summaries from the LLM session and logs them to the console.
  4. Returns the two summaries as an array.

Export

The function is exported as a module using module.exports = summerizeArticle.