readEvalRows function

List<EvalRow> readEvalRows(
  1. String path
)

Reads path: .jsonl as one JSON object per non-empty line with a string prompt and an optional string reference (prompts may then hold newlines); anything else as one prompt per non-empty line, trimmed.

Implementation

List<EvalRow> readEvalRows(String path) {
  final lines = File(path).readAsLinesSync();
  if (!path.endsWith('.jsonl')) {
    return [
      for (final line in lines)
        if (line.trim().isNotEmpty) EvalRow(line.trim()),
    ];
  }
  final rows = <EvalRow>[];
  for (final (i, line) in lines.indexed) {
    if (line.trim().isEmpty) continue;
    final Object? value;
    try {
      value = jsonDecode(line);
    } on FormatException catch (e) {
      throw FormatException('$path:${i + 1}: ${e.message}');
    }
    if (value is! Map ||
        value['prompt'] is! String ||
        (value['reference'] != null && value['reference'] is! String)) {
      throw FormatException(
        '$path:${i + 1}: expected an object with a string "prompt"',
      );
    }
    rows.add(EvalRow(value['prompt'] as String, value['reference'] as String?));
  }
  return rows;
}