@arizeai/phoenix-evals 0.5.0 → 0.6.0

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.
Files changed (150) hide show
  1. package/README.md +42 -0
  2. package/dist/esm/core/EvaluatorBase.d.ts +19 -0
  3. package/dist/esm/core/EvaluatorBase.d.ts.map +1 -0
  4. package/dist/esm/core/EvaluatorBase.js +18 -0
  5. package/dist/esm/core/EvaluatorBase.js.map +1 -0
  6. package/dist/esm/index.d.ts +1 -0
  7. package/dist/esm/index.d.ts.map +1 -1
  8. package/dist/esm/index.js +1 -0
  9. package/dist/esm/index.js.map +1 -1
  10. package/dist/esm/llm/ClassificationEvaluator.d.ts +20 -3
  11. package/dist/esm/llm/ClassificationEvaluator.d.ts.map +1 -1
  12. package/dist/esm/llm/ClassificationEvaluator.js +29 -2
  13. package/dist/esm/llm/ClassificationEvaluator.js.map +1 -1
  14. package/dist/esm/llm/LLMEvaluator.d.ts +4 -7
  15. package/dist/esm/llm/LLMEvaluator.d.ts.map +1 -1
  16. package/dist/esm/llm/LLMEvaluator.js +4 -7
  17. package/dist/esm/llm/LLMEvaluator.js.map +1 -1
  18. package/dist/esm/llm/createClassificationEvaluator.d.ts +1 -1
  19. package/dist/esm/llm/createClassificationEvaluator.d.ts.map +1 -1
  20. package/dist/esm/llm/createClassificationEvaluator.js.map +1 -1
  21. package/dist/esm/llm/createDocumentRelevancyEvaluator.d.ts +8 -7
  22. package/dist/esm/llm/createDocumentRelevancyEvaluator.d.ts.map +1 -1
  23. package/dist/esm/llm/createDocumentRelevancyEvaluator.js +1 -2
  24. package/dist/esm/llm/createDocumentRelevancyEvaluator.js.map +1 -1
  25. package/dist/esm/llm/createHallucinationEvaluator.d.ts +6 -6
  26. package/dist/esm/llm/createHallucinationEvaluator.d.ts.map +1 -1
  27. package/dist/esm/llm/createHallucinationEvaluator.js +1 -2
  28. package/dist/esm/llm/createHallucinationEvaluator.js.map +1 -1
  29. package/dist/esm/template/applyTemplate.d.ts +3 -3
  30. package/dist/esm/template/applyTemplate.d.ts.map +1 -1
  31. package/dist/esm/template/applyTemplate.js +15 -1
  32. package/dist/esm/template/applyTemplate.js.map +1 -1
  33. package/dist/esm/template/createTemplateVariablesProxy.d.ts +1 -1
  34. package/dist/esm/template/createTemplateVariablesProxy.d.ts.map +1 -1
  35. package/dist/esm/template/createTemplateVariablesProxy.js.map +1 -1
  36. package/dist/esm/template/getTemplateVariables.d.ts +2 -2
  37. package/dist/esm/template/getTemplateVariables.d.ts.map +1 -1
  38. package/dist/esm/template/getTemplateVariables.js +16 -0
  39. package/dist/esm/template/getTemplateVariables.js.map +1 -1
  40. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  41. package/dist/esm/types/data.d.ts +194 -0
  42. package/dist/esm/types/data.d.ts.map +1 -0
  43. package/dist/esm/types/data.js +2 -0
  44. package/dist/esm/types/data.js.map +1 -0
  45. package/dist/esm/types/evals.d.ts +19 -5
  46. package/dist/esm/types/evals.d.ts.map +1 -1
  47. package/dist/esm/types/index.d.ts +1 -0
  48. package/dist/esm/types/index.d.ts.map +1 -1
  49. package/dist/esm/types/index.js +1 -0
  50. package/dist/esm/types/index.js.map +1 -1
  51. package/dist/esm/types/otel.d.ts +14 -13
  52. package/dist/esm/types/otel.d.ts.map +1 -1
  53. package/dist/esm/types/templating.d.ts +8 -6
  54. package/dist/esm/types/templating.d.ts.map +1 -1
  55. package/dist/esm/utils/bindEvaluator.d.ts +219 -0
  56. package/dist/esm/utils/bindEvaluator.d.ts.map +1 -0
  57. package/dist/esm/utils/bindEvaluator.js +163 -0
  58. package/dist/esm/utils/bindEvaluator.js.map +1 -0
  59. package/dist/esm/utils/index.d.ts +2 -0
  60. package/dist/esm/utils/index.d.ts.map +1 -0
  61. package/dist/esm/utils/index.js +2 -0
  62. package/dist/esm/utils/index.js.map +1 -0
  63. package/dist/esm/utils/objectMappingUtils.d.ts +166 -0
  64. package/dist/esm/utils/objectMappingUtils.d.ts.map +1 -0
  65. package/dist/esm/utils/objectMappingUtils.js +191 -0
  66. package/dist/esm/utils/objectMappingUtils.js.map +1 -0
  67. package/dist/src/core/EvaluatorBase.d.ts +19 -0
  68. package/dist/src/core/EvaluatorBase.d.ts.map +1 -0
  69. package/dist/src/core/EvaluatorBase.js +17 -0
  70. package/dist/src/core/EvaluatorBase.js.map +1 -0
  71. package/dist/src/index.d.ts +1 -0
  72. package/dist/src/index.d.ts.map +1 -1
  73. package/dist/src/index.js +1 -0
  74. package/dist/src/index.js.map +1 -1
  75. package/dist/src/llm/ClassificationEvaluator.d.ts +20 -3
  76. package/dist/src/llm/ClassificationEvaluator.d.ts.map +1 -1
  77. package/dist/src/llm/ClassificationEvaluator.js +13 -2
  78. package/dist/src/llm/ClassificationEvaluator.js.map +1 -1
  79. package/dist/src/llm/LLMEvaluator.d.ts +4 -7
  80. package/dist/src/llm/LLMEvaluator.d.ts.map +1 -1
  81. package/dist/src/llm/LLMEvaluator.js +16 -5
  82. package/dist/src/llm/LLMEvaluator.js.map +1 -1
  83. package/dist/src/llm/createClassificationEvaluator.d.ts +1 -1
  84. package/dist/src/llm/createClassificationEvaluator.d.ts.map +1 -1
  85. package/dist/src/llm/createClassificationEvaluator.js.map +1 -1
  86. package/dist/src/llm/createDocumentRelevancyEvaluator.d.ts +8 -7
  87. package/dist/src/llm/createDocumentRelevancyEvaluator.d.ts.map +1 -1
  88. package/dist/src/llm/createDocumentRelevancyEvaluator.js +2 -2
  89. package/dist/src/llm/createDocumentRelevancyEvaluator.js.map +1 -1
  90. package/dist/src/llm/createHallucinationEvaluator.d.ts +6 -6
  91. package/dist/src/llm/createHallucinationEvaluator.d.ts.map +1 -1
  92. package/dist/src/llm/createHallucinationEvaluator.js +2 -2
  93. package/dist/src/llm/createHallucinationEvaluator.js.map +1 -1
  94. package/dist/src/template/applyTemplate.d.ts +3 -3
  95. package/dist/src/template/applyTemplate.d.ts.map +1 -1
  96. package/dist/src/template/applyTemplate.js +12 -1
  97. package/dist/src/template/applyTemplate.js.map +1 -1
  98. package/dist/src/template/createTemplateVariablesProxy.d.ts +1 -1
  99. package/dist/src/template/createTemplateVariablesProxy.d.ts.map +1 -1
  100. package/dist/src/template/createTemplateVariablesProxy.js.map +1 -1
  101. package/dist/src/template/getTemplateVariables.d.ts +2 -2
  102. package/dist/src/template/getTemplateVariables.d.ts.map +1 -1
  103. package/dist/src/template/getTemplateVariables.js +16 -0
  104. package/dist/src/template/getTemplateVariables.js.map +1 -1
  105. package/dist/src/types/data.d.ts +194 -0
  106. package/dist/src/types/data.d.ts.map +1 -0
  107. package/dist/src/types/data.js +3 -0
  108. package/dist/src/types/data.js.map +1 -0
  109. package/dist/src/types/evals.d.ts +19 -5
  110. package/dist/src/types/evals.d.ts.map +1 -1
  111. package/dist/src/types/index.d.ts +1 -0
  112. package/dist/src/types/index.d.ts.map +1 -1
  113. package/dist/src/types/index.js +1 -0
  114. package/dist/src/types/index.js.map +1 -1
  115. package/dist/src/types/otel.d.ts +14 -13
  116. package/dist/src/types/otel.d.ts.map +1 -1
  117. package/dist/src/types/templating.d.ts +8 -6
  118. package/dist/src/types/templating.d.ts.map +1 -1
  119. package/dist/src/utils/bindEvaluator.d.ts +219 -0
  120. package/dist/src/utils/bindEvaluator.d.ts.map +1 -0
  121. package/dist/src/utils/bindEvaluator.js +166 -0
  122. package/dist/src/utils/bindEvaluator.js.map +1 -0
  123. package/dist/src/utils/index.d.ts +2 -0
  124. package/dist/src/utils/index.d.ts.map +1 -0
  125. package/dist/src/utils/index.js +18 -0
  126. package/dist/src/utils/index.js.map +1 -0
  127. package/dist/src/utils/objectMappingUtils.d.ts +166 -0
  128. package/dist/src/utils/objectMappingUtils.d.ts.map +1 -0
  129. package/dist/src/utils/objectMappingUtils.js +191 -0
  130. package/dist/src/utils/objectMappingUtils.js.map +1 -0
  131. package/dist/tsconfig.tsbuildinfo +1 -1
  132. package/package.json +3 -1
  133. package/src/core/EvaluatorBase.ts +43 -0
  134. package/src/index.ts +1 -0
  135. package/src/llm/ClassificationEvaluator.ts +41 -5
  136. package/src/llm/LLMEvaluator.ts +7 -16
  137. package/src/llm/createClassificationEvaluator.ts +1 -1
  138. package/src/llm/createDocumentRelevancyEvaluator.ts +17 -12
  139. package/src/llm/createHallucinationEvaluator.ts +12 -10
  140. package/src/template/applyTemplate.ts +22 -9
  141. package/src/template/createTemplateVariablesProxy.ts +3 -3
  142. package/src/template/getTemplateVariables.ts +18 -2
  143. package/src/types/data.ts +200 -0
  144. package/src/types/evals.ts +27 -7
  145. package/src/types/index.ts +1 -0
  146. package/src/types/otel.ts +15 -13
  147. package/src/types/templating.ts +9 -6
  148. package/src/utils/bindEvaluator.ts +229 -0
  149. package/src/utils/index.ts +1 -0
  150. package/src/utils/objectMappingUtils.ts +202 -0
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Binds an evaluator to a specific data structure using input mapping.
3
+ *
4
+ * This function creates a new evaluator instance that automatically transforms
5
+ * your data structure to match what the evaluator expects. This is particularly
6
+ * useful when your data schema doesn't match the evaluator's expected input format.
7
+ *
8
+ * @param evaluator - The evaluator to bind (e.g., a hallucination evaluator)
9
+ * @param context - The binding context containing the input mapping configuration
10
+ * @returns A new evaluator instance with the input mapping applied
11
+ *
12
+ * @example
13
+ * **Basic usage with simple field mapping:**
14
+ * ```typescript
15
+ * import { bindEvaluator, createHallucinationEvaluator } from "@arizeai/phoenix-evals";
16
+ * import { openai } from "@ai-sdk/openai";
17
+ *
18
+ * type MyData = {
19
+ * question: string;
20
+ * context: string;
21
+ * answer: string;
22
+ * };
23
+ *
24
+ * const evaluator = bindEvaluator<MyData>(
25
+ * createHallucinationEvaluator({ model: openai("gpt-4") }),
26
+ * {
27
+ * inputMapping: {
28
+ * input: "question", // Evaluator expects "input", map from "question"
29
+ * reference: "context", // Evaluator expects "reference", map from "context"
30
+ * output: "answer", // Evaluator expects "output", map from "answer"
31
+ * },
32
+ * }
33
+ * );
34
+ *
35
+ * // Now you can evaluate with your data structure
36
+ * const result = await evaluator.evaluate({
37
+ * question: "What is AI?",
38
+ * context: "AI is artificial intelligence...",
39
+ * answer: "AI stands for artificial intelligence",
40
+ * });
41
+ * ```
42
+ *
43
+ * @example
44
+ * **Using nested property access:**
45
+ * ```typescript
46
+ * type ApiResponse = {
47
+ * request: {
48
+ * body: {
49
+ * query: string;
50
+ * context: string;
51
+ * };
52
+ * };
53
+ * response: {
54
+ * data: {
55
+ * text: string;
56
+ * };
57
+ * };
58
+ * };
59
+ *
60
+ * const evaluator = bindEvaluator<ApiResponse>(
61
+ * createHallucinationEvaluator({ model: openai("gpt-4") }),
62
+ * {
63
+ * inputMapping: {
64
+ * input: "request.body.query",
65
+ * reference: "request.body.context",
66
+ * output: "response.data.text",
67
+ * },
68
+ * }
69
+ * );
70
+ * ```
71
+ *
72
+ * @example
73
+ * **Using function-based mapping for data transformation:**
74
+ * ```typescript
75
+ * type RawData = {
76
+ * question: string;
77
+ * contexts: string[]; // Array of context strings
78
+ * answer: string;
79
+ * };
80
+ *
81
+ * const evaluator = bindEvaluator<RawData>(
82
+ * createHallucinationEvaluator({ model: openai("gpt-4") }),
83
+ * {
84
+ * inputMapping: {
85
+ * input: "question",
86
+ * // Transform array to single string
87
+ * reference: (data) => data.contexts.join("\n\n"),
88
+ * output: "answer",
89
+ * },
90
+ * }
91
+ * );
92
+ * ```
93
+ *
94
+ * @example
95
+ * **Using JSONPath for complex queries:**
96
+ * ```typescript
97
+ * type ComplexData = {
98
+ * conversation: {
99
+ * messages: Array<{ role: string; content: string }>;
100
+ * };
101
+ * metadata: {
102
+ * sources: string[];
103
+ * };
104
+ * };
105
+ *
106
+ * const evaluator = bindEvaluator<ComplexData>(
107
+ * createHallucinationEvaluator({ model: openai("gpt-4") }),
108
+ * {
109
+ * inputMapping: {
110
+ * // Extract last user message
111
+ * input: "$.conversation.messages[?(@.role=='user')].content[-1]",
112
+ * // Extract all sources
113
+ * reference: "$.metadata.sources[*]",
114
+ * // Extract last assistant message
115
+ * output: "$.conversation.messages[?(@.role=='assistant')].content[-1]",
116
+ * },
117
+ * }
118
+ * );
119
+ * ```
120
+ *
121
+ * @example
122
+ * **Binding multiple evaluators with different mappings:**
123
+ * ```typescript
124
+ * type EvaluationData = {
125
+ * userQuery: string;
126
+ * systemContext: string;
127
+ * modelOutput: string;
128
+ * expectedOutput?: string;
129
+ * };
130
+ *
131
+ * // Hallucination evaluator
132
+ * const hallucinationEvaluator = bindEvaluator<EvaluationData>(
133
+ * createHallucinationEvaluator({ model: openai("gpt-4") }),
134
+ * {
135
+ * inputMapping: {
136
+ * input: "userQuery",
137
+ * reference: "systemContext",
138
+ * output: "modelOutput",
139
+ * },
140
+ * }
141
+ * );
142
+ *
143
+ * // Document relevancy evaluator (if it exists)
144
+ * const relevancyEvaluator = bindEvaluator<EvaluationData>(
145
+ * createDocumentRelevancyEvaluator({ model: openai("gpt-4") }),
146
+ * {
147
+ * inputMapping: {
148
+ * query: "userQuery",
149
+ * document: "systemContext",
150
+ * output: "modelOutput",
151
+ * },
152
+ * }
153
+ * );
154
+ * ```
155
+ */
156
+ export function bindEvaluator(evaluator, context) {
157
+ let boundEvaluator = evaluator;
158
+ if (context.inputMapping) {
159
+ boundEvaluator = boundEvaluator.bindInputMapping(context.inputMapping);
160
+ }
161
+ return boundEvaluator;
162
+ }
163
+ //# sourceMappingURL=bindEvaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bindEvaluator.js","sourceRoot":"","sources":["../../../src/utils/bindEvaluator.ts"],"names":[],"mappings":"AAgEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0JG;AACH,MAAM,UAAU,aAAa,CAC3B,SAAoC,EACpC,OAAmC;IAEnC,IAAI,cAAc,GAA8B,SAAS,CAAC;IAC1D,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;QACzB,cAAc,GAAG,cAAc,CAAC,gBAAgB,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC;IACzE,CAAC;IACD,OAAO,cAAc,CAAC;AACxB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./bindEvaluator.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./bindEvaluator.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,166 @@
1
+ import { ObjectMapping } from "../types/data.js";
2
+ /**
3
+ * Remaps an object by applying field mappings while preserving original data.
4
+ *
5
+ * This function takes your original data object and a mapping configuration,
6
+ * then returns a new object that contains:
7
+ * - All original fields from the input data
8
+ * - Additional/overridden fields based on the mapping
9
+ *
10
+ * The mapping allows you to extract values using:
11
+ * - Simple property names: `"fieldName"`
12
+ * - Dot notation: `"user.profile.name"`
13
+ * - Array access: `"items[0].id"`
14
+ * - JSONPath expressions: `"$.items[*].id"`
15
+ * - Function extractors: `(data) => data.customField`
16
+ *
17
+ * @param data - The input data object to remap
18
+ * @param mapping - The mapping configuration defining how to extract/transform values
19
+ * @returns A new object with original fields plus mapped fields
20
+ *
21
+ * @example
22
+ * **Basic remapping:**
23
+ * ```typescript
24
+ * const data = {
25
+ * name: "John",
26
+ * age: 30,
27
+ * email: "john@example.com",
28
+ * };
29
+ *
30
+ * const mapping: ObjectMapping<typeof data> = {
31
+ * userName: "name",
32
+ * userAge: "age",
33
+ * };
34
+ *
35
+ * const result = remapObject(data, mapping);
36
+ * // Result: {
37
+ * // name: "John",
38
+ * // age: 30,
39
+ * // email: "john@example.com",
40
+ * // userName: "John", // Added from mapping
41
+ * // userAge: 30, // Added from mapping
42
+ * // }
43
+ * ```
44
+ *
45
+ * @example
46
+ * **Nested property extraction:**
47
+ * ```typescript
48
+ * const data = {
49
+ * user: {
50
+ * profile: {
51
+ * firstName: "John",
52
+ * lastName: "Doe",
53
+ * },
54
+ * email: "john@example.com",
55
+ * },
56
+ * };
57
+ *
58
+ * const mapping: ObjectMapping<typeof data> = {
59
+ * firstName: "user.profile.firstName",
60
+ * lastName: "user.profile.lastName",
61
+ * email: "user.email",
62
+ * };
63
+ *
64
+ * const result = remapObject(data, mapping);
65
+ * // Result includes original data plus:
66
+ * // {
67
+ * // firstName: "John",
68
+ * // lastName: "Doe",
69
+ * // email: "john@example.com",
70
+ * // }
71
+ * ```
72
+ *
73
+ * @example
74
+ * **Array element access:**
75
+ * ```typescript
76
+ * const data = {
77
+ * items: [
78
+ * { id: 1, name: "Apple" },
79
+ * { id: 2, name: "Banana" },
80
+ * ],
81
+ * };
82
+ *
83
+ * const mapping: ObjectMapping<typeof data> = {
84
+ * firstItemId: "items[0].id",
85
+ * firstItemName: "items[0].name",
86
+ * };
87
+ *
88
+ * const result = remapObject(data, mapping);
89
+ * // Result includes:
90
+ * // {
91
+ * // firstItemId: 1,
92
+ * // firstItemName: "Apple",
93
+ * // }
94
+ * ```
95
+ *
96
+ * @example
97
+ * **Function-based transformation:**
98
+ * ```typescript
99
+ * const data = {
100
+ * firstName: "John",
101
+ * lastName: "Doe",
102
+ * scores: [85, 92, 78],
103
+ * };
104
+ *
105
+ * const mapping: ObjectMapping<typeof data> = {
106
+ * fullName: (data) => `${data.firstName} ${data.lastName}`,
107
+ * averageScore: (data) =>
108
+ * data.scores.reduce((a, b) => a + b, 0) / data.scores.length,
109
+ * };
110
+ *
111
+ * const result = remapObject(data, mapping);
112
+ * // Result includes:
113
+ * // {
114
+ * // fullName: "John Doe",
115
+ * // averageScore: 85,
116
+ * // }
117
+ * ```
118
+ *
119
+ * @example
120
+ * **Field override:**
121
+ * ```typescript
122
+ * const data = {
123
+ * name: "John",
124
+ * status: "inactive",
125
+ * };
126
+ *
127
+ * const mapping: ObjectMapping<typeof data> = {
128
+ * // Override existing field
129
+ * status: (data) => data.name === "John" ? "active" : "inactive",
130
+ * // Add new field
131
+ * displayName: (data) => `User: ${data.name}`,
132
+ * };
133
+ *
134
+ * const result = remapObject(data, mapping);
135
+ * // Result:
136
+ * // {
137
+ * // name: "John",
138
+ * // status: "active", // Overridden
139
+ * // displayName: "User: John", // Added
140
+ * // }
141
+ * ```
142
+ *
143
+ * @example
144
+ * **Real-world evaluator usage:**
145
+ * ```typescript
146
+ * // Your data structure
147
+ * const example = {
148
+ * question: "What is AI?",
149
+ * context: "AI is artificial intelligence...",
150
+ * answer: "AI stands for artificial intelligence",
151
+ * };
152
+ *
153
+ * // Evaluator expects: { input, reference, output }
154
+ * const mapping: ObjectMapping<typeof example> = {
155
+ * input: "question",
156
+ * reference: "context",
157
+ * output: "answer",
158
+ * };
159
+ *
160
+ * const remapped = remapObject(example, mapping);
161
+ * // Now remapped has: { question, context, answer, input, reference, output }
162
+ * // The evaluator can access input, reference, and output fields
163
+ * ```
164
+ */
165
+ export declare function remapObject<DataType extends Record<string, unknown>>(data: DataType, mapping: ObjectMapping<DataType>): DataType;
166
+ //# sourceMappingURL=objectMappingUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"objectMappingUtils.d.ts","sourceRoot":"","sources":["../../../src/utils/objectMappingUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAe,MAAM,eAAe,CAAC;AAI3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkKG;AACH,wBAAgB,WAAW,CAAC,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAClE,IAAI,EAAE,QAAQ,EACd,OAAO,EAAE,aAAa,CAAC,QAAQ,CAAC,GAC/B,QAAQ,CAUV"}
@@ -0,0 +1,191 @@
1
+ import { JSONPath } from "jsonpath-plus";
2
+ /**
3
+ * Remaps an object by applying field mappings while preserving original data.
4
+ *
5
+ * This function takes your original data object and a mapping configuration,
6
+ * then returns a new object that contains:
7
+ * - All original fields from the input data
8
+ * - Additional/overridden fields based on the mapping
9
+ *
10
+ * The mapping allows you to extract values using:
11
+ * - Simple property names: `"fieldName"`
12
+ * - Dot notation: `"user.profile.name"`
13
+ * - Array access: `"items[0].id"`
14
+ * - JSONPath expressions: `"$.items[*].id"`
15
+ * - Function extractors: `(data) => data.customField`
16
+ *
17
+ * @param data - The input data object to remap
18
+ * @param mapping - The mapping configuration defining how to extract/transform values
19
+ * @returns A new object with original fields plus mapped fields
20
+ *
21
+ * @example
22
+ * **Basic remapping:**
23
+ * ```typescript
24
+ * const data = {
25
+ * name: "John",
26
+ * age: 30,
27
+ * email: "john@example.com",
28
+ * };
29
+ *
30
+ * const mapping: ObjectMapping<typeof data> = {
31
+ * userName: "name",
32
+ * userAge: "age",
33
+ * };
34
+ *
35
+ * const result = remapObject(data, mapping);
36
+ * // Result: {
37
+ * // name: "John",
38
+ * // age: 30,
39
+ * // email: "john@example.com",
40
+ * // userName: "John", // Added from mapping
41
+ * // userAge: 30, // Added from mapping
42
+ * // }
43
+ * ```
44
+ *
45
+ * @example
46
+ * **Nested property extraction:**
47
+ * ```typescript
48
+ * const data = {
49
+ * user: {
50
+ * profile: {
51
+ * firstName: "John",
52
+ * lastName: "Doe",
53
+ * },
54
+ * email: "john@example.com",
55
+ * },
56
+ * };
57
+ *
58
+ * const mapping: ObjectMapping<typeof data> = {
59
+ * firstName: "user.profile.firstName",
60
+ * lastName: "user.profile.lastName",
61
+ * email: "user.email",
62
+ * };
63
+ *
64
+ * const result = remapObject(data, mapping);
65
+ * // Result includes original data plus:
66
+ * // {
67
+ * // firstName: "John",
68
+ * // lastName: "Doe",
69
+ * // email: "john@example.com",
70
+ * // }
71
+ * ```
72
+ *
73
+ * @example
74
+ * **Array element access:**
75
+ * ```typescript
76
+ * const data = {
77
+ * items: [
78
+ * { id: 1, name: "Apple" },
79
+ * { id: 2, name: "Banana" },
80
+ * ],
81
+ * };
82
+ *
83
+ * const mapping: ObjectMapping<typeof data> = {
84
+ * firstItemId: "items[0].id",
85
+ * firstItemName: "items[0].name",
86
+ * };
87
+ *
88
+ * const result = remapObject(data, mapping);
89
+ * // Result includes:
90
+ * // {
91
+ * // firstItemId: 1,
92
+ * // firstItemName: "Apple",
93
+ * // }
94
+ * ```
95
+ *
96
+ * @example
97
+ * **Function-based transformation:**
98
+ * ```typescript
99
+ * const data = {
100
+ * firstName: "John",
101
+ * lastName: "Doe",
102
+ * scores: [85, 92, 78],
103
+ * };
104
+ *
105
+ * const mapping: ObjectMapping<typeof data> = {
106
+ * fullName: (data) => `${data.firstName} ${data.lastName}`,
107
+ * averageScore: (data) =>
108
+ * data.scores.reduce((a, b) => a + b, 0) / data.scores.length,
109
+ * };
110
+ *
111
+ * const result = remapObject(data, mapping);
112
+ * // Result includes:
113
+ * // {
114
+ * // fullName: "John Doe",
115
+ * // averageScore: 85,
116
+ * // }
117
+ * ```
118
+ *
119
+ * @example
120
+ * **Field override:**
121
+ * ```typescript
122
+ * const data = {
123
+ * name: "John",
124
+ * status: "inactive",
125
+ * };
126
+ *
127
+ * const mapping: ObjectMapping<typeof data> = {
128
+ * // Override existing field
129
+ * status: (data) => data.name === "John" ? "active" : "inactive",
130
+ * // Add new field
131
+ * displayName: (data) => `User: ${data.name}`,
132
+ * };
133
+ *
134
+ * const result = remapObject(data, mapping);
135
+ * // Result:
136
+ * // {
137
+ * // name: "John",
138
+ * // status: "active", // Overridden
139
+ * // displayName: "User: John", // Added
140
+ * // }
141
+ * ```
142
+ *
143
+ * @example
144
+ * **Real-world evaluator usage:**
145
+ * ```typescript
146
+ * // Your data structure
147
+ * const example = {
148
+ * question: "What is AI?",
149
+ * context: "AI is artificial intelligence...",
150
+ * answer: "AI stands for artificial intelligence",
151
+ * };
152
+ *
153
+ * // Evaluator expects: { input, reference, output }
154
+ * const mapping: ObjectMapping<typeof example> = {
155
+ * input: "question",
156
+ * reference: "context",
157
+ * output: "answer",
158
+ * };
159
+ *
160
+ * const remapped = remapObject(example, mapping);
161
+ * // Now remapped has: { question, context, answer, input, reference, output }
162
+ * // The evaluator can access input, reference, and output fields
163
+ * ```
164
+ */
165
+ export function remapObject(data, mapping) {
166
+ return {
167
+ ...data,
168
+ ...Object.fromEntries(Object.entries(mapping).map(([key, value]) => [
169
+ key,
170
+ getMappedObjectValue(data, value),
171
+ ])),
172
+ };
173
+ }
174
+ /**
175
+ * Extracts a value from a data object using a value getter.
176
+ *
177
+ * This internal function handles the actual extraction logic, supporting both
178
+ * string-based paths (including JSONPath) and function-based extractors.
179
+ *
180
+ * @param data - The data object to extract from
181
+ * @param valueGetter - The value getter (string path or function)
182
+ * @returns The extracted value
183
+ *
184
+ * @internal
185
+ */
186
+ function getMappedObjectValue(data, valueGetter) {
187
+ return typeof valueGetter === "function"
188
+ ? valueGetter(data)
189
+ : JSONPath({ path: valueGetter, json: data, wrap: false });
190
+ }
191
+ //# sourceMappingURL=objectMappingUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"objectMappingUtils.js","sourceRoot":"","sources":["../../../src/utils/objectMappingUtils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkKG;AACH,MAAM,UAAU,WAAW,CACzB,IAAc,EACd,OAAgC;IAEhC,OAAO;QACL,GAAG,IAAI;QACP,GAAG,MAAM,CAAC,WAAW,CACnB,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC;YAC5C,GAAG;YACH,oBAAoB,CAAC,IAAI,EAAE,KAAK,CAAC;SAClC,CAAC,CACH;KACF,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;GAWG;AACH,SAAS,oBAAoB,CAC3B,IAAc,EACd,WAAkC;IAElC,OAAO,OAAO,WAAW,KAAK,UAAU;QACtC,CAAC,CAAC,WAAW,CAAC,IAAI,CAAC;QACnB,CAAC,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC;AAC/D,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { CreateEvaluatorArgs, EvaluationKind, EvaluationResult, EvaluatorInterface, OptimizationDirection, TelemetryConfig } from "../types";
2
+ import { ObjectMapping } from "../types/data";
3
+ /**
4
+ * Base class for all evaluators
5
+ */
6
+ export declare abstract class EvaluatorBase<RecordType extends Record<string, unknown>> implements EvaluatorInterface<RecordType> {
7
+ readonly name: string;
8
+ readonly kind: EvaluationKind;
9
+ readonly optimizationDirection?: OptimizationDirection;
10
+ readonly inputMapping?: ObjectMapping<RecordType>;
11
+ readonly telemetry?: TelemetryConfig;
12
+ constructor({ name, kind, optimizationDirection, inputMapping, telemetry, }: CreateEvaluatorArgs<RecordType>);
13
+ abstract evaluate(_example: RecordType): Promise<EvaluationResult>;
14
+ /**
15
+ * Binds the input mapping to the evaluator. It makes a a copy of the evaluator and returns it.
16
+ */
17
+ abstract bindInputMapping(inputMapping: ObjectMapping<RecordType>): EvaluatorBase<RecordType>;
18
+ }
19
+ //# sourceMappingURL=EvaluatorBase.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EvaluatorBase.d.ts","sourceRoot":"","sources":["../../../src/core/EvaluatorBase.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,EACrB,eAAe,EAChB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C;;GAEG;AACH,8BAAsB,aAAa,CAAC,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAC5E,YAAW,kBAAkB,CAAC,UAAU,CAAC;IAEzC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAC9B,QAAQ,CAAC,qBAAqB,CAAC,EAAE,qBAAqB,CAAC;IACvD,QAAQ,CAAC,YAAY,CAAC,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IAClD,QAAQ,CAAC,SAAS,CAAC,EAAE,eAAe,CAAC;gBACzB,EACV,IAAI,EACJ,IAAI,EACJ,qBAAqB,EACrB,YAAY,EACZ,SAAS,GACV,EAAE,mBAAmB,CAAC,UAAU,CAAC;IAOlC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,EAAE,UAAU,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAElE;;OAEG;IACH,QAAQ,CAAC,gBAAgB,CACvB,YAAY,EAAE,aAAa,CAAC,UAAU,CAAC,GACtC,aAAa,CAAC,UAAU,CAAC;CAC7B"}
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EvaluatorBase = void 0;
4
+ /**
5
+ * Base class for all evaluators
6
+ */
7
+ class EvaluatorBase {
8
+ constructor({ name, kind, optimizationDirection, inputMapping, telemetry, }) {
9
+ this.name = name;
10
+ this.kind = kind;
11
+ this.optimizationDirection = optimizationDirection;
12
+ this.inputMapping = inputMapping;
13
+ this.telemetry = telemetry;
14
+ }
15
+ }
16
+ exports.EvaluatorBase = EvaluatorBase;
17
+ //# sourceMappingURL=EvaluatorBase.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EvaluatorBase.js","sourceRoot":"","sources":["../../../src/core/EvaluatorBase.ts"],"names":[],"mappings":";;;AAUA;;GAEG;AACH,MAAsB,aAAa;IAQjC,YAAY,EACV,IAAI,EACJ,IAAI,EACJ,qBAAqB,EACrB,YAAY,EACZ,SAAS,GACuB;QAChC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,qBAAqB,GAAG,qBAAqB,CAAC;QACnD,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;CASF;AA7BD,sCA6BC"}
@@ -1,4 +1,5 @@
1
1
  export * from "./llm";
