1
import Table from 'cli-table3';
2
import chalk from 'chalk';
3
import type { EvaluateSummary } from './types';
5
function ellipsize(str: string, maxLen: number) {
6
if (str.length > maxLen) {
7
return str.slice(0, maxLen - 3) + '...';
12
export function generateTable(summary: EvaluateSummary, tableCellMaxLength = 250, maxRows = 25) {
13
const maxWidth = process.stdout.columns ? process.stdout.columns - 10 : 120;
14
const head = summary.table.head;
15
const headLength = head.prompts.length + head.vars.length;
16
const allProvidersSame = head.prompts.every((p) => p.provider === head.prompts[0].provider);
17
const table = new Table({
20
...head.prompts.map((prompt) =>
21
allProvidersSame ? prompt.display : `[${prompt.provider}] ${prompt.display}`,
23
].map((h) => ellipsize(h, tableCellMaxLength)),
24
colWidths: Array(headLength).fill(Math.floor(maxWidth / headLength)),
26
wrapOnWordBoundary: true,
28
head: ['blue', 'bold'],
32
for (const row of summary.table.body.slice(0, maxRows)) {
34
...row.vars.map((v) => ellipsize(v, tableCellMaxLength)),
35
...row.outputs.map(({ pass, score, text }) => {
36
text = ellipsize(text, tableCellMaxLength);
38
return chalk.green('[PASS] ') + text;
42
chalk.red('[FAIL] ') +
45
.map((c, idx) => (idx === 0 ? chalk.red.bold(c) : c))
56
export function wrapTable(rows: Record<string, string | number>[]) {
57
const maxWidth = process.stdout.columns ? process.stdout.columns - 10 : 120;
58
const head = Object.keys(rows[0]);
59
const table = new Table({
61
colWidths: Array(head.length).fill(Math.floor(maxWidth / head.length)),
63
wrapOnWordBoundary: true,
65
for (const row of rows) {
66
table.push(Object.values(row));