• Converts an unknown value to an EvaluationResult.

    This function provides a flexible way to normalize various return types from evaluator functions into a standardized EvaluationResult format. It handles multiple input types:

    • Numbers: Converted to { score: number }
    • Strings: Converted to { label: string }
    • Objects: Extracts score, label, and explanation properties if present
    • Other types: Returns an empty EvaluationResult object

    This is particularly useful when creating evaluators from functions that may return different types, ensuring consistent evaluation result formatting.

    Parameters

    • result: unknown

      The value to convert to an EvaluationResult. Can be:

      • A number (converted to score)
      • A string (converted to label)
      • An object with optional score, label, and/or explanation properties
      • Any other value (returns empty object)

    Returns EvaluationResult

    An EvaluationResult object with extracted properties

    Convert a number to an EvaluationResult:

    const result = toEvaluationResult(0.95);
    // Returns: { score: 0.95 }

    Convert a string to an EvaluationResult:

    const result = toEvaluationResult("correct");
    // Returns: { label: "correct" }

    Convert an object with all properties:

    const result = toEvaluationResult({
    score: 0.9,
    label: "high",
    explanation: "High quality output"
    });
    // Returns: { score: 0.9, label: "high", explanation: "High quality output" }

    Convert an object with partial properties:

    const result = toEvaluationResult({ score: 0.8 });
    // Returns: { score: 0.8 }

    Handle null or undefined:

    const result = toEvaluationResult(null);
    // Returns: {}