@arizeai/phoenix-client 1.1.0 → 1.3.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.
Files changed (103) hide show
  1. package/dist/esm/__generated__/api/v1.d.ts +375 -0
  2. package/dist/esm/__generated__/api/v1.d.ts.map +1 -1
  3. package/dist/esm/client.d.ts +13 -1
  4. package/dist/esm/client.d.ts.map +1 -1
  5. package/dist/esm/client.js +4 -1
  6. package/dist/esm/client.js.map +1 -1
  7. package/dist/esm/experiments/instrumention.d.ts +18 -0
  8. package/dist/esm/experiments/instrumention.d.ts.map +1 -0
  9. package/dist/esm/experiments/instrumention.js +34 -0
  10. package/dist/esm/experiments/instrumention.js.map +1 -0
  11. package/dist/esm/experiments/runExperiment.d.ts +64 -26
  12. package/dist/esm/experiments/runExperiment.d.ts.map +1 -1
  13. package/dist/esm/experiments/runExperiment.js +290 -76
  14. package/dist/esm/experiments/runExperiment.js.map +1 -1
  15. package/dist/esm/spans/addSpanAnnotation.d.ts +39 -0
  16. package/dist/esm/spans/addSpanAnnotation.d.ts.map +1 -0
  17. package/dist/esm/spans/addSpanAnnotation.js +44 -0
  18. package/dist/esm/spans/addSpanAnnotation.js.map +1 -0
  19. package/dist/esm/spans/index.d.ts +3 -0
  20. package/dist/esm/spans/index.d.ts.map +1 -0
  21. package/dist/esm/spans/index.js +3 -0
  22. package/dist/esm/spans/index.js.map +1 -0
  23. package/dist/esm/spans/logSpanAnnotations.d.ts +51 -0
  24. package/dist/esm/spans/logSpanAnnotations.d.ts.map +1 -0
  25. package/dist/esm/spans/logSpanAnnotations.js +53 -0
  26. package/dist/esm/spans/logSpanAnnotations.js.map +1 -0
  27. package/dist/esm/spans/types.d.ts +43 -0
  28. package/dist/esm/spans/types.d.ts.map +1 -0
  29. package/dist/esm/spans/types.js +18 -0
  30. package/dist/esm/spans/types.js.map +1 -0
  31. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  32. package/dist/esm/types/annotations.d.ts +2 -1
  33. package/dist/esm/types/annotations.d.ts.map +1 -1
  34. package/dist/esm/types/experiments.d.ts +1 -8
  35. package/dist/esm/types/experiments.d.ts.map +1 -1
  36. package/dist/esm/utils/ensureString.d.ts +8 -0
  37. package/dist/esm/utils/ensureString.d.ts.map +1 -0
  38. package/dist/esm/utils/ensureString.js +14 -0
  39. package/dist/esm/utils/ensureString.js.map +1 -0
  40. package/dist/esm/utils/formatPromptMessages.d.ts.map +1 -1
  41. package/dist/esm/utils/getPromptBySelector.d.ts.map +1 -1
  42. package/dist/esm/utils/objectAsAttributes.d.ts +3 -0
  43. package/dist/esm/utils/objectAsAttributes.d.ts.map +1 -0
  44. package/dist/esm/utils/objectAsAttributes.js +4 -0
  45. package/dist/esm/utils/objectAsAttributes.js.map +1 -0
  46. package/dist/src/__generated__/api/v1.d.ts +375 -0
  47. package/dist/src/__generated__/api/v1.d.ts.map +1 -1
  48. package/dist/src/client.d.ts +13 -1
  49. package/dist/src/client.d.ts.map +1 -1
  50. package/dist/src/client.js +1 -1
  51. package/dist/src/client.js.map +1 -1
  52. package/dist/src/experiments/instrumention.d.ts +18 -0
  53. package/dist/src/experiments/instrumention.d.ts.map +1 -0
  54. package/dist/src/experiments/instrumention.js +38 -0
  55. package/dist/src/experiments/instrumention.js.map +1 -0
  56. package/dist/src/experiments/runExperiment.d.ts +64 -26
  57. package/dist/src/experiments/runExperiment.d.ts.map +1 -1
  58. package/dist/src/experiments/runExperiment.js +291 -80
  59. package/dist/src/experiments/runExperiment.js.map +1 -1
  60. package/dist/src/spans/addSpanAnnotation.d.ts +39 -0
  61. package/dist/src/spans/addSpanAnnotation.d.ts.map +1 -0
  62. package/dist/src/spans/addSpanAnnotation.js +59 -0
  63. package/dist/src/spans/addSpanAnnotation.js.map +1 -0
  64. package/dist/src/spans/index.d.ts +3 -0
  65. package/dist/src/spans/index.d.ts.map +1 -0
  66. package/dist/src/spans/index.js +19 -0
  67. package/dist/src/spans/index.js.map +1 -0
  68. package/dist/src/spans/logSpanAnnotations.d.ts +51 -0
  69. package/dist/src/spans/logSpanAnnotations.d.ts.map +1 -0
  70. package/dist/src/spans/logSpanAnnotations.js +68 -0
  71. package/dist/src/spans/logSpanAnnotations.js.map +1 -0
  72. package/dist/src/spans/types.d.ts +43 -0
  73. package/dist/src/spans/types.d.ts.map +1 -0
  74. package/dist/src/spans/types.js +22 -0
  75. package/dist/src/spans/types.js.map +1 -0
  76. package/dist/src/types/annotations.d.ts +2 -1
  77. package/dist/src/types/annotations.d.ts.map +1 -1
  78. package/dist/src/types/experiments.d.ts +1 -8
  79. package/dist/src/types/experiments.d.ts.map +1 -1
  80. package/dist/src/utils/ensureString.d.ts +8 -0
  81. package/dist/src/utils/ensureString.d.ts.map +1 -0
  82. package/dist/src/utils/ensureString.js +18 -0
  83. package/dist/src/utils/ensureString.js.map +1 -0
  84. package/dist/src/utils/formatPromptMessages.d.ts.map +1 -1
  85. package/dist/src/utils/getPromptBySelector.d.ts.map +1 -1
  86. package/dist/src/utils/objectAsAttributes.d.ts +3 -0
  87. package/dist/src/utils/objectAsAttributes.d.ts.map +1 -0
  88. package/dist/src/utils/objectAsAttributes.js +7 -0
  89. package/dist/src/utils/objectAsAttributes.js.map +1 -0
  90. package/dist/tsconfig.tsbuildinfo +1 -1
  91. package/package.json +15 -1
  92. package/src/__generated__/api/v1.ts +375 -0
  93. package/src/client.ts +4 -1
  94. package/src/experiments/instrumention.ts +52 -0
  95. package/src/experiments/runExperiment.ts +394 -119
  96. package/src/spans/addSpanAnnotation.ts +59 -0
  97. package/src/spans/index.ts +2 -0
  98. package/src/spans/logSpanAnnotations.ts +71 -0
  99. package/src/spans/types.ts +60 -0
  100. package/src/types/annotations.ts +4 -1
  101. package/src/types/experiments.ts +1 -8
  102. package/src/utils/ensureString.ts +14 -0
  103. package/src/utils/objectAsAttributes.ts +9 -0
