1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#!/usr/bin/env node
// Reads every review of reviews/ with the decision model of agent.yaml (or of
// the config file given as the first argument) and prints one line per review,
// worst first. Each answer is checked against expected.json: a "✗" marks a
// sentiment, topic, competitor or recommendation the model got wrong (a
// recommendation expected as null is too ambiguous to check). No chat model is
// involved: each review is one `mm -config <file> -p` call, whose stdout is the
// /v1/systemone JSON.
//
// node analyse-reviews.js # agent.yaml
// node analyse-reviews.js omni.yaml # another model, same questions
//
// MM_BIN names the mm binary, "mm" by default.
const { execFileSync } = require("child_process");
const fs = require("fs");
const path = require("path");
const here = __dirname;
const config = path.resolve(process.argv[2] || path.join(here, "agent.yaml"));
const expected = JSON.parse(fs.readFileSync(path.join(here, "expected.json"), "utf8"));
const folder = path.join(here, "reviews");
// The sentiment score runs from 0 (very negative) to 4 (very positive); the
// expected answers name a band, which is easier to agree on than a level.
function band(score) {
if (score < 1.5) return "negative";
if (score <= 2.5) return "mixed";
return "positive";
}
const mark = (ok) => (ok ? " " : "✗");
const rows = [];
let right = 0;
let asked = 0;
for (const file of fs.readdirSync(folder).filter((f) => f.endsWith(".txt")).sort()) {
const review = fs.readFileSync(path.join(folder, file), "utf8").trim();
const started = Date.now();
const out = execFileSync(process.env.MM_BIN || "mm", ["-config", config, "-p", review], {
encoding: "utf8",
stdio: ["ignore", "pipe", "inherit"],
});
const ms = Date.now() - started;
const a = JSON.parse(out).answers;
const want = expected[file];
const competitor = a.competitor.noul >= 0.5;
const recommend = a.recommend.noul >= 0.5;
const checks = {
sentiment: want.sentiment.includes(band(a.sentiment.score)),
topic: want.topic.includes(a.topic.choice),
competitor: competitor === want.competitor,
};
if (want.recommend !== null) checks.recommend = recommend === want.recommend;
for (const ok of Object.values(checks)) {
asked++;
if (ok) right++;
}
rows.push({
file,
score: a.sentiment.score,
sentiment: `${a.sentiment.score.toFixed(2)} ${a.sentiment.legend[String(Math.round(a.sentiment.score))]}${mark(checks.sentiment)}`,
topic: `${a.topic.choice} (${a.topic.confidence.toFixed(2)})${mark(checks.topic)}`,
competitor: `${competitor ? "yes" : "no"}${mark(checks.competitor)}`,
recommend: `${recommend ? "yes" : "no"}${checks.recommend === false ? "✗" : " "}`,
ms,
});
}
rows.sort((x, y) => x.score - y.score);
console.log("file sentiment topic competitor recommend time");
for (const r of rows) {
console.log(
`${r.file.padEnd(8)}${r.sentiment.padEnd(22)}${r.topic.padEnd(28)}${r.competitor.padEnd(12)}${r.recommend.padEnd(11)}${r.ms} ms`,
);
}
console.log(`\n${right}/${asked} answers as expected (sentiment band, topic, competitor, recommendation)`);
|