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.
npm run import -- "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,
}
/**
* 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;
askLlamaAboutFunctions
An asynchronous function that queries a Large Language Model (LLM) about a list of functions.
query
: The input query to be matched with a function.functions
: An array of function names.descriptions
: An optional array of descriptions for the functions (default: false
).categories
: An optional boolean to specify whether the input is a list of categories (default: false
).llmPrompt
function from the create llm session
module.The askLlamaAboutFunctions
function is exported as a module.