MCPcopy
hub / github.com/promptfoo/promptfoo / assertionFromString

Function assertionFromString

src/csv.ts:22–130  ·  view source on GitHub ↗
(expected: string)

Source from the content-addressed store, hash-verified

20}
21
22export function assertionFromString(expected: string): Assertion {
23 // Legacy options
24 if (
25 expected.startsWith('javascript:') ||
26 expected.startsWith('fn:') ||
27 expected.startsWith('eval:') ||
28 (expected.startsWith('file://') && isJavascriptFile(expected.slice('file://'.length)))
29 ) {
30 // TODO(1.0): delete eval: legacy option
31 let sliceLength = 0;
32 if (expected.startsWith('javascript:')) {
33 sliceLength = 'javascript:'.length;
34 }
35 if (expected.startsWith('fn:')) {
36 sliceLength = 'fn:'.length;
37 }
38 if (expected.startsWith('eval:')) {
39 sliceLength = 'eval:'.length;
40 }
41
42 const functionBody = expected.slice(sliceLength).trim();
43 return {
44 type: 'javascript',
45 value: functionBody,
46 };
47 }
48 if (expected.startsWith('grade:') || expected.startsWith('llm-rubric:')) {
49 return {
50 type: 'llm-rubric',
51 value: expected.slice(expected.startsWith('grade:') ? 6 : 11),
52 };
53 }
54 if (
55 expected.startsWith('python:') ||
56 (expected.startsWith('file://') && (expected.endsWith('.py') || expected.includes('.py:')))
57 ) {
58 const sliceLength = expected.startsWith('python:') ? 'python:'.length : 'file://'.length;
59 const functionBody = expected.slice(sliceLength).trim();
60 return {
61 type: 'python',
62 value: functionBody,
63 };
64 }
65
66 const regexMatch = expected.match(getAssertionRegex());
67
68 if (regexMatch) {
69 const [_, notPrefix, type, thresholdStr, value] = regexMatch as [
70 string,
71 string,
72 BaseAssertionTypes,
73 string,
74 // Note: whether value is defined depends on the type of assertion.
75 string?,
76 ];
77 const fullType: AssertionType = notPrefix ? `not-${type}` : type;
78 const parsedThreshold = thresholdStr ? Number.parseFloat(thresholdStr) : Number.NaN;
79 const threshold = Number.isFinite(parsedThreshold) ? parsedThreshold : undefined;

Callers 2

csv.test.tsFile · 0.90
testCaseFromCsvRowFunction · 0.85

Calls 3

isJavascriptFileFunction · 0.90
getAssertionRegexFunction · 0.85
splitMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…