@arizeai/phoenix-client 3.0.0 → 3.2.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 (91) hide show
  1. package/README.md +22 -1
  2. package/dist/esm/__generated__/api/v1.d.ts +188 -50
  3. package/dist/esm/__generated__/api/v1.d.ts.map +1 -1
  4. package/dist/esm/experiments/instrumention.d.ts +6 -1
  5. package/dist/esm/experiments/instrumention.d.ts.map +1 -1
  6. package/dist/esm/experiments/instrumention.js +15 -15
  7. package/dist/esm/experiments/instrumention.js.map +1 -1
  8. package/dist/esm/experiments/runExperiment.d.ts +12 -2
  9. package/dist/esm/experiments/runExperiment.d.ts.map +1 -1
  10. package/dist/esm/experiments/runExperiment.js +5 -2
  11. package/dist/esm/experiments/runExperiment.js.map +1 -1
  12. package/dist/esm/schemas/llm/converters.js +4 -4
  13. package/dist/esm/schemas/llm/converters.js.map +1 -1
  14. package/dist/esm/spans/addDocumentAnnotation.d.ts +45 -0
  15. package/dist/esm/spans/addDocumentAnnotation.d.ts.map +1 -0
  16. package/dist/esm/spans/addDocumentAnnotation.js +45 -0
  17. package/dist/esm/spans/addDocumentAnnotation.js.map +1 -0
  18. package/dist/esm/spans/addSpanAnnotation.d.ts +8 -2
  19. package/dist/esm/spans/addSpanAnnotation.d.ts.map +1 -1
  20. package/dist/esm/spans/addSpanAnnotation.js +5 -5
  21. package/dist/esm/spans/addSpanAnnotation.js.map +1 -1
  22. package/dist/esm/spans/index.d.ts +2 -0
  23. package/dist/esm/spans/index.d.ts.map +1 -1
  24. package/dist/esm/spans/index.js +2 -0
  25. package/dist/esm/spans/index.js.map +1 -1
  26. package/dist/esm/spans/logDocumentAnnotations.d.ts +58 -0
  27. package/dist/esm/spans/logDocumentAnnotations.d.ts.map +1 -0
  28. package/dist/esm/spans/logDocumentAnnotations.js +55 -0
  29. package/dist/esm/spans/logDocumentAnnotations.js.map +1 -0
  30. package/dist/esm/spans/logSpanAnnotations.d.ts +7 -1
  31. package/dist/esm/spans/logSpanAnnotations.d.ts.map +1 -1
  32. package/dist/esm/spans/logSpanAnnotations.js +5 -5
  33. package/dist/esm/spans/logSpanAnnotations.js.map +1 -1
  34. package/dist/esm/spans/types.d.ts +48 -0
  35. package/dist/esm/spans/types.d.ts.map +1 -1
  36. package/dist/esm/spans/types.js +41 -8
  37. package/dist/esm/spans/types.js.map +1 -1
  38. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  39. package/dist/esm/utils/formatPromptMessages.d.ts.map +1 -1
  40. package/dist/esm/utils/getPromptBySelector.d.ts.map +1 -1
  41. package/dist/src/__generated__/api/v1.d.ts +188 -50
  42. package/dist/src/__generated__/api/v1.d.ts.map +1 -1
  43. package/dist/src/experiments/instrumention.d.ts +6 -1
  44. package/dist/src/experiments/instrumention.d.ts.map +1 -1
  45. package/dist/src/experiments/instrumention.js +13 -13
  46. package/dist/src/experiments/instrumention.js.map +1 -1
  47. package/dist/src/experiments/runExperiment.d.ts +12 -2
  48. package/dist/src/experiments/runExperiment.d.ts.map +1 -1
  49. package/dist/src/experiments/runExperiment.js +5 -2
  50. package/dist/src/experiments/runExperiment.js.map +1 -1
  51. package/dist/src/schemas/llm/converters.js +4 -4
  52. package/dist/src/schemas/llm/converters.js.map +1 -1
  53. package/dist/src/spans/addDocumentAnnotation.d.ts +45 -0
  54. package/dist/src/spans/addDocumentAnnotation.d.ts.map +1 -0
  55. package/dist/src/spans/addDocumentAnnotation.js +49 -0
  56. package/dist/src/spans/addDocumentAnnotation.js.map +1 -0
  57. package/dist/src/spans/addSpanAnnotation.d.ts +8 -2
  58. package/dist/src/spans/addSpanAnnotation.d.ts.map +1 -1
  59. package/dist/src/spans/addSpanAnnotation.js +6 -6
  60. package/dist/src/spans/addSpanAnnotation.js.map +1 -1
  61. package/dist/src/spans/index.d.ts +2 -0
  62. package/dist/src/spans/index.d.ts.map +1 -1
  63. package/dist/src/spans/index.js +2 -0
  64. package/dist/src/spans/index.js.map +1 -1
  65. package/dist/src/spans/logDocumentAnnotations.d.ts +58 -0
  66. package/dist/src/spans/logDocumentAnnotations.d.ts.map +1 -0
  67. package/dist/src/spans/logDocumentAnnotations.js +58 -0
  68. package/dist/src/spans/logDocumentAnnotations.js.map +1 -0
  69. package/dist/src/spans/logSpanAnnotations.d.ts +7 -1
  70. package/dist/src/spans/logSpanAnnotations.d.ts.map +1 -1
  71. package/dist/src/spans/logSpanAnnotations.js +5 -6
  72. package/dist/src/spans/logSpanAnnotations.js.map +1 -1
  73. package/dist/src/spans/types.d.ts +48 -0
  74. package/dist/src/spans/types.d.ts.map +1 -1
  75. package/dist/src/spans/types.js +45 -10
  76. package/dist/src/spans/types.js.map +1 -1
  77. package/dist/src/utils/formatPromptMessages.d.ts.map +1 -1
  78. package/dist/src/utils/getPromptBySelector.d.ts.map +1 -1
  79. package/dist/tsconfig.tsbuildinfo +1 -1
  80. package/package.json +1 -1
  81. package/src/__generated__/api/v1.d.ts +1240 -1238
  82. package/src/__generated__/api/v1.ts +188 -50
  83. package/src/experiments/instrumention.ts +27 -14
  84. package/src/experiments/runExperiment.ts +15 -0
  85. package/src/schemas/llm/converters.ts +4 -4
  86. package/src/spans/addDocumentAnnotation.ts +66 -0
  87. package/src/spans/addSpanAnnotation.ts +12 -6
  88. package/src/spans/index.ts +2 -0
  89. package/src/spans/logDocumentAnnotations.ts +79 -0
  90. package/src/spans/logSpanAnnotations.ts +11 -5
  91. package/src/spans/types.ts +112 -8
