llm writing | decode xlsx spreadsheet | | Search

The elaborateLlama function analyzes an XLSX spreadsheet, creates prompt sequences from its data, and sends them to a Large Language Model (LLM) for responses, which are then logged and written to a temporary text file. The function is designed to handle asynchronous operations and can be exported as a module for use in other applications.

Run example

npm run import -- "elaborate list with llm"

elaborate list with llm

const os = require('os')
const fs = require('fs')

const analyzeSpreadsheet = importer.import("decode xlsx spreadsheet")

const TEMP_DIR = os.tmpdir();
let counter = 0


async function elaborateLlama(sheetFile, sheetName, rangeString, prefix, suffix) {

  let results = await analyzeSpreadsheet(sheetFile, sheetName, rangeString)

  console.log(results)

  const {llmPrompt} = await importer.import("create llm session")
  
  if(!prefix) {
    prefix = 'Imagine a scene that is much more exotic, weird, bizarre and detailed with this topic:\n'
  }
  if(!suffix) {
    suffix = '\nFit as much detail as possible in to two or three sentences.'
  }
  let results2 = []

  for(let i = 0; i < results.length; i++) {
    let q1 = prefix + results[i] + suffix
    console.log('User: ' + q1)
    let a1 = await llmPrompt(q1)
    console.log('AI: ' + a1)
    results2.push(a1)
  }

  const filename = path.join(TEMP_DIR, 'temporary_xlsx_' + (counter % 4) + '.txt')
  if(fs.existsSync(filename))
    fs.unlinkSync(filename)
  counter++
  console.log('Writing ', filename)
  fs.writeFileSync(filename, results2.join('\n'))

  return results2
}


module.exports = elaborateLlama

What the code could have been:

const os = require('os');
const fs = require('fs');
const path = require('path');
const importer = require('./importer'); // assuming importer is in a separate file

const TEMP_DIR = os.tmpdir();
let counter = 0;

/**
 * Analyzes an XLSX spreadsheet, generates questions based on the data, and
 * uses a large language model to answer them.
 *
 * @param {string} sheetFile - Path to the XLSX file
 * @param {string} sheetName - Name of the sheet to analyze
 * @param {string} rangeString - Range of cells to analyze
 * @param {string} prefix - Prefix for the question prompt (default: 'Imagine a scene that is much more exotic, weird, bizarre and detailed with this topic:\n')
 * @param {string} suffix - Suffix for the question prompt (default: '\nFit as much detail as possible in to two or three sentences.')
 * @returns {Promise} - List of answers from the large language model
 */
async function elaborateLlama(sheetFile, sheetName, rangeString, prefix = 'Imagine a scene that is much more exotic, weird, bizarre and detailed with this topic:\n', suffix = '\nFit as much detail as possible in to two or three sentences.') {
  try {
    const analyzeSpreadsheet = await importer.import('decode xlsx spreadsheet');
    const { llmPrompt } = await importer.import('create llm session');

    const results = await analyzeSpreadsheet(sheetFile, sheetName, rangeString);
    console.log(results);

    const results2 = await Promise.all(results.map((result) => {
      const q1 = prefix + result + suffix;
      console.log('User:'+ q1);
      return llmPrompt(q1).then((a1) => {
        console.log('AI:'+ a1);
        return a1;
      });
    }));

    const filename = path.join(TEMP_DIR, `temporary_xlsx_${counter % 4}.txt`);
    if (fs.existsSync(filename)) {
      fs.unlinkSync(filename);
    }
    counter++;
    console.log(`Writing to ${filename}`);
    fs.writeFileSync(filename, results2.join('\n'));

    return results2;
  } catch (error) {
    console.error(error);
    throw error;
  }
}

module.exports = elaborateLlama;

Breakdown of the Code

Importing Modules

Function elaborateLlama

Parameters

Returns

Function Body

  1. Calls analyzeSpreadsheet to analyze the spreadsheet and logs the results.
  2. Imports a function llmPrompt from a module named importer that creates an LLM session.
  3. Checks if prefix and suffix are not provided and sets default values if they are not.
  4. Loops through the results of the spreadsheet analysis and creates a prompt by concatenating prefix, the result, and suffix.
  5. Sends the prompt to the LLM using llmPrompt and logs the response.
  6. Pushes the response to an array results2.
  7. Writes the array of responses to a temporary text file.
  8. Returns the array of responses.

Exporting the Function

Note