llama | ask llm about code | ask llm to summerize | Search

The code provides a module that exports a function askLlamaAboutNotebooks which asks the LLaMA language model about functions in notebook files within a directory and its subdirectories. The function breaks down the query into chunks of 20 and sends them to the model for response.

Run example

npm run import -- "ask llm about notebooks"

ask llm about notebooks


const path = require('path')
const {listInProject} = importer.import("list project files")
const {askLlamaAboutFunctions} = importer.import("ask llm about functions")

function findNotebooks(dirname) {
    return listInProject(dirname, '{,*,*/,*/*/*,*/*/*/*}*.ipynb')
}

async function askLlamaAboutNotebooks(query) {
  let notebooks = findNotebooks(path.resolve(__dirname, '../'))
  let functions = []
  let descriptions = []

  for(let i = 0; i < notebooks.length; i++) {
    let notebookName = path.basename(notebooks[i]).replace('.ipynb', '')
    let notebookCamel = notebookName.substring(0, 1).toLocaleUpperCase() + notebookName.substring(1)
    let folderName = path.basename(path.dirname(notebooks[i]))
    let folderCamel = folderName.substring(0, 1).toLocaleUpperCase() + folderName.substring(1)

    functions[functions.length] = 'search' + folderCamel.replace(/[^a-z0-9]/gi, '') + notebookCamel.replace(/[^a-z0-9]/gi, '').substring(0, 15)
    descriptions[descriptions.length] = folderCamel + ' ' + notebookCamel

    // TODO: convert this to something like function.length + description.length + 4 < 2048
    if(functions.length == 20) {
      let result = await askLlamaAboutFunctions(query, functions, descriptions)
      if(result) {
        descriptions = [descriptions[functions.indexOf(result)]]
        functions = [result]
      } else {
        descriptions = []
        functions = []
      }
    }
  }
  if(functions.length > 1) {
    let result = await askLlamaAboutFunctions(query, functions, descriptions)
    if(result) functions = [result]
    else functions = []
  }

  return functions
}


module.exports = {
  askLlamaAboutNotebooks,
}

What the code could have been:

const path = require('path');
const { listInProject, askLlamaAboutFunctions } = require('./importer');

/**
 * Finds all Jupyter Notebooks in the specified directory.
 * @param {string} dirname - The directory to search for Notebooks.
 * @returns {string[]} An array of Notebook paths.
 */
async function findNotebooks(dirname) {
  return listInProject(dirname, '{,*,*/,*/*/*,*/*/*/*}*.ipynb');
}

/**
 * Asks the LLM about Notebooks and returns the most relevant function.
 * @param {string} query - The query to ask the LLM.
 * @returns {Promise<string>} The most relevant function.
 */
async function askLlamaAboutNotebooks(query) {
  const notebooks = await findNotebooks(path.resolve(__dirname, '../'));
  const functions = [];
  const descriptions = [];

  // Process each Notebook
  for (const notebook of notebooks) {
    const { name, camelCase, folderName, folderCamel } = getNotebookInfo(notebook);
    functions.push(createFunctionName(folderCamel, camelCase));
    descriptions.push(`${folderCamel} ${camelCase}`);
    if (functions.length === 20) {
      // Ask the LLM about the functions
      const result = await askLlamaAboutFunctions(query, functions, descriptions);
      if (result) {
        functions = [result];
        descriptions = [descriptions[functions.indexOf(result)]];
      } else {
        functions = [];
        descriptions = [];
      }
    }
  }

  // Ask the LLM about the remaining functions
  if (functions.length > 1) {
    const result = await askLlamaAboutFunctions(query, functions, descriptions);
    if (result) functions = [result];
    else functions = [];
  }

  return functions;
}

/**
 * Gets the Notebook info from the path.
 * @param {string} path - The Notebook path.
 * @returns {{ name: string, camelCase: string, folderName: string, folderCamel: string }}.
 */
function getNotebookInfo(path) {
  const name = path.basename(path).replace('.ipynb', '');
  const camelCase = name.substring(0, 1).toLocaleUpperCase() + name.substring(1);
  const folderName = path.basename(path.dirname(path));
  const folderCamel = folderName.substring(0, 1).toLocaleUpperCase() + folderName.substring(1);
  return { name, camelCase, folderName, folderCamel };
}

/**
 * Creates a function name by combining the folder and Notebook names.
 * @param {string} folderCamel - The folder name in camel case.
 * @param {string} camelCase - The notebook name in camel case.
 * @returns {string} The function name.
 */
function createFunctionName(folderCamel, camelCase) {
  return `search${folderCamel.replace(/[^a-z0-9]/gi, '')}${camelCase.replace(/[^a-z0-9]/gi, '').substring(0, 15)}`;
}

module.exports = { askLlamaAboutNotebooks };

Code Breakdown

Dependencies

Functions

findNotebooks(dirname)

askLlamaAboutNotebooks(query)

Module Exports

Notes