kyang6 / llmparser

Classify and extract structured data with LLMs
https://llmparser.com
MIT License
407 stars 23 forks source link

Is there an example to parse coding? #6

Open cliren opened 1 year ago

cliren commented 1 year ago

Feature Request

Is your feature request related to a problem? Please describe.

Would like to extract JS function for the following prompt.

function addNumbers(num1, num2) {
  return num1 + num2;
}

Describe the solution you'd like

Describe alternatives you've considered

Tried creating a JS template:

import { TemplateBase } from './base';

export class JavaScriptTemplate extends TemplateBase {
  protected template: string;

  constructor(template: string) {
    super();
    this.template = template;
  }

  public numChars(): number {
    return this.template.length;
  }

  public numTokens(): number {
    return this.template.split(/\s+/).length;
  }

  public render(replacements: Record<string, string>): string {
    let result = '';
    const lines = this.template.split('\n');
    for (const line of lines) {
      if (line.startsWith('```js')) {
        result += '```js\n';
        let code = '';
        while (lines.length > 0 && !lines[0].startsWith('```')) {
          code += `${lines.shift() ?? ''}\n`;  // Use template literal
        }
        result += code;
        result += '```\n';
      } else {
        result += this.replacePlaceholders(line, replacements) + '\n';
      }
    }
    return result;
  }
}

Using it like this:

export const JS_PROMPT = new JavaScriptTemplate(`
You are a JavaScript parser. You will be given JavaScript code and you must return an abstract syntax tree (AST) representing that code.

Here is an example AST:
{
  "type": "Program",
  "body": [
    {
      "type": "VariableDeclaration",
      "declarations": [
        {
          "type": "VariableDeclarator",
          "id": {
            "type": "Identifier",
            "name": "x"
          },
          "init": {
            "type": "Literal",
            "value": 10
          }
        }
      ],
      "kind": "let"
    }
  ]
}

The AST must follow the ESTree spec (https://github.com/estree/estree)

Here is the JavaScript code to parse`);
kyang6 commented 1 year ago

Interesting use case, what does it extract today and what is missing for it to really work?

cliren commented 1 year ago

With basic setup, it returns null/undefined.