Skip to content

Instantly share code, notes, and snippets.

@raxityo
Created July 5, 2023 18:57
Show Gist options
  • Save raxityo/30bfc6cd0b42a26cd5d87d6d05dc6ad8 to your computer and use it in GitHub Desktop.
Save raxityo/30bfc6cd0b42a26cd5d87d6d05dc6ad8 to your computer and use it in GitHub Desktop.

Revisions

  1. raxityo created this gist Jul 5, 2023.
    50 changes: 50 additions & 0 deletions generate_math_equations.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    const template = {
    input: [
    {
    role: "system",
    content:
    "You are a skilled mathematician capable of solving math equations. Walk through the answer. Solve the equations with only the numerical answer rounded to 4 decimal places. Include the answer in square brackets []. Do not include comma in the answer."
    },
    {
    role: "user",
    content: "<TO_BE_FILLED>"
    }
    ],
    ideal: "<TO_BE_FILLED>"
    };
    const { appendFileSync, writeFileSync } = require("fs");
    const { toWords } = require("number-to-words");
    const signMap = {
    "-": "minus",
    "+": "plus",
    "*": "multiplied by",
    "/": "divided by"
    };
    let lines = "";
    const addLine = (content, ideal) => {
    const line = structuredClone(template);
    line.input[1].content = content;
    // Use ["[1234.0000]", "[1234]"] for the ideal value if applicable.
    if (`${ideal}`.includes(".0000")) {
    line.ideal = [`[${ideal}]`, `[${ideal}]`.replace(".0000", "")];
    } else {
    line.ideal = `[${ideal}]`;
    }
    lines += JSON.stringify(line);
    };
    for (i = 0; i < 100; i++) {
    const first = Math.round(Math.random() * 100);
    const second = Math.round(Math.random() * 1000);
    const third = Math.round(Math.random() * 10000);
    const firstSign = ["-", "+", "*", "/"][Math.floor(Math.random() * 4)];
    const secondSign = ["-", "+", "*", "/"][Math.floor(Math.random() * 4)];
    const mathEq = `${first}${firstSign}${second}${secondSign}${third}`;
    const mathEqWords = `${toWords(first)} ${signMap[firstSign]} ${toWords(
    second
    )} ${signMap[secondSign]} ${toWords(third)}`;
    const result = eval(mathEq).toFixed(4);
    addLine(mathEqWords, result);
    lines += "\n";
    }

    writeFileSync("math_equations.jsonl", lines);