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.
npm run import -- "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
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
const os = require('os')
: Imports the os
module, which provides information about the operating system.const fs = require('fs')
: Imports the fs
(File System) module, which provides functions for interacting with the file system.const analyzeSpreadsheet = importer.import('decode xlsx spreadsheet')
: Imports an analyzeSpreadsheet
function from a module named importer
that decodes an XLSX spreadsheet.const TEMP_DIR = os.tmpdir();
: Gets the temporary directory path from the os
module.let counter = 0
: Initializes a counter variable to 0.elaborateLlama
sheetFile
: The file path to the XLSX spreadsheet.sheetName
: The name of the sheet in the spreadsheet.rangeString
: The range of cells to analyze in the spreadsheet.prefix
(optional): The prefix to add to each question prompt.suffix
(optional): The suffix to add to each question prompt.analyzeSpreadsheet
to analyze the spreadsheet and logs the results.llmPrompt
from a module named importer
that creates an LLM session.prefix
and suffix
are not provided and sets default values if they are not.prefix
, the result, and suffix
.llmPrompt
and logs the response.results2
.module.exports = elaborateLlama
: Exports the elaborateLlama
function as a module.await
to handle promises.path
module is not imported, but it is used in the code. This may cause an error if not imported.