llm chat | relevant history timestamps | handle conversation | Search

This code defines a set of functions and constants to facilitate interacting with an API, including generating a description of the API functions and asking the user to match a prompt with an API function. The code exports four values, allowing other parts of the program to access and use the API functions and their corresponding exports and descriptions.

Run example

npm run import -- "classify llm prompt"

classify llm prompt

const getExports = importer.import("get exports from source")


const API_FUNCTIONS = [
  'load message history',
  'general chit chat',
  'brians resume',
  'file system access',
  'access web information',
  'stable diffusion request',
  'request ollama vision',
  //'ask another llm for help',
  'add conversation context',
  'llm save memories'
  // 'switch llm models'
]

const API_EXPORTS = API_FUNCTIONS.map(func => getExports(importer.interpret(func).code)[0])

const API_DESCRIPTION = API_FUNCTIONS
  .map(func => importer.interpret(func))
  .filter((code, i) => API_EXPORTS[i])
  .map((code, i) => API_EXPORTS[i] + ' - ' + code.markdown.join('').trim()
    .replaceAll(/^#.*?\n/gi, '')
    .replaceAll(code.questions, '')
    .replaceAll(/\s*[\n]+[\s\r\n]*/gi, '-'))
  .join('\n')

async function askLlamaMatchingFunction(promptModel, prompt, image) {

  let q1 = 'Given the following functions:\n'
    + API_DESCRIPTION + '\nDo any of these functions relate to this prompt:\n'
    + (image ? 'An image has been provided as a part of the input.' : '')
    + prompt + '\nOnly return the function names in order of relevance.'
  console.log('User: ' + q1)
  let a1 = await promptModel(q1)
  console.log('AI: ' + a1)

  let matchingFunctions = a1.split(/\s*\n\s*|\s*,\s*/gi)
    .map((matchStr) => {
      for (let i = 0; i < API_FUNCTIONS.length; i++) {
        if (!API_EXPORTS[i]) {
          continue
        }
        if (API_EXPORTS[i].includes(matchStr) || matchStr.match(API_EXPORTS[i])) {
          return ({
            importStr: API_FUNCTIONS[i],
            exportStr: API_EXPORTS[i]
          })
        }
      }
    })
    .filter(func => a1.length > 0 && func)
    .map(func => func.importStr)

  return matchingFunctions
}

module.exports = {
  askLlamaMatchingFunction,
  API_FUNCTIONS,
  API_EXPORTS,
  API_DESCRIPTION,
}

What the code could have been:

const API_FUNCTIONS = [
  'Load Message History',
  'General Chit Chat',
  'Brian\'s Resume',
  'File System Access',
  'Access Web Information',
  'Stable Diffusion Request',
  'Request Ollama Vision',
  // 'Ask Another LLM for Help',
  'Add Conversation Context',
  'LLM Save Memories',
  // 'Switch LLM Models'
]

Breakdown of the Code

Section 1: Importing and Defining Constants

Section 2: Defining the askLlamaMatchingFunction Function

Section 3: Exports