@arizeai/phoenix-client
Version:
A client for the Phoenix API
51 lines • 1.78 kB
JavaScript
/**
* Build and validate annotation result fields
*/
function buildAnnotationResult(annotation, annotationType) {
const result = {};
// Build result with trimming for string fields
if (annotation.label !== undefined) {
result.label = annotation.label.trim() || null;
}
if (annotation.score !== undefined) {
result.score = annotation.score;
}
if (annotation.explanation !== undefined) {
result.explanation = annotation.explanation.trim() || null;
}
// Validate that at least one result field is provided
const hasValidResult = result.label || result.score !== undefined || result.explanation;
if (!hasValidResult) {
throw new Error(`At least one of label, score, or explanation must be provided for ${annotationType} annotation`);
}
return result;
}
/**
* Convert a SpanAnnotation to the API format
*/
export function toSpanAnnotationData(annotation) {
const result = buildAnnotationResult(annotation, "span");
return {
span_id: annotation.spanId.trim(),
name: annotation.name.trim(),
annotator_kind: annotation.annotatorKind ?? "HUMAN",
result,
metadata: annotation.metadata ?? null,
identifier: annotation.identifier?.trim() ?? "",
};
}
/**
* Convert a DocumentAnnotation to the API format
*/
export function toDocumentAnnotationData(annotation) {
const result = buildAnnotationResult(annotation, "document");
return {
span_id: annotation.spanId.trim(),
document_position: annotation.documentPosition,
name: annotation.name.trim(),
annotator_kind: annotation.annotatorKind ?? "HUMAN",
result,
metadata: annotation.metadata ?? null,
};
}
//# sourceMappingURL=types.js.map