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

csv.ts 4.1 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  1. // Helpers for parsing CSV eval files, shared by frontend and backend. Cannot import native modules.
  2. import type { Assertion, AssertionType, CsvRow, TestCase } from './types';
  3. const DEFAULT_SEMANTIC_SIMILARITY_THRESHOLD = 0.8;
  4. export function testCaseFromCsvRow(row: CsvRow): TestCase {
  5. const vars: Record<string, string> = {};
  6. const asserts: Assertion[] = [];
  7. const options: TestCase['options'] = {};
  8. let providerOutput: string | object | undefined;
  9. let description: string | undefined;
  10. for (const [key, value] of Object.entries(row)) {
  11. if (key.startsWith('__expected')) {
  12. if (value.trim() !== '') {
  13. asserts.push(assertionFromString(value));
  14. }
  15. } else if (key === '__prefix') {
  16. options.prefix = value;
  17. } else if (key === '__suffix') {
  18. options.suffix = value;
  19. } else if (key === '__description') {
  20. description = value;
  21. } else if (key === '__providerOutput') {
  22. providerOutput = value;
  23. } else {
  24. vars[key] = value;
  25. }
  26. }
  27. return {
  28. vars,
  29. ...(providerOutput ? { providerOutput } : {}),
  30. assert: asserts,
  31. options,
  32. ...(description ? { description } : {}),
  33. };
  34. }
  35. export function assertionFromString(expected: string): Assertion {
  36. // Legacy options
  37. if (
  38. expected.startsWith('javascript:') ||
  39. expected.startsWith('fn:') ||
  40. expected.startsWith('eval:')
  41. ) {
  42. // TODO(1.0): delete eval: legacy option
  43. let sliceLength;
  44. if (expected.startsWith('javascript:')) {
  45. sliceLength = 'javascript:'.length;
  46. }
  47. if (expected.startsWith('fn:')) {
  48. sliceLength = 'fn:'.length;
  49. }
  50. if (expected.startsWith('eval:')) {
  51. sliceLength = 'eval:'.length;
  52. }
  53. const functionBody = expected.slice(sliceLength).trim();
  54. return {
  55. type: 'javascript',
  56. value: functionBody,
  57. };
  58. }
  59. if (expected.startsWith('grade:') || expected.startsWith('llm-rubric:')) {
  60. return {
  61. type: 'llm-rubric',
  62. value: expected.slice(6),
  63. };
  64. }
  65. if (expected.startsWith('python:')) {
  66. const sliceLength = 'python:'.length;
  67. const functionBody = expected.slice(sliceLength).trim();
  68. return {
  69. type: 'python',
  70. value: functionBody,
  71. };
  72. }
  73. // New options
  74. const assertionRegex =
  75. /^(not-)?(equals|contains-any|contains-all|icontains-any|icontains-all|contains-json|is-json|regex|icontains|contains|webhook|rouge-n|similar|starts-with|levenshtein|classifier|model-graded-factuality|factuality|model-graded-closedqa|answer-relevance|context-recall|context-relevance|context-faithfulness|is-valid-openai-function-call|is-valid-openai-tools-call|latency|perplexity|perplexity-score|cost)(?:\((\d+(?:\.\d+)?)\))?(?::([\s\S]*))?$/;
  76. const regexMatch = expected.match(assertionRegex);
  77. if (regexMatch) {
  78. const [_, notPrefix, type, thresholdStr, value] = regexMatch;
  79. const fullType = notPrefix ? `not-${type}` : type;
  80. const threshold = parseFloat(thresholdStr);
  81. if (
  82. type === 'contains-any' ||
  83. type === 'contains-all' ||
  84. type === 'icontains-any' ||
  85. type === 'icontains-all'
  86. ) {
  87. return {
  88. type: fullType as AssertionType,
  89. value: value.split(',').map((s) => s.trim()),
  90. };
  91. } else if (type === 'contains-json' || type === 'is-json') {
  92. return {
  93. type: fullType as AssertionType,
  94. value: value,
  95. };
  96. } else if (
  97. type === 'rouge-n' ||
  98. type === 'similar' ||
  99. type === 'starts-with' ||
  100. type === 'levenshtein' ||
  101. type === 'classifier' ||
  102. type === 'answer-relevance' ||
  103. type === 'context-recall' ||
  104. type === 'context-relevance' ||
  105. type === 'context-faithfulness' ||
  106. type === 'latency' ||
  107. type === 'perplexity' ||
  108. type === 'perplexity-score' ||
  109. type === 'cost'
  110. ) {
  111. return {
  112. type: fullType as AssertionType,
  113. value,
  114. threshold: threshold || (type === 'similar' ? DEFAULT_SEMANTIC_SIMILARITY_THRESHOLD : 0.75),
  115. };
  116. } else {
  117. return {
  118. type: fullType as AssertionType,
  119. value,
  120. };
  121. }
  122. }
  123. // Default to equality
  124. return {
  125. type: 'equals',
  126. value: expected,
  127. };
  128. }
Tip!

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

Comments

Loading...