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.
npm run import -- "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
}
// 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
};
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.
The following functions are imported from other modules:
askLlamaAboutFunctions
: Asks Llama about functions and returns a list of functions.askLlamaAboutCategories
: Asks Llama about categories and returns a list of categories.lookupCell
: Looks up a cell in the notebook.cacheCells
: Caches cells in the notebook.functionCache
: Caches RPC functions with LLM descriptions.cellCache
: Caches cells in the notebook.storeAllLlamaFunctions
: Stores all notebook LLM functions.This function performs the following steps:
storeAllLlamaFunctions
to store all notebook LLM functions.askLlamaAboutCategories
with the given query to get a list of categories.cellCache
array and filters out cells that do not contain questions or code.functionCache
and if it has an export. If not, it skips to the next cell.askLlamaAboutFunctions
with the query, functions, and descriptions. If a result is returned, it appends it to the matchingFunction
array.askLlamaAboutFunctions
with the remaining functions and descriptions, if any, and returns the matchingFunction
array.The askLlamaMatchingFunction
is exported as a module, making it available for use in other parts of the application.