@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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typeUtils.js","sourceRoot":"","sources":["../../../src/utils/typeUtils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAc,KAAc;IACnD,OAAO,CACL,CAAC,CAAC,KAAK;QACP,8DAA8D;QAC9D,OAAQ,KAAa,EAAE,IAAI,KAAK,UAAU;QAC1C,8DAA8D;QAC9D,OAAQ,KAAa,EAAE,KAAK,KAAK,UAAU,CAC5C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { CreateEvaluatorArgs, EvaluationResult, ObjectMapping } from "../types";
2
+ import { EvaluatorBase } from "./EvaluatorBase";
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,33 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.FunctionEvaluator = void 0;
15
+ const EvaluatorBase_1 = require("./EvaluatorBase");
16
+ /**
17
+ * A class that constructs an evaluator based on an evaluate function.
18
+ */
19
+ class FunctionEvaluator extends EvaluatorBase_1.EvaluatorBase {
20
+ constructor(_a) {
21
+ var { evaluateFn } = _a, args = __rest(_a, ["evaluateFn"]);
22
+ super(Object.assign({}, args));
23
+ this.evaluateFn = evaluateFn;
24
+ }
25
+ async evaluate(args) {
26
+ return this.evaluateFn(args);
27
+ }
28
+ bindInputMapping(inputMapping) {
29
+ return new FunctionEvaluator(Object.assign(Object.assign({}, this), { inputMapping }));
30
+ }
31
+ }
32
+ exports.FunctionEvaluator = FunctionEvaluator;
33
+ //# sourceMappingURL=FunctionEvaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"FunctionEvaluator.js","sourceRoot":"","sources":["../../../src/core/FunctionEvaluator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAEA,mDAAgD;AAMhD;;GAEG;AACH,MAAa,iBAEX,SAAQ,6BAAyB;IAEjC,YAAY,EAA0D;YAA1D,EAAE,UAAU,OAA8C,EAAzC,IAAI,cAArB,cAAuB,CAAF;QAC/B,KAAK,mBAAM,IAAI,EAAG,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,iCAAM,IAAI,KAAE,YAAY,IAAG,CAAC;IAC1D,CAAC;CACF;AAhBD,8CAgBC"}
@@ -0,0 +1,6 @@
1
+ import { AnyFn, EvaluatorFn } from "../types";
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,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.asEvaluatorFn = asEvaluatorFn;
4
+ const typeUtils_1 = require("../utils/typeUtils");
5
+ const toEvaluationResult_1 = require("./toEvaluationResult");
6
+ /**
7
+ * A function that converts a generic function into an evaluator function
8
+ */
9
+ function asEvaluatorFn(fn) {
10
+ return async (...args) => {
11
+ let result = fn(...args);
12
+ if ((0, typeUtils_1.isPromise)(result)) {
13
+ result = await result;
14
+ }
15
+ return (0, toEvaluationResult_1.toEvaluationResult)(result);
16
+ };
17
+ }
18
+ //# sourceMappingURL=asEvaluatorFn.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"asEvaluatorFn.js","sourceRoot":"","sources":["../../../src/helpers/asEvaluatorFn.ts"],"names":[],"mappings":";;AAQA,sCAUC;AAjBD,kDAA+C;AAE/C,6DAA0D;AAE1D;;GAEG;AACH,SAAgB,aAAa,CAC3B,EAAS;IAET,OAAO,KAAK,EAAE,GAAG,IAAI,EAAE,EAAE;QACvB,IAAI,MAAM,GAAG,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;QACzB,IAAI,IAAA,qBAAS,EAAC,MAAM,CAAC,EAAE,CAAC;YACtB,MAAM,GAAG,MAAM,MAAM,CAAC;QACxB,CAAC;QACD,OAAO,IAAA,uCAAkB,EAAC,MAAM,CAAC,CAAC;IACpC,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,141 @@
1
+ import { EvaluatorBase } from "../core/EvaluatorBase";
2
+ import { EvaluationKind, OptimizationDirection, TelemetryConfig } from "../types";
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,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createEvaluator = createEvaluator;
4
+ const openinference_core_1 = require("@arizeai/openinference-core");
5
+ const FunctionEvaluator_1 = require("../core/FunctionEvaluator");
6
+ const asEvaluatorFn_1 = require("./asEvaluatorFn");
7
+ function generateUniqueName() {
8
+ return `evaluator-${Math.random().toString(36).substring(2, 15)}`;
9
+ }
10
+ /**
11
+ * A factory function for creating a custom evaluator from any function.
12
+ *
13
+ * This function wraps a user-provided function into an evaluator that can be used
14
+ * with Phoenix experiments and evaluations. The function can be synchronous or
15
+ * asynchronous, and can return a number, an {@link EvaluationResult} object, or
16
+ * a value that will be automatically converted to an evaluation result.
17
+ *
18
+ * The evaluator will automatically:
19
+ * - Convert the function's return value to an {@link EvaluationResult}
20
+ * - Handle both sync and async functions
21
+ * - Wrap the function with OpenTelemetry spans if telemetry is enabled
22
+ * - Infer the evaluator name from the function name if not provided
23
+ *
24
+ * @typeParam RecordType - The type of the input record that the evaluator expects.
25
+ * Must extend `Record<string, unknown>`.
26
+ * @typeParam Fn - The type of the function being wrapped. Must be a function that
27
+ * accepts the record type and returns a value compatible with {@link EvaluationResult}.
28
+ *
29
+ * @param fn - The function to wrap as an evaluator. Can be synchronous or asynchronous.
30
+ * The function should accept a record of type `RecordType` and return either:
31
+ * - A number (will be converted to `{ score: number }`)
32
+ * - An {@link EvaluationResult} object
33
+ * - Any value that can be converted to an evaluation result
34
+ *
35
+ * @param options - Optional configuration for the evaluator. See {@link CreateEvaluatorOptions}
36
+ * for details on available options.
37
+ *
38
+ * @returns An {@link EvaluatorInterface} that can be used with Phoenix experiments
39
+ * and evaluation workflows.
40
+ *
41
+ * @example
42
+ * Basic usage with a simple scoring function:
43
+ * ```typescript
44
+ * const accuracyEvaluator = CreateEvaluator(
45
+ * ({ output, expected }) => {
46
+ * return output === expected ? 1 : 0;
47
+ * },
48
+ * {
49
+ * name: "accuracy",
50
+ * kind: "CODE",
51
+ * optimizationDirection: "MAXIMIZE"
52
+ * }
53
+ * );
54
+ *
55
+ * const result = await accuracyEvaluator.evaluate({
56
+ * output: "correct answer",
57
+ * expected: "correct answer"
58
+ * });
59
+ * // result: { score: 1 }
60
+ * ```
61
+ *
62
+ *
63
+ * @example
64
+ * Returning a full EvaluationResult:
65
+ * ```typescript
66
+ * const qualityEvaluator = CreateEvaluator(
67
+ * ({ output }) => {
68
+ * const score = calculateQuality(output);
69
+ * return {
70
+ * score,
71
+ * label: score > 0.8 ? "high" : "low",
72
+ * explanation: `Quality score: ${score}`
73
+ * };
74
+ * },
75
+ * { name: "quality" }
76
+ * );
77
+ * ```
78
+ */
79
+ function createEvaluator(fn, options) {
80
+ const { name, kind, optimizationDirection, telemetry = { isEnabled: true }, } = options || {};
81
+ const evaluatorName = name || fn.name || generateUniqueName();
82
+ let evaluateFn = (0, asEvaluatorFn_1.asEvaluatorFn)(fn);
83
+ // Add OpenTelemetry span wrapping if telemetry is enabled
84
+ if (telemetry && telemetry.isEnabled) {
85
+ evaluateFn = (0, openinference_core_1.withSpan)(evaluateFn, {
86
+ tracer: telemetry.tracer,
87
+ name: evaluatorName,
88
+ kind: "EVALUATOR",
89
+ });
90
+ }
91
+ return new FunctionEvaluator_1.FunctionEvaluator({
92
+ evaluateFn,
93
+ name: evaluatorName,
94
+ kind: kind || "CODE",
95
+ optimizationDirection: optimizationDirection || "MAXIMIZE",
96
+ telemetry,
97
+ });
98
+ }
99
+ //# sourceMappingURL=createEvaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createEvaluator.js","sourceRoot":"","sources":["../../../src/helpers/createEvaluator.ts"],"names":[],"mappings":";;AA2JA,0CA4BC;AAvLD,oEAAuD;AAGvD,iEAA8D;AAO9D,mDAAgD;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,SAAgB,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,IAAA,6BAAa,EAAa,EAAE,CAAC,CAAC;IAE/C,0DAA0D;IAC1D,IAAI,SAAS,IAAI,SAAS,CAAC,SAAS,EAAE,CAAC;QACrC,UAAU,GAAG,IAAA,6BAAQ,EAAC,UAAU,EAAE;YAChC,MAAM,EAAE,SAAS,CAAC,MAAM;YACxB,IAAI,EAAE,aAAa;YACnB,IAAI,EAAE,WAAW;SAClB,CAAC,CAAC;IACL,CAAC;IACD,OAAO,IAAI,qCAAiB,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,2 @@
1
+ export * from "./createEvaluator";
2
+ //# 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,mBAAmB,CAAC"}
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./createEvaluator"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/helpers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC"}
@@ -0,0 +1,67 @@
1
+ import { EvaluationResult } from "../types";
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,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toEvaluationResult = toEvaluationResult;
4
+ /**
5
+ * Type guard to check if a value has a score property.
6
+ *
7
+ * @param result - The value to check
8
+ * @returns True if the value is an object with a numeric score property
9
+ *
10
+ * @internal
11
+ */
12
+ function resultHasScore(result) {
13
+ return (typeof result === "object" &&
14
+ result !== null &&
15
+ "score" in result &&
16
+ typeof result.score === "number");
17
+ }
18
+ /**
19
+ * Type guard to check if a value has a label property.
20
+ *
21
+ * @param result - The value to check
22
+ * @returns True if the value is an object with a string label property
23
+ *
24
+ * @internal
25
+ */
26
+ function resultHasLabel(result) {
27
+ return (typeof result === "object" &&
28
+ result !== null &&
29
+ "label" in result &&
30
+ typeof result.label === "string");
31
+ }
32
+ /**
33
+ * Type guard to check if a value has an explanation property.
34
+ *
35
+ * @param result - The value to check
36
+ * @returns True if the value is an object with a string explanation property
37
+ *
38
+ * @internal
39
+ */
40
+ function resultHasExplanation(result) {
41
+ return (typeof result === "object" &&
42
+ result !== null &&
43
+ "explanation" in result &&
44
+ typeof result.explanation === "string");
45
+ }
46
+ /**
47
+ * Converts an unknown value to an {@link EvaluationResult}.
48
+ *
49
+ * This function provides a flexible way to normalize various return types from
50
+ * evaluator functions into a standardized `EvaluationResult` format. It handles
51
+ * multiple input types:
52
+ *
53
+ * - **Numbers**: Converted to `{ score: number }`
54
+ * - **Strings**: Converted to `{ label: string }`
55
+ * - **Objects**: Extracts `score`, `label`, and `explanation` properties if present
56
+ * - **Other types**: Returns an empty `EvaluationResult` object
57
+ *
58
+ * This is particularly useful when creating evaluators from functions that may
59
+ * return different types, ensuring consistent evaluation result formatting.
60
+ *
61
+ * @param result - The value to convert to an EvaluationResult. Can be:
62
+ * - A number (converted to score)
63
+ * - A string (converted to label)
64
+ * - An object with optional `score`, `label`, and/or `explanation` properties
65
+ * - Any other value (returns empty object)
66
+ *
67
+ * @returns An {@link EvaluationResult} object with extracted properties
68
+ *
69
+ * @example
70
+ * Convert a number to an EvaluationResult:
71
+ * ```typescript
72
+ * const result = toEvaluationResult(0.95);
73
+ * // Returns: { score: 0.95 }
74
+ * ```
75
+ *
76
+ * @example
77
+ * Convert a string to an EvaluationResult:
78
+ * ```typescript
79
+ * const result = toEvaluationResult("correct");
80
+ * // Returns: { label: "correct" }
81
+ * ```
82
+ *
83
+ * @example
84
+ * Convert an object with all properties:
85
+ * ```typescript
86
+ * const result = toEvaluationResult({
87
+ * score: 0.9,
88
+ * label: "high",
89
+ * explanation: "High quality output"
90
+ * });
91
+ * // Returns: { score: 0.9, label: "high", explanation: "High quality output" }
92
+ * ```
93
+ *
94
+ * @example
95
+ * Convert an object with partial properties:
96
+ * ```typescript
97
+ * const result = toEvaluationResult({ score: 0.8 });
98
+ * // Returns: { score: 0.8 }
99
+ * ```
100
+ *
101
+ * @example
102
+ * Handle null or undefined:
103
+ * ```typescript
104
+ * const result = toEvaluationResult(null);
105
+ * // Returns: {}
106
+ * ```
107
+ *
108
+ * @public
109
+ */
110
+ function toEvaluationResult(result) {
111
+ if (typeof result === "number") {
112
+ return {
113
+ score: result,
114
+ };
115
+ }
116
+ if (typeof result === "string") {
117
+ return {
118
+ label: result,
119
+ };
120
+ }
121
+ if (typeof result === "object" && result !== null) {
122
+ const evaluationResult = {};
123
+ if (resultHasScore(result)) {
124
+ evaluationResult.score = result.score;
125
+ }
126
+ if (resultHasLabel(result)) {
127
+ evaluationResult.label = result.label;
128
+ }
129
+ if (resultHasExplanation(result)) {
130
+ evaluationResult.explanation = result.explanation;
131
+ }
132
+ return evaluationResult;
133
+ }
134
+ return {};
135
+ }
136
+ //# sourceMappingURL=toEvaluationResult.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toEvaluationResult.js","sourceRoot":"","sources":["../../../src/helpers/toEvaluationResult.ts"],"names":[],"mappings":";;AAuHA,gDAyBC;AA9ID;;;;;;;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,SAAgB,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";
2
2
  export * from "./template";
