llama | | ask llm about categories | Search

This code defines an asynchronous function askLlamaMatchingFunction that searches for matching functions based on a given query by utilizing imported functions and caching cell and RPC function data. The function stores, filters, and queries LLM functions to return an array of matching functions.

Run example

npm run import -- "search llama"

search llama

const {askLlamaAboutFunctions} = importer.import("ask llm about functions")
const {askLlamaAboutCategories} = importer.import("ask llm about categories")
const lookupCell = importer.lookupCell
const { cacheCells } = importer.import("cache notebook")
const { functionCache } = importer.import("cache rpc functions with llm descriptions")
const { cellCache } = importer.import("cell cache")
const {storeAllLlamaFunctions} = importer.import("store all notebook llm functions")

async function askLlamaMatchingFunction(query) {
  await storeAllLlamaFunctions()

  let filterCategories = await askLlamaAboutCategories(query)
  if(filterCategories.length > 0) {
    console.log(filterCategories)
  }

  let matchingFunction = []
  let functions = []
  let descriptions = []

  for(let i = 0; i < cellCache.length; i++) {
    let cell = cellCache[i]
    if(!cell[2].questions || !cell[2].questions[0]) continue
    let code = lookupCell(cell[1], cacheCells)

    if(!functionCache[cell[1]].exports) continue
    if(!functionCache[cell[1]].exports[0]) continue

    // limit number of calls based on filter
    if(filterCategories.length > 0 && !filterCategories.includes(code.filename)) {
      continue
    }

    // finally append to array that will be used to find the function
    functions[functions.length] = functionCache[cell[1]].exports[0]
    descriptions[descriptions.length] = functionCache[cell[1]].summary

    if(functions.length == 20) {
        let result = await askLlamaAboutFunctions(query, functions, descriptions)
        descriptions = []
        functions = []
        if(result)
          matchingFunction = matchingFunction.concat(result)
    }
  }
  if(functions.length > 0) {
      let result = await askLlamaAboutFunctions(query, functions, descriptions)
      descriptions = []
      functions = []
      if(result)
        matchingFunction = matchingFunction.concat(result)
  }
  return matchingFunction
}

module.exports = {
  askLlamaMatchingFunction
}

What the code could have been:

// Import necessary modules and functions
const { askLlamaAboutFunctions, askLlamaAboutCategories } = require('./ask-llm-about-functions');
const { lookupCell, cacheCells } = require('./lookup-cell');
const { functionCache, cellCache } = require('./cache');
const { storeAllLlamaFunctions } = require('./store-all-notebook-llm-functions');

/**
 * Asks the LLaMA about functions matching the given query
 * @param {string} query - The query to match LLaMA functions against
 * @returns {Promise<Array>} An array of functions that match the query
 */
async function askLlamaMatchingFunction(query) {
  // Store all LLaMA functions before searching
  await storeAllLlamaFunctions();

  try {
    // Get the filtered categories from the LLaMA
    const filterCategories = await askLlamaAboutCategories(query);
    // Print the filtered categories for debugging purposes
    console.log(filterCategories);

    // Initialize arrays to store matching functions and descriptions
    let matchingFunctions = [];
    let functions = [];
    let descriptions = [];

    // Iterate over each cell in the cache
    for (const cell of cellCache) {
      // Skip cells without questions
      if (!cell[2].questions ||!cell[2].questions[0]) continue;

      // Get the code from the cell
      const code = lookupCell(cell[1], cacheCells);

      // Skip cells without exports
      if (!functionCache[cell[1]].exports) continue;

      // Limit the number of calls based on the filter
      if (filterCategories.length > 0 &&!filterCategories.includes(code.filename)) {
        continue;
      }

      // Append the function and description to the arrays
      functions.push(functionCache[cell[1]].exports[0]);
      descriptions.push(functionCache[cell[1]].summary);

      // If the array is full, ask the LLaMA about the functions and descriptions
      if (functions.length === 20) {
        const result = await askLlamaAboutFunctions(query, functions, descriptions);
        // Clear the arrays for the next batch
        functions = [];
        descriptions = [];
        // Concatenate the result to the matching functions array
        matchingFunctions = matchingFunctions.concat(result);
      }
    }

    // If there are remaining functions, ask the LLaMA about them
    if (functions.length > 0) {
      const result = await askLlamaAboutFunctions(query, functions, descriptions);
      // Clear the arrays
      functions = [];
      descriptions = [];
      // Concatenate the result to the matching functions array
      matchingFunctions = matchingFunctions.concat(result);
    }

    // Return the matching functions
    return matchingFunctions;
  } catch (error) {
    // Log any errors that occur during the process
    console.error(error);
    return [];
  }
}

module.exports = {
  askLlamaMatchingFunction
};

Overview

This code defines an asynchronous function askLlamaMatchingFunction that searches for matching functions based on a given query. It utilizes various imported functions to perform the search and returns an array of matching functions.

Imported Functions

The following functions are imported from other modules:

askLlamaMatchingFunction

This function performs the following steps:

  1. Calls storeAllLlamaFunctions to store all notebook LLM functions.
  2. Calls askLlamaAboutCategories with the given query to get a list of categories.
  3. If categories are found, it logs them to the console.
  4. Iterates through the cellCache array and filters out cells that do not contain questions or code.
  5. For each cell, it checks if the code is in the functionCache and if it has an export. If not, it skips to the next cell.
  6. It limits the number of calls based on the filter categories.
  7. It appends the functions and descriptions to separate arrays.
  8. If the arrays have 20 or more elements, it calls askLlamaAboutFunctions with the query, functions, and descriptions. If a result is returned, it appends it to the matchingFunction array.
  9. Finally, it calls askLlamaAboutFunctions with the remaining functions and descriptions, if any, and returns the matchingFunction array.

Exports

The askLlamaMatchingFunction is exported as a module, making it available for use in other parts of the application.