@@ -0,0 +1,79 @@
1
+ import { createClient } from "../client";
2
+ import { ClientFn } from "../types/core";
3
+ import { DocumentAnnotation, toDocumentAnnotationData } from "./types";
4
+
5
+ /**
6
+ * Parameters to log multiple document annotations
7
+ */
8
+ export interface LogDocumentAnnotationsParams extends ClientFn {
9
+ /**
10
+ * The document annotations to log
11
+ */
12
+ documentAnnotations: DocumentAnnotation[];
13
+ /**
14
+ * If true, the request will be fulfilled synchronously and return the annotation IDs.
15
+ * If false, the request will be processed asynchronously and return null.
16
+ * @default false
17
+ */
18
+ sync?: boolean;
19
+ }
20
+
21
+ /**
22
+ * Log multiple document annotations in a single request.
23
+ *
24
+ * Each annotation can be of type "LLM", "CODE", or "HUMAN" and can include a label, score, explanation, and metadata.
25
+ * At least one of label, score, or explanation must be provided for each annotation.
26
+ *
27
+ * @param params - The parameters to log document annotations
28
+ * @returns The IDs of the created annotations
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * const results = await logDocumentAnnotations({
33
+ * documentAnnotations: [
34
+ * {
35
+ * spanId: "123abc",
36
+ * documentPosition: 0,
37
+ * name: "relevance_score",
38
+ * label: "relevant",
39
+ * score: 0.95,
40
+ * annotatorKind: "LLM",
41
+ * explanation: "Document is highly relevant to the query",
42
+ * metadata: {
43
+ * model: "gpt-4"
44
+ * }
45
+ * },
46
+ * {
47
+ * spanId: "123abc",
48
+ * documentPosition: 1,
49
+ * name: "relevance_score",
50
+ * label: "somewhat_relevant",
51
+ * score: 0.6,
52
+ * annotatorKind: "LLM"
53
+ * }
54
+ * ]
55
+ * });
56
+ * ```
57
+ */
58
+ export async function logDocumentAnnotations({
59
+ client: _client,
60
+ documentAnnotations,
61
+ sync = false,
62
+ }: LogDocumentAnnotationsParams): Promise<{ id: string }[]> {
63
+ const client = _client ?? createClient();
64
+
65
+ const { data, error } = await client.POST("/v1/document_annotations", {
66
+ params: {
67
+ query: { sync },
68
+ },
69
+ body: {
70
+ data: documentAnnotations.map(toDocumentAnnotationData),
71
+ },
72
+ });
73
+
74
+ if (error) {
75
+ throw new Error(`Failed to log document annotations: ${error}`);
76
+ }
77
+
78
+ return data?.data || [];
79
+ }
@@ -10,6 +10,12 @@ export interface LogSpanAnnotationsParams extends ClientFn {
10
10
  * The span annotations to log
11
11
  */
12
12
  spanAnnotations: SpanAnnotation[];
13
+ /**
14
+ * If true, the request will be fulfilled synchronously and return the annotation IDs.
15
+ * If false, the request will be processed asynchronously and return null.
16
+ * @default false
17
+ */
18
+ sync?: boolean;
13
19
  }
