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.
A NodeJS script to generate math equations to submit evals for ChatGPT. Used by https://github.com/openai/evals/pull/1269.
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment