@arizeai/phoenix-evals 0.6.1 → 0.6.5

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 (85) hide show
  1. package/README.md +1 -0
  2. package/dist/esm/core/FunctionEvaluator.d.ts +16 -0
  3. package/dist/esm/core/FunctionEvaluator.d.ts.map +1 -0
  4. package/dist/esm/core/FunctionEvaluator.js +18 -0
  5. package/dist/esm/core/FunctionEvaluator.js.map +1 -0
  6. package/dist/esm/helpers/asEvaluatorFn.d.ts +6 -0
  7. package/dist/esm/helpers/asEvaluatorFn.d.ts.map +1 -0
  8. package/dist/esm/helpers/asEvaluatorFn.js +15 -0
  9. package/dist/esm/helpers/asEvaluatorFn.js.map +1 -0
  10. package/dist/esm/helpers/createEvaluator.d.ts +141 -0
  11. package/dist/esm/helpers/createEvaluator.d.ts.map +1 -0
  12. package/dist/esm/helpers/createEvaluator.js +96 -0
  13. package/dist/esm/helpers/createEvaluator.js.map +1 -0
  14. package/dist/esm/helpers/index.d.ts +4 -0
  15. package/dist/esm/helpers/index.d.ts.map +1 -0
  16. package/dist/esm/helpers/index.js +4 -0
  17. package/dist/esm/helpers/index.js.map +1 -0
  18. package/dist/esm/helpers/toEvaluationResult.d.ts +67 -0
  19. package/dist/esm/helpers/toEvaluationResult.d.ts.map +1 -0
  20. package/dist/esm/helpers/toEvaluationResult.js +133 -0
  21. package/dist/esm/helpers/toEvaluationResult.js.map +1 -0
  22. package/dist/esm/index.d.ts +1 -0
  23. package/dist/esm/index.d.ts.map +1 -1
  24. package/dist/esm/index.js +1 -0
  25. package/dist/esm/index.js.map +1 -1
  26. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  27. package/dist/esm/types/base.d.ts +2 -0
  28. package/dist/esm/types/base.d.ts.map +1 -0
  29. package/dist/esm/types/base.js +2 -0
  30. package/dist/esm/types/base.js.map +1 -0
  31. package/dist/esm/types/index.d.ts +1 -0
  32. package/dist/esm/types/index.d.ts.map +1 -1
  33. package/dist/esm/types/index.js +1 -0
  34. package/dist/esm/types/index.js.map +1 -1
  35. package/dist/esm/utils/typeUtils.d.ts +7 -0
  36. package/dist/esm/utils/typeUtils.d.ts.map +1 -0
  37. package/dist/esm/utils/typeUtils.js +13 -0
  38. package/dist/esm/utils/typeUtils.js.map +1 -0
  39. package/dist/src/core/FunctionEvaluator.d.ts +16 -0
  40. package/dist/src/core/FunctionEvaluator.d.ts.map +1 -0
  41. package/dist/src/core/FunctionEvaluator.js +33 -0
  42. package/dist/src/core/FunctionEvaluator.js.map +1 -0
  43. package/dist/src/helpers/asEvaluatorFn.d.ts +6 -0
  44. package/dist/src/helpers/asEvaluatorFn.d.ts.map +1 -0
  45. package/dist/src/helpers/asEvaluatorFn.js +18 -0
  46. package/dist/src/helpers/asEvaluatorFn.js.map +1 -0
  47. package/dist/src/helpers/createEvaluator.d.ts +141 -0
  48. package/dist/src/helpers/createEvaluator.d.ts.map +1 -0
  49. package/dist/src/helpers/createEvaluator.js +99 -0
  50. package/dist/src/helpers/createEvaluator.js.map +1 -0
  51. package/dist/src/helpers/index.d.ts +4 -0
  52. package/dist/src/helpers/index.d.ts.map +1 -0
  53. package/dist/src/helpers/index.js +20 -0
  54. package/dist/src/helpers/index.js.map +1 -0
  55. package/dist/src/helpers/toEvaluationResult.d.ts +67 -0
  56. package/dist/src/helpers/toEvaluationResult.d.ts.map +1 -0
  57. package/dist/src/helpers/toEvaluationResult.js +136 -0
  58. package/dist/src/helpers/toEvaluationResult.js.map +1 -0
  59. package/dist/src/index.d.ts +1 -0
  60. package/dist/src/index.d.ts.map +1 -1
  61. package/dist/src/index.js +1 -0
  62. package/dist/src/index.js.map +1 -1
  63. package/dist/src/types/base.d.ts +2 -0
  64. package/dist/src/types/base.d.ts.map +1 -0
  65. package/dist/src/types/base.js +3 -0
  66. package/dist/src/types/base.js.map +1 -0
  67. package/dist/src/types/index.d.ts +1 -0
  68. package/dist/src/types/index.d.ts.map +1 -1
  69. package/dist/src/types/index.js +1 -0
  70. package/dist/src/types/index.js.map +1 -1
  71. package/dist/src/utils/typeUtils.d.ts +7 -0
  72. package/dist/src/utils/typeUtils.d.ts.map +1 -0
  73. package/dist/src/utils/typeUtils.js +16 -0
  74. package/dist/src/utils/typeUtils.js.map +1 -0
  75. package/dist/tsconfig.tsbuildinfo +1 -1
  76. package/package.json +15 -1
  77. package/src/core/FunctionEvaluator.ts +28 -0
  78. package/src/helpers/asEvaluatorFn.ts +19 -0
  79. package/src/helpers/createEvaluator.ts +184 -0
  80. package/src/helpers/index.ts +3 -0
  81. package/src/helpers/toEvaluationResult.ts +145 -0
  82. package/src/index.ts +1 -0
  83. package/src/types/base.ts +2 -0
  84. package/src/types/index.ts +1 -0
  85. package/src/utils/typeUtils.ts +14 -0
