The relevantHistory
function retrieves relevant conversation history files based on a given prompt, returning an array of file names. This function requires importer.import('general chit chat')
to be called prior to usage and uses synchronous file system operations, logging input and output to the console.
npm run import -- "relevant llm history"
const path = require('path')
const fs = require('fs')
const {ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL} = importer.import("general chit chat")
async function relevantHistory(promptModel, session, prompt) {
if(!session) {
return ''
}
// TODO: extract time frame to, from prompt
let history = fs.readdirSync(PROJECT_PATH)
let historyFiles = []
for(let i = 0; i < history.length; i++) {
if(history[i].match('-' + DEFAULT_MODEL + '-' + session + '.json')) {
historyFiles.push(path.basename(history[i]))
}
}
let now = new Date()
let q1 = 'Current date: ' + now.getFullYear() + '-' + String(now.getMonth() + 1).padStart(2, '0')
+ '\nGiven the following chat history:\n'
+ historyFiles.map(f => f.substring(0, 7)).join('\n')
+ '\nGiven the following prompt:\n'
+ prompt + '\nReturn a short range of Year-Month for related dates.'
console.log('User: ' + q1)
let a1 = await promptModel(q1)
console.log('AI: ' + a1)
// TODO: create a range of month out of the response
let relevantFiles = []
for(let i = 0; i < historyFiles.length; i++) {
if(a1.match(historyFiles[i])) {
let convoFile = path.join(PROJECT_PATH, historyFiles[i])
if(fs.existsSync(convoFile)) {
ACTIVE_CONVERSATIONS[convoFile] = JSON.parse(fs.readFileSync(convoFile))
} else {
ACTIVE_CONVERSATIONS[convoFile] = {}
}
relevantFiles.push(historyFiles[i])
}
}
return relevantFiles
}
module.exports = relevantHistory
```javascript
const fs = require('fs');
const path = require('path');
const { ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL } = require('./general-chit-chat');
/**
* Retrieves a range of relevant conversation history files based on the user's prompt.
*
* @param {Object} promptModel - The model used for prompt-based conversations.
* @param {Object} session - The current conversation session.
* @param {string} prompt - The user's prompt to determine relevant history.
* @returns {string[]} An array of conversation history file names relevant to the user's prompt.
*/
async function relevantHistory(promptModel, session, prompt) {
if (!session) {
return [];
}
const historyFiles = await getRelevantHistoryFiles(session);
const relevantFiles = await getRelevantConversations(promptModel, historyFiles, prompt);
return relevantFiles;
}
/**
* Retrieves a list of relevant conversation history files based on the user's prompt.
*
* @param {Object} promptModel - The model used for prompt-based conversations.
* @param {string[]} historyFiles - The list of conversation history file names.
* @param {string} prompt - The user's prompt to determine relevant history.
* @returns {Promise<string[]>} A promise resolving to an array of conversation history file names relevant to the user's prompt.
*/
async function getRelevantConversations(promptModel, historyFiles, prompt) {
const now = new Date();
const q1 = `Current date: ${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}\nGiven the following chat history:\n${historyFiles.join('\n')}\nGiven the following prompt:\n${prompt}\nReturn a short range of Year-Month for related dates.`;
console.log(`User: ${q1}`);
const a1 = await promptModel(q1);
console.log(`AI: ${a1}`);
const relevantFiles = historyFiles.filter((file) => a1.match(file));
await Promise.all(relevantFiles.map((file) => {
const convoFile = path.join(PROJECT_PATH, file);
if (!fs.existsSync(convoFile)) {
return;
}
const content = fs.readFileSync(convoFile, 'utf8');
ACTIVE_CONVERSATIONS[convoFile] = JSON.parse(content);
}));
return relevantFiles;
}
/**
* Retrieves a list of relevant conversation history files based on the user's session.
*
* @param {Object} session - The current conversation session.
* @returns {string[]} A promise resolving to an array of conversation history file names relevant to the user's session.
*/
async function getRelevantHistoryFiles(session) {
const history = fs.readdirSync(PROJECT_PATH);
const historyFiles = history
.filter((file) => file.match(`-${DEFAULT_MODEL}-${session}.json`))
.map(path.basename);
return historyFiles;
}
module.exports = relevantHistory;
```
relevantHistory
Retrieves relevant conversation history files based on a given prompt.
promptModel
: a function to interact with the modelsession
: the session ID (required for file naming)prompt
: the input prompt to determine relevant historyAn array of file names representing the relevant conversation history files.
importer.import('general chit chat')
to be called prior to usage, exporting ACTIVE_CONVERSATIONS
, PROJECT_PATH
, and DEFAULT_MODEL
.fs.readdirSync(PROJECT_PATH)
and fs.readFileSync
which are synchronous file system operations and may not be suitable for large-scale applications.JSON.parse(fs.readFileSync(convoFile))
to load conversation history, which assumes a valid JSON file.