llama | ask llm for a shorter list of categories | ask llm about code | Search

The askLlamaAboutFunctions function is an asynchronous query that matches a given input with a function listed in an array, providing an optional description and categorization. It constructs a query string, sends it to a Large Language Model, and returns the matched function name.

Run example

npm run import -- "ask llm about functions"

ask llm about functions


async function askLlamaAboutFunctions(query, functions, descriptions, categories = false) {
  const {llmPrompt} = await importer.import("create llm session")

  let q1 = "Given a list of " + (categories ? 'categories' : "functions") + ":\n";
  for(let i = 0; i < functions.length; i++) {
      if(descriptions[i]) {
          q1 += functions[i] + ' - ' + descriptions[i] + '\n'
      } else {
          q1 += functions[i] + '\n'
      }
  }
  q1 += 'which one most closely matches the query \"' + query + '\"?'
  console.log("User: " + q1);
  const a1 = await llmPrompt(q1);
  console.log("AI: " + a1);
  // TODO: parse function name
  let result = a1.trim().split(/["`*'\n]/gi).filter(x => functions.indexOf(x) > -1)[0]
  return result
}

module.exports = {
  askLlamaAboutFunctions,
}

What the code could have been:

/**
 * Asks Llama about functions or categories to determine which one most closely matches the given query.
 *
 * @param {string} query - The query to match.
 * @param {string[]} functions - A list of functions or categories to consider.
 * @param {string[]} [descriptions] - A list of descriptions for the functions or categories.
 * @param {boolean} [categories=false] - Flag to indicate if the input is categories.
 * @returns {string} The function or category that most closely matches the query.
 */
async function askLlamaAboutFunctions(query, functions, descriptions = [], categories = false) {
  const {
    llmPrompt,
  } = await import('create-llm-session');

  // Create the prompt with the list of functions or categories
  const prompt = functions
   .map((func, index) => {
      return categories
       ? `${func} - ${descriptions[index] || ''}`
        : func;
    })
   .join('\n');

  // Add the final question to the prompt
  const finalPrompt = `Given the list of ${categories? 'categories' : 'functions'}:\n${prompt}\nWhich one most closely matches the query "${query}"?`;

  console.log('User:', finalPrompt);

  // Get the AI response
  const aiResponse = await llmPrompt(finalPrompt);
  console.log('AI:', aiResponse);

  // Extract the matched function or category from the AI response
  const result = functions.find(func => aiResponse.toLowerCase().includes(func.toLowerCase()));

  return result;
}

export default askLlamaAboutFunctions;

Code Breakdown

Function: askLlamaAboutFunctions

Purpose

An asynchronous function that queries a Large Language Model (LLM) about a list of functions.

Parameters

Steps

  1. Imports the llmPrompt function from the create llm session module.
  2. Constructs a query string by listing the functions and their descriptions (if provided).
  3. Sends the query to the LLM and receives a response.
  4. Parses the response to find the function name that matches the query.
  5. Returns the matched function name.

Export

The askLlamaAboutFunctions function is exported as a module.