79 lines
4.9 KiB
TypeScript
79 lines
4.9 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { tmpdir } from "node:os";
|
|
import { SUPPORTED } from "../src/benchmarks/ifeval-checks";
|
|
import { SOURCES } from "../src/benchmarks/datasets";
|
|
|
|
async function run(id: string, kwargs: object, text: string | null) {
|
|
const w = mkdtempSync(join(tmpdir(), "if-"));
|
|
if (text != null) writeFileSync(join(w, "response.txt"), text);
|
|
// Real kwargs list every key with null for the unused ones, so the task is built the way the importer builds it.
|
|
const full = { num_highlights: null, relation: null, num_words: null, ...kwargs };
|
|
const task = SOURCES.ifeval.toTask({ key: "t", prompt: "p", instruction_id_list: [id], kwargs: [full] });
|
|
writeFileSync(join(w, ".g.py"), task.grade);
|
|
const p = Bun.spawn(["python3", "-I", ".g.py"], { cwd: w, stdout: "pipe", stderr: "pipe" });
|
|
return p.exited;
|
|
}
|
|
|
|
// [instruction id, kwargs, a response that satisfies it, a response that breaks it]
|
|
const cases: [string, object, string, string][] = [
|
|
["keywords:existence", { keywords: ["moon", "star"] }, "The Moon and a star.", "Only the moon."],
|
|
["keywords:frequency", { keyword: "cat", frequency: 2, relation: "at least" }, "cat cat", "cat"],
|
|
["keywords:frequency", { keyword: "cat", frequency: 2, relation: "less than" }, "cat", "cat cat"],
|
|
["keywords:forbidden_words", { forbidden_words: ["bad"] }, "all good", "so bad"],
|
|
["keywords:letter_frequency", { letter: "z", let_frequency: 2, let_relation: "at least" }, "zoo zebra", "zoo"],
|
|
["length_constraints:number_words", { num_words: 5, relation: "at least" }, "one two three four five", "one two"],
|
|
["length_constraints:number_words", { num_words: 5, relation: "less than" }, "one two", "one two three four five six"],
|
|
["length_constraints:number_sentences", { num_sentences: 2, relation: "at least" }, "One. Two.", "One."],
|
|
["length_constraints:number_paragraphs", { num_paragraphs: 2 }, "a\n***\nb", "a\n***\nb\n***\nc"],
|
|
["length_constraints:nth_paragraph_first_word", { num_paragraphs: 2, nth_paragraph: 2, first_word: "second" }, "first one\n\nSecond one", "first one\n\nthird one"],
|
|
["detectable_content:number_placeholders", { num_placeholders: 2 }, "[a] and [b]", "[a]"],
|
|
["detectable_content:postscript", { postscript_marker: "P.S." }, "Hi\nP.S. bye", "Hi bye"],
|
|
["detectable_format:number_bullet_lists", { num_bullets: 2 }, "* a\n* b", "* a\n* b\n* c"],
|
|
["detectable_format:constrained_response", {}, "My answer is yes.", "Yes"],
|
|
["detectable_format:number_highlighted_sections", { num_highlights: 2 }, "*a* and *b*", "*a*"],
|
|
["detectable_format:multiple_sections", { section_spliter: "Section", num_sections: 2 }, "Section 1\nx\nSection 2\ny", "Section 1\nx"],
|
|
["detectable_format:json_format", {}, "```json\n{\"a\": 1}\n```", "not json"],
|
|
["detectable_format:title", {}, "<<A title>>\ntext", "no title"],
|
|
["combination:two_responses", {}, "one******two", "one"],
|
|
["combination:repeat_prompt", { prompt_to_repeat: "Say hi" }, "Say hi\nthen more", "then more"],
|
|
["startend:end_checker", { end_phrase: "Is there anything else?" }, "Done. Is there anything else?", "Done."],
|
|
["startend:quotation", {}, "\"wrapped\"", "plain"],
|
|
["change_case:capital_word_frequency", { capital_frequency: 2, capital_relation: "at least" }, "THE CAT sat", "the cat"],
|
|
["change_case:english_capital", {}, "ALL CAPS", "Not all"],
|
|
["change_case:english_lowercase", {}, "all lower", "Not all"],
|
|
["punctuation:no_comma", {}, "no commas here", "one, two"],
|
|
];
|
|
|
|
for (const [id, kwargs, good, bad] of cases) {
|
|
test(`ifeval ${id}: ${JSON.stringify(kwargs)}`, async () => {
|
|
expect(await run(id, kwargs, good)).toBe(0);
|
|
expect(await run(id, kwargs, bad)).not.toBe(0);
|
|
expect(await run(id, kwargs, null)).not.toBe(0);
|
|
});
|
|
}
|
|
|
|
test("every supported instruction has a test case", () => {
|
|
expect(new Set(cases.map((c) => c[0]))).toEqual(SUPPORTED);
|
|
});
|
|
|
|
// A grader that crashes would fail every answer, so run each imported task on a sample response and expect a verdict.
|
|
import { existsSync, readFileSync } from "node:fs";
|
|
const ds = new URL("../data/datasets/ifeval.json", import.meta.url).pathname;
|
|
test.skipIf(!existsSync(ds))("every imported IFEval grader gives a verdict, never a crash", async () => {
|
|
const rows = JSON.parse(readFileSync(ds, "utf8")) as any[];
|
|
expect(rows.length).toBe(510);
|
|
const crashed: string[] = [];
|
|
await Promise.all(rows.map(async (r) => {
|
|
const task = SOURCES.ifeval.toTask(r);
|
|
const w = mkdtempSync(join(tmpdir(), "if-"));
|
|
writeFileSync(join(w, "response.txt"), "A short sample response.");
|
|
writeFileSync(join(w, ".g.py"), task.grade);
|
|
const p = Bun.spawn(["python3", "-I", ".g.py"], { cwd: w, stdout: "pipe", stderr: "pipe" });
|
|
const out = await new Response(p.stdout).text();
|
|
if (!/^(ok|violated: )/.test(out)) crashed.push(`${r.key}: ${out.slice(0, 80)}`);
|
|
await p.exited;
|
|
}));
|
|
expect(crashed).toEqual([]);
|
|
}, 120_000);
|