14
20
 
15
21
  /**
@@ -50,10 +56,14 @@ export interface LogSpanAnnotationsParams extends ClientFn {
50
56
  export async function logSpanAnnotations({
51
57
  client: _client,
52
58
  spanAnnotations,
59
+ sync = false,
53
60
  }: LogSpanAnnotationsParams): Promise<{ id: string }[]> {
54
61
  const client = _client ?? createClient();
55
62
 
56
63
  const { data, error } = await client.POST("/v1/span_annotations", {
64
+ params: {
65
+ query: { sync },
66
+ },
57
67
  body: {
58
68
  data: spanAnnotations.map(toSpanAnnotationData),
59
69
  },
@@ -63,9 +73,5 @@ export async function logSpanAnnotations({
63
73
  throw new Error(`Failed to log span annotations: ${error}`);
64
74
  }
65
75
 
66
- if (!data?.data?.length) {
67
- throw new Error("No annotation IDs returned from server");
68
- }
69
-
70
- return data.data;
76
+ return data?.data || [];
71
77
  }
@@ -3,6 +3,9 @@ import { paths } from "../__generated__/api/v1";
3
3
  type SpanAnnotationData =
4
4
  paths["/v1/span_annotations"]["post"]["requestBody"]["content"]["application/json"]["data"][0];
5
5
 
6
+ type SpanDocumentAnnotationData =
7
+ paths["/v1/document_annotations"]["post"]["requestBody"]["content"]["application/json"]["data"][0];
8
+
6
9
  /**
7
10
  * Parameters for a single span annotation
8
11
  */
