Skip to content

armgit / camel

Public Imported from GitHub
0

🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org

feat: Add support for StaticDataset initialization from JSONL (#1860)

Apokryphosx36df73a4db04
diff --git a/camel/datasets/static_dataset.py b/camel/datasets/static_dataset.py
index c49818a0c..8949f0fbd 100644
--- a/camel/datasets/static_dataset.py
+++ b/camel/datasets/static_dataset.py
@@ -60,7 +60,7 @@ class StaticDataset(Dataset):
Input data, which can be one of the following:
- A Hugging Face Dataset (:obj:`HFDataset`).
- A PyTorch Dataset (:obj:`torch.utils.data.Dataset`).
- - A :obj:`Path` object representing a JSON file.
+ - A :obj:`Path` object representing a JSON or JSONL file.
- A list of dictionaries with :obj:`DataPoint`-compatible
fields.
seed (int): Random seed for reproducibility.
@@ -112,6 +112,7 @@ class StaticDataset(Dataset):
Raises:
TypeError: If the input data type is unsupported.
+ ValueError: If the Path has an unsupported file extension.
"""
if isinstance(data, HFDataset):
@@ -119,7 +120,16 @@ class StaticDataset(Dataset):
elif isinstance(data, Dataset):
raw_data = self._init_from_pytorch_dataset(data)
elif isinstance(data, Path):
- raw_data = self._init_from_json_path(data)
+ if data.suffix == ".jsonl":
+ raw_data = self._init_from_jsonl_path(data)
+ elif data.suffix == ".json":
+ raw_data = self._init_from_json_path(data)
+ else:
+ raise ValueError(
+ f"Unsupported file extension: {data.suffix}."
+ " Please enter a .json or .jsonl object."
+ )
+
elif isinstance(data, list):
raw_data = self._init_from_list(data)
else:
@@ -322,6 +332,48 @@ class StaticDataset(Dataset):
)
return loaded_data
+ def _init_from_jsonl_path(self, data: Path) -> List[Dict[str, Any]]:
+ r"""Load and parse a dataset from a JSONL file.
+
+ Args:
+ data (Path): Path to the JSONL file.
+
+ Returns:
+ List[Dict[str, Any]]: A list of datapoint dictionaries.
+
+ Raises:
+ FileNotFoundError: If the specified JSONL file does not exist.
+ ValueError: If a line in the file contains invalid JSON or
+ is not a dictionary.
+ """
+ if not data.exists():
+ raise FileNotFoundError(f"JSONL file not found: {data}")
+
+ raw_data = []
+ logger.debug(f"Loading JSONL from {data}")
+ with data.open('r', encoding='utf-8') as f:
+ for line_number, line in enumerate(f, start=1):
+ line = line.strip()
+ if not line:
+ continue # Skip blank lines if any exist.
+ try:
+ record = json.loads(line)
+ except json.JSONDecodeError as e:
+ raise ValueError(
+ f"Invalid JSON on line {line_number} in file "
+ f"{data}: {e}"
+ )
+ raw_data.append(record)
+ logger.info(f"Successfully loaded {len(raw_data)} items from {data}")
+
+ for i, item in enumerate(raw_data):
+ if not isinstance(item, dict):
+ raise ValueError(
+ f"Expected a dictionary at record {i+1} (line {i+1}), "
+ f"got {type(item).__name__}"
+ )
+ return raw_data
+
def _init_from_list(
self, data: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
diff --git a/test/datasets/test_base_dataset.py b/test/datasets/test_base_dataset.py
index 1675ac09c..c84e8adb5 100644
--- a/test/datasets/test_base_dataset.py
+++ b/test/datasets/test_base_dataset.py
@@ -598,6 +598,210 @@ def test_static_dataset_init_from_json_file():
), "Invalid items should be filtered out in non-strict mode."
+def test_static_dataset_init_from_jsonl_file():
+ r"""
+ Test StaticDataset initialization from a JSONL file with various scenarios.
+ """
+ import json
+ import tempfile
+ from pathlib import Path
+
+ import pytest
+
+ # **Test 1: Initialization with a valid JSONL file (only required fields)**
+ with tempfile.NamedTemporaryFile(
+ mode='w', suffix='.jsonl', delete=False
+ ) as tmp_file:
+ valid_data = [
+ {
+ "question": "What is 2 + 2?",
+ "rationale": "Addition of 2 and 2.",
+ "final_answer": "4",
+ },
+ {
+ "question": "Is the sky blue?",
+ "rationale": "Observation of clear weather.",
+ "final_answer": "yes",
+ },
+ ]
+ # Write each dictionary as a separate JSON line.
+ for item in valid_data:
+ tmp_file.write(json.dumps(item) + "\n")
+ tmp_file_path = Path(tmp_file.name)
+
+ dataset = StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
+ assert len(dataset) == 2, "Dataset should contain 2 items."
+ assert isinstance(
+ dataset[0], DataPoint
+ ), "Items should be DataPoint instances."
+ assert (
+ dataset[0].question == "What is 2 + 2?"
+ ), "Question should match input."
+ assert (
+ dataset[0].rationale == "Addition of 2 and 2."
+ ), "Rationale should match input."
+ assert dataset[0].final_answer == "4", "Final answer should match input."
+ assert (
+ dataset[1].question == "Is the sky blue?"
+ ), "Second question should match input."
+ assert (
+ dataset[1].rationale == "Observation of clear weather."
+ ), "Second rationale should match input."
+ assert (
+ dataset[1].final_answer == "yes"
+ ), "Second final answer should match input."
+ assert (
+ dataset[0].metadata is None
+ ), "Metadata should be None when not provided."
+
+ # **Test 2: Initialization with invalid JSONL file (malformed JSON)**
+ with tempfile.NamedTemporaryFile(
+ mode='w', suffix='.jsonl', delete=False
+ ) as tmp_file:
+ # Write a malformed JSON line (missing closing brace).
+ tmp_file.write(
+ '{"question": "Test", "rationale": "Test",'
+ '"final_answer": "Test"\n'
+ )
+ tmp_file_path = Path(tmp_file.name)
+
+ with pytest.raises(ValueError, match="Invalid JSON on line"):
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
+
+ # **Test 3: Initialization with JSONL file containing non-dict items**
+ with tempfile.NamedTemporaryFile(
+ mode='w', suffix='.jsonl', delete=False
+ ) as tmp_file:
+ tmp_file.write(
+ json.dumps(
+ {
+ "question": "Test",
+ "rationale": "Test",
+ "final_answer": "Test",
+ }
+ )
+ + "\n"
+ )
+ # Write a non-dict item on the second line.
+ tmp_file.write(json.dumps("not a dict") + "\n")
+ tmp_file_path = Path(tmp_file.name)
+
+ with pytest.raises(ValueError, match="Expected a dictionary at record 2"):
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
+
+ # **Test 4: Initialization with a missing JSONL file**
+ missing_file_path = Path("non_existent_file.jsonl")
+ with pytest.raises(FileNotFoundError, match="JSONL file not found:"):
+ StaticDataset(data=missing_file_path, min_samples=1, strict=True)
+
+ # **Test 5: Initialization with an empty JSONL file**
+ with tempfile.NamedTemporaryFile(
+ mode='w', suffix='.jsonl', delete=False
+ ) as tmp_file:
+ tmp_file_path = Path(tmp_file.name)
+
+ # Sub-test 5a: Empty dataset with min_samples=1
+ with pytest.raises(
+ ValueError, match="The dataset does not contain enough samples"
+ ):
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
+
+ # Sub-test 5b: Empty dataset with min_samples=0
+ dataset_empty = StaticDataset(
+ data=tmp_file_path, min_samples=0, strict=True
+ )
+ assert (
+ len(dataset_empty) == 0
+ ), "Empty dataset should have zero length when min_samples=0."
+
+ # **Test 6: Initialization with optional fields and additional fields**
+ with tempfile.NamedTemporaryFile(
+ mode='w', suffix='.jsonl', delete=False
+ ) as tmp_file:
+ data_with_optional = [
+ {
+ "question": "What is the capital of France?",
+ "rationale": "France is a country in Europe.",
+ "final_answer": "Paris",
+ "metadata": {"source": "geography", "id": 123},
+ "extra_field": "this should be ignored",
+ }
+ ]
+ for item in data_with_optional:
+ tmp_file.write(json.dumps(item) + "\n")
+ tmp_file_path = Path(tmp_file.name)
+
+ dataset_optional = StaticDataset(
+ data=tmp_file_path, min_samples=1, strict=True
+ )
+ assert len(dataset_optional) == 1, "Dataset should contain 1 item."
+ assert isinstance(
+ dataset_optional[0], DataPoint
+ ), "Item should be a DataPoint instance."
+ assert (
+ dataset_optional[0].question == "What is the capital of France?"
+ ), "Question should match."
+ assert (
+ dataset_optional[0].rationale == "France is a country in Europe."
+ ), "Rationale should match."
+ assert (
+ dataset_optional[0].final_answer == "Paris"
+ ), "Final answer should match."
+ assert dataset_optional[0].metadata == {
+ "source": "geography",
+ "id": 123,
+ }, "Metadata should match."
+ assert (
+ "extra_field" not in dataset_optional[0].__dict__
+ ), "Extra field should not be present in DataPoint."
+
+ # **Test 7: JSONL-specific edge cases:
+ # Missing required field in strict mode**
+ with tempfile.NamedTemporaryFile(
+ mode='w', suffix='.jsonl', delete=False
+ ) as tmp_file:
+ # Missing 'rationale' field.
+ tmp_file.write(
+ json.dumps(
+ {
+ "question": "What is 3 + 3?",
+ "final_answer": "6",
+ }
+ )
+ + "\n"
+ )
+ tmp_file_path = Path(tmp_file.name)
+
+ with pytest.raises(
+ ValueError,
+ match="Sample at index 0 has invalid 'rationale'",
+ ):
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
+
+ # **Test 8: JSONL-specific edge cases:
+ # Missing required field in non-strict mode**
+ with tempfile.NamedTemporaryFile(
+ mode='w', suffix='.jsonl', delete=False
+ ) as tmp_file:
+ tmp_file.write(
+ json.dumps(
+ {
+ "question": "What is 3 + 3?",
+ "final_answer": "6",
+ }
+ )
+ + "\n"
+ )
+ tmp_file_path = Path(tmp_file.name)
+
+ dataset_non_strict = StaticDataset(
+ data=tmp_file_path, min_samples=0, strict=False
+ )
+ assert (
+ len(dataset_non_strict) == 0
+ ), "Invalid items should be filtered out in non-strict mode."
+
+
def test_static_dataset_init_from_list():
r"""
Test StaticDataset initialization from a list of