|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | + |
| 14 | +import json |
| 15 | +from typing import Dict, Any, Optional |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | + |
| 19 | +class DatasetFormatDetector: |
| 20 | + """Utility class for detecting dataset formats.""" |
| 21 | + |
| 22 | + # Schema directory |
| 23 | + SCHEMA_DIR = Path(__file__).parent / "schemas" |
| 24 | + |
| 25 | + @staticmethod |
| 26 | + def _load_schema(format_name: str) -> Dict[str, Any]: |
| 27 | + """Load JSON schema for a format.""" |
| 28 | + schema_path = DatasetFormatDetector.SCHEMA_DIR / f"{format_name}.json" |
| 29 | + if schema_path.exists(): |
| 30 | + with open(schema_path) as f: |
| 31 | + return json.load(f) |
| 32 | + return {} |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def validate_dataset(file_path: str) -> bool: |
| 36 | + """ |
| 37 | + Validate if the dataset adheres to any known format. |
| 38 | + |
| 39 | + Args: |
| 40 | + file_path: Path to the JSONL file |
| 41 | + |
| 42 | + Returns: |
| 43 | + True if dataset is valid according to any known format, False otherwise |
| 44 | + """ |
| 45 | + import jsonschema |
| 46 | + |
| 47 | + # Schema-based formats |
| 48 | + schema_formats = [ |
| 49 | + "dpo", "converse", "hf_preference", "hf_prompt_completion", |
| 50 | + "verl", "openai_chat", "genqa" |
| 51 | + ] |
| 52 | + |
| 53 | + try: |
| 54 | + with open(file_path, 'r') as f: |
| 55 | + for line in f: |
| 56 | + line = line.strip() |
| 57 | + if line: |
| 58 | + data = json.loads(line) |
| 59 | + |
| 60 | + # Try schema validation first |
| 61 | + for format_name in schema_formats: |
| 62 | + schema = DatasetFormatDetector._load_schema(format_name) |
| 63 | + if schema: |
| 64 | + try: |
| 65 | + jsonschema.validate(instance=data, schema=schema) |
| 66 | + return True |
| 67 | + except jsonschema.exceptions.ValidationError: |
| 68 | + continue |
| 69 | + |
| 70 | + # Check for RFT-style format (messages + additional fields) |
| 71 | + if DatasetFormatDetector._is_rft_format(data): |
| 72 | + return True |
| 73 | + break |
| 74 | + return False |
| 75 | + except (json.JSONDecodeError, FileNotFoundError, IOError): |
| 76 | + return False |
| 77 | + |
| 78 | + @staticmethod |
| 79 | + def _is_rft_format(data: Dict[str, Any]) -> bool: |
| 80 | + """Check if data matches RFT format pattern.""" |
| 81 | + if not isinstance(data, dict) or "messages" not in data: |
| 82 | + return False |
| 83 | + |
| 84 | + messages = data["messages"] |
| 85 | + if not isinstance(messages, list) or not messages: |
| 86 | + return False |
| 87 | + |
| 88 | + # Check message structure |
| 89 | + for msg in messages: |
| 90 | + if not isinstance(msg, dict): |
| 91 | + return False |
| 92 | + if "role" not in msg or "content" not in msg: |
| 93 | + return False |
| 94 | + if not isinstance(msg["role"], str) or not isinstance(msg["content"], str): |
| 95 | + return False |
| 96 | + |
| 97 | + return True |
0 commit comments