@@ -23,6 +26,10 @@ export interface SpanAnnotation {
23
26
  * The score assigned by the annotation
24
27
  */
25
28
  score?: number;
29
+ /**
30
+ * Explanation of the annotation result
31
+ */
32
+ explanation?: string;
26
33
  /**
27
34
  * The identifier of the annotation. If provided, the annotation will be updated if it already exists.
28
35
  */
@@ -39,22 +46,119 @@ export interface SpanAnnotation {
39
46
  annotatorKind?: SpanAnnotationData["annotator_kind"];
40
47
  }
41
48
 
49
+ /**
50
+ * Parameters for a single document annotation
51
+ */
52
+ export interface DocumentAnnotation {
53
+ /**
54
+ * The OpenTelemetry Span ID (hex format without 0x prefix)
55
+ */
56
+ spanId: string;
57
+ /**
58
+ * The 0-based index of the document within the span
59
+ */
60
+ documentPosition: number;
61
+ /**
62
+ * The name of the annotation
63
+ */
64
+ name: string;
65
+ /**
66
+ * The label assigned by the annotation
67
+ */
68
+ label?: string;
69
+ /**
70
+ * The score assigned by the annotation
71
+ */
72
+ score?: number;
73
+ /**
74
+ * Explanation of the annotation result
75
+ */
76
+ explanation?: string;
77
+ /**
78
+ * Metadata for the annotation
79
+ */
80
+ metadata?: Record<string, unknown>;
81
+ /**
82
+ * The kind of annotator used for the annotation
83
+ * Can be "HUMAN", "LLM", or "CODE"
84
+ * @default "HUMAN"
85
+ */
86
+ annotatorKind?: SpanDocumentAnnotationData["annotator_kind"];
87
+ }
88
+
89
+ type AnnotationResult = {
90
+ label?: string | null;
91
+ score?: number | null;
92
+ explanation?: string | null;
93
+ };
94
+
95
+ /**
96
+ * Build and validate annotation result fields
97
+ */
98
+ function buildAnnotationResult(
99
+ annotation: Pick<
100
+ SpanAnnotation | DocumentAnnotation,
101
+ "label" | "score" | "explanation"
102
+ >,
103
+ annotationType: "span" | "document"
104
+ ): AnnotationResult {
105
+ const result: AnnotationResult = {};
106
+
107
+ // Build result with trimming for string fields
108
+ if (annotation.label !== undefined) {
109
+ result.label = annotation.label.trim() || null;
110
+ }
111
+ if (annotation.score !== undefined) {
112
+ result.score = annotation.score;
113
+ }
114
+ if (annotation.explanation !== undefined) {
115
+ result.explanation = annotation.explanation.trim() || null;
116
+ }
117
+
118
+ // Validate that at least one result field is provided
119
+ const hasValidResult =
120
+ result.label || result.score !== undefined || result.explanation;
121
+ if (!hasValidResult) {
122
+ throw new Error(
123
+ `At least one of label, score, or explanation must be provided for ${annotationType} annotation`
124
+ );
125
+ }
126
+
127
+ return result;
128
+ }
129
+
42
130
  /**
43
131
  * Convert a SpanAnnotation to the API format
44
132
  */
45
133
  export function toSpanAnnotationData(
46
134
  annotation: SpanAnnotation
47
135
  ): SpanAnnotationData {
136
+ const result = buildAnnotationResult(annotation, "span");
137
+
138
+ return {
139
+ span_id: annotation.spanId.trim(),
140
+ name: annotation.name.trim(),
141
+ annotator_kind: annotation.annotatorKind ?? "HUMAN",
142
+ result,
143
+ metadata: annotation.metadata ?? null,
144
+ identifier: annotation.identifier?.trim() ?? "",
145
+ };
146
+ }
147
+
148
+ /**
149
+ * Convert a DocumentAnnotation to the API format
150
+ */
151
+ export function toDocumentAnnotationData(
152
+ annotation: DocumentAnnotation
153
+ ): SpanDocumentAnnotationData {
154
+ const result = buildAnnotationResult(annotation, "document");
155
+
48
156
  return {
49
- span_id: annotation.spanId,
50
- name: annotation.name,
157
+ span_id: annotation.spanId.trim(),
158
+ document_position: annotation.documentPosition,
159
+ name: annotation.name.trim(),
51
160
  annotator_kind: annotation.annotatorKind ?? "HUMAN",
52
- result: {
53
- label: annotation.label ?? null,
54
- score: annotation.score ?? null,
55
- explanation: null,
56
- },
161
+ result,
57
162
  metadata: annotation.metadata ?? null,
58
- identifier: annotation.identifier ?? "",
59
163
  };
60
164
  }