@arizeai/phoenix-evals 0.6.1 → 0.6.2

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 (84) hide show
  1. package/dist/esm/core/FunctionEvaluator.d.ts +16 -0
  2. package/dist/esm/core/FunctionEvaluator.d.ts.map +1 -0
  3. package/dist/esm/core/FunctionEvaluator.js +18 -0
  4. package/dist/esm/core/FunctionEvaluator.js.map +1 -0
  5. package/dist/esm/helpers/asEvaluatorFn.d.ts +6 -0
  6. package/dist/esm/helpers/asEvaluatorFn.d.ts.map +1 -0
  7. package/dist/esm/helpers/asEvaluatorFn.js +15 -0
  8. package/dist/esm/helpers/asEvaluatorFn.js.map +1 -0
  9. package/dist/esm/helpers/createEvaluator.d.ts +141 -0
  10. package/dist/esm/helpers/createEvaluator.d.ts.map +1 -0
  11. package/dist/esm/helpers/createEvaluator.js +96 -0
  12. package/dist/esm/helpers/createEvaluator.js.map +1 -0
  13. package/dist/esm/helpers/index.d.ts +2 -0
  14. package/dist/esm/helpers/index.d.ts.map +1 -0
  15. package/dist/esm/helpers/index.js +2 -0
  16. package/dist/esm/helpers/index.js.map +1 -0
  17. package/dist/esm/helpers/toEvaluationResult.d.ts +67 -0
  18. package/dist/esm/helpers/toEvaluationResult.d.ts.map +1 -0
  19. package/dist/esm/helpers/toEvaluationResult.js +133 -0
  20. package/dist/esm/helpers/toEvaluationResult.js.map +1 -0
  21. package/dist/esm/index.d.ts +1 -0
  22. package/dist/esm/index.d.ts.map +1 -1
  23. package/dist/esm/index.js +1 -0
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  26. package/dist/esm/types/base.d.ts +2 -0
  27. package/dist/esm/types/base.d.ts.map +1 -0
  28. package/dist/esm/types/base.js +2 -0
  29. package/dist/esm/types/base.js.map +1 -0
  30. package/dist/esm/types/index.d.ts +1 -0
  31. package/dist/esm/types/index.d.ts.map +1 -1
  32. package/dist/esm/types/index.js +1 -0
  33. package/dist/esm/types/index.js.map +1 -1
  34. package/dist/esm/utils/typeUtils.d.ts +7 -0
  35. package/dist/esm/utils/typeUtils.d.ts.map +1 -0
  36. package/dist/esm/utils/typeUtils.js +13 -0
  37. package/dist/esm/utils/typeUtils.js.map +1 -0
  38. package/dist/src/core/FunctionEvaluator.d.ts +16 -0
  39. package/dist/src/core/FunctionEvaluator.d.ts.map +1 -0
  40. package/dist/src/core/FunctionEvaluator.js +33 -0
  41. package/dist/src/core/FunctionEvaluator.js.map +1 -0
  42. package/dist/src/helpers/asEvaluatorFn.d.ts +6 -0
  43. package/dist/src/helpers/asEvaluatorFn.d.ts.map +1 -0
  44. package/dist/src/helpers/asEvaluatorFn.js +18 -0
  45. package/dist/src/helpers/asEvaluatorFn.js.map +1 -0
  46. package/dist/src/helpers/createEvaluator.d.ts +141 -0
  47. package/dist/src/helpers/createEvaluator.d.ts.map +1 -0
  48. package/dist/src/helpers/createEvaluator.js +99 -0
  49. package/dist/src/helpers/createEvaluator.js.map +1 -0
  50. package/dist/src/helpers/index.d.ts +2 -0
  51. package/dist/src/helpers/index.d.ts.map +1 -0
  52. package/dist/src/helpers/index.js +18 -0
  53. package/dist/src/helpers/index.js.map +1 -0
  54. package/dist/src/helpers/toEvaluationResult.d.ts +67 -0
  55. package/dist/src/helpers/toEvaluationResult.d.ts.map +1 -0
  56. package/dist/src/helpers/toEvaluationResult.js +136 -0
  57. package/dist/src/helpers/toEvaluationResult.js.map +1 -0
  58. package/dist/src/index.d.ts +1 -0
  59. package/dist/src/index.d.ts.map +1 -1
  60. package/dist/src/index.js +1 -0
  61. package/dist/src/index.js.map +1 -1
  62. package/dist/src/types/base.d.ts +2 -0
  63. package/dist/src/types/base.d.ts.map +1 -0
  64. package/dist/src/types/base.js +3 -0
  65. package/dist/src/types/base.js.map +1 -0
  66. package/dist/src/types/index.d.ts +1 -0
  67. package/dist/src/types/index.d.ts.map +1 -1
  68. package/dist/src/types/index.js +1 -0
  69. package/dist/src/types/index.js.map +1 -1
  70. package/dist/src/utils/typeUtils.d.ts +7 -0
  71. package/dist/src/utils/typeUtils.d.ts.map +1 -0
  72. package/dist/src/utils/typeUtils.js +16 -0
  73. package/dist/src/utils/typeUtils.js.map +1 -0
  74. package/dist/tsconfig.tsbuildinfo +1 -1
  75. package/package.json +7 -1
  76. package/src/core/FunctionEvaluator.ts +28 -0
  77. package/src/helpers/asEvaluatorFn.ts +19 -0
  78. package/src/helpers/createEvaluator.ts +184 -0
  79. package/src/helpers/index.ts +1 -0
  80. package/src/helpers/toEvaluationResult.ts +145 -0
  81. package/src/index.ts +1 -0
  82. package/src/types/base.ts +2 -0
  83. package/src/types/index.ts +1 -0
  84. package/src/utils/typeUtils.ts +14 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arizeai/phoenix-evals",
