@orq-ai/evaluators 1.0.0-12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/lib/comparison-evaluators.d.ts +26 -0
- package/dist/lib/comparison-evaluators.d.ts.map +1 -0
- package/dist/lib/comparison-evaluators.js +148 -0
- package/dist/lib/cosine-similarity-evaluator.d.ts +62 -0
- package/dist/lib/cosine-similarity-evaluator.d.ts.map +1 -0
- package/dist/lib/cosine-similarity-evaluator.js +162 -0
- package/dist/lib/evaluators.d.ts +2 -0
- package/dist/lib/evaluators.d.ts.map +1 -0
- package/dist/lib/evaluators.js +3 -0
- package/dist/lib/json-evaluators.d.ts +25 -0
- package/dist/lib/json-evaluators.d.ts.map +1 -0
- package/dist/lib/json-evaluators.js +177 -0
- package/dist/lib/string-evaluators.d.ts +30 -0
- package/dist/lib/string-evaluators.d.ts.map +1 -0
- package/dist/lib/string-evaluators.js +168 -0
- package/dist/lib/vector-utils.d.ts +27 -0
- package/dist/lib/vector-utils.d.ts.map +1 -0
- package/dist/lib/vector-utils.js +54 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Evaluator } from "@orq-ai/evaluatorq";
|
|
2
|
+
/**
|
|
3
|
+
* Validates that a string output does not exceed a maximum length
|
|
4
|
+
*/
|
|
5
|
+
export declare function maxLength(max: number): Evaluator;
|
|
6
|
+
/**
|
|
7
|
+
* Validates that a string output meets a minimum length requirement
|
|
8
|
+
*/
|
|
9
|
+
export declare function minLength(min: number): Evaluator;
|
|
10
|
+
/**
|
|
11
|
+
* Validates that the output contains a specific substring
|
|
12
|
+
*/
|
|
13
|
+
export declare function contains(substring: string, caseSensitive?: boolean): Evaluator;
|
|
14
|
+
/**
|
|
15
|
+
* Validates that the output matches a regular expression pattern
|
|
16
|
+
*/
|
|
17
|
+
export declare function matchesPattern(pattern: RegExp | string, description?: string): Evaluator;
|
|
18
|
+
/**
|
|
19
|
+
* Validates that the output starts with a specific prefix
|
|
20
|
+
*/
|
|
21
|
+
export declare function startsWith(prefix: string): Evaluator;
|
|
22
|
+
/**
|
|
23
|
+
* Validates that the output ends with a specific suffix
|
|
24
|
+
*/
|
|
25
|
+
export declare function endsWith(suffix: string): Evaluator;
|
|
26
|
+
/**
|
|
27
|
+
* Validates that the output is not empty or whitespace-only
|
|
28
|
+
*/
|
|
29
|
+
export declare const notEmpty: Evaluator;
|
|
30
|
+
//# sourceMappingURL=string-evaluators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string-evaluators.d.ts","sourceRoot":"","sources":["../../src/lib/string-evaluators.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEpD;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAsBhD;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAsBhD;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,aAAa,UAAO,GAAG,SAAS,CAyB3E;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CA0BxF;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAqBpD;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAqBlD;AAED;;GAEG;AACH,eAAO,MAAM,QAAQ,EAAE,SAkBtB,CAAC"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Validates that a string output does not exceed a maximum length
|
|
3
|
+
*/
|
|
4
|
+
export function maxLength(max) {
|
|
5
|
+
return {
|
|
6
|
+
name: `max-length-${max}`,
|
|
7
|
+
scorer: async ({ output }) => {
|
|
8
|
+
if (output === undefined || output === null) {
|
|
9
|
+
return {
|
|
10
|
+
value: false,
|
|
11
|
+
explanation: "Output is null or undefined",
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const length = typeof output === "string" ? output.length : String(output).length;
|
|
15
|
+
const isValid = length <= max;
|
|
16
|
+
return {
|
|
17
|
+
value: isValid,
|
|
18
|
+
explanation: isValid
|
|
19
|
+
? `Length ${length} is within limit (≤${max})`
|
|
20
|
+
: `Length ${length} exceeds limit of ${max}`,
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Validates that a string output meets a minimum length requirement
|
|
27
|
+
*/
|
|
28
|
+
export function minLength(min) {
|
|
29
|
+
return {
|
|
30
|
+
name: `min-length-${min}`,
|
|
31
|
+
scorer: async ({ output }) => {
|
|
32
|
+
if (output === undefined || output === null) {
|
|
33
|
+
return {
|
|
34
|
+
value: false,
|
|
35
|
+
explanation: "Output is null or undefined",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const length = typeof output === "string" ? output.length : String(output).length;
|
|
39
|
+
const isValid = length >= min;
|
|
40
|
+
return {
|
|
41
|
+
value: isValid,
|
|
42
|
+
explanation: isValid
|
|
43
|
+
? `Length ${length} meets minimum (≥${min})`
|
|
44
|
+
: `Length ${length} is below minimum ${min}`,
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Validates that the output contains a specific substring
|
|
51
|
+
*/
|
|
52
|
+
export function contains(substring, caseSensitive = true) {
|
|
53
|
+
return {
|
|
54
|
+
name: `contains-${substring.toLowerCase().replace(/\s+/g, "-")}`,
|
|
55
|
+
scorer: async ({ output }) => {
|
|
56
|
+
if (output === undefined || output === null) {
|
|
57
|
+
return {
|
|
58
|
+
value: false,
|
|
59
|
+
explanation: "Output is null or undefined",
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const outputStr = String(output);
|
|
63
|
+
const searchStr = substring;
|
|
64
|
+
const found = caseSensitive
|
|
65
|
+
? outputStr.includes(searchStr)
|
|
66
|
+
: outputStr.toLowerCase().includes(searchStr.toLowerCase());
|
|
67
|
+
return {
|
|
68
|
+
value: found,
|
|
69
|
+
explanation: found
|
|
70
|
+
? `Output contains "${substring}"`
|
|
71
|
+
: `Output does not contain "${substring}"`,
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Validates that the output matches a regular expression pattern
|
|
78
|
+
*/
|
|
79
|
+
export function matchesPattern(pattern, description) {
|
|
80
|
+
const regex = typeof pattern === "string" ? new RegExp(pattern) : pattern;
|
|
81
|
+
const patternStr = regex.source;
|
|
82
|
+
return {
|
|
83
|
+
name: `matches-pattern-${patternStr.slice(0, 20).replace(/[^a-z0-9]/gi, "-")}`,
|
|
84
|
+
scorer: async ({ output }) => {
|
|
85
|
+
if (output === undefined || output === null) {
|
|
86
|
+
return {
|
|
87
|
+
value: false,
|
|
88
|
+
explanation: "Output is null or undefined",
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const matches = regex.test(String(output));
|
|
92
|
+
return {
|
|
93
|
+
value: matches,
|
|
94
|
+
explanation: matches
|
|
95
|
+
? description || `Output matches pattern /${patternStr}/`
|
|
96
|
+
: description
|
|
97
|
+
? `Output does not match required pattern: ${description}`
|
|
98
|
+
: `Output does not match pattern /${patternStr}/`,
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Validates that the output starts with a specific prefix
|
|
105
|
+
*/
|
|
106
|
+
export function startsWith(prefix) {
|
|
107
|
+
return {
|
|
108
|
+
name: `starts-with-${prefix.toLowerCase().replace(/\s+/g, "-")}`,
|
|
109
|
+
scorer: async ({ output }) => {
|
|
110
|
+
if (output === undefined || output === null) {
|
|
111
|
+
return {
|
|
112
|
+
value: false,
|
|
113
|
+
explanation: "Output is null or undefined",
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
const starts = String(output).startsWith(prefix);
|
|
117
|
+
return {
|
|
118
|
+
value: starts,
|
|
119
|
+
explanation: starts
|
|
120
|
+
? `Output starts with "${prefix}"`
|
|
121
|
+
: `Output does not start with "${prefix}"`,
|
|
122
|
+
};
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Validates that the output ends with a specific suffix
|
|
128
|
+
*/
|
|
129
|
+
export function endsWith(suffix) {
|
|
130
|
+
return {
|
|
131
|
+
name: `ends-with-${suffix.toLowerCase().replace(/\s+/g, "-")}`,
|
|
132
|
+
scorer: async ({ output }) => {
|
|
133
|
+
if (output === undefined || output === null) {
|
|
134
|
+
return {
|
|
135
|
+
value: false,
|
|
136
|
+
explanation: "Output is null or undefined",
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const ends = String(output).endsWith(suffix);
|
|
140
|
+
return {
|
|
141
|
+
value: ends,
|
|
142
|
+
explanation: ends
|
|
143
|
+
? `Output ends with "${suffix}"`
|
|
144
|
+
: `Output does not end with "${suffix}"`,
|
|
145
|
+
};
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Validates that the output is not empty or whitespace-only
|
|
151
|
+
*/
|
|
152
|
+
export const notEmpty = {
|
|
153
|
+
name: "not-empty",
|
|
154
|
+
scorer: async ({ output }) => {
|
|
155
|
+
if (output === undefined || output === null) {
|
|
156
|
+
return {
|
|
157
|
+
value: false,
|
|
158
|
+
explanation: "Output is null or undefined",
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
const trimmed = String(output).trim();
|
|
162
|
+
const isNotEmpty = trimmed.length > 0;
|
|
163
|
+
return {
|
|
164
|
+
value: isNotEmpty,
|
|
165
|
+
explanation: isNotEmpty ? "Output is not empty" : "Output is empty or whitespace-only",
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector utilities for cosine similarity and dot product calculations
|
|
3
|
+
*
|
|
4
|
+
* thanks @alexanderop
|
|
5
|
+
* @see https://alexop.dev/posts/how-to-implement-a-cosine-similarity-function-in-typescript-for-vector-comparison/
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Calculates the cosine similarity between two vectors
|
|
9
|
+
*/
|
|
10
|
+
export declare function cosineSimilarity(vecA: number[], vecB: number[]): number;
|
|
11
|
+
/**
|
|
12
|
+
* Calculates the dot product of two vectors
|
|
13
|
+
*/
|
|
14
|
+
export declare function dotProduct(vecA: number[], vecB: number[]): number;
|
|
15
|
+
/**
|
|
16
|
+
* Calculates the magnitude (length) of a vector
|
|
17
|
+
*/
|
|
18
|
+
export declare function magnitude(vec: number[]): number;
|
|
19
|
+
/**
|
|
20
|
+
* Normalizes a vector (converts to unit vector)
|
|
21
|
+
*/
|
|
22
|
+
export declare function normalize(vec: number[]): number[];
|
|
23
|
+
/**
|
|
24
|
+
* Converts cosine similarity to angular distance in degrees
|
|
25
|
+
*/
|
|
26
|
+
export declare function similarityToDegrees(similarity: number): number;
|
|
27
|
+
//# sourceMappingURL=vector-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector-utils.d.ts","sourceRoot":"","sources":["../../src/lib/vector-utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAgBvE;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAQjE;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,CAE/C;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAQjD;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAI9D"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Vector utilities for cosine similarity and dot product calculations
|
|
3
|
+
*
|
|
4
|
+
* thanks @alexanderop
|
|
5
|
+
* @see https://alexop.dev/posts/how-to-implement-a-cosine-similarity-function-in-typescript-for-vector-comparison/
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Calculates the cosine similarity between two vectors
|
|
9
|
+
*/
|
|
10
|
+
export function cosineSimilarity(vecA, vecB) {
|
|
11
|
+
if (vecA.length !== vecB.length) {
|
|
12
|
+
throw new Error(`Vector dimensions don't match: ${vecA.length} vs ${vecB.length}`);
|
|
13
|
+
}
|
|
14
|
+
const dotProduct = vecA.reduce((sum, a, i) => sum + a * vecB[i], 0);
|
|
15
|
+
const magnitudeA = Math.hypot(...vecA);
|
|
16
|
+
const magnitudeB = Math.hypot(...vecB);
|
|
17
|
+
if (magnitudeA === 0 || magnitudeB === 0) {
|
|
18
|
+
return 0;
|
|
19
|
+
}
|
|
20
|
+
return dotProduct / (magnitudeA * magnitudeB);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Calculates the dot product of two vectors
|
|
24
|
+
*/
|
|
25
|
+
export function dotProduct(vecA, vecB) {
|
|
26
|
+
if (vecA.length !== vecB.length) {
|
|
27
|
+
throw new Error(`Vector dimensions don't match: ${vecA.length} vs ${vecB.length}`);
|
|
28
|
+
}
|
|
29
|
+
return vecA.reduce((sum, a, i) => sum + a * vecB[i], 0);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Calculates the magnitude (length) of a vector
|
|
33
|
+
*/
|
|
34
|
+
export function magnitude(vec) {
|
|
35
|
+
return Math.hypot(...vec);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Normalizes a vector (converts to unit vector)
|
|
39
|
+
*/
|
|
40
|
+
export function normalize(vec) {
|
|
41
|
+
const mag = magnitude(vec);
|
|
42
|
+
if (mag === 0) {
|
|
43
|
+
return Array(vec.length).fill(0);
|
|
44
|
+
}
|
|
45
|
+
return vec.map((v) => v / mag);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Converts cosine similarity to angular distance in degrees
|
|
49
|
+
*/
|
|
50
|
+
export function similarityToDegrees(similarity) {
|
|
51
|
+
// Clamp similarity to [-1, 1] to handle floating point errors
|
|
52
|
+
const clampedSimilarity = Math.max(-1, Math.min(1, similarity));
|
|
53
|
+
return Math.acos(clampedSimilarity) * (180 / Math.PI);
|
|
54
|
+
}
|