3
3
  export * from "./types";
4
4
  export * from "./utils";
5
+ export * from "./helpers";
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/src/index.js CHANGED
@@ -18,4 +18,5 @@ __exportStar(require("./llm"), exports);
18
18
  __exportStar(require("./template"), exports);
19
19
  __exportStar(require("./types"), exports);
20
20
  __exportStar(require("./utils"), exports);
21
+ __exportStar(require("./helpers"), exports);
21
22
  //# 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;AACxB,0CAAwB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wCAAsB;AACtB,6CAA2B;AAC3B,0CAAwB;AACxB,0CAAwB;AACxB,4CAA0B"}
@@ -0,0 +1,2 @@
1
+ export type AnyFn = (...args: any[]) => any;
2
+ //# sourceMappingURL=base.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.d.ts","sourceRoot":"","sources":["../../../src/types/base.ts"],"names":[],"mappings":"AACA,MAAM,MAAM,KAAK,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC"}
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=base.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base.js","sourceRoot":"","sources":["../../../src/types/base.ts"],"names":[],"mappings":""}
@@ -2,4 +2,5 @@ export * from "./evals";
2
2
  export * from "./templating";
3
3
  export * from "./otel";
4
4
  export * from "./data";
5
+ export * from "./base";
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC"}
@@ -18,4 +18,5 @@ __exportStar(require("./evals"), exports);
18
18
  __exportStar(require("./templating"), exports);
19
19
  __exportStar(require("./otel"), exports);
20
20
  __exportStar(require("./data"), exports);
21
+ __exportStar(require("./base"), exports);
21
22
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+CAA6B;AAC7B,yCAAuB;AACvB,yCAAuB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+CAA6B;AAC7B,yCAAuB;AACvB,yCAAuB;AACvB,yCAAuB"}