promptfoo

Форк
0
/
table.ts 
69 строк · 2.2 Кб
1
import Table from 'cli-table3';
2
import chalk from 'chalk';
3
import type { EvaluateSummary } from './types';
4

5
function ellipsize(str: string, maxLen: number) {
6
  if (str.length > maxLen) {
7
    return str.slice(0, maxLen - 3) + '...';
8
  }
9
  return str;
10
}
11

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({
18
    head: [
19
      ...head.vars,
20
      ...head.prompts.map((prompt) =>
21
        allProvidersSame ? prompt.display : `[${prompt.provider}] ${prompt.display}`,
22
      ),
23
    ].map((h) => ellipsize(h, tableCellMaxLength)),
24
    colWidths: Array(headLength).fill(Math.floor(maxWidth / headLength)),
25
    wordWrap: true,
26
    wrapOnWordBoundary: true, // if false, ansi colors break
27
    style: {
28
      head: ['blue', 'bold'],
29
    },
30
  });
31
  // Skip first row (header) and add the rest. Color PASS/FAIL
32
  for (const row of summary.table.body.slice(0, maxRows)) {
33
    table.push([
34
      ...row.vars.map((v) => ellipsize(v, tableCellMaxLength)),
35
      ...row.outputs.map(({ pass, score, text }) => {
36
        text = ellipsize(text, tableCellMaxLength);
37
        if (pass) {
38
          return chalk.green('[PASS] ') + text;
39
        } else if (!pass) {
40
          // color everything red up until '---'
41
          return (
42
            chalk.red('[FAIL] ') +
43
            text
44
              .split('---')
45
              .map((c, idx) => (idx === 0 ? chalk.red.bold(c) : c))
46
              .join('---')
47
          );
48
        }
49
        return text;
50
      }),
51
    ]);
52
  }
53
  return table;
54
}
55

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({
60
    head,
61
    colWidths: Array(head.length).fill(Math.floor(maxWidth / head.length)),
62
    wordWrap: true,
63
    wrapOnWordBoundary: true,
64
  });
65
  for (const row of rows) {
66
    table.push(Object.values(row));
67
  }
68
  return table;
69
}
70

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.