|
| 1 | +import { |
| 2 | + defineCommand, |
| 3 | + detectOutputFormat, |
| 4 | + uploadDataset, |
| 5 | + validateDataset, |
| 6 | + MAX_DATASET_BYTES, |
| 7 | + BailianError, |
| 8 | + ExitCode, |
| 9 | + type Config, |
| 10 | + type GlobalFlags, |
| 11 | + type DatasetFile, |
| 12 | + type ValidationResult, |
| 13 | +} from "bailian-cli-core"; |
| 14 | +import { failIfMissing } from "../../output/prompt.ts"; |
| 15 | +import { emitResult, emitBare } from "../../output/output.ts"; |
| 16 | + |
| 17 | +/** |
| 18 | + * Format a single validation issue as a one-line string. |
| 19 | + */ |
| 20 | +function formatIssue(issue: ValidationResult["errors"][number]): string { |
| 21 | + const where: string[] = []; |
| 22 | + if (issue.line !== undefined) where.push(`line ${issue.line}`); |
| 23 | + if (issue.path) where.push(issue.path); |
| 24 | + const tag = where.length ? ` [${where.join(" · ")}]` : ""; |
| 25 | + return ` ${issue.severity.toUpperCase()} ${issue.code}${tag}: ${issue.message}`; |
| 26 | +} |
| 27 | + |
| 28 | +export default defineCommand({ |
| 29 | + name: "dataset upload", |
| 30 | + description: "Upload a dataset file (.jsonl) to Bailian", |
| 31 | + usage: "bl dataset upload --file <path> [--purpose <name>] [--no-validate] [--full-validate]", |
| 32 | + options: [ |
| 33 | + { |
| 34 | + flag: "--file <path>", |
| 35 | + description: "Local .jsonl dataset file (≤300MB)", |
| 36 | + required: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + flag: "--purpose <name>", |
| 40 | + description: 'Dataset purpose tag (default: "fine-tune"; e.g. "evaluation")', |
| 41 | + }, |
| 42 | + { |
| 43 | + flag: "--no-validate", |
| 44 | + description: "Skip the local JSONL pre-flight check (not recommended)", |
| 45 | + type: "boolean", |
| 46 | + }, |
| 47 | + { |
| 48 | + flag: "--full-validate", |
| 49 | + description: "JSON.parse every line instead of sampling (slower)", |
| 50 | + type: "boolean", |
| 51 | + }, |
| 52 | + ], |
| 53 | + examples: [ |
| 54 | + "bl dataset upload --file train.jsonl", |
| 55 | + "bl dataset upload --file eval.jsonl --purpose evaluation", |
| 56 | + "bl dataset upload --file train.jsonl --full-validate", |
| 57 | + "bl dataset upload --file train.jsonl --no-validate", |
| 58 | + ], |
| 59 | + notes: [ |
| 60 | + "Only .jsonl is supported in this release. The default validator expects a", |
| 61 | + 'ChatML schema (each line a JSON object with a "messages" array). Other', |
| 62 | + "purposes may carry a different schema in the future and would be served", |
| 63 | + "by a purpose-specific validator at that point.", |
| 64 | + "The dataset upload cap is 300MB per file.", |
| 65 | + "Upload uses the OpenAI-compatible /compatible-mode/v1/files endpoint so", |
| 66 | + "the purpose tag is persisted (the DashScope-native /api/v1/files drops it).", |
| 67 | + ], |
| 68 | + async run(config: Config, flags: GlobalFlags) { |
| 69 | + const filePath = flags.file as string | undefined; |
| 70 | + if (!filePath) failIfMissing("file", "bl dataset upload --file <path>"); |
| 71 | + |
| 72 | + const purpose = (flags.purpose as string | undefined) || "fine-tune"; |
| 73 | + const skipValidate = Boolean(flags.noValidate); |
| 74 | + const fullValidate = Boolean(flags.fullValidate); |
| 75 | + const format = detectOutputFormat(config.output); |
| 76 | + |
| 77 | + if (!skipValidate) { |
| 78 | + const result = await validateDataset(filePath!, { fullValidate }); |
| 79 | + if (!result.valid) { |
| 80 | + const lines = [ |
| 81 | + `Dataset validation failed for ${filePath}`, |
| 82 | + ...result.errors.slice(0, 10).map(formatIssue), |
| 83 | + ]; |
| 84 | + if (result.errors.length > 10) { |
| 85 | + lines.push(` … and ${result.errors.length - 10} more error(s).`); |
| 86 | + } |
| 87 | + lines.push( |
| 88 | + "", |
| 89 | + "Hint: re-run `bl dataset validate --file <path>` for the full report,", |
| 90 | + " or pass --no-validate to skip this check at your own risk.", |
| 91 | + ); |
| 92 | + throw new BailianError(lines.join("\n"), ExitCode.GENERAL); |
| 93 | + } |
| 94 | + // Surface warnings to stderr but keep going. |
| 95 | + if (result.warnings.length > 0 && !config.quiet) { |
| 96 | + process.stderr.write( |
| 97 | + `Dataset validation passed with ${result.warnings.length} warning(s):\n`, |
| 98 | + ); |
| 99 | + for (const warning of result.warnings.slice(0, 5)) |
| 100 | + process.stderr.write(`${formatIssue(warning)}\n`); |
| 101 | + if (result.warnings.length > 5) { |
| 102 | + process.stderr.write(` … and ${result.warnings.length - 5} more.\n`); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (config.dryRun) { |
| 108 | + emitResult( |
| 109 | + { |
| 110 | + action: "dataset.upload", |
| 111 | + file: filePath, |
| 112 | + purpose, |
| 113 | + max_bytes: MAX_DATASET_BYTES, |
| 114 | + validate: !skipValidate, |
| 115 | + }, |
| 116 | + format, |
| 117 | + ); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + const uploaded: DatasetFile = await uploadDataset(config, { |
| 122 | + filePath: filePath!, |
| 123 | + purpose, |
| 124 | + }); |
| 125 | + |
| 126 | + if (config.quiet) { |
| 127 | + emitBare(uploaded.file_id); |
| 128 | + } else if (format === "text") { |
| 129 | + emitBare(`Uploaded ${uploaded.name} → file_id=${uploaded.file_id}`); |
| 130 | + } else { |
| 131 | + emitResult(uploaded, format); |
| 132 | + } |
| 133 | + }, |
| 134 | +}); |
0 commit comments