llm chat | relevant llm history | relevant history timestamps | Search

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.

Run example

npm run import -- "relevant chat keywords"

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

What the code could have been:

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;

Function: relevantKeywords

Description:

This function searches through loaded user history conversations to find relevant keywords for a given prompt.

Parameters:

Return Value:

An array of keywords that match the prompt.

Code Breakdown:

  1. Load all loaded user history conversations by filtering ACTIVE_CONVERSATIONS based on the current session.
  2. For each conversation, extract timestamps and load the corresponding messages.
  3. Extract keywords from each message and filter out extraneous characters.
  4. Combine the extracted keywords into a single array without duplicates.
  5. Create a new prompt (q2) that includes the extracted keywords and the original prompt.
  6. Use the promptModel to retrieve relevant keywords for the new prompt (a2).
  7. Compare a2 with the extracted keywords and return an array of matching keywords.

Import Requirements: