The askLlamaAboutCode
function takes a string of code, uses an LLM to provide a short breakdown, and logs the prompt and response. It limits input code to 2048 characters and trims the LLM response.
npm run import -- "ask llm about code"
async function askLlamaAboutCode(code) {
const {llmPrompt} = await importer.import("create llm session")
const q2 = "Give me a short breakdown of this code:\n" + code.substr(0, 2048) + "\nDocumentation only, discard any friendly remarks.";
console.log("User: " + q2);
const a2 = await llmPrompt(q2);
console.log("AI: " + a2);
return a2.trim()
}
module.exports = {
askLlamaAboutCode,
}
const { createLlmSession } = require('./importer');
const logger = console;
/**
* Ask LLaMA about the provided code snippet.
*
* @param {string} code - The code snippet to be analyzed.
* @returns {Promise} A breakdown of the code snippet.
*/
async function askLlamaAboutCode(code) {
// Create a new LLaMA session
const llmSession = await createLlmSession();
// Define the prompt with a maximum code length of 2048 characters
const maxCodeLength = 2048;
const prompt = `Give me a short breakdown of this code:\n${code.substring(0, maxCodeLength)}\nDocumentation only, discard any friendly remarks.`;
// Log the user's query for debugging purposes
logger.log('User:', prompt);
// Ask LLaMA for a breakdown of the code
const response = await llmSession.llmPrompt(prompt);
// Log the AI's response for debugging purposes
logger.log('AI:', response);
// Return the AI's response with trailing whitespace removed
return response.trim();
}
module.exports = {
askLlamaAboutCode,
};
This code defines a function askLlamaAboutCode
that takes a string of code as input and uses a language model (LLM) to provide a short breakdown of the code.
importer
module.q2
by concatenating a default prompt with the first 2048 characters of the input code.llmPrompt
function and logs the response with an "AI:" label.importer
module is not a standard Node.js module, so it's likely a custom module that exports an import
function to load external modules.llmPrompt
function is also not a standard Node.js module, so it's likely a custom function that interacts with an LLM API or service.substr(0, 2048)
call limits the input code to 2048 characters, which may be a limitation or a precaution to prevent overwhelming the LLM with excessive code.trim()
call removes any leading or trailing whitespace from the LLM response, which may be useful for cleaning up the output.