Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

1.7 KB
Raw Blame
import { type ScoreData } from "./types";
import {
  type ScoreDataTypeType,
  type ScoreTargetTrace,
  type ScoreTarget,
  type ScoreTargetSession,
  ScoreDataTypeEnum,
} from "@langfuse/shared";

export const isNumericDataType = (dataType: ScoreDataTypeType) =>
  dataType === ScoreDataTypeEnum.NUMERIC;

export const isCategoricalDataType = (dataType: ScoreDataTypeType) =>
  dataType === ScoreDataTypeEnum.CATEGORICAL;

export const isBooleanDataType = (dataType: ScoreDataTypeType) =>
  dataType === ScoreDataTypeEnum.BOOLEAN;

export const isTextDataType = (dataType: ScoreDataTypeType) =>
  dataType === ScoreDataTypeEnum.TEXT;

export const isScoreUnsaved = (scoreId?: string | null): boolean => !scoreId;

export const toOrderedScoresList = (list: ScoreData[]): ScoreData[] =>
  list.sort((a, b) => a.key.localeCompare(b.key));

export const isTraceScore = (
  scoreTarget: ScoreTarget,
): scoreTarget is ScoreTargetTrace => scoreTarget.type === "trace";

export const isSessionScore = (
  scoreTarget: ScoreTarget,
): scoreTarget is ScoreTargetSession => scoreTarget.type === "session";

export const formatAnnotateDescription = <Target extends ScoreTarget>(
  scoreTarget: Target,
): string => {
  let sourceEntity = "session";
  if (isTraceScore(scoreTarget)) {
    sourceEntity = scoreTarget.observationId ? "observation" : "trace";
  }
  return `Annotate ${sourceEntity} with scores to capture human evaluation across different dimensions.`;
};

export function formatScoreRange(
  min: number | null | undefined,
  max: number | null | undefined,
) {
  if (min != null && max != null) return `${min} – ${max}`;
  if (min != null) return `Min ${min}`;
  if (max != null) return `Max ${max}`;
  return "Enter a number";
}