package/README.md CHANGED
@@ -19,6 +19,7 @@
19
19
  <a href="https://arize-ai.github.io/phoenix/">
20
20
  <img src="https://img.shields.io/badge/docs-blue?logo=typescript&logoColor=white" alt="Documentation">
21
21
  </a>
22
+ <img referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=8e8e8b34-7900-43fa-a38f-1f070bd48c64&page=js/packages/phoenix-evals/README.md" />
22
23
  </p>
23
24
 
24
25
  This package provides a TypeScript evaluation library. It is vendor agnostic and can be used in isolation of any framework or platform. This package is still under active development and is subject to change.
@@ -0,0 +1,16 @@
1
+ import { CreateEvaluatorArgs, EvaluationResult, ObjectMapping } from "../types/index.js";
2
+ import { EvaluatorBase } from "./EvaluatorBase.js";
3
+ type FunctionEvaluatorArgs<RecordType extends Record<string, unknown>> = CreateEvaluatorArgs<RecordType> & {
4
+ evaluateFn: (args: RecordType) => Promise<EvaluationResult>;
5
+ };
6
+ /**
7
+ * A class that constructs an evaluator based on an evaluate function.
8
+ */
9
+ export declare class FunctionEvaluator<RecordType extends Record<string, unknown>> extends EvaluatorBase<RecordType> {
10
+ readonly evaluateFn: (args: RecordType) => Promise<EvaluationResult>;
11
+ constructor({ evaluateFn, ...args }: FunctionEvaluatorArgs<RecordType>);
12
+ evaluate(args: RecordType): Promise<EvaluationResult>;
13
+ bindInputMapping(inputMapping: ObjectMapping<RecordType>): FunctionEvaluator<RecordType>;
14
+ }
15
+ export {};
16
+ //# sourceMappingURL=FunctionEvaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FunctionEvaluator.d.ts","sourceRoot":"","sources":["../../../src/core/FunctionEvaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEhF,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD,KAAK,qBAAqB,CAAC,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IACnE,mBAAmB,CAAC,UAAU,CAAC,GAAG;IAChC,UAAU,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC;CAC7D,CAAC;AACJ;;GAEG;AACH,qBAAa,iBAAiB,CAC5B,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAC1C,SAAQ,aAAa,CAAC,UAAU,CAAC;IACjC,QAAQ,CAAC,UAAU,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,OAAO,CAAC,gBAAgB,CAAC,CAAC;gBACzD,EAAE,UAAU,EAAE,GAAG,IAAI,EAAE,EAAE,qBAAqB,CAAC,UAAU,CAAC;IAIhE,QAAQ,CAAC,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAG3D,gBAAgB,CACd,YAAY,EAAE,aAAa,CAAC,UAAU,CAAC,GACtC,iBAAiB,CAAC,UAAU,CAAC;CAGjC"}
@@ -0,0 +1,18 @@
1
+ import { EvaluatorBase } from "./EvaluatorBase.js";
2
+ /**
3
+ * A class that constructs an evaluator based on an evaluate function.
4
+ */
5
+ export class FunctionEvaluator extends EvaluatorBase {
6
+ evaluateFn;
7
+ constructor({ evaluateFn, ...args }) {
8
+ super({ ...args });
9
+ this.evaluateFn = evaluateFn;
10
+ }
11
+ async evaluate(args) {
12
+ return this.evaluateFn(args);
13
+ }
14
+ bindInputMapping(inputMapping) {
15
+ return new FunctionEvaluator({ ...this, inputMapping });
16
+ }
17
+ }
18
+ //# sourceMappingURL=FunctionEvaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FunctionEvaluator.js","sourceRoot":"","sources":["../../../src/core/FunctionEvaluator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAMhD;;GAEG;AACH,MAAM,OAAO,iBAEX,SAAQ,aAAyB;IACxB,UAAU,CAAkD;IACrE,YAAY,EAAE,UAAU,EAAE,GAAG,IAAI,EAAqC;QACpE,KAAK,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;QACnB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IACD,KAAK,CAAC,QAAQ,CAAC,IAAgB;QAC7B,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;IAC/B,CAAC;IACD,gBAAgB,CACd,YAAuC;QAEvC,OAAO,IAAI,iBAAiB,CAAC,EAAE,GAAG,IAAI,EAAE,YAAY,EAAE,CAAC,CAAC;IAC1D,CAAC;CACF"}
@@ -0,0 +1,6 @@
1
+ import { AnyFn, EvaluatorFn } from "../types/index.js";
2
+ /**
3
+ * A function that converts a generic function into an evaluator function
4
+ */
5
+ export declare function asEvaluatorFn<RecordType extends Record<string, unknown>>(fn: AnyFn): EvaluatorFn<RecordType>;
6
+ //# sourceMappingURL=asEvaluatorFn.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asEvaluatorFn.d.ts","sourceRoot":"","sources":["../../../src/helpers/asEvaluatorFn.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAK9C;;GAEG;AACH,wBAAgB,aAAa,CAAC,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACtE,EAAE,EAAE,KAAK,GACR,WAAW,CAAC,UAAU,CAAC,CAQzB"}
@@ -0,0 +1,15 @@
1
+ import { isPromise } from "../utils/typeUtils.js";
2
+ import { toEvaluationResult } from "./toEvaluationResult.js";
3
+ /**
4
+ * A function that converts a generic function into an evaluator function
5
+ */
6
+ export function asEvaluatorFn(fn) {
7
+ return async (...args) => {
8
+ let result = fn(...args);
9
+ if (isPromise(result)) {
10
+ result = await result;
11
+ }
12
+ return toEvaluationResult(result);
13
+ };
14
+ }
15
+ //# sourceMappingURL=asEvaluatorFn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asEvaluatorFn.js","sourceRoot":"","sources":["../../../src/helpers/asEvaluatorFn.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;GAEG;AACH,MAAM,UAAU,aAAa,CAC3B,EAAS;IAET,OAAO,KAAK,EAAE,GAAG,IAAI,EAAE,EAAE;QACvB,IAAI,MAAM,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;QACzB,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;YACtB,MAAM,GAAG,MAAM,MAAM,CAAC;QACxB,CAAC;QACD,OAAO,kBAAkB,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,141 @@
1
+ import { EvaluatorBase } from "../core/EvaluatorBase.js";
2
+ import { EvaluationKind, OptimizationDirection, TelemetryConfig } from "../types/index.js";
3
+ type AnyFn = (...args: any[]) => any;
4
+ /**
5
+ * Options for creating a custom evaluator using {@link createEvaluator}.
6
+ *
7
+ * @public
8
+ */
9
+ export type CreateEvaluatorOptions = {
10
+ /**
11
+ * The name of the evaluator / metric that it measures.
12
+ *
13
+ * If not provided, the function will attempt to infer the name from the function's `name` property.
14
+ * If the function has no name, a unique name will be generated.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const evaluator = createEvaluator(myFunction, { name: "custom-metric" });
19
+ * ```
20
+ */
21
+ name?: string;
22
+ /**
23
+ * The kind of the evaluation.
24
+ *
25
+ * - `"CODE"`: Code-based evaluator that runs custom logic
26
+ * - `"LLM"`: LLM-based evaluator that uses a language model
27
+ *
28
+ * @defaultValue `"CODE"`
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * const evaluator = createEvaluator(myFunction, { kind: "CODE" });
33
+ * ```
34
+ */
35
+ kind?: EvaluationKind;
36
+ /**
37
+ * The direction to optimize the numeric evaluation score.
38
+ *
39
+ * - `"MAXIMIZE"`: Higher scores are better (e.g., accuracy, F1 score)
40
+ * - `"MINIMIZE"`: Lower scores are better (e.g., error rate, latency)
41
+ *
42
+ * @defaultValue `"MAXIMIZE"`
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * const evaluator = createEvaluator(myFunction, {
47
+ * optimizationDirection: "MAXIMIZE"
48
+ * });
49
+ * ```
50
+ */
51
+ optimizationDirection?: OptimizationDirection;
52
+ /**
53
+ * The telemetry configuration for the evaluator.
54
+ *
55
+ * When enabled, the evaluator will automatically create OpenTelemetry spans
56
+ * for tracing and observability. This allows you to track evaluator performance
57
+ * and debug issues in distributed systems.
58
+ *
59
+ * @defaultValue `{ isEnabled: true }`
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const evaluator = createEvaluator(myFunction, {
64
+ * telemetry: { isEnabled: true, tracer: myTracer }
65
+ * });
66
+ * ```
67
+ */
68
+ telemetry?: TelemetryConfig;
69
+ };
70
+ /**
71
+ * A factory function for creating a custom evaluator from any function.
72
+ *
73
+ * This function wraps a user-provided function into an evaluator that can be used
74
+ * with Phoenix experiments and evaluations. The function can be synchronous or
75
+ * asynchronous, and can return a number, an {@link EvaluationResult} object, or
76
+ * a value that will be automatically converted to an evaluation result.
77
+ *
78
+ * The evaluator will automatically:
79
+ * - Convert the function's return value to an {@link EvaluationResult}
80
+ * - Handle both sync and async functions
81
+ * - Wrap the function with OpenTelemetry spans if telemetry is enabled
82
+ * - Infer the evaluator name from the function name if not provided
83
+ *
84
+ * @typeParam RecordType - The type of the input record that the evaluator expects.
85
+ * Must extend `Record<string, unknown>`.
86
+ * @typeParam Fn - The type of the function being wrapped. Must be a function that
87
+ * accepts the record type and returns a value compatible with {@link EvaluationResult}.
88
+ *
89
+ * @param fn - The function to wrap as an evaluator. Can be synchronous or asynchronous.
90
+ * The function should accept a record of type `RecordType` and return either:
91
+ * - A number (will be converted to `{ score: number }`)
92
+ * - An {@link EvaluationResult} object
93
+ * - Any value that can be converted to an evaluation result
94
+ *
95
+ * @param options - Optional configuration for the evaluator. See {@link CreateEvaluatorOptions}
96
+ * for details on available options.
97
+ *
98
+ * @returns An {@link EvaluatorInterface} that can be used with Phoenix experiments
99
+ * and evaluation workflows.
100
+ *
101
+ * @example
102
+ * Basic usage with a simple scoring function:
103
+ * ```typescript
104
+ * const accuracyEvaluator = createEvaluator(
105
+ * ({ output, expected }) => {
106
+ * return output === expected ? 1 : 0;
107
+ * },
108
+ * {
109
+ * name: "accuracy",
110
+ * kind: "CODE",
111
+ * optimizationDirection: "MAXIMIZE"
112
+ * }
113
+ * );
114
+ *
115
+ * const result = await accuracyEvaluator.evaluate({
116
+ * output: "correct answer",
117
+ * expected: "correct answer"
118
+ * });
119
+ * // result: { score: 1 }
120
+ * ```
121
+ *
122
+ *
123
+ * @example
124
+ * Returning a full EvaluationResult:
125
+ * ```typescript
126
+ * const qualityEvaluator = createEvaluator(
127
+ * ({ output }) => {
128
+ * const score = calculateQuality(output);
129
+ * return {
130
+ * score,
131
+ * label: score > 0.8 ? "high" : "low",
132
+ * explanation: `Quality score: ${score}`
133
+ * };
134
+ * },
135
+ * { name: "quality" }
136
+ * );
137
+ * ```
138
+ */
139
+ export declare function createEvaluator<RecordType extends Record<string, unknown> = Record<string, unknown>, Fn extends AnyFn = AnyFn>(fn: Fn, options?: CreateEvaluatorOptions): EvaluatorBase<RecordType>;
140
+ export {};
141
+ //# sourceMappingURL=createEvaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEvaluator.d.ts","sourceRoot":"","sources":["../../../src/helpers/createEvaluator.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EACL,cAAc,EACd,qBAAqB,EACrB,eAAe,EAChB,MAAM,UAAU,CAAC;AAKlB,KAAK,KAAK,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAMrC;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC;;;;;;;;;;OAUG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;;;;;;;;;;OAYG;IACH,IAAI,CAAC,EAAE,cAAc,CAAC;IACtB;;;;;;;;;;;;;;OAcG;IACH,qBAAqB,CAAC,EAAE,qBAAqB,CAAC;IAC9C;;;;;;;;;;;;;;;OAeG;IACH,SAAS,CAAC,EAAE,eAAe,CAAC;CAC7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AACH,wBAAgB,eAAe,CAC7B,UAAU,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpE,EAAE,SAAS,KAAK,GAAG,KAAK,EACxB,EAAE,EAAE,EAAE,EAAE,OAAO,CAAC,EAAE,sBAAsB,GAAG,aAAa,CAAC,UAAU,CAAC,CAyBrE"}
@@ -0,0 +1,96 @@
1
+ import { withSpan } from "@arizeai/openinference-core";
2
+ import { FunctionEvaluator } from "../core/FunctionEvaluator.js";
3
+ import { asEvaluatorFn } from "./asEvaluatorFn.js";
4
+ function generateUniqueName() {
5
+ return `evaluator-${Math.random().toString(36).substring(2, 15)}`;
6
+ }
7
+ /**
8
+ * A factory function for creating a custom evaluator from any function.
9
+ *
10
+ * This function wraps a user-provided function into an evaluator that can be used
11
+ * with Phoenix experiments and evaluations. The function can be synchronous or
12
+ * asynchronous, and can return a number, an {@link EvaluationResult} object, or
13
+ * a value that will be automatically converted to an evaluation result.
14
+ *
15
+ * The evaluator will automatically:
16
+ * - Convert the function's return value to an {@link EvaluationResult}
17
+ * - Handle both sync and async functions
18
+ * - Wrap the function with OpenTelemetry spans if telemetry is enabled
19
+ * - Infer the evaluator name from the function name if not provided
20
+ *
21
+ * @typeParam RecordType - The type of the input record that the evaluator expects.
22
+ * Must extend `Record<string, unknown>`.
23
+ * @typeParam Fn - The type of the function being wrapped. Must be a function that
24
+ * accepts the record type and returns a value compatible with {@link EvaluationResult}.
25
+ *
26
+ * @param fn - The function to wrap as an evaluator. Can be synchronous or asynchronous.
27
+ * The function should accept a record of type `RecordType` and return either:
28
+ * - A number (will be converted to `{ score: number }`)
29
+ * - An {@link EvaluationResult} object
30
+ * - Any value that can be converted to an evaluation result
31
+ *
32
+ * @param options - Optional configuration for the evaluator. See {@link CreateEvaluatorOptions}
33
+ * for details on available options.
34
+ *
35
+ * @returns An {@link EvaluatorInterface} that can be used with Phoenix experiments
36
+ * and evaluation workflows.
37
+ *
38
+ * @example
39
+ * Basic usage with a simple scoring function:
40
+ * ```typescript
41
+ * const accuracyEvaluator = createEvaluator(
42
+ * ({ output, expected }) => {
43
+ * return output === expected ? 1 : 0;
44
+ * },
45
+ * {
46
+ * name: "accuracy",
47
+ * kind: "CODE",
48
+ * optimizationDirection: "MAXIMIZE"
49
+ * }
50
+ * );
51
+ *
52
+ * const result = await accuracyEvaluator.evaluate({
53
+ * output: "correct answer",
54
+ * expected: "correct answer"
55
+ * });
56
+ * // result: { score: 1 }
57
+ * ```
58
+ *
59
+ *
60
+ * @example
61
+ * Returning a full EvaluationResult:
62
+ * ```typescript
63
+ * const qualityEvaluator = createEvaluator(
64
+ * ({ output }) => {
65
+ * const score = calculateQuality(output);
66
+ * return {
67
+ * score,
68
+ * label: score > 0.8 ? "high" : "low",
69
+ * explanation: `Quality score: ${score}`
70
+ * };
71
+ * },
72
+ * { name: "quality" }
73
+ * );
74
+ * ```
75
+ */
76
+ export function createEvaluator(fn, options) {
77
+ const { name, kind, optimizationDirection, telemetry = { isEnabled: true }, } = options || {};
78
+ const evaluatorName = name || fn.name || generateUniqueName();
79
+ let evaluateFn = asEvaluatorFn(fn);
80
+ // Add OpenTelemetry span wrapping if telemetry is enabled
81
+ if (telemetry && telemetry.isEnabled) {
82
+ evaluateFn = withSpan(evaluateFn, {
83
+ tracer: telemetry.tracer,
84
+ name: evaluatorName,
85
+ kind: "EVALUATOR",
86
+ });
87
+ }
88
+ return new FunctionEvaluator({
89
+ evaluateFn,
90
+ name: evaluatorName,
91
+ kind: kind || "CODE",
92
+ optimizationDirection: optimizationDirection || "MAXIMIZE",
93
+ telemetry,
94
+ });
95
+ }
96
+ //# sourceMappingURL=createEvaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEvaluator.js","sourceRoot":"","sources":["../../../src/helpers/createEvaluator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AAGvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAO9D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAKhD,SAAS,kBAAkB;IACzB,OAAO,aAAa,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC;AACpE,CAAC;AAqED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AACH,MAAM,UAAU,eAAe,CAG7B,EAAM,EAAE,OAAgC;IACxC,MAAM,EACJ,IAAI,EACJ,IAAI,EACJ,qBAAqB,EACrB,SAAS,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,GAChC,GAAG,OAAO,IAAI,EAAE,CAAC;IAClB,MAAM,aAAa,GAAG,IAAI,IAAI,EAAE,CAAC,IAAI,IAAI,kBAAkB,EAAE,CAAC;IAC9D,IAAI,UAAU,GAAG,aAAa,CAAa,EAAE,CAAC,CAAC;IAE/C,0DAA0D;IAC1D,IAAI,SAAS,IAAI,SAAS,CAAC,SAAS,EAAE,CAAC;QACrC,UAAU,GAAG,QAAQ,CAAC,UAAU,EAAE;YAChC,MAAM,EAAE,SAAS,CAAC,MAAM;YACxB,IAAI,EAAE,aAAa;YACnB,IAAI,EAAE,WAAW;SAClB,CAAC,CAAC;IACL,CAAC;IACD,OAAO,IAAI,iBAAiB,CAAa;QACvC,UAAU;QACV,IAAI,EAAE,aAAa;QACnB,IAAI,EAAE,IAAI,IAAI,MAAM;QACpB,qBAAqB,EAAE,qBAAqB,IAAI,UAAU;QAC1D,SAAS;KACV,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./asEvaluatorFn.js";
2
+ export * from "./createEvaluator.js";
3
+ export * from "./toEvaluationResult.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/helpers/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export * from "./asEvaluatorFn.js";
2
+ export * from "./createEvaluator.js";
3
+ export * from "./toEvaluationResult.js";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/helpers/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1,67 @@
1
+ import { EvaluationResult } from "../types/index.js";
2
+ /**
3
+ * Converts an unknown value to an {@link EvaluationResult}.
4
+ *
5
+ * This function provides a flexible way to normalize various return types from
6
+ * evaluator functions into a standardized `EvaluationResult` format. It handles
7
+ * multiple input types:
8
+ *
9
+ * - **Numbers**: Converted to `{ score: number }`
10
+ * - **Strings**: Converted to `{ label: string }`
11
+ * - **Objects**: Extracts `score`, `label`, and `explanation` properties if present
12
+ * - **Other types**: Returns an empty `EvaluationResult` object
13
+ *
14
+ * This is particularly useful when creating evaluators from functions that may
15
+ * return different types, ensuring consistent evaluation result formatting.
16
+ *
17
+ * @param result - The value to convert to an EvaluationResult. Can be:
18
+ * - A number (converted to score)
19
+ * - A string (converted to label)
20
+ * - An object with optional `score`, `label`, and/or `explanation` properties
21
+ * - Any other value (returns empty object)
22
+ *
23
+ * @returns An {@link EvaluationResult} object with extracted properties
24
+ *
25
+ * @example
26
+ * Convert a number to an EvaluationResult:
27
+ * ```typescript
28
+ * const result = toEvaluationResult(0.95);
29
+ * // Returns: { score: 0.95 }
30
+ * ```
31
+ *
32
+ * @example
33
+ * Convert a string to an EvaluationResult:
34
+ * ```typescript
35
+ * const result = toEvaluationResult("correct");
36
+ * // Returns: { label: "correct" }
37
+ * ```
38
+ *
39
+ * @example
40
+ * Convert an object with all properties:
41
+ * ```typescript
42
+ * const result = toEvaluationResult({
43
+ * score: 0.9,
44
+ * label: "high",
45
+ * explanation: "High quality output"
46
+ * });
47
+ * // Returns: { score: 0.9, label: "high", explanation: "High quality output" }
48
+ * ```
49
+ *
50
+ * @example
51
+ * Convert an object with partial properties:
52
+ * ```typescript
53
+ * const result = toEvaluationResult({ score: 0.8 });
54
+ * // Returns: { score: 0.8 }
55
+ * ```
56
+ *
57
+ * @example
58
+ * Handle null or undefined:
59
+ * ```typescript
60
+ * const result = toEvaluationResult(null);
61
+ * // Returns: {}
62
+ * ```
63
+ *
64
+ * @public
65
+ */
66
+ export declare function toEvaluationResult(result: unknown): EvaluationResult;
67
+ //# sourceMappingURL=toEvaluationResult.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toEvaluationResult.d.ts","sourceRoot":"","sources":["../../../src/helpers/toEvaluationResult.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAuD5C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,OAAO,GAAG,gBAAgB,CAyBpE"}
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Type guard to check if a value has a score property.
3
+ *
4
+ * @param result - The value to check
5
+ * @returns True if the value is an object with a numeric score property
6
+ *
7
+ * @internal
8
+ */
9
+ function resultHasScore(result) {
10
+ return (typeof result === "object" &&
11
+ result !== null &&
12
+ "score" in result &&
13
+ typeof result.score === "number");
14
+ }
15
+ /**
16
+ * Type guard to check if a value has a label property.
17
+ *
18
+ * @param result - The value to check
19
+ * @returns True if the value is an object with a string label property
20
+ *
21
+ * @internal
22
+ */
23
+ function resultHasLabel(result) {
24
+ return (typeof result === "object" &&
25
+ result !== null &&
26
+ "label" in result &&
27
+ typeof result.label === "string");
28
+ }
29
+ /**
30
+ * Type guard to check if a value has an explanation property.
31
+ *
32
+ * @param result - The value to check
33
+ * @returns True if the value is an object with a string explanation property
34
+ *
35
+ * @internal
36
+ */
37
+ function resultHasExplanation(result) {
38
+ return (typeof result === "object" &&
39
+ result !== null &&
40
+ "explanation" in result &&
41
+ typeof result.explanation === "string");
42
+ }
43
+ /**
44
+ * Converts an unknown value to an {@link EvaluationResult}.
45
+ *
46
+ * This function provides a flexible way to normalize various return types from
47
+ * evaluator functions into a standardized `EvaluationResult` format. It handles
48
+ * multiple input types:
49
+ *
50
+ * - **Numbers**: Converted to `{ score: number }`
51
+ * - **Strings**: Converted to `{ label: string }`
52
+ * - **Objects**: Extracts `score`, `label`, and `explanation` properties if present
53
+ * - **Other types**: Returns an empty `EvaluationResult` object
54
+ *
55
+ * This is particularly useful when creating evaluators from functions that may
56
+ * return different types, ensuring consistent evaluation result formatting.
57
+ *
58
+ * @param result - The value to convert to an EvaluationResult. Can be:
59
+ * - A number (converted to score)
60
+ * - A string (converted to label)
61
+ * - An object with optional `score`, `label`, and/or `explanation` properties
62
+ * - Any other value (returns empty object)
63
+ *
64
+ * @returns An {@link EvaluationResult} object with extracted properties
65
+ *
66
+ * @example
67
+ * Convert a number to an EvaluationResult:
68
+ * ```typescript
69
+ * const result = toEvaluationResult(0.95);
70
+ * // Returns: { score: 0.95 }
71
+ * ```
72
+ *
73
+ * @example
74
+ * Convert a string to an EvaluationResult:
75
+ * ```typescript
76
+ * const result = toEvaluationResult("correct");
77
+ * // Returns: { label: "correct" }
78
+ * ```
79
+ *
80
+ * @example
81
+ * Convert an object with all properties:
82
+ * ```typescript
83
+ * const result = toEvaluationResult({
84
+ * score: 0.9,
85
+ * label: "high",
86
+ * explanation: "High quality output"
87
+ * });
88
+ * // Returns: { score: 0.9, label: "high", explanation: "High quality output" }
89
+ * ```
90
+ *
91
+ * @example
92
+ * Convert an object with partial properties:
93
+ * ```typescript
94
+ * const result = toEvaluationResult({ score: 0.8 });
95
+ * // Returns: { score: 0.8 }
96
+ * ```
97
+ *
98
+ * @example
99
+ * Handle null or undefined:
100
+ * ```typescript
101
+ * const result = toEvaluationResult(null);
102
+ * // Returns: {}
103
+ * ```
104
+ *
105
+ * @public
106
+ */
107
+ export function toEvaluationResult(result) {
108
+ if (typeof result === "number") {
109
+ return {
110
+ score: result,
111
+ };
112
+ }
113
+ if (typeof result === "string") {
114
+ return {
115
+ label: result,
116
+ };
117
+ }
118
+ if (typeof result === "object" && result !== null) {
119
+ const evaluationResult = {};
120
+ if (resultHasScore(result)) {
121
+ evaluationResult.score = result.score;
122
+ }
123
+ if (resultHasLabel(result)) {
124
+ evaluationResult.label = result.label;
125
+ }
126
+ if (resultHasExplanation(result)) {
127
+ evaluationResult.explanation = result.explanation;
128
+ }
129
+ return evaluationResult;
130
+ }
131
+ return {};
132
+ }
133
+ //# sourceMappingURL=toEvaluationResult.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toEvaluationResult.js","sourceRoot":"","sources":["../../../src/helpers/toEvaluationResult.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,SAAS,cAAc,CAAC,MAAe;IACrC,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,MAAM,KAAK,IAAI;QACf,OAAO,IAAI,MAAM;QACjB,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,CACjC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,cAAc,CAAC,MAAe;IACrC,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,MAAM,KAAK,IAAI;QACf,OAAO,IAAI,MAAM;QACjB,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,CACjC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,oBAAoB,CAC3B,MAAe;IAEf,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,MAAM,KAAK,IAAI;QACf,aAAa,IAAI,MAAM;QACvB,OAAO,MAAM,CAAC,WAAW,KAAK,QAAQ,CACvC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAe;IAChD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO;YACL,KAAK,EAAE,MAAM;SACd,CAAC;IACJ,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;QAC/B,OAAO;YACL,KAAK,EAAE,MAAM;SACd,CAAC;IACJ,CAAC;IACD,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;QAClD,MAAM,gBAAgB,GAAqB,EAAE,CAAC;QAC9C,IAAI,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;YAC3B,gBAAgB,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QACxC,CAAC;QACD,IAAI,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;YAC3B,gBAAgB,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QACxC,CAAC;QACD,IAAI,oBAAoB,CAAC,MAAM,CAAC,EAAE,CAAC;YACjC,gBAAgB,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;QACpD,CAAC;QACD,OAAO,gBAAgB,CAAC;IAC1B,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC"}
@@ -2,4 +2,5 @@ export * from "./llm/index.js";
2
2
  export * from "./template/index.js";
3
3
  export * from "./types/index.js";
4
4
  export * from "./utils/index.js";
5
+ export * from "./helpers/index.js";
5
6
  //# 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;AACxB,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;AACxB,cAAc,WAAW,CAAC"}
package/dist/esm/index.js CHANGED
@@ -2,4 +2,5 @@ export * from "./llm/index.js";
2
2
  export * from "./template/index.js";
3
3
  export * from "./types/index.js";
4
4
  export * from "./utils/index.js";
5
+ export * from "./helpers/index.js";
5
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,OAAO,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC"}