llm scaffold | make code file with llm | add llm code cell | Search

This Node.js script uses a Large Language Model (LLM) to generate code by constructing prompts, sending them to the LLM, and extracting code blocks from the response. The script performs tasks such as project path resolution, Git pull, and code interpretation to ensure the code is up-to-date and accurate before sending the prompt to the LLM.

Run example

npm run import -- "edit code cell in notebook"

edit code cell in notebook

const fs = require('fs')
const path = require('path')
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
const {spawnSync} = require('child_process')
const updateCode = importer.import("update code cell")

async function generateCell(prompt, codeCell, github) {
  const llmCode = await importer.import("llm code")

  if(!github) {
    console.error('Project not specified.')
    return
  }

  // TODO: local pull
  if(!fs.existsSync(github)
    && fs.existsSync(path.join(PROFILE_PATH, github))) {
      github = path.join(PROFILE_PATH, github)
  }

  if(github.includes('://')
    && fs.existsSync(path.basename(github).replace('.git', ''))
  ) {
    github = path.join(PROFILE_PATH, path.basename(github).replace('.git', ''))
  }

  if(!fs.existsSync(github)) {
    console.error('Project not found.')
    return
  }

  let baseName = path.basename(github)

  await spawnSync('git', ['pull'], {
    cwd: github,
    timeout: 3000,
    stdio: ['pipe', 'pipe', 'pipe']
  })

  if(typeof codeCell == 'string') {
    codeCell = importer.interpret(codeCell)
  }

  if(!codeCell) {
    console.error('Code cell not found.')
    return
  }

  // TODO: compare existing project files
  let q1 = 'Give the project files:\n' + fs.readdirSync(github).join('\n')
    + 'And the existing code for the file ' + codeCell.id + ':\n' 
    + codeCell.code
    + '\nFulfill the following instructions on the code file, only return the block of new code:'
    + '\n' + prompt
    + '\n' + 'Response with only the block of code and no summary or reasoning.'

  // TODO: create new files
  console.log('User: ' + q1)
  let a1 = await llmCode(q1)
  console.log('AI: ' + a1)

  // try to extract code blocks
  let code = a1.matchAll(/```(bash|javascript|code)*\n[\s\S]*?\n```/gi)

  // extract code blocks from response
  let codeBlocks = ''
  for(let match of code) {
    codeBlocks += match[0].replace(/^```(bash|javascript|code)*\n|\n```$/gi, '') + '\n'
  }
  if(!codeBlocks) {
    console.log('Error, couldn\'t find code in:' + a1)
    return
  }

  
  // TODO: populate code files with goals
  //fs.writeFileSync(path.join(github, codeFile), codeBlocks)
  updateCode(codeCell, codeBlocks)

  // TODO: populate goals with actual code

}

module.exports = generateCell

What the code could have been:

'use strict';

const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
const importer = require('./importer'); // assumes importer is in a separate file
const updateCode = importer.import('update code cell');
const LLMCode = importer.import('llm code');

const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';

async function generateCell(prompt, codeCell, github) {
  if (!github) {
    throw new Error('Project not specified.');
  }

  let githubPath = getProjectPath(github);
  if (!fs.existsSync(githubPath)) {
    throw new Error('Project not found.');
  }

  await pullProject(githubPath);

  const llmCode = new LLMCode();
  if (typeof codeCell ==='string') {
    codeCell = await importer.interpret(codeCell);
  }
  if (!codeCell) {
    throw new Error('Code cell not found.');
  }

  const q1 = createQ1(githubPath, codeCell, prompt);
  const a1 = await llmCode.generateCode(q1);

  const codeBlocks = extractCodeBlocks(a1);
  if (!codeBlocks) {
    throw new Error('Error, couldn\'t find code in response.');
  }

  await updateCode(codeCell, codeBlocks);
}

async function getProjectPath(github) {
  if (fs.existsSync(path.join(PROFILE_PATH, github)) &&!github.includes('://')) {
    return path.join(PROFILE_PATH, github);
  }
  return github;
}

async function pullProject(githubPath) {
  try {
    await spawnSync('git', ['pull'], {
      cwd: githubPath,
      timeout: 3000,
      stdio: ['pipe', 'pipe', 'pipe'],
    });
  } catch (error) {
    console.error('Error pulling project:', error);
    throw new Error('Error pulling project.');
  }
}

function createQ1(githubPath, codeCell, prompt) {
  const projectFiles = fs.readdirSync(githubPath).join('\n');
  const existingCode = codeCell.code;
  return `Give the project files:\n${projectFiles}And the existing code for the file ${codeCell.id}:\n${existingCode}\nFulfill the following instructions on the code file, only return the block of new code:\n${prompt}\nResponse with only the block of code and no summary or reasoning.`;
}

function extractCodeBlocks(response) {
  const regex = /\`\`\`(bash|javascript|code)*\n([\s\S]*?)\n\`\`\`/gi;
  const matches = response.matchAll(regex);
  if (!matches) {
    return null;
  }
  return matches.reduce((acc, match) => acc + match[2].trim() + '\n', '');
}

module.exports = generateCell;

Breakdown of the Code

This Node.js script appears to be a part of a larger project that utilizes a Large Language Model (LLM) to generate code for a given project. Here's a brief overview of the code:

Dependencies and Imports

The script begins by importing the necessary dependencies:

Main Function: generateCell

The generateCell function is the main entry point of the script. It takes three parameters:

The function performs the following tasks:

  1. Project Path Resolution: It attempts to resolve the project path by checking various environment variables and file system checks.
  2. Git Pull: If the project path is a Git repository, it performs a git pull operation to ensure the code is up-to-date.
  3. Code Interpretation: If the codeCell parameter is a string, it uses the importer.interpret function to parse and interpret the code.
  4. Generate Code: It constructs a prompt string for the LLM, which includes information about the project files, existing code for the target file, and the instructions to follow.
  5. LLM Response: It sends the prompt to the LLM (using the llmCode function) and extracts the code blocks from the response.
  6. Code Extraction: It attempts to extract code blocks from the LLM's response and returns them as a string.

Notable Aspects