2
2
  export * from "./template";
3
3
  export * from "./types";
4
+ export * from "./utils";
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
package/dist/src/index.js CHANGED
@@ -17,4 +17,5 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./llm"), exports);
18
18
  __exportStar(require("./template"), exports);
19
19
  __exportStar(require("./types"), exports);
20
+ __exportStar(require("./utils"), exports);
20
21
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wCAAsB;AACtB,6CAA2B;AAC3B,0CAAwB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wCAAsB;AACtB,6CAA2B;AAC3B,0CAAwB;AACxB,0CAAwB"}
@@ -1,17 +1,34 @@
1
- import { CreateClassificationEvaluatorArgs, EvaluatorFn, Template, WithPromptTemplate } from "../types";
1
+ import { ClassificationChoicesMap, CreateClassificationEvaluatorArgs, EvaluatorFn, PromptTemplate, WithPromptTemplate } from "../types";
2
+ import { ObjectMapping } from "../types/data";
2
3
  import { LLMEvaluator } from "./LLMEvaluator";
4
+ import { LanguageModel } from "ai";
3
5
  /**
4
6
  * An LLM evaluator that performs evaluation via classification
5
7
  */
6
8
  export declare class ClassificationEvaluator<RecordType extends Record<string, unknown>> extends LLMEvaluator<RecordType> implements WithPromptTemplate {
7
9
  readonly evaluatorFn: EvaluatorFn<RecordType>;
8
- readonly promptTemplate: Template;
10
+ readonly promptTemplate: PromptTemplate;
11
+ /**
12
+ * A dynamically computed set of prompt template variables
13
+ */
9
14
  private _promptTemplateVariables;
10
- constructor(args: CreateClassificationEvaluatorArgs);
15
+ /**
16
+ * The model to use for classification
17
+ */
18
+ readonly model: LanguageModel;
19
+ /**
20
+ * The choices to classify the example into
21
+ */
22
+ readonly choices: ClassificationChoicesMap;
23
+ constructor(args: CreateClassificationEvaluatorArgs<RecordType>);
11
24
  evaluate: (example: RecordType) => Promise<import("../types").EvaluationResult>;
12
25
  /**
13
26
  * List out the prompt template variables needed to perform evaluation
14
27
  */
15
28
  get promptTemplateVariables(): string[];
29
+ /**
30
+ * Binds the input mapping to the evaluator. It makes a a copy of the evaluator and returns it.
31
+ */
32
+ bindInputMapping(inputMapping: ObjectMapping<RecordType>): ClassificationEvaluator<RecordType>;
16
33
  }
