@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.
- package/README.md +42 -0
- package/dist/esm/core/EvaluatorBase.d.ts +19 -0
- package/dist/esm/core/EvaluatorBase.d.ts.map +1 -0
- package/dist/esm/core/EvaluatorBase.js +18 -0
- package/dist/esm/core/EvaluatorBase.js.map +1 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/llm/ClassificationEvaluator.d.ts +20 -3
- package/dist/esm/llm/ClassificationEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/ClassificationEvaluator.js +29 -2
- package/dist/esm/llm/ClassificationEvaluator.js.map +1 -1
- package/dist/esm/llm/LLMEvaluator.d.ts +4 -7
- package/dist/esm/llm/LLMEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/LLMEvaluator.js +4 -7
- package/dist/esm/llm/LLMEvaluator.js.map +1 -1
- package/dist/esm/llm/createClassificationEvaluator.d.ts +1 -1
- package/dist/esm/llm/createClassificationEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/createClassificationEvaluator.js.map +1 -1
- package/dist/esm/llm/createDocumentRelevancyEvaluator.d.ts +8 -7
- package/dist/esm/llm/createDocumentRelevancyEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/createDocumentRelevancyEvaluator.js +1 -2
- package/dist/esm/llm/createDocumentRelevancyEvaluator.js.map +1 -1
- package/dist/esm/llm/createHallucinationEvaluator.d.ts +6 -6
- package/dist/esm/llm/createHallucinationEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/createHallucinationEvaluator.js +1 -2
- package/dist/esm/llm/createHallucinationEvaluator.js.map +1 -1
- package/dist/esm/template/applyTemplate.d.ts +3 -3
- package/dist/esm/template/applyTemplate.d.ts.map +1 -1
- package/dist/esm/template/applyTemplate.js +15 -1
- package/dist/esm/template/applyTemplate.js.map +1 -1
- package/dist/esm/template/createTemplateVariablesProxy.d.ts +1 -1
- package/dist/esm/template/createTemplateVariablesProxy.d.ts.map +1 -1
- package/dist/esm/template/createTemplateVariablesProxy.js.map +1 -1
- package/dist/esm/template/getTemplateVariables.d.ts +2 -2
- package/dist/esm/template/getTemplateVariables.d.ts.map +1 -1
- package/dist/esm/template/getTemplateVariables.js +16 -0
- package/dist/esm/template/getTemplateVariables.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/esm/types/data.d.ts +194 -0
- package/dist/esm/types/data.d.ts.map +1 -0
- package/dist/esm/types/data.js +2 -0
- package/dist/esm/types/data.js.map +1 -0
- package/dist/esm/types/evals.d.ts +19 -5
- package/dist/esm/types/evals.d.ts.map +1 -1
- package/dist/esm/types/index.d.ts +1 -0
- package/dist/esm/types/index.d.ts.map +1 -1
- package/dist/esm/types/index.js +1 -0
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/otel.d.ts +14 -13
- package/dist/esm/types/otel.d.ts.map +1 -1
- package/dist/esm/types/templating.d.ts +8 -6
- package/dist/esm/types/templating.d.ts.map +1 -1
- package/dist/esm/utils/bindEvaluator.d.ts +219 -0
- package/dist/esm/utils/bindEvaluator.d.ts.map +1 -0
- package/dist/esm/utils/bindEvaluator.js +163 -0
- package/dist/esm/utils/bindEvaluator.js.map +1 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/index.d.ts.map +1 -0
- package/dist/esm/utils/index.js +2 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/objectMappingUtils.d.ts +166 -0
- package/dist/esm/utils/objectMappingUtils.d.ts.map +1 -0
- package/dist/esm/utils/objectMappingUtils.js +191 -0
- package/dist/esm/utils/objectMappingUtils.js.map +1 -0
- package/dist/src/core/EvaluatorBase.d.ts +19 -0
- package/dist/src/core/EvaluatorBase.d.ts.map +1 -0
- package/dist/src/core/EvaluatorBase.js +17 -0
- package/dist/src/core/EvaluatorBase.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/llm/ClassificationEvaluator.d.ts +20 -3
- package/dist/src/llm/ClassificationEvaluator.d.ts.map +1 -1
- package/dist/src/llm/ClassificationEvaluator.js +13 -2
- package/dist/src/llm/ClassificationEvaluator.js.map +1 -1
- package/dist/src/llm/LLMEvaluator.d.ts +4 -7
- package/dist/src/llm/LLMEvaluator.d.ts.map +1 -1
- package/dist/src/llm/LLMEvaluator.js +16 -5
- package/dist/src/llm/LLMEvaluator.js.map +1 -1
- package/dist/src/llm/createClassificationEvaluator.d.ts +1 -1
- package/dist/src/llm/createClassificationEvaluator.d.ts.map +1 -1
- package/dist/src/llm/createClassificationEvaluator.js.map +1 -1
- package/dist/src/llm/createDocumentRelevancyEvaluator.d.ts +8 -7
- package/dist/src/llm/createDocumentRelevancyEvaluator.d.ts.map +1 -1
- package/dist/src/llm/createDocumentRelevancyEvaluator.js +2 -2
- package/dist/src/llm/createDocumentRelevancyEvaluator.js.map +1 -1
- package/dist/src/llm/createHallucinationEvaluator.d.ts +6 -6
- package/dist/src/llm/createHallucinationEvaluator.d.ts.map +1 -1
- package/dist/src/llm/createHallucinationEvaluator.js +2 -2
- package/dist/src/llm/createHallucinationEvaluator.js.map +1 -1
- package/dist/src/template/applyTemplate.d.ts +3 -3
- package/dist/src/template/applyTemplate.d.ts.map +1 -1
- package/dist/src/template/applyTemplate.js +12 -1
- package/dist/src/template/applyTemplate.js.map +1 -1
- package/dist/src/template/createTemplateVariablesProxy.d.ts +1 -1
- package/dist/src/template/createTemplateVariablesProxy.d.ts.map +1 -1
- package/dist/src/template/createTemplateVariablesProxy.js.map +1 -1
- package/dist/src/template/getTemplateVariables.d.ts +2 -2
- package/dist/src/template/getTemplateVariables.d.ts.map +1 -1
- package/dist/src/template/getTemplateVariables.js +16 -0
- package/dist/src/template/getTemplateVariables.js.map +1 -1
- package/dist/src/types/data.d.ts +194 -0
- package/dist/src/types/data.d.ts.map +1 -0
- package/dist/src/types/data.js +3 -0
- package/dist/src/types/data.js.map +1 -0
- package/dist/src/types/evals.d.ts +19 -5
- package/dist/src/types/evals.d.ts.map +1 -1
- package/dist/src/types/index.d.ts +1 -0
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/index.js +1 -0
- package/dist/src/types/index.js.map +1 -1
- package/dist/src/types/otel.d.ts +14 -13
- package/dist/src/types/otel.d.ts.map +1 -1
- package/dist/src/types/templating.d.ts +8 -6
- package/dist/src/types/templating.d.ts.map +1 -1
- package/dist/src/utils/bindEvaluator.d.ts +219 -0
- package/dist/src/utils/bindEvaluator.d.ts.map +1 -0
- package/dist/src/utils/bindEvaluator.js +166 -0
- package/dist/src/utils/bindEvaluator.js.map +1 -0
- package/dist/src/utils/index.d.ts +2 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/index.js +18 -0
- package/dist/src/utils/index.js.map +1 -0
- package/dist/src/utils/objectMappingUtils.d.ts +166 -0
- package/dist/src/utils/objectMappingUtils.d.ts.map +1 -0
- package/dist/src/utils/objectMappingUtils.js +191 -0
- package/dist/src/utils/objectMappingUtils.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -1
- package/src/core/EvaluatorBase.ts +43 -0
- package/src/index.ts +1 -0
- package/src/llm/ClassificationEvaluator.ts +41 -5
- package/src/llm/LLMEvaluator.ts +7 -16
- package/src/llm/createClassificationEvaluator.ts +1 -1
- package/src/llm/createDocumentRelevancyEvaluator.ts +17 -12
- package/src/llm/createHallucinationEvaluator.ts +12 -10
- package/src/template/applyTemplate.ts +22 -9
- package/src/template/createTemplateVariablesProxy.ts +3 -3
- package/src/template/getTemplateVariables.ts +18 -2
- package/src/types/data.ts +200 -0
- package/src/types/evals.ts +27 -7
- package/src/types/index.ts +1 -0
- package/src/types/otel.ts +15 -13
- package/src/types/templating.ts +9 -6
- package/src/utils/bindEvaluator.ts +229 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/objectMappingUtils.ts +202 -0
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
import { getTemplateVariables } from "../template";
|
|
2
2
|
import {
|
|
3
|
+
ClassificationChoicesMap,
|
|
3
4
|
CreateClassificationEvaluatorArgs,
|
|
4
5
|
EvaluatorFn,
|
|
5
|
-
|
|
6
|
+
PromptTemplate,
|
|
6
7
|
WithPromptTemplate,
|
|
7
8
|
} from "../types";
|
|
9
|
+
import { ObjectMapping } from "../types/data";
|
|
10
|
+
import { remapObject } from "../utils/objectMappingUtils";
|
|
8
11
|
|
|
9
12
|
import { createClassifierFn } from "./createClassifierFn";
|
|
10
13
|
import { LLMEvaluator } from "./LLMEvaluator";
|
|
11
14
|
|
|
15
|
+
import { LanguageModel } from "ai";
|
|
16
|
+
|
|
12
17
|
/**
|
|
13
18
|
* An LLM evaluator that performs evaluation via classification
|
|
14
19
|
*/
|
|
@@ -17,15 +22,35 @@ export class ClassificationEvaluator<RecordType extends Record<string, unknown>>
|
|
|
17
22
|
implements WithPromptTemplate
|
|
18
23
|
{
|
|
19
24
|
readonly evaluatorFn: EvaluatorFn<RecordType>;
|
|
20
|
-
readonly promptTemplate:
|
|
25
|
+
readonly promptTemplate: PromptTemplate;
|
|
26
|
+
/**
|
|
27
|
+
* A dynamically computed set of prompt template variables
|
|
28
|
+
*/
|
|
21
29
|
private _promptTemplateVariables: string[] | undefined;
|
|
22
|
-
|
|
30
|
+
/**
|
|
31
|
+
* The model to use for classification
|
|
32
|
+
*/
|
|
33
|
+
readonly model: LanguageModel;
|
|
34
|
+
/**
|
|
35
|
+
* The choices to classify the example into
|
|
36
|
+
*/
|
|
37
|
+
readonly choices: ClassificationChoicesMap;
|
|
38
|
+
|
|
39
|
+
constructor(args: CreateClassificationEvaluatorArgs<RecordType>) {
|
|
23
40
|
super(args);
|
|
24
41
|
this.promptTemplate = args.promptTemplate;
|
|
25
|
-
this.
|
|
42
|
+
this.model = args.model;
|
|
43
|
+
this.choices = args.choices;
|
|
44
|
+
this.evaluatorFn = createClassifierFn<RecordType>({
|
|
45
|
+
...args,
|
|
46
|
+
});
|
|
26
47
|
}
|
|
27
48
|
evaluate = (example: RecordType) => {
|
|
28
|
-
return this.evaluatorFn(
|
|
49
|
+
return this.evaluatorFn(
|
|
50
|
+
this.inputMapping
|
|
51
|
+
? remapObject<RecordType>(example, this.inputMapping)
|
|
52
|
+
: example
|
|
53
|
+
);
|
|
29
54
|
};
|
|
30
55
|
/**
|
|
31
56
|
* List out the prompt template variables needed to perform evaluation
|
|
@@ -40,4 +65,15 @@ export class ClassificationEvaluator<RecordType extends Record<string, unknown>>
|
|
|
40
65
|
// Give a copy of the variables
|
|
41
66
|
return [...this._promptTemplateVariables];
|
|
42
67
|
}
|
|
68
|
+
/**
|
|
69
|
+
* Binds the input mapping to the evaluator. It makes a a copy of the evaluator and returns it.
|
|
70
|
+
*/
|
|
71
|
+
bindInputMapping(
|
|
72
|
+
inputMapping: ObjectMapping<RecordType>
|
|
73
|
+
): ClassificationEvaluator<RecordType> {
|
|
74
|
+
return new ClassificationEvaluator({
|
|
75
|
+
...this,
|
|
76
|
+
inputMapping,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
43
79
|
}
|
package/src/llm/LLMEvaluator.ts
CHANGED
|
@@ -1,22 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
EvaluationResult,
|
|
4
|
-
Evaluator,
|
|
5
|
-
OptimizationDirection,
|
|
6
|
-
} from "../types";
|
|
1
|
+
import { EvaluatorBase } from "../core/EvaluatorBase";
|
|
2
|
+
import { CreateLLMEvaluatorArgs } from "../types";
|
|
7
3
|
|
|
8
4
|
/**
|
|
9
5
|
* Base class for llm evaluation metrics / scores
|
|
10
6
|
*/
|
|
11
|
-
export abstract class LLMEvaluator<
|
|
12
|
-
|
|
13
|
-
{
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
readonly optimizationDirection?: OptimizationDirection;
|
|
17
|
-
constructor({ name, optimizationDirection }: CreateEvaluatorArgs) {
|
|
18
|
-
this.name = name;
|
|
19
|
-
this.optimizationDirection = optimizationDirection;
|
|
7
|
+
export abstract class LLMEvaluator<
|
|
8
|
+
RecordType extends Record<string, unknown>,
|
|
9
|
+
> extends EvaluatorBase<RecordType> {
|
|
10
|
+
constructor({ ...args }: CreateLLMEvaluatorArgs<RecordType>) {
|
|
11
|
+
super({ kind: "LLM", ...args });
|
|
20
12
|
}
|
|
21
|
-
abstract evaluate(_example: RecordType): Promise<EvaluationResult>;
|
|
22
13
|
}
|
|
@@ -5,7 +5,7 @@ import { ClassificationEvaluator } from "./ClassificationEvaluator";
|
|
|
5
5
|
export function createClassificationEvaluator<
|
|
6
6
|
RecordType extends Record<string, unknown>,
|
|
7
7
|
>(
|
|
8
|
-
args: CreateClassificationEvaluatorArgs
|
|
8
|
+
args: CreateClassificationEvaluatorArgs<RecordType>
|
|
9
9
|
): ClassificationEvaluator<RecordType> {
|
|
10
10
|
return new ClassificationEvaluator<RecordType>(args);
|
|
11
11
|
}
|
|
@@ -2,19 +2,24 @@ import {
|
|
|
2
2
|
DOCUMENT_RELEVANCY_CHOICES,
|
|
3
3
|
DOCUMENT_RELEVANCY_TEMPLATE,
|
|
4
4
|
} from "../default_templates/DOCUMENT_RELEVANCY_TEMPLATE";
|
|
5
|
-
import { CreateClassificationEvaluatorArgs
|
|
5
|
+
import { CreateClassificationEvaluatorArgs } from "../types/evals";
|
|
6
6
|
|
|
7
|
+
import { ClassificationEvaluator } from "./ClassificationEvaluator";
|
|
7
8
|
import { createClassificationEvaluator } from "./createClassificationEvaluator";
|
|
8
9
|
|
|
9
|
-
export interface DocumentRelevancyEvaluatorArgs
|
|
10
|
-
extends
|
|
11
|
-
|
|
10
|
+
export interface DocumentRelevancyEvaluatorArgs<
|
|
11
|
+
RecordType extends Record<
|
|
12
|
+
string,
|
|
13
|
+
unknown
|
|
14
|
+
> = DocumentRelevancyEvaluationRecord,
|
|
15
|
+
> extends Omit<
|
|
16
|
+
CreateClassificationEvaluatorArgs<RecordType>,
|
|
12
17
|
"promptTemplate" | "choices" | "optimizationDirection" | "name"
|
|
13
18
|
> {
|
|
14
|
-
optimizationDirection?: CreateClassificationEvaluatorArgs["optimizationDirection"];
|
|
15
|
-
name?: CreateClassificationEvaluatorArgs["name"];
|
|
16
|
-
choices?: CreateClassificationEvaluatorArgs["choices"];
|
|
17
|
-
promptTemplate?: CreateClassificationEvaluatorArgs["promptTemplate"];
|
|
19
|
+
optimizationDirection?: CreateClassificationEvaluatorArgs<RecordType>["optimizationDirection"];
|
|
20
|
+
name?: CreateClassificationEvaluatorArgs<RecordType>["name"];
|
|
21
|
+
choices?: CreateClassificationEvaluatorArgs<RecordType>["choices"];
|
|
22
|
+
promptTemplate?: CreateClassificationEvaluatorArgs<RecordType>["promptTemplate"];
|
|
18
23
|
}
|
|
19
24
|
|
|
20
25
|
/**
|
|
@@ -57,7 +62,9 @@ export function createDocumentRelevancyEvaluator<
|
|
|
57
62
|
string,
|
|
58
63
|
unknown
|
|
59
64
|
> = DocumentRelevancyEvaluationRecord,
|
|
60
|
-
>(
|
|
65
|
+
>(
|
|
66
|
+
args: DocumentRelevancyEvaluatorArgs<RecordType>
|
|
67
|
+
): ClassificationEvaluator<RecordType> {
|
|
61
68
|
const {
|
|
62
69
|
choices = DOCUMENT_RELEVANCY_CHOICES,
|
|
63
70
|
promptTemplate = DOCUMENT_RELEVANCY_TEMPLATE,
|
|
@@ -65,13 +72,11 @@ export function createDocumentRelevancyEvaluator<
|
|
|
65
72
|
name = "document_relevancy",
|
|
66
73
|
...rest
|
|
67
74
|
} = args;
|
|
68
|
-
|
|
69
75
|
return createClassificationEvaluator<RecordType>({
|
|
70
|
-
...
|
|
76
|
+
...rest,
|
|
71
77
|
promptTemplate,
|
|
72
78
|
choices,
|
|
73
79
|
optimizationDirection,
|
|
74
80
|
name,
|
|
75
|
-
...rest,
|
|
76
81
|
});
|
|
77
82
|
}
|
|
@@ -7,15 +7,16 @@ import { CreateClassificationEvaluatorArgs } from "../types/evals";
|
|
|
7
7
|
import { ClassificationEvaluator } from "./ClassificationEvaluator";
|
|
8
8
|
import { createClassificationEvaluator } from "./createClassificationEvaluator";
|
|
9
9
|
|
|
10
|
-
export interface HallucinationEvaluatorArgs
|
|
11
|
-
extends
|
|
12
|
-
|
|
10
|
+
export interface HallucinationEvaluatorArgs<
|
|
11
|
+
RecordType extends Record<string, unknown> = HallucinationEvaluationRecord,
|
|
12
|
+
> extends Omit<
|
|
13
|
+
CreateClassificationEvaluatorArgs<RecordType>,
|
|
13
14
|
"promptTemplate" | "choices" | "optimizationDirection" | "name"
|
|
14
15
|
> {
|
|
15
|
-
optimizationDirection?: CreateClassificationEvaluatorArgs["optimizationDirection"];
|
|
16
|
-
name?: CreateClassificationEvaluatorArgs["name"];
|
|
17
|
-
choices?: CreateClassificationEvaluatorArgs["choices"];
|
|
18
|
-
promptTemplate?: CreateClassificationEvaluatorArgs["promptTemplate"];
|
|
16
|
+
optimizationDirection?: CreateClassificationEvaluatorArgs<RecordType>["optimizationDirection"];
|
|
17
|
+
name?: CreateClassificationEvaluatorArgs<RecordType>["name"];
|
|
18
|
+
choices?: CreateClassificationEvaluatorArgs<RecordType>["choices"];
|
|
19
|
+
promptTemplate?: CreateClassificationEvaluatorArgs<RecordType>["promptTemplate"];
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
/**
|
|
@@ -35,7 +36,9 @@ export type HallucinationEvaluationRecord = {
|
|
|
35
36
|
*/
|
|
36
37
|
export function createHallucinationEvaluator<
|
|
37
38
|
RecordType extends Record<string, unknown> = HallucinationEvaluationRecord,
|
|
38
|
-
>(
|
|
39
|
+
>(
|
|
40
|
+
args: HallucinationEvaluatorArgs<RecordType>
|
|
41
|
+
): ClassificationEvaluator<RecordType> {
|
|
39
42
|
const {
|
|
40
43
|
choices = HALLUCINATION_CHOICES,
|
|
41
44
|
promptTemplate = HALLUCINATION_TEMPLATE,
|
|
@@ -44,11 +47,10 @@ export function createHallucinationEvaluator<
|
|
|
44
47
|
...rest
|
|
45
48
|
} = args;
|
|
46
49
|
return createClassificationEvaluator<RecordType>({
|
|
47
|
-
...
|
|
50
|
+
...rest,
|
|
48
51
|
promptTemplate,
|
|
49
52
|
choices,
|
|
50
53
|
optimizationDirection,
|
|
51
54
|
name,
|
|
52
|
-
...rest,
|
|
53
55
|
});
|
|
54
56
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PromptTemplate, RenderedPrompt } from "../types/templating";
|
|
2
2
|
|
|
3
3
|
import { createTemplateVariablesProxy } from "./createTemplateVariablesProxy";
|
|
4
4
|
|
|
@@ -9,16 +9,29 @@ import Mustache from "mustache";
|
|
|
9
9
|
* Uses the Mustache library to apply the variables to the template
|
|
10
10
|
*/
|
|
11
11
|
export function formatTemplate(args: {
|
|
12
|
-
template:
|
|
12
|
+
template: PromptTemplate;
|
|
13
13
|
variables: Record<string, unknown>;
|
|
14
|
-
}) {
|
|
14
|
+
}): RenderedPrompt {
|
|
15
15
|
const { template, variables } = args;
|
|
16
16
|
const variablesProxy = createTemplateVariablesProxy(variables);
|
|
17
|
+
if (typeof template === "string") {
|
|
18
|
+
return renderTemplateString(template, variablesProxy);
|
|
19
|
+
}
|
|
20
|
+
return template.map((message) => {
|
|
21
|
+
if (message.content !== undefined && typeof message.content === "string") {
|
|
22
|
+
return {
|
|
23
|
+
...message,
|
|
24
|
+
content: renderTemplateString(message.content, variablesProxy),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
return message;
|
|
28
|
+
}) as RenderedPrompt;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function renderTemplateString(
|
|
32
|
+
template: string,
|
|
33
|
+
variables: Record<string, unknown>
|
|
34
|
+
) {
|
|
17
35
|
// Disable HTML escaping by providing a custom escape function that returns text as-is
|
|
18
|
-
return Mustache.render(
|
|
19
|
-
template,
|
|
20
|
-
variablesProxy,
|
|
21
|
-
{},
|
|
22
|
-
{ escape: (text) => text }
|
|
23
|
-
);
|
|
36
|
+
return Mustache.render(template, variables, {}, { escape: (text) => text });
|
|
24
37
|
}
|
|
@@ -3,17 +3,17 @@
|
|
|
3
3
|
* This allows Mustache to access properties of objects (e.g., {{user.name}}) while
|
|
4
4
|
* stringifying objects that are accessed as leaf values (e.g., {{user.profile}}).
|
|
5
5
|
*/
|
|
6
|
-
export function createTemplateVariablesProxy(obj:
|
|
6
|
+
export function createTemplateVariablesProxy<T>(obj: T): T {
|
|
7
7
|
if (obj === null || obj === undefined) {
|
|
8
8
|
return obj;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
if (Array.isArray(obj)) {
|
|
12
|
-
return obj.map(createTemplateVariablesProxy);
|
|
12
|
+
return obj.map(createTemplateVariablesProxy) as T;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
if (typeof obj === "object") {
|
|
16
|
-
return new Proxy(obj
|
|
16
|
+
return new Proxy(obj, {
|
|
17
17
|
get(target, prop: string | symbol) {
|
|
18
18
|
// Handle toString and valueOf to stringify the object when accessed directly
|
|
19
19
|
if (prop === "toString") {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PromptTemplate } from "../types/templating";
|
|
2
2
|
|
|
3
3
|
import Mustache from "mustache";
|
|
4
4
|
|
|
5
5
|
type GetTemplateVariableArgs = {
|
|
6
|
-
template:
|
|
6
|
+
template: PromptTemplate;
|
|
7
7
|
};
|
|
8
8
|
/**
|
|
9
9
|
* Parse out the template variables of a prompt
|
|
@@ -12,6 +12,22 @@ type GetTemplateVariableArgs = {
|
|
|
12
12
|
*/
|
|
13
13
|
export function getTemplateVariables(args: GetTemplateVariableArgs): string[] {
|
|
14
14
|
const { template } = args;
|
|
15
|
+
if (typeof template === "string") {
|
|
16
|
+
return getTemplateVariablesFromString(template);
|
|
17
|
+
}
|
|
18
|
+
return template.reduce((acc, message) => {
|
|
19
|
+
if (message.content !== undefined && typeof message.content === "string") {
|
|
20
|
+
return [...acc, ...getTemplateVariablesFromString(message.content)];
|
|
21
|
+
}
|
|
22
|
+
return acc;
|
|
23
|
+
}, [] as string[]);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Parse out the template variables of a string template
|
|
27
|
+
* @param template - The template to get the variables from
|
|
28
|
+
* @returns {string[]} a list of prompt template variables
|
|
29
|
+
*/
|
|
30
|
+
function getTemplateVariablesFromString(template: string): string[] {
|
|
15
31
|
const templateSpans = Mustache.parse(template);
|
|
16
32
|
return templateSpans.reduce((acc, templateSpan) => {
|
|
17
33
|
const [spanType, value] = templateSpan;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A value extractor that can retrieve data from an object using various methods.
|
|
3
|
+
*
|
|
4
|
+
* This type supports multiple ways to extract values from your data structure:
|
|
5
|
+
* - **String paths**: Simple property names, dot notation, or JSONPath expressions
|
|
6
|
+
* - **Function extractors**: Custom transformation functions
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* **Simple property access:**
|
|
10
|
+
* ```typescript
|
|
11
|
+
* const getter: ValueGetter<{ name: string }> = "name";
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* **Dot notation for nested properties:**
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const getter: ValueGetter<{ user: { profile: { name: string } } }> = "user.profile.name";
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* **Array element access:**
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const getter: ValueGetter<{ items: string[] }> = "items[0]";
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* **JSONPath expression:**
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const getter: ValueGetter<{ items: Array<{ id: number }> }> = "$.items[*].id";
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* **Function-based extraction:**
|
|
34
|
+
* ```typescript
|
|
35
|
+
* const getter: ValueGetter<{ firstName: string; lastName: string }> =
|
|
36
|
+
* (data) => `${data.firstName} ${data.lastName}`;
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* **Complex transformation:**
|
|
41
|
+
* ```typescript
|
|
42
|
+
* const getter: ValueGetter<{ scores: number[] }> =
|
|
43
|
+
* (data) => data.scores.reduce((a, b) => a + b, 0) / data.scores.length;
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @typeParam DataType - The type of the data object to extract values from
|
|
47
|
+
*/
|
|
48
|
+
export type ValueGetter<DataType extends Record<string, unknown>> =
|
|
49
|
+
| string
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
+
| ((data: DataType) => any);
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A mapping configuration that transforms data from one structure to another.
|
|
55
|
+
*
|
|
56
|
+
* This type defines how to map fields from your data structure to the fields
|
|
57
|
+
* expected by an evaluator or other component. The mapping is flexible and
|
|
58
|
+
* supports multiple extraction methods.
|
|
59
|
+
*
|
|
60
|
+
* **Key Features:**
|
|
61
|
+
* - Preserves original data fields
|
|
62
|
+
* - Adds/overrides fields with mapped values
|
|
63
|
+
* - Supports nested property access
|
|
64
|
+
* - Supports array element access
|
|
65
|
+
* - Supports JSONPath expressions for complex queries
|
|
66
|
+
* - Supports function-based transformations
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* **Basic field mapping:**
|
|
70
|
+
* ```typescript
|
|
71
|
+
* type MyData = {
|
|
72
|
+
* userQuery: string;
|
|
73
|
+
* context: string;
|
|
74
|
+
* response: string;
|
|
75
|
+
* };
|
|
76
|
+
*
|
|
77
|
+
* const mapping: ObjectMapping<MyData> = {
|
|
78
|
+
* input: "userQuery", // Map "input" to "userQuery"
|
|
79
|
+
* reference: "context", // Map "reference" to "context"
|
|
80
|
+
* output: "response", // Map "output" to "response"
|
|
81
|
+
* };
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* **Nested property mapping:**
|
|
86
|
+
* ```typescript
|
|
87
|
+
* type ApiData = {
|
|
88
|
+
* request: {
|
|
89
|
+
* body: {
|
|
90
|
+
* query: string;
|
|
91
|
+
* context: string;
|
|
92
|
+
* };
|
|
93
|
+
* };
|
|
94
|
+
* response: {
|
|
95
|
+
* data: {
|
|
96
|
+
* text: string;
|
|
97
|
+
* };
|
|
98
|
+
* };
|
|
99
|
+
* };
|
|
100
|
+
*
|
|
101
|
+
* const mapping: ObjectMapping<ApiData> = {
|
|
102
|
+
* input: "request.body.query",
|
|
103
|
+
* reference: "request.body.context",
|
|
104
|
+
* output: "response.data.text",
|
|
105
|
+
* };
|
|
106
|
+
* ```
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* **Array element access:**
|
|
110
|
+
* ```typescript
|
|
111
|
+
* type DataWithArrays = {
|
|
112
|
+
* messages: Array<{ role: string; content: string }>;
|
|
113
|
+
* sources: string[];
|
|
114
|
+
* };
|
|
115
|
+
*
|
|
116
|
+
* const mapping: ObjectMapping<DataWithArrays> = {
|
|
117
|
+
* firstMessage: "messages[0].content",
|
|
118
|
+
* lastSource: "sources[-1]", // Last element
|
|
119
|
+
* allRoles: "$.messages[*].role", // JSONPath for all roles
|
|
120
|
+
* };
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* **Function-based transformations:**
|
|
125
|
+
* ```typescript
|
|
126
|
+
* type RawData = {
|
|
127
|
+
* firstName: string;
|
|
128
|
+
* lastName: string;
|
|
129
|
+
* contexts: string[];
|
|
130
|
+
* scores: number[];
|
|
131
|
+
* };
|
|
132
|
+
*
|
|
133
|
+
* const mapping: ObjectMapping<RawData> = {
|
|
134
|
+
* // Combine fields
|
|
135
|
+
* fullName: (data) => `${data.firstName} ${data.lastName}`,
|
|
136
|
+
* // Transform array to string
|
|
137
|
+
* contextText: (data) => data.contexts.join("\n\n"),
|
|
138
|
+
* // Calculate derived value
|
|
139
|
+
* averageScore: (data) =>
|
|
140
|
+
* data.scores.reduce((a, b) => a + b, 0) / data.scores.length,
|
|
141
|
+
* // Conditional logic
|
|
142
|
+
* status: (data) => data.scores.length > 0 ? "active" : "inactive",
|
|
143
|
+
* };
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* **Mixed mapping types:**
|
|
148
|
+
* ```typescript
|
|
149
|
+
* type ComplexData = {
|
|
150
|
+
* user: {
|
|
151
|
+
* name: string;
|
|
152
|
+
* email: string;
|
|
153
|
+
* };
|
|
154
|
+
* items: Array<{ id: number; name: string }>;
|
|
155
|
+
* metadata: {
|
|
156
|
+
* tags: string[];
|
|
157
|
+
* };
|
|
158
|
+
* };
|
|
159
|
+
*
|
|
160
|
+
* const mapping: ObjectMapping<ComplexData> = {
|
|
161
|
+
* // Simple dot notation
|
|
162
|
+
* userName: "user.name",
|
|
163
|
+
* // Array access
|
|
164
|
+
* firstItemId: "items[0].id",
|
|
165
|
+
* // JSONPath for complex query
|
|
166
|
+
* allItemNames: "$.items[*].name",
|
|
167
|
+
* // Function for transformation
|
|
168
|
+
* formattedTags: (data) => data.metadata.tags.map(t => `#${t}`).join(" "),
|
|
169
|
+
* };
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* **Real-world evaluator binding:**
|
|
174
|
+
* ```typescript
|
|
175
|
+
* import { bindEvaluator, createHallucinationEvaluator } from "@arizeai/phoenix-evals";
|
|
176
|
+
*
|
|
177
|
+
* type QAData = {
|
|
178
|
+
* question: string;
|
|
179
|
+
* context: string;
|
|
180
|
+
* answer: string;
|
|
181
|
+
* };
|
|
182
|
+
*
|
|
183
|
+
* const mapping: ObjectMapping<QAData> = {
|
|
184
|
+
* input: "question", // Evaluator expects "input"
|
|
185
|
+
* reference: "context", // Evaluator expects "reference"
|
|
186
|
+
* output: "answer", // Evaluator expects "output"
|
|
187
|
+
* };
|
|
188
|
+
*
|
|
189
|
+
* const evaluator = bindEvaluator(
|
|
190
|
+
* createHallucinationEvaluator({ model: openai("gpt-4") }),
|
|
191
|
+
* { inputMapping: mapping }
|
|
192
|
+
* );
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* @typeParam DataType - The type of the data object being mapped
|
|
196
|
+
*/
|
|
197
|
+
export type ObjectMapping<DataType extends Record<string, unknown>> = Record<
|
|
198
|
+
string,
|
|
199
|
+
ValueGetter<DataType>
|
|
200
|
+
>;
|
package/src/types/evals.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { ObjectMapping } from "./data";
|
|
1
2
|
import { WithTelemetry } from "./otel";
|
|
2
|
-
import {
|
|
3
|
+
import { PromptTemplate } from "./templating";
|
|
3
4
|
|
|
4
5
|
import { LanguageModel } from "ai";
|
|
5
6
|
|
|
@@ -78,25 +79,44 @@ export interface CreateClassifierArgs extends WithTelemetry {
|
|
|
78
79
|
/**
|
|
79
80
|
* The prompt template to use for classification
|
|
80
81
|
*/
|
|
81
|
-
promptTemplate:
|
|
82
|
+
promptTemplate: PromptTemplate;
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
export interface CreateEvaluatorArgs
|
|
85
|
+
export interface CreateEvaluatorArgs<
|
|
86
|
+
ExampleType extends Record<string, unknown> = Record<string, unknown>,
|
|
87
|
+
> extends WithTelemetry {
|
|
85
88
|
/**
|
|
86
89
|
* The name of the metric that the evaluator produces
|
|
87
90
|
* E.x. "correctness"
|
|
88
91
|
*/
|
|
89
92
|
name: string;
|
|
93
|
+
/**
|
|
94
|
+
* The kind of the evaluation. Also known as the "kind" of evaluator.
|
|
95
|
+
*/
|
|
96
|
+
kind: EvaluationKind;
|
|
90
97
|
/**
|
|
91
98
|
* If present, represents the direction in which you want the metric to be optimized
|
|
92
99
|
* E.x. "MAXIMIZE" means you want the number to be higher.
|
|
93
100
|
*/
|
|
94
101
|
optimizationDirection?: OptimizationDirection;
|
|
102
|
+
/**
|
|
103
|
+
* The mapping of the input to evaluate to the shape that the evaluator expects
|
|
104
|
+
*/
|
|
105
|
+
inputMapping?: ObjectMapping<ExampleType>;
|
|
95
106
|
}
|
|
96
107
|
|
|
97
|
-
export
|
|
98
|
-
|
|
99
|
-
|
|
108
|
+
export type CreateLLMEvaluatorArgs<RecordType extends Record<string, unknown>> =
|
|
109
|
+
Omit<CreateEvaluatorArgs<RecordType>, "kind">;
|
|
110
|
+
|
|
111
|
+
export interface CreateClassificationEvaluatorArgs<
|
|
112
|
+
RecordType extends Record<string, unknown>,
|
|
113
|
+
> extends CreateClassifierArgs,
|
|
114
|
+
CreateLLMEvaluatorArgs<RecordType> {
|
|
115
|
+
/**
|
|
116
|
+
* The prompt template to use for classification
|
|
117
|
+
*/
|
|
118
|
+
promptTemplate: PromptTemplate;
|
|
119
|
+
}
|
|
100
120
|
|
|
101
121
|
export type EvaluatorFn<ExampleType extends Record<string, unknown>> = (
|
|
102
122
|
args: ExampleType
|
|
@@ -136,7 +156,7 @@ interface EvaluatorDescription {
|
|
|
136
156
|
* The Base Evaluator interface
|
|
137
157
|
* This is the interface that all evaluators must implement
|
|
138
158
|
*/
|
|
139
|
-
export interface
|
|
159
|
+
export interface EvaluatorInterface<ExampleType extends Record<string, unknown>>
|
|
140
160
|
extends EvaluatorDescription {
|
|
141
161
|
/**
|
|
142
162
|
* The function that evaluates the example
|
package/src/types/index.ts
CHANGED
package/src/types/otel.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { Tracer } from "@opentelemetry/api";
|
|
2
2
|
|
|
3
|
+
export type TelemetryConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* Whether OpenTelemetry is enabled on the call.
|
|
6
|
+
* Defaults to true for visibility into the evals calls.
|
|
7
|
+
* @default true
|
|
8
|
+
*/
|
|
9
|
+
isEnabled?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* The tracer to use for the call.
|
|
12
|
+
* If not provided, the traces will get picked up by the global tracer.
|
|
13
|
+
*/
|
|
14
|
+
tracer?: Tracer;
|
|
15
|
+
};
|
|
16
|
+
|
|
3
17
|
export type WithTelemetry = {
|
|
4
|
-
telemetry?:
|
|
5
|
-
/**
|
|
6
|
-
* Whether OpenTelemetry is enabled on the call.
|
|
7
|
-
* Defaults to true for visibility into the evals calls.
|
|
8
|
-
* @default true
|
|
9
|
-
*/
|
|
10
|
-
isEnabled?: boolean;
|
|
11
|
-
/**
|
|
12
|
-
* The tracer to use for the call.
|
|
13
|
-
* If not provided, the traces will get picked up by the global tracer.
|
|
14
|
-
*/
|
|
15
|
-
tracer?: Tracer;
|
|
16
|
-
};
|
|
18
|
+
telemetry?: TelemetryConfig;
|
|
17
19
|
};
|
package/src/types/templating.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import type { ModelMessage } from "ai";
|
|
2
|
+
|
|
3
|
+
export type PromptTemplate = string | Array<ModelMessage>;
|
|
4
|
+
export type RenderedPrompt = string | Array<ModelMessage>;
|
|
2
5
|
export type TemplateVariables = Record<string, unknown>;
|
|
3
6
|
|
|
4
|
-
/**
|
|
5
|
-
* A class or object that has a prompt template
|
|
6
|
-
*/
|
|
7
7
|
export interface WithPromptTemplate {
|
|
8
|
-
readonly promptTemplate:
|
|
9
|
-
|
|
8
|
+
readonly promptTemplate: PromptTemplate;
|
|
9
|
+
/**
|
|
10
|
+
* List out the prompt template variables needed to perform evaluation
|
|
11
|
+
*/
|
|
12
|
+
readonly promptTemplateVariables: string[];
|
|
10
13
|
}
|