|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from llm_dataset_foundry.pipeline.config import load_config |
| 7 | +from llm_dataset_foundry.pipeline.orchestrator import DatasetFoundryPipeline |
| 8 | + |
| 9 | +EXAMPLES = Path(__file__).resolve().parent.parent / "examples" |
| 10 | + |
| 11 | + |
| 12 | +def _config_text(out: Path) -> str: |
| 13 | + docs = (EXAMPLES / "normalized-documents" / "documents.jsonl").as_posix() |
| 14 | + interactions = (EXAMPLES / "traces" / "interactions.jsonl").as_posix() |
| 15 | + retrieval = (EXAMPLES / "traces" / "retrieval.jsonl").as_posix() |
| 16 | + return f""" |
| 17 | +ingest: |
| 18 | + normalized_documents_file: {docs} |
| 19 | + interaction_events_file: {interactions} |
| 20 | + retrieval_events_file: {retrieval} |
| 21 | + max_records: 1000 |
| 22 | +dataset: |
| 23 | + dataset_name: example-dataset |
| 24 | + dataset_id: example-ds |
| 25 | + dataset_version: v1 |
| 26 | + schema_version: "1.0" |
| 27 | + model_version: example-model-v1 |
| 28 | +quality: |
| 29 | + dedup_enabled: true |
| 30 | + min_text_length: 5 |
| 31 | +splits: |
| 32 | + seed: 42 |
| 33 | + train_ratio: 0.8 |
| 34 | + validation_ratio: 0.1 |
| 35 | + test_ratio: 0.1 |
| 36 | +output: |
| 37 | + curated_dataset_path: {(out / "curated").as_posix()} |
| 38 | + reports_path: {(out / "reports").as_posix()} |
| 39 | + manifests_path: {(out / "manifests").as_posix()} |
| 40 | +""" |
| 41 | + |
| 42 | + |
| 43 | +def _run(tmp_path: Path): |
| 44 | + config_path = tmp_path / "foundry.yaml" |
| 45 | + config_path.write_text(_config_text(tmp_path / "out"), encoding="utf-8") |
| 46 | + config = load_config(config_path) |
| 47 | + result = DatasetFoundryPipeline(config=config).run() |
| 48 | + return config, result |
| 49 | + |
| 50 | + |
| 51 | +def test_pipeline_run_loads_example_data(tmp_path: Path) -> None: |
| 52 | + _, result = _run(tmp_path) |
| 53 | + assert result.documents_loaded > 0 |
| 54 | + assert result.interaction_traces_loaded > 0 |
| 55 | + assert result.retrieval_traces_loaded > 0 |
| 56 | + assert result.prompt_response_records > 0 |
| 57 | + assert result.retrieval_eval_records > 0 |
| 58 | + |
| 59 | + |
| 60 | +def test_pipeline_writes_all_artifacts(tmp_path: Path) -> None: |
| 61 | + config, _ = _run(tmp_path) |
| 62 | + curated = config.output.curated_dataset_path |
| 63 | + reports = config.output.reports_path |
| 64 | + manifests = config.output.manifests_path |
| 65 | + |
| 66 | + for name in ("prompt_response.jsonl", "retrieval_evaluation.jsonl", "split_assignments.jsonl"): |
| 67 | + assert (curated / name).exists(), name |
| 68 | + assert (reports / "quality_report.json").exists() |
| 69 | + assert (manifests / "dataset_manifest.json").exists() |
| 70 | + assert (manifests / "dataset_version_metadata.json").exists() |
| 71 | + |
| 72 | + |
| 73 | +def test_manifest_is_valid_json_with_expected_fields(tmp_path: Path) -> None: |
| 74 | + config, result = _run(tmp_path) |
| 75 | + manifest = json.loads((config.output.manifests_path / "dataset_manifest.json").read_text(encoding="utf-8")) |
| 76 | + assert manifest["dataset_id"] == "example-ds" |
| 77 | + assert manifest["dataset_version"] == "v1" |
| 78 | + assert manifest["model_version"] == "example-model-v1" |
| 79 | + counts = manifest["record_counts"] |
| 80 | + assert counts["prompt_response"] == result.prompt_response_records |
| 81 | + assert counts["retrieval_evaluation"] == result.retrieval_eval_records |
| 82 | + # Split buckets always present, and sum to the total record count. |
| 83 | + assert counts["train"] + counts["validation"] + counts["test"] == ( |
| 84 | + result.prompt_response_records + result.retrieval_eval_records |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def test_split_assignments_cover_every_record(tmp_path: Path) -> None: |
| 89 | + config, result = _run(tmp_path) |
| 90 | + lines = ( |
| 91 | + (config.output.curated_dataset_path / "split_assignments.jsonl") |
| 92 | + .read_text(encoding="utf-8") |
| 93 | + .splitlines() |
| 94 | + ) |
| 95 | + assert len(lines) == result.prompt_response_records + result.retrieval_eval_records |
| 96 | + for line in lines: |
| 97 | + row = json.loads(line) |
| 98 | + assert row["split"] in {"train", "validation", "test"} |
| 99 | + assert row["record_id"] |
0 commit comments