The relevantKeywords function searches through a user's past conversations to find relevant keywords for a given prompt, using a specified model to retrieve and match keywords. It returns an array of keywords that match the prompt, eliminating duplicates and extraneous characters from extracted keywords.
npm run import -- "relevant chat keywords"const path = require('path')
const fs = require('fs')
const {ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL} = importer.import("general chit chat")
async function relevantKeywords(promptModel, session, prompt) {
// TODO: search all loaded user histories for keywords
let keywords = []
let loadedConversations = Object.keys(ACTIVE_CONVERSATIONS)
.filter(key => key.match('-' + DEFAULT_MODEL + '-' + session + '.json'))
loadedConversations.sort((a, b) => b - a)
for(let i = 0; i < loadedConversations.length; i++) {
let conversation = ACTIVE_CONVERSATIONS[loadedConversations[i]]
let timestamps = Object.keys(conversation).filter(k => k != 'summaries' && k != 'memories')
timestamps.sort((a, b) => b - a)
for(let j = 0; j < timestamps.length; j++) {
let message = conversation[timestamps[j]]
let newKeywords = message.keywords
newKeywords = newKeywords.replace(/.*?:\s*\n\s*/gi, '')
newKeywords = newKeywords.replaceAll('*', '').replaceAll('# ', '').replaceAll('#', '').replaceAll(/[0-9]+\.\s*/gi, '')
newKeywords = newKeywords.split(/\s*\n\s*|\s*,\s*/gi)
keywords = keywords.concat(newKeywords.map(k => k.trim()))
}
}
keywords = keywords.filter((k, i, arr) => k && arr.indexOf(k) == i)
let q2 = 'Given the following keywords:\n'
+ keywords.join('\n')
+ '\nGiven the following prompt:\n'
+ prompt + '\nReturn only a few keywords related to the prompt.'
console.log('User: ' + q2)
let a2 = await promptModel(q2)
console.log('AI: ' + a2)
let matching = []
for(let i = 0; i < keywords.length; i++) {
if(a2.match(keywords[i])) {
matching.push(keywords[i])
}
}
return matching
}
module.exports = relevantKeywords
const path = require('path');
const fs = require('fs');
const { ACTIVE_CONVERSATIONS, PROJECT_PATH, DEFAULT_MODEL } = require('./general-chit-chats');
const logger = console;
async function relevantKeywords(promptModel, session, prompt) {
try {
const loadedConversations = Object.keys(ACTIVE_CONVERSATIONS)
.filter((key) => key.match(`-${DEFAULT_MODEL}-${session}.json`))
.sort((a, b) => b.localeCompare(a));
const loadedKeywords = loadedConversations.reduce((keywords, conversationFile) => {
const conversation = ACTIVE_CONVERSATIONS[conversationFile];
return keywords.concat(
Object.keys(conversation)
.filter((key) => key!=='summaries' && key!=='memories')
.map((timestamp) => conversation[timestamp].keywords)
.flat()
.map((keyword) => keyword.trim())
.filter((k, i, arr) => k && arr.indexOf(k) === i)
);
}, []);
const question2 = `Given the following keywords:\n${loadedKeywords.join('\n')}\n\nGiven the following prompt:\n${prompt}\nReturn only a few keywords related to the prompt.`
const response2 = await promptModel(question2);
logger.log(`User: ${question2}`);
logger.log(`AI: ${response2}`);
const matchingKeywords = loadedKeywords.filter((keyword) => response2.match(keyword));
return matchingKeywords;
} catch (error) {
logger.error('Error in relevantKeywords:', error);
throw error;
}
}
module.exports = relevantKeywords;relevantKeywordsThis function searches through loaded user history conversations to find relevant keywords for a given prompt.
promptModel: The model used to retrieve relevant keywords.session: The session identifier.prompt: The prompt for which to find relevant keywords.An array of keywords that match the prompt.
ACTIVE_CONVERSATIONS based on the current session.q2) that includes the extracted keywords and the original prompt.promptModel to retrieve relevant keywords for the new prompt (a2).a2 with the extracted keywords and return an array of matching keywords.pathfsimporter (with general chit chat module)DEFAULT_MODEL, PROJECT_PATH, and ACTIVE_CONVERSATIONS variables from general chit chat module.