Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

table.ts 2.2 KB

You have to be logged in to leave a comment. Sign In
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
  1. import Table from 'cli-table3';
  2. import chalk from 'chalk';
  3. import type { EvaluateSummary } from './types';
  4. function ellipsize(str: string, maxLen: number) {
  5. if (str.length > maxLen) {
  6. return str.slice(0, maxLen - 3) + '...';
  7. }
  8. return str;
  9. }
  10. export function generateTable(summary: EvaluateSummary, tableCellMaxLength = 250, maxRows = 25) {
  11. const maxWidth = process.stdout.columns ? process.stdout.columns - 10 : 120;
  12. const head = summary.table.head;
  13. const headLength = head.prompts.length + head.vars.length;
  14. const allProvidersSame = head.prompts.every((p) => p.provider === head.prompts[0].provider);
  15. const table = new Table({
  16. head: [
  17. ...head.vars,
  18. ...head.prompts.map((prompt) =>
  19. allProvidersSame ? prompt.label : `[${prompt.provider}] ${prompt.label}`,
  20. ),
  21. ].map((h) => ellipsize(h, tableCellMaxLength)),
  22. colWidths: Array(headLength).fill(Math.floor(maxWidth / headLength)),
  23. wordWrap: true,
  24. wrapOnWordBoundary: true, // if false, ansi colors break
  25. style: {
  26. head: ['blue', 'bold'],
  27. },
  28. });
  29. // Skip first row (header) and add the rest. Color PASS/FAIL
  30. for (const row of summary.table.body.slice(0, maxRows)) {
  31. table.push([
  32. ...row.vars.map((v) => ellipsize(v, tableCellMaxLength)),
  33. ...row.outputs.map(({ pass, score, text }) => {
  34. text = ellipsize(text, tableCellMaxLength);
  35. if (pass) {
  36. return chalk.green('[PASS] ') + text;
  37. } else if (!pass) {
  38. // color everything red up until '---'
  39. return (
  40. chalk.red('[FAIL] ') +
  41. text
  42. .split('---')
  43. .map((c, idx) => (idx === 0 ? chalk.red.bold(c) : c))
  44. .join('---')
  45. );
  46. }
  47. return text;
  48. }),
  49. ]);
  50. }
  51. return table;
  52. }
  53. export function wrapTable(rows: Record<string, string | number>[]) {
  54. const maxWidth = process.stdout.columns ? process.stdout.columns - 10 : 120;
  55. const head = Object.keys(rows[0]);
  56. const table = new Table({
  57. head,
  58. colWidths: Array(head.length).fill(Math.floor(maxWidth / head.length)),
  59. wordWrap: true,
  60. wrapOnWordBoundary: true,
  61. });
  62. for (const row of rows) {
  63. table.push(Object.values(row));
  64. }
  65. return table;
  66. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...