import Foundation /// Writes `report.md` and `report.json` (`docs/MODEL_EVALUATION.md` §4). public enum EvaluationReportWriter { /// Writes both report files into `directory`. public static func write(_ report: EvaluationReport, to directory: URL) throws { let fileManager = FileManager.default try fileManager.createDirectory(at: directory, withIntermediateDirectories: true) let json = try JSONEncoder.reportEncoder.encode(report) try json.write(to: directory.appendingPathComponent("report.json"), options: .atomic) try markdown(report).write( to: directory.appendingPathComponent("report.md"), atomically: true, encoding: .utf8 ) } /// The Markdown report. public static func markdown(_ report: EvaluationReport) -> String { var lines = [ "# Sprachmittler evaluation", "", "Generated: \(ISO8601DateFormatter().string(from: report.generatedAt))", "", "## Environment", "", ] lines.append(contentsOf: environmentLines(report.environment)) lines.append("") for model in report.models { lines.append(contentsOf: modelLines(model)) lines.append("") } lines.append(contentsOf: observations) return lines.joined(separator: "\n") } private static func environmentLines(_ environment: EvaluationEnvironment) -> [String] { var lines = [ "| Key | Value |", "|---|---|", "| Ollama | \(environment.ollamaVersion) |", "| Server | \(environment.serverVersion) |", "| Hardware | \(environment.hardware) |", "| macOS | \(environment.macOS) |", ] for (model, digest) in environment.models.sorted(by: { $0.key < $1.key }) { lines.append("| \(model) | \(digest) |") } return lines } private static func modelLines(_ model: ModelRunResult) -> [String] { var lines = [ "## \(model.model)", "", "Temperature \(format(model.temperature)), \(model.runs) run(s) per segment.", "", "| Model | Temp | Category | Segments | Fail % | chrF | BLEU | Token retention | " + "Num. consistency | First token p50/p90 | Warm s/seg p50/p90 |", "|---|---|---|---|---|---|---|---|---|---|---|", ] for category in model.categories { lines.append( "| \(model.model) | \(format(model.temperature)) | \(category.category.rawValue) | " + "\(category.segments) | \(format(category.failureRate)) | \(format(category.chrF)) | " + "\(format(category.bleu)) | \(format(category.tokenRetention)) | " + "\(format(category.numericConsistency)) | " + "\(latency(category.firstTokenP50, category.firstTokenP90)) | " + "\(seconds(category.totalP50, category.totalP90)) |" ) } return lines } private static var observations: [String] { [ "## Observations", "", "- Automated metrics only; review a sample in the panel and record correction minutes.", "- Token retention below 1.0 or a failure rate above 2% disqualifies a default candidate.", "", "## Limitations", "", "- chrF/BLEU are mechanical and do not judge meaning or style.", "- Memory is not included in this report; use `ollama ps` and record it in PERFORMANCE.md.", "- Private corpus text and raw outputs stay under the ignored results directory.", "", ] } private static func format(_ value: Double) -> String { String(format: "%.2f", value) } private static func latency(_ p50: Int?, _ p90: Int?) -> String { let left = p50.map(String.init) ?? "—" let right = p90.map(String.init) ?? "—" return "\(left)/\(right) ms" } private static func seconds(_ p50: Int?, _ p90: Int?) -> String { let left = p50.map { String(format: "%.2f", Double($0) / 1000) } ?? "—" let right = p90.map { String(format: "%.2f", Double($0) / 1000) } ?? "—" return "\(left)/\(right) s" } } private extension JSONEncoder { static var reportEncoder: JSONEncoder { let encoder = JSONEncoder() encoder.outputFormatting = [.prettyPrinted, .sortedKeys] encoder.dateEncodingStrategy = .iso8601 return encoder } }