@@ -0,0 +1,59 @@
1
+ import { createClient } from "../client";
2
+ import { ClientFn } from "../types/core";
3
+ import { SpanAnnotation, toSpanAnnotationData } from "./types";
4
+
5
+ /**
6
+ * Parameters to add a span annotation
7
+ */
8
+ interface AddSpanAnnotationParams extends ClientFn {
9
+ spanAnnotation: SpanAnnotation;
10
+ }
11
+
12
+ /**
13
+ * Add an annotation to a span.
14
+ *
15
+ * The annotation can be of type "LLM", "CODE", or "HUMAN" and can include a label, score, and metadata.
16
+ * If an identifier is provided and an annotation with that identifier already exists, it will be updated.
17
+ *
18
+ * @param params - The parameters to add a span annotation
19
+ * @returns The ID of the created or updated annotation
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const result = await addSpanAnnotation({
24
+ * spanAnnotation: {
25
+ * spanId: "123abc",
26
+ * name: "quality_score",
27
+ * label: "good",
28
+ * score: 0.95,
29
+ * annotatorKind: "LLM",
30
+ * identifier: "custom_id_123",
31
+ * metadata: {
32
+ * model: "gpt-4"
33
+ * }
34
+ * }
35
+ * });
36
+ * ```
37
+ */
38
+ export async function addSpanAnnotation({
39
+ client: _client,
40
+ spanAnnotation,
41
+ }: AddSpanAnnotationParams): Promise<{ id: string }> {
42
+ const client = _client ?? createClient();
43
+
44
+ const { data, error } = await client.POST("/v1/span_annotations", {
45
+ body: {
46
+ data: [toSpanAnnotationData(spanAnnotation)],
47
+ },
48
+ });
49
+
50
+ if (error) {
51
+ throw new Error(`Failed to add span annotation: ${error}`);
52
+ }
53
+
54
+ if (!data?.data?.[0]?.id) {
55
+ throw new Error("No annotation ID returned from server");
56
+ }
57
+
58
+ return data.data[0];
59
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./addSpanAnnotation";
2
+ export * from "./logSpanAnnotations";
@@ -0,0 +1,71 @@
1
+ import { createClient } from "../client";
2
+ import { ClientFn } from "../types/core";
3
+ import { SpanAnnotation, toSpanAnnotationData } from "./types";
4
+
5
+ /**
6
+ * Parameters to log multiple span annotations
7
+ */
8
+ interface LogSpanAnnotationsParams extends ClientFn {
9
+ /**
10
+ * The span annotations to log
11
+ */
12
+ spanAnnotations: SpanAnnotation[];
13
+ }
14
+
15
+ /**
16
+ * Log multiple span annotations in a single request.
17
+ *
18
+ * Each annotation can be of type "LLM", "CODE", or "HUMAN" and can include a label, score, and metadata.
19
+ * If an identifier is provided and an annotation with that identifier already exists, it will be updated.
20
+ *
21
+ * @param params - The parameters to log span annotations
22
+ * @returns The IDs of the created or updated annotations
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const results = await logSpanAnnotations({
27
+ * spanAnnotations: [
28
+ * {
29
+ * spanId: "123abc",
30
+ * name: "quality_score",
31
+ * label: "good",
32
+ * score: 0.95,
33
+ * annotatorKind: "LLM",
34
+ * identifier: "custom_id_123",
35
+ * metadata: {
36
+ * model: "gpt-4"
37
+ * }
38
+ * },
39
+ * {
40
+ * spanId: "456def",
41
+ * name: "sentiment",
42
+ * label: "positive",
43
+ * score: 0.8,
44
+ * annotatorKind: "CODE"
45
+ * }
46
+ * ]
47
+ * });
48
+ * ```
49
+ */
50
+ export async function logSpanAnnotations({
51
+ client: _client,
52
+ spanAnnotations,
53
+ }: LogSpanAnnotationsParams): Promise<{ id: string }[]> {
54
+ const client = _client ?? createClient();
55
+
56
+ const { data, error } = await client.POST("/v1/span_annotations", {
57
+ body: {
58
+ data: spanAnnotations.map(toSpanAnnotationData),
59
+ },
60
+ });
61
+
62
+ if (error) {
63
+ throw new Error(`Failed to log span annotations: ${error}`);
64
+ }
65
+
66
+ if (!data?.data?.length) {
67
+ throw new Error("No annotation IDs returned from server");
68
+ }
69
+
70
+ return data.data;
71
+ }
@@ -0,0 +1,60 @@
1
+ import { paths } from "../__generated__/api/v1";
2
+
3
+ type SpanAnnotationData =
4
+ paths["/v1/span_annotations"]["post"]["requestBody"]["content"]["application/json"]["data"][0];
5
+
6
+ /**
7
+ * Parameters for a single span annotation
8
+ */
9
+ export interface SpanAnnotation {
10
+ /**
11
+ * The OpenTelemetry Span ID (hex format without 0x prefix)
12
+ */
13
+ spanId: string;
14
+ /**
15
+ * The name of the annotation
16
+ */
17
+ name: string;
18
+ /**
19
+ * The label assigned by the annotation
20
+ */
21
+ label?: string;
22
+ /**
23
+ * The score assigned by the annotation
24
+ */
25
+ score?: number;
26
+ /**
27
+ * The identifier of the annotation. If provided, the annotation will be updated if it already exists.
28
+ */
29
+ identifier?: string;
30
+ /**
31
+ * Metadata for the annotation
32
+ */
33
+ metadata?: Record<string, unknown>;
34
+ /**
35
+ * The kind of annotator used for the annotation
36
+ * Can be "HUMAN", "LLM", or "CODE"
37
+ * @default "HUMAN"
38
+ */
39
+ annotatorKind?: SpanAnnotationData["annotator_kind"];
40
+ }
41
+
42
+ /**
43
+ * Convert a SpanAnnotation to the API format
44
+ */
45
+ export function toSpanAnnotationData(
46
+ annotation: SpanAnnotation
47
+ ): SpanAnnotationData {
48
+ return {
49
+ span_id: annotation.spanId,
50
+ name: annotation.name,
51
+ annotator_kind: annotation.annotatorKind ?? "HUMAN",
52
+ result: {
53
+ label: annotation.label ?? null,
54
+ score: annotation.score ?? null,
55
+ explanation: null,
56
+ },
57
+ metadata: annotation.metadata ?? null,
58
+ identifier: annotation.identifier ?? "",
59
+ };
60
+ }
@@ -1 +1,4 @@
1
- export type AnnotatorKind = "HUMAN" | "LLM";
1
+ import { components } from "../__generated__/api/v1";
2
+
3
+ export type AnnotatorKind =
4
+ components["schemas"]["SpanAnnotationData"]["annotator_kind"];
@@ -8,7 +8,6 @@ import { Example } from "./datasets";
8
8
  export interface Experiment extends Node {
9
9
  datasetId: string;
10
10
  datasetVersionId: string;
11
- repetitions: number;
12
11
  /**
13
12
  * The project under which the experiment task traces are recorded
14
13
  */
@@ -16,7 +15,6 @@ export interface Experiment extends Node {
16
15
  }
17
16
 
18
17
  export interface RanExperiment extends Experiment {
19
- params: ExperimentParameters;
20
18
  runs: Record<string, ExperimentRun>;
21
19
  evaluationRuns?: ExperimentEvaluationRun[];
22
20
  }
@@ -32,7 +30,6 @@ export interface ExperimentRun extends Node {
32
30
  */
33
31
  experimentId: string;
34
32
  datasetExampleId: string;
35
- repetitionNumber: number;
36
33
  output?: string | Record<string, unknown> | null;
37
34
  error: string | null;
38
35
  traceId: string | null;
@@ -59,6 +56,7 @@ export type EvaluatorParams = {
59
56
 
60
57
  export type Evaluator = {
61
58
  name: string;
59
+ kind: AnnotatorKind;
62
60
  evaluate: (
63
61
  args: EvaluatorParams
64
62
  ) => Promise<EvaluationResult> | EvaluationResult;
@@ -100,9 +98,4 @@ export interface ExperimentParameters {
100
98
  * The number of examples to run the experiment on
101
99
  */
102
100
  nExamples: number;
103
- /**
104
- * The number of repetitions to run the experiment
105
- * e.g. the number of times to run the task
106
- */
107
- nRepetitions: number;
108
101
  }
@@ -0,0 +1,14 @@
1
+ import { safelyStringifyJSON } from "./safelyStringifyJSON";
2
+
3
+ /**
4
+ * Ensures that a value is a string.
5
+ * If the value is not a string, it will be converted to a string using `safelyStringifyJSON`.
6
+ * @param value - The value to ensure is a string.
7
+ * @returns The value as a string.
8
+ */
9
+ export function ensureString(value: unknown): string {
10
+ if (typeof value === "string") {
11
+ return value;
12
+ }
13
+ return safelyStringifyJSON(value)?.json ?? "";
14
+ }
@@ -0,0 +1,9 @@
1
+ import { AttributeValue } from "@opentelemetry/api";
2
+
3
+ export function objectAsAttributes<T extends Record<string, unknown>>(
4
+ obj: T
5
+ ): Record<string, AttributeValue> {
6
+ return Object.fromEntries(
7
+ Object.entries(obj).filter(([_, value]) => value !== null)
8
+ ) as Record<string, AttributeValue>;
9
+ }