@arizeai/phoenix-evals 0.4.0 → 0.5.1

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 (47) hide show
  1. package/dist/esm/llm/ClassificationEvaluator.d.ts +3 -3
  2. package/dist/esm/llm/ClassificationEvaluator.d.ts.map +1 -1
  3. package/dist/esm/llm/ClassificationEvaluator.js.map +1 -1
  4. package/dist/esm/template/applyTemplate.d.ts +3 -3
  5. package/dist/esm/template/applyTemplate.d.ts.map +1 -1
  6. package/dist/esm/template/applyTemplate.js +16 -0
  7. package/dist/esm/template/applyTemplate.js.map +1 -1
  8. package/dist/esm/template/createTemplateVariablesProxy.d.ts +7 -0
  9. package/dist/esm/template/createTemplateVariablesProxy.d.ts.map +1 -0
  10. package/dist/esm/template/createTemplateVariablesProxy.js +61 -0
  11. package/dist/esm/template/createTemplateVariablesProxy.js.map +1 -0
  12. package/dist/esm/template/getTemplateVariables.d.ts +2 -2
  13. package/dist/esm/template/getTemplateVariables.d.ts.map +1 -1
  14. package/dist/esm/template/getTemplateVariables.js +16 -0
  15. package/dist/esm/template/getTemplateVariables.js.map +1 -1
  16. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  17. package/dist/esm/types/evals.d.ts +2 -1
  18. package/dist/esm/types/evals.d.ts.map +1 -1
  19. package/dist/esm/types/templating.d.ts +10 -1
  20. package/dist/esm/types/templating.d.ts.map +1 -1
  21. package/dist/src/llm/ClassificationEvaluator.d.ts +3 -3
  22. package/dist/src/llm/ClassificationEvaluator.d.ts.map +1 -1
  23. package/dist/src/llm/ClassificationEvaluator.js.map +1 -1
  24. package/dist/src/template/applyTemplate.d.ts +3 -3
  25. package/dist/src/template/applyTemplate.d.ts.map +1 -1
  26. package/dist/src/template/applyTemplate.js +13 -0
  27. package/dist/src/template/applyTemplate.js.map +1 -1
  28. package/dist/src/template/createTemplateVariablesProxy.d.ts +7 -0
  29. package/dist/src/template/createTemplateVariablesProxy.d.ts.map +1 -0
  30. package/dist/src/template/createTemplateVariablesProxy.js +64 -0
  31. package/dist/src/template/createTemplateVariablesProxy.js.map +1 -0
  32. package/dist/src/template/getTemplateVariables.d.ts +2 -2
  33. package/dist/src/template/getTemplateVariables.d.ts.map +1 -1
  34. package/dist/src/template/getTemplateVariables.js +16 -0
  35. package/dist/src/template/getTemplateVariables.js.map +1 -1
  36. package/dist/src/types/evals.d.ts +2 -1
  37. package/dist/src/types/evals.d.ts.map +1 -1
  38. package/dist/src/types/templating.d.ts +10 -1
  39. package/dist/src/types/templating.d.ts.map +1 -1
  40. package/dist/tsconfig.tsbuildinfo +1 -1
  41. package/package.json +2 -1
  42. package/src/llm/ClassificationEvaluator.ts +7 -5
  43. package/src/template/applyTemplate.ts +24 -3
  44. package/src/template/createTemplateVariablesProxy.ts +69 -0
  45. package/src/template/getTemplateVariables.ts +18 -2
  46. package/src/types/evals.ts +2 -1
  47. package/src/types/templating.ts +12 -1
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Creates a Proxy that wraps an object to stringify nested object values when accessed directly.
3
+ * This allows Mustache to access properties of objects (e.g., {{user.name}}) while
4
+ * stringifying objects that are accessed as leaf values (e.g., {{user.profile}}).
5
+ */
6
+ export function createTemplateVariablesProxy<T>(obj: T): T {
7
+ if (obj === null || obj === undefined) {
8
+ return obj;
9
+ }
10
+
11
+ if (Array.isArray(obj)) {
12
+ return obj.map(createTemplateVariablesProxy) as T;
13
+ }
14
+
15
+ if (typeof obj === "object") {
16
+ return new Proxy(obj, {
17
+ get(target, prop: string | symbol) {
18
+ // Handle toString and valueOf to stringify the object when accessed directly
19
+ if (prop === "toString") {
20
+ return () => JSON.stringify(target);
21
+ }
22
+ if (prop === "valueOf") {
23
+ return () => JSON.stringify(target);
24
+ }
25
+
26
+ if (typeof prop !== "string") {
27
+ return Reflect.get(target, prop);
28
+ }
29
+
30
+ const value = Reflect.get(target, prop);
31
+
32
+ // If the value is an object (not array, not null), wrap it in a proxy
33
+ // so it can be stringified if accessed directly, or have its properties accessed
34
+ if (
35
+ value !== null &&
36
+ typeof value === "object" &&
37
+ !Array.isArray(value)
38
+ ) {
39
+ return createTemplateVariablesProxy(value);
40
+ }
41
+
42
+ return value;
43
+ },
44
+ // Override valueOf and toString to stringify the object when Mustache tries to render it directly
45
+ // Mustache will call toString() when it needs to render an object as a string
46
+ has(target, prop) {
47
+ if (prop === "toString" || prop === "valueOf") {
48
+ return true;
49
+ }
50
+ return Reflect.has(target, prop);
51
+ },
52
+ ownKeys(target) {
53
+ return Reflect.ownKeys(target);
54
+ },
55
+ getOwnPropertyDescriptor(target, prop) {
56
+ if (prop === "toString") {
57
+ return {
58
+ enumerable: false,
59
+ configurable: true,
60
+ value: () => JSON.stringify(target),
61
+ };
62
+ }
63
+ return Reflect.getOwnPropertyDescriptor(target, prop);
64
+ },
65
+ });
66
+ }
67
+
68
+ return obj;
69
+ }
@@ -1,9 +1,9 @@
1
- import { Template } from "../types/templating";
1
+ import { PromptTemplate } from "../types/templating";
2
2
 
3
3
  import Mustache from "mustache";
4
4
 
5
5
  type GetTemplateVariableArgs = {
6
- template: 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;
@@ -1,4 +1,5 @@
1
1
  import { WithTelemetry } from "./otel";
2
+ import { PromptTemplate } from "./templating";
2
3
 
3
4
  import { LanguageModel } from "ai";
4
5
 
@@ -77,7 +78,7 @@ export interface CreateClassifierArgs extends WithTelemetry {
77
78
  /**
78
79
  * The prompt template to use for classification
79
80
  */
80
- promptTemplate: string;
81
+ promptTemplate: PromptTemplate;
81
82
  }
82
83
 
83
84
  export interface CreateEvaluatorArgs {
@@ -1,2 +1,13 @@
1
- export type Template = string;
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>;
6
+
7
+ export interface WithPromptTemplate {
8
+ readonly promptTemplate: PromptTemplate;
9
+ /**
10
+ * List out the prompt template variables needed to perform evaluation
11
+ */
12
+ readonly promptTemplateVariables: string[];
13
+ }