3
- "version": "0.6.1",
3
+ "version": "0.6.2",
4
4
  "description": "A library for running evaluations for AI use cases",
5
5
  "main": "dist/src/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -25,6 +25,10 @@
25
25
  "./types/*": {
26
26
  "import": "./dist/esm/types/*.d.ts",
27
27
  "require": "./dist/src/types/*.d.ts"
28
+ },
29
+ "./helpers/*": {
30
+ "import": "./dist/esm/helpers/*.js",
31
+ "require": "./dist/src/helpers/*.js"
28
32
  }
29
33
  },
30
34
  "files": [
@@ -45,6 +49,7 @@
45
49
  "@ai-sdk/google": "^2.0.40",
46
50
  "@ai-sdk/openai": "^2.0.0",
47
51
  "@arizeai/openinference-instrumentation-openai": "^2.3.0",
52
+ "@opentelemetry/sdk-trace-node": "^1.30.1",
48
53
  "@types/mustache": "^4.2.6",
49
54
  "@types/node": "^24.0.10",
50
55
  "msw": "^2.11.6",
@@ -58,6 +63,7 @@
58
63
  "node": ">=18"
59
64
  },
60
65
  "dependencies": {
66
+ "@arizeai/openinference-core": "^2.0.0",
61
67
  "@opentelemetry/api": "^1.9.0",
62
68
  "ai": "^5.0.0",
63
69
  "jsonpath-plus": "^10.3.0",
@@ -0,0 +1,28 @@
1
+ import { CreateEvaluatorArgs, EvaluationResult, ObjectMapping } from "../types";
2
+
3
+ import { EvaluatorBase } from "./EvaluatorBase";
4
+
5
+ type FunctionEvaluatorArgs<RecordType extends Record<string, unknown>> =
6
+ CreateEvaluatorArgs<RecordType> & {
7
+ evaluateFn: (args: RecordType) => Promise<EvaluationResult>;
8
+ };
9
+ /**
10
+ * A class that constructs an evaluator based on an evaluate function.
11
+ */
12
+ export class FunctionEvaluator<
13
+ RecordType extends Record<string, unknown>,
14
+ > extends EvaluatorBase<RecordType> {
15
+ readonly evaluateFn: (args: RecordType) => Promise<EvaluationResult>;
16
+ constructor({ evaluateFn, ...args }: FunctionEvaluatorArgs<RecordType>) {
17
+ super({ ...args });
18
+ this.evaluateFn = evaluateFn;
19
+ }
20
+ async evaluate(args: RecordType): Promise<EvaluationResult> {
21
+ return this.evaluateFn(args);
22
+ }
23
+ bindInputMapping(
24
+ inputMapping: ObjectMapping<RecordType>
25
+ ): FunctionEvaluator<RecordType> {
26
+ return new FunctionEvaluator({ ...this, inputMapping });
27
+ }
28
+ }
@@ -0,0 +1,19 @@
1
+ import { AnyFn, EvaluatorFn } from "../types";
2
+ import { isPromise } from "../utils/typeUtils";
3
+
4
+ import { toEvaluationResult } from "./toEvaluationResult";
5
+
6
+ /**
7
+ * A function that converts a generic function into an evaluator function
8
+ */
9
+ export function asEvaluatorFn<RecordType extends Record<string, unknown>>(
10
+ fn: AnyFn
11
+ ): EvaluatorFn<RecordType> {
12
+ return async (...args) => {
13
+ let result = fn(...args);
14
+ if (isPromise(result)) {
15
+ result = await result;
16
+ }
17
+ return toEvaluationResult(result);
18
+ };
19
+ }
@@ -0,0 +1,184 @@
1
+ import { withSpan } from "@arizeai/openinference-core";
2
+
3
+ import { EvaluatorBase } from "../core/EvaluatorBase";
4
+ import { FunctionEvaluator } from "../core/FunctionEvaluator";
5
+ import {
6
+ EvaluationKind,
7
+ OptimizationDirection,
8
+ TelemetryConfig,
9
+ } from "../types";
10
+
11
+ import { asEvaluatorFn } from "./asEvaluatorFn";
12
+
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ type AnyFn = (...args: any[]) => any;
15
+
16
+ function generateUniqueName(): string {
17
+ return `evaluator-${Math.random().toString(36).substring(2, 15)}`;
18
+ }
19
+
20
+ /**
21
+ * Options for creating a custom evaluator using {@link CreateEvaluator}.
22
+ *
23
+ * @public
24
+ */
25
+ export type CreateEvaluatorOptions = {
26
+ /**
27
+ * The name of the evaluator / metric that it measures.
28
+ *
29
+ * If not provided, the function will attempt to infer the name from the function's `name` property.
30
+ * If the function has no name, a unique name will be generated.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * const evaluator = CreateEvaluator(myFunction, { name: "custom-metric" });
35
+ * ```
36
+ */
37
+ name?: string;
38
+ /**
39
+ * The kind of the evaluation.
40
+ *
41
+ * - `"CODE"`: Code-based evaluator that runs custom logic
42
+ * - `"LLM"`: LLM-based evaluator that uses a language model
43
+ *
44
+ * @defaultValue `"CODE"`
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const evaluator = CreateEvaluator(myFunction, { kind: "CODE" });
49
+ * ```
50
+ */
51
+ kind?: EvaluationKind;
52
+ /**
53
+ * The direction to optimize the numeric evaluation score.
54
+ *
55
+ * - `"MAXIMIZE"`: Higher scores are better (e.g., accuracy, F1 score)
56
+ * - `"MINIMIZE"`: Lower scores are better (e.g., error rate, latency)
57
+ *
58
+ * @defaultValue `"MAXIMIZE"`
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const evaluator = CreateEvaluator(myFunction, {
63
+ * optimizationDirection: "MAXIMIZE"
64
+ * });
65
+ * ```
66
+ */
67
+ optimizationDirection?: OptimizationDirection;
68
+ /**
69
+ * The telemetry configuration for the evaluator.
70
+ *
71
+ * When enabled, the evaluator will automatically create OpenTelemetry spans
72
+ * for tracing and observability. This allows you to track evaluator performance
73
+ * and debug issues in distributed systems.
74
+ *
75
+ * @defaultValue `{ isEnabled: true }`
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const evaluator = CreateEvaluator(myFunction, {
80
+ * telemetry: { isEnabled: true, tracer: myTracer }
81
+ * });
82
+ * ```
83
+ */
84
+ telemetry?: TelemetryConfig;
85
+ };
86
+
87
+ /**
88
+ * A factory function for creating a custom evaluator from any function.
89
+ *
90
+ * This function wraps a user-provided function into an evaluator that can be used
91
+ * with Phoenix experiments and evaluations. The function can be synchronous or
92
+ * asynchronous, and can return a number, an {@link EvaluationResult} object, or
93
+ * a value that will be automatically converted to an evaluation result.
94
+ *
95
+ * The evaluator will automatically:
96
+ * - Convert the function's return value to an {@link EvaluationResult}
97
+ * - Handle both sync and async functions
98
+ * - Wrap the function with OpenTelemetry spans if telemetry is enabled
99
+ * - Infer the evaluator name from the function name if not provided
100
+ *
101
+ * @typeParam RecordType - The type of the input record that the evaluator expects.
102
+ * Must extend `Record<string, unknown>`.
103
+ * @typeParam Fn - The type of the function being wrapped. Must be a function that
104
+ * accepts the record type and returns a value compatible with {@link EvaluationResult}.
105
+ *
106
+ * @param fn - The function to wrap as an evaluator. Can be synchronous or asynchronous.
107
+ * The function should accept a record of type `RecordType` and return either:
108
+ * - A number (will be converted to `{ score: number }`)
109
+ * - An {@link EvaluationResult} object
110
+ * - Any value that can be converted to an evaluation result
111
+ *
112
+ * @param options - Optional configuration for the evaluator. See {@link CreateEvaluatorOptions}
113
+ * for details on available options.
114
+ *
115
+ * @returns An {@link EvaluatorInterface} that can be used with Phoenix experiments
116
+ * and evaluation workflows.
117
+ *
118
+ * @example
119
+ * Basic usage with a simple scoring function:
120
+ * ```typescript
121
+ * const accuracyEvaluator = CreateEvaluator(
122
+ * ({ output, expected }) => {
123
+ * return output === expected ? 1 : 0;
124
+ * },
125
+ * {
126
+ * name: "accuracy",
127
+ * kind: "CODE",
128
+ * optimizationDirection: "MAXIMIZE"
129
+ * }
130
+ * );
131
+ *
132
+ * const result = await accuracyEvaluator.evaluate({
133
+ * output: "correct answer",
134
+ * expected: "correct answer"
135
+ * });
136
+ * // result: { score: 1 }
137
+ * ```
138
+ *
139
+ *
140
+ * @example
141
+ * Returning a full EvaluationResult:
142
+ * ```typescript
143
+ * const qualityEvaluator = CreateEvaluator(
144
+ * ({ output }) => {
145
+ * const score = calculateQuality(output);
146
+ * return {
147
+ * score,
148
+ * label: score > 0.8 ? "high" : "low",
149
+ * explanation: `Quality score: ${score}`
150
+ * };
151
+ * },
152
+ * { name: "quality" }
153
+ * );
154
+ * ```
155
+ */
156
+ export function createEvaluator<
157
+ RecordType extends Record<string, unknown> = Record<string, unknown>,
158
+ Fn extends AnyFn = AnyFn,
159
+ >(fn: Fn, options?: CreateEvaluatorOptions): EvaluatorBase<RecordType> {
160
+ const {
161
+ name,
162
+ kind,
163
+ optimizationDirection,
164
+ telemetry = { isEnabled: true },
165
+ } = options || {};
166
+ const evaluatorName = name || fn.name || generateUniqueName();
167
+ let evaluateFn = asEvaluatorFn<RecordType>(fn);
168
+
169
+ // Add OpenTelemetry span wrapping if telemetry is enabled
170
+ if (telemetry && telemetry.isEnabled) {
171
+ evaluateFn = withSpan(evaluateFn, {
172
+ tracer: telemetry.tracer,
173
+ name: evaluatorName,
174
+ kind: "EVALUATOR",
175
+ });
176
+ }
177
+ return new FunctionEvaluator<RecordType>({
178
+ evaluateFn,
179
+ name: evaluatorName,
180
+ kind: kind || "CODE",
181
+ optimizationDirection: optimizationDirection || "MAXIMIZE",
182
+ telemetry,
183
+ });
184
+ }
@@ -0,0 +1 @@
1
+ export * from "./createEvaluator";
@@ -0,0 +1,145 @@
1
+ import { EvaluationResult } from "../types";
2
+
3
+ /**
4
+ * Type guard to check if a value has a score property.
5
+ *
6
+ * @param result - The value to check
7
+ * @returns True if the value is an object with a numeric score property
8
+ *
9
+ * @internal
10
+ */
11
+ function resultHasScore(result: unknown): result is { score: number } {
12
+ return (
13
+ typeof result === "object" &&
14
+ result !== null &&
15
+ "score" in result &&
16
+ typeof result.score === "number"
17
+ );
18
+ }
19
+
20
+ /**
21
+ * Type guard to check if a value has a label property.
22
+ *
23
+ * @param result - The value to check
24
+ * @returns True if the value is an object with a string label property
25
+ *
26
+ * @internal
27
+ */
28
+ function resultHasLabel(result: unknown): result is { label: string } {
29
+ return (
30
+ typeof result === "object" &&
31
+ result !== null &&
32
+ "label" in result &&
33
+ typeof result.label === "string"
34
+ );
35
+ }
36
+
37
+ /**
38
+ * Type guard to check if a value has an explanation property.
39
+ *
40
+ * @param result - The value to check
41
+ * @returns True if the value is an object with a string explanation property
42
+ *
43
+ * @internal
44
+ */
45
+ function resultHasExplanation(
46
+ result: unknown
47
+ ): result is { explanation: string } {
48
+ return (
49
+ typeof result === "object" &&
50
+ result !== null &&
51
+ "explanation" in result &&
52
+ typeof result.explanation === "string"
53
+ );
54
+ }
55
+
56
+ /**
57
+ * Converts an unknown value to an {@link EvaluationResult}.
58
+ *
59
+ * This function provides a flexible way to normalize various return types from
60
+ * evaluator functions into a standardized `EvaluationResult` format. It handles
61
+ * multiple input types:
62
+ *
63
+ * - **Numbers**: Converted to `{ score: number }`
64
+ * - **Strings**: Converted to `{ label: string }`
65
+ * - **Objects**: Extracts `score`, `label`, and `explanation` properties if present
66
+ * - **Other types**: Returns an empty `EvaluationResult` object
67
+ *
68
+ * This is particularly useful when creating evaluators from functions that may
69
+ * return different types, ensuring consistent evaluation result formatting.
70
+ *
71
+ * @param result - The value to convert to an EvaluationResult. Can be:
72
+ * - A number (converted to score)
73
+ * - A string (converted to label)
74
+ * - An object with optional `score`, `label`, and/or `explanation` properties
75
+ * - Any other value (returns empty object)
76
+ *
77
+ * @returns An {@link EvaluationResult} object with extracted properties
78
+ *
79
+ * @example
80
+ * Convert a number to an EvaluationResult:
81
+ * ```typescript
82
+ * const result = toEvaluationResult(0.95);
83
+ * // Returns: { score: 0.95 }
84
+ * ```
85
+ *
86
+ * @example
87
+ * Convert a string to an EvaluationResult:
88
+ * ```typescript
89
+ * const result = toEvaluationResult("correct");
90
+ * // Returns: { label: "correct" }
91
+ * ```
92
+ *
93
+ * @example
94
+ * Convert an object with all properties:
95
+ * ```typescript
96
+ * const result = toEvaluationResult({
97
+ * score: 0.9,
98
+ * label: "high",
99
+ * explanation: "High quality output"
100
+ * });
101
+ * // Returns: { score: 0.9, label: "high", explanation: "High quality output" }
102
+ * ```
103
+ *
104
+ * @example
105
+ * Convert an object with partial properties:
106
+ * ```typescript
107
+ * const result = toEvaluationResult({ score: 0.8 });
108
+ * // Returns: { score: 0.8 }
109
+ * ```
110
+ *
111
+ * @example
112
+ * Handle null or undefined:
113
+ * ```typescript
114
+ * const result = toEvaluationResult(null);
115
+ * // Returns: {}
116
+ * ```
117
+ *
118
+ * @public
119
+ */
120
+ export function toEvaluationResult(result: unknown): EvaluationResult {
121
+ if (typeof result === "number") {
122
+ return {
123
+ score: result,
124
+ };
125
+ }
126
+ if (typeof result === "string") {
127
+ return {
128
+ label: result,
129
+ };
130
+ }
131
+ if (typeof result === "object" && result !== null) {
132
+ const evaluationResult: EvaluationResult = {};
133
+ if (resultHasScore(result)) {
134
+ evaluationResult.score = result.score;
135
+ }
136
+ if (resultHasLabel(result)) {
137
+ evaluationResult.label = result.label;
138
+ }
139
+ if (resultHasExplanation(result)) {
140
+ evaluationResult.explanation = result.explanation;
141
+ }
142
+ return evaluationResult;
143
+ }
144
+ return {};
145
+ }
package/src/index.ts CHANGED
@@ -2,3 +2,4 @@ export * from "./llm";
2
2
  export * from "./template";
3
3
  export * from "./types";
4
4
  export * from "./utils";
5
+ export * from "./helpers";
@@ -0,0 +1,2 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export type AnyFn = (...args: any[]) => any;
@@ -2,3 +2,4 @@ export * from "./evals";
2
2
  export * from "./templating";
3
3
  export * from "./otel";
4
4
  export * from "./data";
5
+ export * from "./base";
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Type guard for if a function is a Promise
3
+ * @param value
4
+ * @returns true if it is a Promise
5
+ */
6
+ export function isPromise<T = unknown>(value: unknown): value is Promise<T> {
7
+ return (
8
+ !!value &&
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ typeof (value as any)?.then === "function" &&
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ typeof (value as any)?.catch === "function"
13
+ );
14
+ }