-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidationService.ts
More file actions
246 lines (214 loc) · 7.17 KB
/
ValidationService.ts
File metadata and controls
246 lines (214 loc) · 7.17 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import type { Provider, Prompt, Dataset, Assertion, ProjectOptions } from '../lib/types';
import { validateDatasetVariables, extractAllVariables } from '../lib/datasetUtils';
import { ProjectNameSchema } from '../lib/schemas';
export interface ValidationResult {
valid: boolean;
error?: string;
warning?: string;
}
export class ValidationService {
/**
* Validates project name
*/
static validateProjectName(projectName: string): ValidationResult {
const result = ProjectNameSchema.safeParse(projectName);
if (!result.success) {
return {
valid: false,
error: result.error.errors[0].message,
};
}
return { valid: true };
}
/**
* Validates providers configuration
*/
static validateProviders(providers: Provider[]): ValidationResult {
if (providers.length === 0) {
return {
valid: false,
error: 'Please add at least one provider',
};
}
const invalidProviders = providers.filter(
(p) => !p.providerId || !p.providerId.includes(':')
);
if (invalidProviders.length > 0) {
return {
valid: false,
error: 'Some providers have invalid or missing provider IDs. Please check the Providers tab.',
};
}
return { valid: true };
}
/**
* Validates prompts configuration
*/
static validatePrompts(prompts: Prompt[]): ValidationResult {
if (prompts.length === 0) {
return {
valid: false,
error: 'Please add at least one prompt',
};
}
// Check if at least one prompt has variables
const allVariables = extractAllVariables(prompts);
if (allVariables.size === 0) {
return {
valid: false,
error: 'Your prompts must contain at least one variable (e.g., {{variable_name}}). Please add variables to your prompt in the Prompts tab.',
};
}
return { valid: true };
}
/**
* Validates dataset against prompt variables
*/
static validateDataset(
prompts: Prompt[],
dataset: Dataset | undefined,
assertions: Assertion[]
): ValidationResult {
const allVariables = extractAllVariables(prompts);
// Always require at least one row in dataset
if (!dataset || !dataset.rows || dataset.rows.length === 0) {
return {
valid: false,
error: `Please add at least one row to your dataset. Your prompts use variables (${Array.from(allVariables).join(', ')}). Add data in the Dataset tab.`,
};
}
// Validate that dataset has all required variables from prompts
const validation = validateDatasetVariables(prompts, dataset.rows);
if (!validation.valid) {
return {
valid: false,
error: `Dataset is missing required variables: ${validation.missing.join(', ')}. Please add these columns to your dataset.`,
};
}
return { valid: true };
}
/**
* Validates assertions configuration
*/
static validateAssertions(
assertions: Assertion[],
dataset: Dataset | undefined,
options?: { enableSecurityTests?: boolean }
): ValidationResult {
const hasSecurityTests = options?.enableSecurityTests;
// Assertions are optional if security tests are enabled
if ((!assertions || assertions.length === 0) && !hasSecurityTests) {
return {
valid: false,
error: 'Please add at least one assertion to validate your test results. Go to the Assertions tab to add assertions, or enable Security Testing in Options.',
};
}
// Check if any assertions require expected_output column
const assertionsRequiringExpectedOutput = ['factuality', 'similar'];
const hasAssertionRequiringExpectedOutput = assertions?.some(
(a) =>
assertionsRequiringExpectedOutput.includes(a.type) &&
a.value &&
typeof a.value === 'string' &&
a.value.includes('{{expected_output}}')
);
if (hasAssertionRequiringExpectedOutput && dataset?.rows && dataset.rows.length > 0) {
const datasetHeaders = Object.keys(dataset.rows[0]);
const hasExpectedColumn = datasetHeaders.some((h) =>
h.toLowerCase().startsWith('expected')
);
if (!hasExpectedColumn) {
const assertionTypesNeedingIt = assertions
?.filter(
(a) =>
assertionsRequiringExpectedOutput.includes(a.type) &&
a.value &&
typeof a.value === 'string' &&
a.value.includes('{{expected_output}}')
)
.map((a) => a.type)
.join(', ') || 'assertions';
return {
valid: false,
error: `Your assertions use {{expected_output}} but your dataset does not have an "expected_output" or "expected_*" column. Required for: ${assertionTypesNeedingIt}`,
};
}
}
return { valid: true };
}
/**
* Validates BigQuery configuration
*/
static validateBigQueryConfig(
options: ProjectOptions & {
bigQueryEnabled?: boolean;
bigQueryProjectId?: string;
bigQueryDatasetId?: string;
bigQueryTableId?: string;
}
): ValidationResult {
const bigQueryEnabled = options.bigQueryEnabled === true;
if (!bigQueryEnabled) {
return { valid: true };
}
const bqProjectId = (options.bigQueryProjectId || '').trim();
const bqDatasetId = (options.bigQueryDatasetId || '').trim();
const bqTableId = (options.bigQueryTableId || '').trim();
if (!bqProjectId || !bqDatasetId || !bqTableId) {
return {
valid: false,
error: 'Store evaluation results in Google BigQuery is turned on but the table details are not configured. Please configure BigQuery settings in the Options tab or disable BigQuery integration.',
};
}
return { valid: true };
}
/**
* Validates entire project before running evaluation
*/
static validateProject(
projectName: string,
providers: Provider[],
prompts: Prompt[],
dataset: Dataset | undefined,
assertions: Assertion[],
options: ProjectOptions & {
enableSecurityTests?: boolean;
bigQueryEnabled?: boolean;
bigQueryProjectId?: string;
bigQueryDatasetId?: string;
bigQueryTableId?: string;
}
): ValidationResult {
// Validate project name
const nameValidation = this.validateProjectName(projectName);
if (!nameValidation.valid) {
return nameValidation;
}
// Validate providers
const providersValidation = this.validateProviders(providers);
if (!providersValidation.valid) {
return providersValidation;
}
// Validate prompts
const promptsValidation = this.validatePrompts(prompts);
if (!promptsValidation.valid) {
return promptsValidation;
}
// Validate dataset
const datasetValidation = this.validateDataset(prompts, dataset, assertions);
if (!datasetValidation.valid) {
return datasetValidation;
}
// Validate assertions
const assertionsValidation = this.validateAssertions(assertions, dataset, options);
if (!assertionsValidation.valid) {
return assertionsValidation;
}
// Validate BigQuery configuration
const bigQueryValidation = this.validateBigQueryConfig(options);
if (!bigQueryValidation.valid) {
return bigQueryValidation;
}
return { valid: true };
}
}