17
34
  //# sourceMappingURL=ClassificationEvaluator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ClassificationEvaluator.d.ts","sourceRoot":"","sources":["../../../src/llm/ClassificationEvaluator.ts"],"names":[],"mappings":"AACA,OAAO,EACL,iCAAiC,EACjC,WAAW,EACX,QAAQ,EACR,kBAAkB,EACnB,MAAM,UAAU,CAAC;AAGlB,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,qBAAa,uBAAuB,CAAC,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAC7E,SAAQ,YAAY,CAAC,UAAU,CAC/B,YAAW,kBAAkB;IAE7B,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC,UAAU,CAAC,CAAC;IAC9C,QAAQ,CAAC,cAAc,EAAE,QAAQ,CAAC;IAClC,OAAO,CAAC,wBAAwB,CAAuB;gBAC3C,IAAI,EAAE,iCAAiC;IAKnD,QAAQ,GAAI,SAAS,UAAU,kDAE7B;IACF;;OAEG;IACH,IAAI,uBAAuB,IAAI,MAAM,EAAE,CAStC;CACF"}
1
+ {"version":3,"file":"ClassificationEvaluator.d.ts","sourceRoot":"","sources":["../../../src/llm/ClassificationEvaluator.ts"],"names":[],"mappings":"AACA,OAAO,EACL,wBAAwB,EACxB,iCAAiC,EACjC,WAAW,EACX,cAAc,EACd,kBAAkB,EACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAI9C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAEnC;;GAEG;AACH,qBAAa,uBAAuB,CAAC,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAC7E,SAAQ,YAAY,CAAC,UAAU,CAC/B,YAAW,kBAAkB;IAE7B,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC,UAAU,CAAC,CAAC;IAC9C,QAAQ,CAAC,cAAc,EAAE,cAAc,CAAC;IACxC;;OAEG;IACH,OAAO,CAAC,wBAAwB,CAAuB;IACvD;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B;;OAEG;IACH,QAAQ,CAAC,OAAO,EAAE,wBAAwB,CAAC;gBAE/B,IAAI,EAAE,iCAAiC,CAAC,UAAU,CAAC;IAS/D,QAAQ,GAAI,SAAS,UAAU,kDAM7B;IACF;;OAEG;IACH,IAAI,uBAAuB,IAAI,MAAM,EAAE,CAStC;IACD;;OAEG;IACH,gBAAgB,CACd,YAAY,EAAE,aAAa,CAAC,UAAU,CAAC,GACtC,uBAAuB,CAAC,UAAU,CAAC;CAMvC"}