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

refactor: Split dataset into 'StaticDataset' and 'GenerativeDataset' (#1801)

halleritef912fb3694e3
diff --git a/camel/datasets/__init__.py b/camel/datasets/__init__.py
index e12a2a0bb..1f92d8b49 100644
--- a/camel/datasets/__init__.py
+++ b/camel/datasets/__init__.py
@@ -12,17 +12,13 @@
# limitations under the License.
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
from .base import (
- BaseDataset,
DataPoint,
GenerativeDataset,
- SeedDataset,
- SyntheticDataset,
+ StaticDataset,
)
__all__ = [
"DataPoint",
- "BaseDataset",
- "SeedDataset",
+ "StaticDataset",
"GenerativeDataset",
- "SyntheticDataset",
]
diff --git a/camel/datasets/base.py b/camel/datasets/base.py
index bcacd0e34..ec96cad4c 100644
--- a/camel/datasets/base.py
+++ b/camel/datasets/base.py
@@ -13,28 +13,26 @@
# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
import json
-import os
import random
+from datetime import datetime
from pathlib import Path
from typing import (
Any,
- Callable,
Dict,
List,
Optional,
Sized,
- TypeVar,
Union,
)
-import torch
from datasets import Dataset as HFDataset
from pydantic import BaseModel, Field, ValidationError
-from torch.utils.data import DataLoader, Dataset
+from torch.utils.data import Dataset
from camel.agents import ChatAgent
from camel.logger import get_logger
from camel.verifiers import BaseVerifier
+from camel.verifiers.models import VerifierInput
logger = get_logger(__name__)
@@ -44,13 +42,9 @@ class DataPoint(BaseModel):
Attributes:
question (str): The primary question or issue to be addressed.
- rationale (str): Logical reasoning or explanation behind the
- answer.
+ rationale (Optional[str]): Logical reasoning or explanation behind the
+ answer. (default: :obj:`None`)
final_answer (str): The final answer.
- raw_markdown (Optional[str]): Raw markdown content for generating
- rewards/hints. (default: :obj:`None`)
- difficulty (Optional[str]): Difficulty level of the question.
- (default: :obj:`None`)
metadata Optional[Dict[str, Any]]: Additional metadata about the data
point. (default: :obj:`None`)
"""
@@ -58,13 +52,12 @@ class DataPoint(BaseModel):
question: str = Field(
..., description="The primary question or issue to be addressed."
)
- rationale: str = Field(
- ..., description="Logical reasoning or explanation behind the answer."
+ rationale: Optional[str] = Field(
+ default=None,
+ description="Logical reasoning or explanation behind the answer.",
)
final_answer: str = Field(..., description="The final answer.")
- difficulty: Optional[str] = Field(
- None, description="Difficulty level of the question."
- )
+
metadata: Optional[Dict[str, Any]] = Field(
default=None, description="Additional metadata about the data point."
)
@@ -90,244 +83,11 @@ class DataPoint(BaseModel):
return cls(**data)
-class BaseDataset(Dataset):
- r"""A dataset contains questions and ground truth data for training.
- It can be either static (e.g., MATH dataset) or generative
- (using an LLM to generate questions).
- """
-
- def __init__(
- self,
- data: List[Dict[str, str]],
- cache_dir: Optional[str] = None,
- **kwargs,
- ):
- r"""Initialize the dataset.
-
- Args:
- data (List[Dict[str, str]]): List of dictionary items to
- create the dataset from.
- cache_dir (Optional[str]): Directory to cache dataset files.
- (default: :obj:`None`)
- **kwargs: Additional dataset parameters.
-
- Note:
- The dataset must be initialized by calling setup() before use.
- """
- self._is_setup = False
- self._raw_data: List[Dict[str, str]] = data if data is not None else []
- self._cache_dir = str(cache_dir) if cache_dir is not None else None
-
- # Store all parameters in metadata dict for compatibility
- self._metadata = {
- 'cache_dir': self._cache_dir,
- **kwargs,
- }
-
- self.data: List[DataPoint] = [] # Will be populated in setup()
-
- async def setup(self) -> None:
- r"""Set up the dataset with necessary resources.
-
- This method:
- 1. Creates cache directory if needed
- 2. Processes raw data into DataPoint objects using vectorized
- operations
- 3. Validates dataset integrity
-
- Raises:
- OSError: If cache directory creation fails.
- Exception: If dataset initialization fails.
- """
- if self._is_setup:
- logger.debug(f"{self.__class__.__name__} already initialized")
- return
-
- try:
- # Create cache directory if specified
- if self._cache_dir:
- try:
- os.makedirs(self._cache_dir, exist_ok=True)
- logger.debug(f"Created cache directory: {self._cache_dir}")
- except OSError as e:
- logger.error(
- f"Failed to create cache directory "
- f"{self._cache_dir}: {e}"
- )
- raise
-
- # Process raw data into DataPoint objects using vectorized
- # operations
- if not self._raw_data:
- self.data = []
- logger.debug("No raw data to process")
- else:
- try:
- # Helper function for validation that can be used with map
- def create_datapoint(item, idx=None):
- try:
- return DataPoint(
- question=item.get('question', ''),
- rationale=item.get('rationale', ''),
- final_answer=item.get('final_answer', ''),
- metadata=item.get('metadata', {})
- if isinstance(item.get('metadata'), dict)
- else {},
- raw_markdown='',
- difficulty='',
- )
- except ValidationError as e:
- idx_str = (
- f" at index {idx}" if idx is not None else ""
- )
- error_msg = (
- f"Sample{idx_str} validation error: {e}"
- )
- logger.error(error_msg)
- raise ValueError(error_msg)
-
- # If raw_data is already a HF dataset, use its map function
- if hasattr(self._raw_data, 'map') and callable(
- self._raw_data.map
- ):
- # Using HF dataset's map for vectorized processing
- processed_data = self._raw_data.map(
- lambda example, idx: {
- 'datapoint': create_datapoint(example, idx)
- },
- with_indices=True,
- )
- self.data = [
- item['datapoint'] for item in processed_data
- ]
- else:
- # Bulk create datapoints
- self.data = [
- create_datapoint(item, i)
- for i, item in enumerate(self._raw_data)
- ]
-
- logger.debug(f"Processed {len(self.data)} data points")
- except Exception as e:
- logger.error(f"Error processing data: {e}")
- raise
-
- self._is_setup = True
- logger.info(f"{self.__class__.__name__} initialized successfully")
-
- except Exception as e:
- logger.error(f"Error during {self.__class__.__name__} setup: {e}")
- await self.cleanup()
- raise
-
- async def cleanup(self) -> None:
- r"""Clean up dataset resources.
-
- This method handles cleanup of resources and resets the dataset state.
- """
- if not self._is_setup:
- return
-
- try:
- # Clear metadata while preserving init config
- init_config = {
- 'cache_dir': self._cache_dir,
- }
- self._metadata = init_config
-
- logger.info(f"{self.__class__.__name__} cleaned up successfully")
-
- except Exception as e:
- logger.error(
- f"Error during {self.__class__.__name__} cleanup: {e}"
- )
- raise
-
- finally:
- # Always mark as uninitialized, even if cleanup fails
- self._is_setup = False
-
- def sample(self) -> DataPoint:
- r"""Sample a random datapoint from the dataset.
-
- Returns:
- DataPoint: A randomly sampled DataPoint.
-
- Raises:
- RuntimeError: If dataset is not initialized.
- """
- if not self._is_setup:
- raise RuntimeError(
- f"{self.__class__.__name__} must be initialized "
- "before sampling"
- )
-
- idx = random.randint(0, len(self) - 1)
- return self[idx]
-
- def __len__(self) -> int:
- r"""Return the size of the dataset."""
- return len(self.data)
-
- def __getitem__(self, idx: int) -> DataPoint:
- r"""Get an item from the dataset.
-
- Args:
- idx (int): Index of the item to get.
-
- Returns:
- DataPoint: DataPoint from the dataset with the given index.
-
- Raises:
- IndexError: If idx is out of bounds.
- """
- if idx < 0 or idx >= len(self):
- raise IndexError(
- f"Index {idx} out of bounds for dataset of size {len(self)}"
- )
-
- return self.data[idx]
-
- @property
- def metadata(self) -> Dict[str, Any]:
- r"""Get dataset metadata."""
- return self._metadata.copy()
-
- def to_pytorch_dataset(
- self,
- transform: Optional[Callable] = None,
- target_transform: Optional[Callable] = None,
- batch_size: Optional[int] = None,
- ) -> Union["PyTorchDataset", "DataLoader"]:
- r"""Convert to a PyTorch dataset or DataLoader.
-
- Args:
- transform (Optional[Callable]): Transform to apply to samples.
- target_transform (Optional[Callable]): Transform to apply to
- targets.
- batch_size (Optional[int]): If provided, returns a DataLoader with
- the specified batch size instead of a PyTorchDataset.
- (default: :obj:`None`)
-
- Returns:
- Union[PyTorchDataset, torch.utils.data.DataLoader]: Dataset in
- PyTorch format or DataLoader if batch_size is provided.
- """
- dataset = PyTorchDataset.from_datapoints(
- self.data,
- transform=transform,
- target_transform=target_transform,
- )
-
- if batch_size is not None:
- return dataset.get_dataloader(batch_size=batch_size)
-
- return dataset
-
-
-class SeedDataset(BaseDataset):
- r"""A dataset containing validated seed examples for data generation.
+class StaticDataset(Dataset):
+ r"""A static dataset containing a list of datapoints.
Ensures that all items adhere to the DataPoint schema.
+ This dataset extends :obj:`Dataset` from PyTorch and should
+ be used when its size is fixed at runtime.
This class can initialize from Hugging Face Datasets,
PyTorch Datasets, JSON file paths, or lists of dictionaries,
@@ -337,46 +97,46 @@ class SeedDataset(BaseDataset):
def __init__(
self,
data: Union[HFDataset, Dataset, Path, List[Dict[str, Any]]],
- cache_dir: Optional[str] = None,
- seed: Optional[int] = None,
+ seed: int = 42,
min_samples: int = 1,
strict: bool = False,
**kwargs,
):
- r"""Initialize the seed dataset and validate integrity.
+ r"""Initialize the static dataset and validate integrity.
Args:
- data (Union[HFDataset, Dataset, Path, List[Dict[str, Any]]]):
- Input data, which can be:
- - A Hugging Face Dataset (HFDataset)
- - A PyTorch Dataset (torch.utils.data.Dataset)
- - A Path object representing the path to a JSON file
- - A list of dictionaries with DataPoint-compatible fields
- seed (Optional[int]): Seed for reproducibility.
- (default: :obj:`1`)
- min_samples (int): Minimum number of samples required.
- (default: :obj:`1`)
- strict (bool): Whether to raise an error on invalid datapoints
- (True) or skip/filter them (False). (default: False)
+ data (:obj:`Union[HFDataset, Dataset,
+ Path, List[Dict[str, Any]]]`):
+ 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 list of dictionaries with :obj:`DataPoint`-compatible
+ fields.
+ seed (:obj:`int`): Random seed for reproducibility.
+ Default is :obj:`42`.
+ min_samples (:obj:`int`): Minimum required number of samples.
+ Default is :obj:`1`.
+ strict (:obj:`bool`): Whether to raise an error on invalid
+ datapoints (:obj:`True`) or skip/filter them (:obj:`False`).
+ Default is :obj:`False`.
**kwargs: Additional dataset parameters.
Raises:
- TypeError: If the data type is not supported.
- ValueError: If dataset size is less than min_samples or
- if sample validation fails.
- FileNotFoundError: If the JSON file path doesn't exist.
- json.JSONDecodeError: If the JSON file is invalid.
+ TypeError: If the input data type is unsupported.
+ ValueError: If the dataset contains fewer than :obj:`min_samples`
+ datapoints or if validation fails.
+ FileNotFoundError: If the specified JSON file path does not exist.
+ json.JSONDecodeError: If the JSON file contains invalid formatting.
"""
- # Initialize BaseDataset with empty data, we'll populate it ourselves
- super().__init__(data=[], cache_dir=cache_dir, **kwargs)
+ # Store all parameters in metadata dict for compatibility
+ self._metadata = {
+ **kwargs,
+ }
self._rng = random.Random(seed)
self._strict = strict
- # Type checking and conversion into list of dicts to have a
- # consistent internal format. Since Seed Dataset should be
- # small, we can load it entirely into memory
-
self.data: List[DataPoint] = self._init_data(data)
self._length = len(self.data)
@@ -389,6 +149,26 @@ class SeedDataset(BaseDataset):
def _init_data(
self, data: Union[HFDataset, Dataset, Path, List[Dict[str, Any]]]
) -> List[DataPoint]:
+ r"""Convert input data from various formats into a list of
+ :obj:`DataPoint` instances.
+
+ Args:
+ data (:obj:`Union[
+ HFDataset,
+ Dataset,
+ Path,
+ List[Dict[str, Any]]
+ ]`):
+ Input dataset in one of the supported formats.
+
+ Returns:
+ :obj:`List[DataPoint]`: A list of validated :obj:`DataPoint`
+ instances.
+
+ Raises:
+ TypeError: If the input data type is unsupported.
+ """
+
if isinstance(data, HFDataset):
raw_data = self._init_from_hf_dataset(data)
elif isinstance(data, Dataset):
@@ -450,7 +230,6 @@ class SeedDataset(BaseDataset):
rationale=rationale,
final_answer=final_answer,
metadata=item.get('metadata'),
- difficulty=item.get('difficulty'),
)
except ValidationError as e:
if self._strict:
@@ -474,17 +253,19 @@ class SeedDataset(BaseDataset):
return self._length
def __getitem__(self, idx: int) -> DataPoint:
- r"""Get an item from the dataset.
+ r"""Retrieve a datapoint by index.
Args:
- idx (int): Index of the item to get.
+ idx (:obj:`int`): Index of the datapoint.
Returns:
- DataPoint: DataPoint from the dataset with the given index.
+ :obj:`DataPoint`: The datapoint corresponding to the given index.
Raises:
- IndexError: If idx is out of bounds.
+ IndexError: If :obj:`idx` is out of bounds (negative or greater
+ than dataset length - 1).
"""
+
if idx < 0 or idx >= self._length:
raise IndexError(
f"Index {idx} out of bounds for dataset of size {self._length}"
@@ -495,11 +276,12 @@ class SeedDataset(BaseDataset):
r"""Sample a random datapoint from the dataset.
Returns:
- DataPoint: A randomly sampled DataPoint.
+ :obj:`DataPoint`: A randomly sampled :obj:`DataPoint`.
Raises:
- RuntimeError: If the dataset is empty.
+ RuntimeError: If the dataset is empty and no samples can be drawn.
"""
+
if self._length == 0:
raise RuntimeError("Dataset is empty, cannot sample.")
idx = self._rng.randint(0, self._length - 1)
@@ -507,15 +289,42 @@ class SeedDataset(BaseDataset):
@property
def metadata(self) -> Dict[str, Any]:
- r"""Get dataset metadata."""
+ r"""Retrieve dataset metadata.
+
+ Returns:
+ :obj:`Dict[str, Any]`: A copy of the dataset metadata dictionary.
+ """
+
return self._metadata.copy()
def _init_from_hf_dataset(self, data: HFDataset) -> List[Dict[str, Any]]:
+ r"""Convert a Hugging Face dataset into a list of dictionaries.
+
+ Args:
+ data (:obj:`HFDataset`): A Hugging Face dataset.
+
+ Returns:
+ :obj:`List[Dict[str, Any]]`: A list of dictionaries representing
+ the dataset, where each dictionary corresponds to a datapoint.
+ """
return [dict(item) for item in data]
def _init_from_pytorch_dataset(
self, data: Dataset
) -> List[Dict[str, Any]]:
+ r"""Convert a PyTorch dataset into a list of dictionaries.
+
+ Args:
+ data (:obj:`Dataset`): A PyTorch dataset.
+
+ Returns:
+ :obj:`List[Dict[str, Any]]`: A list of dictionaries representing
+ the dataset.
+
+ Raises:
+ TypeError: If the dataset does not implement :obj:`__len__()`
+ or contains non-dictionary elements.
+ """
if not isinstance(data, Sized):
raise TypeError(
f"{type(data).__name__} does not implement `__len__()`."
@@ -533,6 +342,20 @@ class SeedDataset(BaseDataset):
return raw_data
def _init_from_json_path(self, data: Path) -> List[Dict[str, Any]]:
+ r"""Load and parse a dataset from a JSON file.
+
+ Args:
+ data (:obj:`Path`): Path to the JSON file.
+
+ Returns:
+ :obj:`List[Dict[str, Any]]`: A list of datapoint dictionaries.
+
+ Raises:
+ FileNotFoundError: If the specified JSON file does not exist.
+ ValueError: If the JSON content is not a list of dictionaries.
+ json.JSONDecodeError: If the JSON file has invalid formatting.
+ """
+
if not data.exists():
raise FileNotFoundError(f"JSON file not found: {data}")
try:
@@ -557,6 +380,18 @@ class SeedDataset(BaseDataset):
def _init_from_list(
self, data: List[Dict[str, Any]]
) -> List[Dict[str, Any]]:
+ r"""Validate and convert a list of dictionaries into a dataset.
+
+ Args:
+ data (:obj:`List[Dict[str, Any]]`): A list of dictionaries where
+ each dictionary must be a valid :obj:`DataPoint`.
+
+ Returns:
+ :obj:`List[Dict[str, Any]]`: The validated list of dictionaries.
+
+ Raises:
+ ValueError: If any item in the list is not a dictionary.
+ """
for i, item in enumerate(data):
if not isinstance(item, dict):
raise ValueError(
@@ -566,111 +401,7 @@ class SeedDataset(BaseDataset):
return data
-class SyntheticDataset(BaseDataset):
- r"""A dataset for storing synthetically generated data points.
-
- This class is used to store datapoints that are generated through
- a generative process, such as using an agent.
- """
-
- def __init__(
- self,
- data: Optional[List[Dict[str, str]]] = None,
- cache_dir: Optional[str] = None,
- **kwargs,
- ):
- r"""Initialize the synthetic dataset.
-
- Args:
- data (Optional[List[Dict[str, str]]]): List of dictionary items to
- create the dataset from. (default: :obj:`None`)
- cache_dir (Optional[str]): Directory to cache dataset files.
- (default: :obj:`None`)
- **kwargs: Additional dataset parameters.
- """
- super().__init__(
- data=data if data is not None else [],
- cache_dir=cache_dir,
- **kwargs,
- )
- self.data: List[DataPoint] = []
-
- def add(self, item: DataPoint) -> None:
- r"""Add a new data point to the dataset.
-
- Args:
- item (DataPoint): The datapoint to add to the dataset.
- """
- self.data.append(item)
-
- def add_batch(self, items: List[DataPoint]) -> None:
- r"""Add multiple data points to the dataset.
-
- Args:
- items (List[DataPoint]): The datapoints to add to the dataset.
- """
- self.data.extend(items)
-
- def to_pytorch_dataset(
- self,
- transform: Optional[Callable] = None,
- target_transform: Optional[Callable] = None,
- batch_size: Optional[int] = None,
- ) -> Union["PyTorchDataset", "DataLoader"]:
- r"""Convert to a PyTorch dataset or DataLoader.
-
- Args:
- transform (Optional[Callable]): Transform to apply to samples.
- target_transform (Optional[Callable]): Transform to apply to
- targets.
- batch_size (Optional[int]): If provided, returns a DataLoader with
- the specified batch size instead of a PyTorchDataset.
- (default: :obj:`None`)
-
- Returns:
- Union[PyTorchDataset, torch.utils.data.DataLoader]: Dataset in
- PyTorch format or DataLoader if batch_size is provided.
- """
- return convert_synthetic_to_pytorch(
- self,
- transform=transform,
- target_transform=target_transform,
- batch_size=batch_size,
- )
-
- def save_pytorch_format(self, path: str, compression: bool = True) -> None:
- r"""Save the dataset to disk in PyTorch format.
-
- Args:
- path (str): Path to save the dataset to.
- compression (bool): Whether to use compression to reduce file size.
- (default: :obj:`True`)
- """
- save_synthetic_dataset(self, path, compression=compression)
-
- def filter(
- self, predicate: Callable[[DataPoint], bool]
- ) -> 'SyntheticDataset':
- r"""Filter the dataset using a predicate function.
-
- Args:
- predicate (Callable[[DataPoint], bool]): Function that takes a
- DataPoint and returns True if it should be kept, False
- otherwise.
-
- Returns:
- SyntheticDataset: A new dataset containing only the filtered items.
- """
- filtered_data = [dp for dp in self.data if predicate(dp)]
-
- # Create a new dataset with the filtered data
- new_dataset = SyntheticDataset()
- new_dataset.add_batch(filtered_data)
-
- return new_dataset
-
-
-class GenerativeDataset(BaseDataset):
+class GenerativeDataset(Dataset):
r"""A dataset for generating synthetic datapoints using external agents and
verifiers.
@@ -680,30 +411,22 @@ class GenerativeDataset(BaseDataset):
def __init__(
self,
- seed_dataset: SeedDataset,
+ seed_dataset: StaticDataset,
verifier: BaseVerifier,
agent: ChatAgent,
- cache_dir: Optional[str] = None,
seed: int = 42,
**kwargs,
):
r"""Initialize the generative dataset.
Args:
- seed_dataset (SeedDataset): Validated dataset to use for examples.
+ seed_dataset (StaticDataset): Validated static dataset to
+ use for examples.
verifier (BaseVerifier): Verifier to validate generated content.
agent (ChatAgent): Agent to generate new datapoints.
- cache_dir (Optional[str]): Directory to cache dataset files.
- (default: :obj:`None`)
seed (int): Random seed for reproducibility. (default: :obj:`42`)
**kwargs: Additional dataset parameters.
"""
- # Initialize with empty data since we'll generate content dynamically
- super().__init__(
- data=[],
- cache_dir=cache_dir,
- **kwargs,
- )
self.seed_dataset = seed_dataset
self.verifier = verifier
@@ -712,8 +435,11 @@ class GenerativeDataset(BaseDataset):
self.seed = seed
random.seed(self.seed)
+ self._data: List[DataPoint] = []
+
def _construct_prompt(self, examples: List[DataPoint]) -> str:
- r"""Construct a prompt for generating new datapoints.
+ r"""Construct a prompt for generating new datapoints
+ using a fixed sample of 3 examples from the seed dataset.
Args:
examples (List[DataPoint]): Examples to include in the prompt.
@@ -732,440 +458,182 @@ class GenerativeDataset(BaseDataset):
prompt += "New datapoint:"
return prompt
- async def generate_new(self, n: int) -> None:
- r"""Generate n new datapoints and add them to the dataset.
+ async def generate_new(
+ self, n: int, max_retries: int = 10
+ ) -> List[DataPoint]:
+ r"""Generates and validates `n` new datapoints through
+ few-shot prompting, with a retry limit.
+
+ Steps:
+ 1. Samples 3 examples from the seed dataset.
+ 2. Constructs a prompt using the selected examples.
+ 3. Uses an agent to generate a new datapoint.
+ 4. Verifies the datapoint using a verifier.
+ 5. Stores valid datapoints in memory.
Args:
n (int): Number of valid datapoints to generate.
+ max_retries (int): Maximum number of retries before stopping.
+
+ Returns:
+ List[DataPoint]: A list of newly generated valid datapoints.
- This method generates new datapoints by:
- 1. Sampling examples from the seed dataset
- 2. Constructing a prompt for the agent
- 3. Generating a new datapoint using the agent
- 4. Verifying the generated datapoint with the verifier
- 5. Adding valid datapoints to the dataset
+ Raises:
+ TypeError: If the agent's output is not a dictionary (or does not
+ match the expected format).
+ KeyError: If required keys are missing from the response.
+ AttributeError: If the verifier response lacks attributes.
+ ValidationError: If a datapoint fails schema validation.
+ RuntimeError: If retries are exhausted before `n` valid datapoints
+ are generated.
+
+ Notes:
+ - Retries on validation failures until `n` valid datapoints exist
+ or `max_retries` is reached, whichever comes first.
+ - If retries are exhausted before reaching `n`, a `RuntimeError`
+ is raised.
+ - Metadata includes a timestamp for tracking datapoint creation.
+ - This method can be overridden to implement custom generation.
"""
valid_data_points: List[DataPoint] = []
+ retries = 0
- while len(valid_data_points) < n:
+ while len(valid_data_points) < n and retries < max_retries:
try:
- indices = random.sample(range(len(self.seed_dataset)), 3)
- examples = [self.seed_dataset[i] for i in indices]
+ examples = [self.seed_dataset.sample() for _ in range(3)]
prompt = self._construct_prompt(examples)
- # Get agent response
- agent_output = (
- self.agent.step(prompt, response_format=DataPoint)
- .msgs[0]
- .parsed
- )
-
- if not isinstance(agent_output, dict):
- raise TypeError("Agent output must be a dictionary")
- if (
- 'question' not in agent_output
- or 'rationale' not in agent_output
- ):
- raise KeyError(
- "Agent output missing required keys: "
- "'question' or 'rationale'"
+ try:
+ agent_output = (
+ self.agent.step(prompt, response_format=DataPoint)
+ .msgs[0]
+ .parsed
)
-
- rationale = agent_output['rationale']
-
- # Verify the generated content
- verifier_response = await self.verifier.verify(rationale)
- if not hasattr(verifier_response, 'content'):
- raise AttributeError(
- "Verifier response missing 'content' attribute"
+ if not isinstance(agent_output, dict):
+ raise TypeError("Agent output must be a dictionary")
+ if (
+ "question" not in agent_output
+ or "rationale" not in agent_output
+ ):
+ raise KeyError(
+ "Missing 'question' or 'rationale' in agent output"
+ )
+ except (TypeError, KeyError) as e:
+ logger.warning(
+ f"Agent output issue: {e}, retrying... "
+ f"({retries + 1}/{max_retries})"
)
-
- if not verifier_response.result:
+ retries += 1
continue
- final_answer = verifier_response.result
-
- # Create and validate the new datapoint
- new_datapoint = {
- 'question': agent_output['question'],
- 'rationale': rationale,
- 'final_answer': final_answer,
- }
-
- datapoint = DataPoint(**new_datapoint)
- valid_data_points.append(datapoint)
-
- except (TypeError, KeyError, AttributeError, ValidationError) as e:
- logger.warning(
- f"Error encountered during generation: {e}, retrying..."
- )
-
- # Add all valid datapoints to the dataset
- for datapoint in valid_data_points:
- self.data.append(datapoint)
- logger.debug("Added new datapoint to dataset")
-
-
-# Define a type variable for return type flexibility
-T_co = TypeVar('T_co', covariant=True)
-
+ rationale = agent_output["rationale"]
-class PyTorchDataset(Dataset[T_co]):
- r"""A PyTorch-compatible dataset implementation that leverages PyTorch's
- efficient data handling capabilities.
- """
-
- def __init__(
- self,
- data: List[Dict[str, Any]],
- transform: Optional[Callable] = None,
- target_transform: Optional[Callable] = None,
- validate: bool = True,
- ):
- r"""Initialize the PyTorch dataset.
-
- Args:
- data (List[Dict[str, Any]]): List of dictionary items to create
- the dataset from.
- transform (Optional[Callable]): A function/transform that takes a
- sample and returns a transformed version for features.
- (default: :obj:`None`)
- target_transform (Optional[Callable]): A function/transform that
- takes a target and returns a transformed version. (default:
- :obj:`None`)
- validate (bool): Whether to validate data points using DataPoint
- schema. (default: :obj:`True`)
-
- Raises:
- ValidationError: If validation is enabled and data doesn't match
- DataPoint schema.
- """
- self.transform = transform
- self.target_transform = target_transform
-
- # Validate and store data
- self._raw_data = data
- self.data = []
+ try:
+ verifier_response = await self.verifier.verify(
+ VerifierInput(
+ llm_response=rationale, ground_truth=None
+ )
+ )
+ if not verifier_response or not verifier_response.result:
+ raise ValueError(
+ "Verifier unsuccessful, response: "
+ f"{verifier_response}"
+ )
+ except (ValueError, AttributeError) as e:
+ logger.warning(
+ f"Verifier issue: {e}, "
+ f"retrying... ({retries + 1}/{max_retries})"
+ )
+ retries += 1
+ continue
- if validate:
- for i, item in enumerate(self._raw_data):
try:
- # Use DataPoint for validation only
- dp = DataPoint(**item)
- self.data.append(dp.to_dict())
+ new_datapoint = DataPoint(
+ question=agent_output["question"],
+ rationale=rationale,
+ final_answer=verifier_response.result,
+ metadata={
+ "synthetic": str(True),
+ "created": datetime.now().isoformat(),
+ },
+ )
except ValidationError as e:
- logger.error(f"Sample {i} validation error: {e}")
- raise ValueError(f"Sample {i} validation error: {e}")
- else:
- # Skip validation and just store the data dictionaries
- self.data = [dict(item) for item in self._raw_data]
-
- def __getitem__(self, index: int) -> T_co:
- r"""Get an item from the dataset.
+ logger.warning(
+ f"Datapoint validation failed: {e}, "
+ f"retrying... ({retries + 1}/{max_retries})"
+ )
+ retries += 1
+ continue
- Args:
- index (int): Index of the item to get.
+ valid_data_points.append(new_datapoint)
- Returns:
- T_co: Item from the dataset, possibly transformed.
- """
- sample = self.data[index]
+ except Exception as e:
+ logger.warning(
+ f"Unexpected error: {e}, retrying..."
+ f" ({retries + 1}/{max_retries})"
+ )
+ retries += 1
- # Apply transformations if provided
- if self.transform is not None:
- sample = self.transform(sample)
+ if len(valid_data_points) < n:
+ raise RuntimeError(
+ f"Failed to generate {n} valid datapoints "
+ f"after {max_retries} retries."
+ )
- return sample # type: ignore[return-value]
+ self._data.extend(valid_data_points)
+ return valid_data_points
def __len__(self) -> int:
- r"""Return the size of the dataset.
-
- Returns:
- int: Number of samples in the dataset.
- """
- return len(self.data)
+ r"""Return the size of the dataset."""
+ return len(self._data)
- @classmethod
- def from_datapoints(
- cls, datapoints: List[DataPoint], **kwargs
- ) -> 'PyTorchDataset':
- r"""Create a PyTorchDataset from a list of DataPoints.
+ def __getitem__(self, idx: int) -> DataPoint:
+ r"""Retrieve a datapoint by index.
Args:
- datapoints (List[DataPoint]): List of DataPoint objects.
- **kwargs: Additional arguments to pass to the constructor.
+ idx (int): Index of the datapoint.
Returns:
- PyTorchDataset: A new PyTorchDataset instance.
- """
- data = [dp.to_dict() for dp in datapoints]
- # We can skip validation since datapoints are already validated
- return cls(data, validate=False, **kwargs)
-
- def to_hf_dataset(self) -> HFDataset:
- r"""Convert to a HuggingFace dataset.
-
- Returns:
- HFDataset: Dataset in HuggingFace format.
- """
- return HFDataset.from_list(self.data)
+ DataPoint: The datapoint corresponding to the given index.
- def save_to_disk(self, path: str) -> None:
- r"""Save the dataset to disk using PyTorch.
-
- Args:
- path (str): Path to save the dataset to.
+ Raises:
+ IndexError: If idx is out of bounds.
"""
- torch.save(self.data, path)
-
- @classmethod
- def load_from_disk(
- cls,
- path: str,
- transform: Optional[Callable] = None,
- target_transform: Optional[Callable] = None,
- ) -> 'PyTorchDataset':
- r"""Load a dataset from disk.
-
- Args:
- path (str): Path to load the dataset from.
- transform (Optional[Callable]): Transform to apply to samples.
- (default: :obj:`None`)
- target_transform (Optional[Callable]): Transform to apply to
- targets. (default: :obj:`None`)
+ if idx < 0 or idx >= len(self._data):
+ raise IndexError(
+ f"Index {idx} out of bounds for dataset of "
+ f"size {len(self._data)}"
+ )
+ return self._data[idx]
- Returns:
- PyTorchDataset: Loaded dataset.
- """
- data = torch.load(path)
- return cls(
- data,
- transform=transform,
- target_transform=target_transform,
- validate=False,
- )
+ def save_to_jsonl(self, file_path: Union[str, Path]) -> None:
+ r"""Saves the dataset to a JSONL (JSON Lines) file.
- @staticmethod
- def collate_fn(
- batch: List[Dict[str, Any]],
- ) -> Dict[str, Union[List[Any], torch.Tensor]]:
- r"""Collate function for PyTorch DataLoader.
+ Each datapoint is stored as a separate JSON object on a new line.
Args:
- batch (List[Dict[str, Any]]): Batch of samples from the dataset.
+ file_path (Union[str, Path]): Path to save the JSONL file.
- Returns:
- Dict[str, Union[List[Any], torch.Tensor]]: Collated batch with
- tensors for numerical data.
- """
- if not batch:
- return {}
-
- # Initialize result dictionary with keys from first item - start with
- # lists only
- result: Dict[str, List[Any]] = {k: [] for k in batch[0].keys()}
-
- # Collect values by key
- for item in batch:
- for k, v in item.items():
- result[k].append(v)
-
- # Convert numeric/boolean lists to tensors where possible
- result_with_tensors: Dict[str, Union[List[Any], torch.Tensor]] = {}
- for k, v in result.items():
- if all(isinstance(x, (int, float, bool)) for x in v):
- try:
- result_with_tensors[k] = torch.tensor(v)
- except (ValueError, TypeError):
- # Keep as list if tensor conversion fails
- result_with_tensors[k] = v
- else:
- result_with_tensors[k] = v
-
- return result_with_tensors
-
- def get_dataloader(
- self,
- batch_size: int = 32,
- shuffle: bool = True,
- num_workers: int = 0,
- pin_memory: bool = False,
- **kwargs,
- ) -> "DataLoader":
- r"""Create a PyTorch DataLoader for this dataset.
-
- Args:
- batch_size (int): Batch size. (default: :obj:`32`)
- shuffle (bool): Whether to shuffle the dataset. (default:
- :obj:`True`)
- num_workers (int): Number of workers for data loading. (default:
- :obj:`0`)
- pin_memory (bool): Whether to pin memory for faster GPU transfer.
- (default: :obj:`False`)
- **kwargs: Additional arguments to pass to DataLoader.
+ Raises:
+ ValueError: If the dataset is empty.
+ IOError: If there is an issue writing to the file.
- Returns:
- torch.utils.data.DataLoader: DataLoader for this dataset.
+ Notes:
+ - Uses `self._data`, which contains the generated datapoints.
+ - Overwrites the file if it already exists.
+ - Ensures compatibility with large datasets by using JSONL format.
"""
- from torch.utils.data import DataLoader
-
- return DataLoader(
- self,
- batch_size=batch_size,
- shuffle=shuffle,
- num_workers=num_workers,
- collate_fn=self.collate_fn,
- pin_memory=pin_memory,
- **kwargs,
- )
-
-
-def convert_hf_to_pytorch(
- hf_dataset: HFDataset,
- transform: Optional[Callable] = None,
- target_transform: Optional[Callable] = None,
- column_mapping: Optional[Dict[str, str]] = None,
- validate: bool = True,
- batch_size: Optional[int] = None,
-) -> Union["PyTorchDataset", "DataLoader"]:
- r"""Convert a HuggingFace dataset to a PyTorchDataset or DataLoader.
-
- This function maps HuggingFace dataset columns to the expected DataPoint
- format, validates the data, and creates a PyTorchDataset or DataLoader.
-
- Args:
- hf_dataset (HFDataset): HuggingFace dataset to convert.
- transform (Optional[Callable]): Transform to apply to samples.
- target_transform (Optional[Callable]): Transform to apply to targets.
- column_mapping (Optional[Dict[str, str]]): Mapping from HuggingFace
- column names to DataPoint field names. If None, assumes columns
- already match DataPoint fields.
- validate (bool): Whether to validate data points using DataPoint
- schema. (default: :obj:`True`)
- batch_size (Optional[int]): If provided, returns a DataLoader with the
- specified batch size instead of a PyTorchDataset. (default:
- :obj:`None`)
-
- Returns:
- Union[PyTorchDataset, torch.utils.data.DataLoader]: Converted dataset
- or DataLoader if batch_size is provided.
- """
- # Convert HuggingFace dataset to list of dicts more efficiently
- mapped_dataset = []
-
- for i in range(len(hf_dataset)):
- item = hf_dataset[i]
- if column_mapping is not None:
- # Apply column mapping if provided
- mapped_item = {}
- for hf_col, dp_field in column_mapping.items():
- if hf_col in item:
- mapped_item[dp_field] = item[hf_col]
- mapped_dataset.append(mapped_item)
- else:
- # Otherwise use item directly
- mapped_dataset.append(dict(item))
-
- # Create PyTorchDataset
- dataset: PyTorchDataset = PyTorchDataset(
- mapped_dataset,
- transform=transform,
- target_transform=target_transform,
- validate=validate,
- )
-
- # Return DataLoader if batch_size is provided
- if batch_size is not None:
- return dataset.get_dataloader(batch_size=batch_size)
+ if not self._data:
+ raise ValueError("Dataset is empty. No data to save.")
- return dataset
+ file_path = Path(file_path)
-
-def convert_synthetic_to_pytorch(
- synthetic_dataset: 'SyntheticDataset',
- transform: Optional[Callable] = None,
- target_transform: Optional[Callable] = None,
- batch_size: Optional[int] = None,
-) -> Union["PyTorchDataset", "DataLoader"]:
- r"""Convert a SyntheticDataset to a PyTorchDataset or DataLoader.
-
- Args:
- synthetic_dataset (SyntheticDataset): Synthetic dataset to convert.
- transform (Optional[Callable]): Transform to apply to samples.
- target_transform (Optional[Callable]): Transform to apply to targets.
- batch_size (Optional[int]): If provided, returns a DataLoader with the
- specified batch size instead of a PyTorchDataset. (default:
- :obj:`None`)
-
- Returns:
- Union[PyTorchDataset, torch.utils.data.DataLoader]: Converted dataset
- or DataLoader if batch_size is provided.
- """
- dataset = PyTorchDataset.from_datapoints(
- synthetic_dataset.data,
- transform=transform,
- target_transform=target_transform,
- )
-
- # Return DataLoader if batch_size is provided
- if batch_size is not None:
- return dataset.get_dataloader(batch_size=batch_size)
-
- return dataset
-
-
-def save_synthetic_dataset(
- synthetic_dataset: 'SyntheticDataset',
- path: str,
- compression: bool = True,
-) -> None:
- r"""Save a synthetic dataset to disk using PyTorch format.
-
- Args:
- synthetic_dataset (SyntheticDataset): Dataset to save.
- path (str): Path to save the dataset to.
- compression (bool): Whether to use compression to reduce file size.
- (default: :obj:`True`)
- """
- pytorch_dataset = convert_synthetic_to_pytorch(synthetic_dataset)
-
- # Save with compression if enabled (uses less disk space)
- if compression:
- torch.save(
- pytorch_dataset.data, # type: ignore[union-attr]
- path,
- _use_new_zipfile_serialization=True,
- )
- else:
- pytorch_dataset.save_to_disk(path) # type: ignore[union-attr]
-
-
-def load_pytorch_dataset(
- path: str,
- transform: Optional[Callable] = None,
- target_transform: Optional[Callable] = None,
- batch_size: Optional[int] = None,
-) -> Union["PyTorchDataset", "DataLoader"]:
- r"""Load a PyTorchDataset from disk.
-
- Args:
- path (str): Path to load the dataset from.
- transform (Optional[Callable]): Transform to apply to samples.
- target_transform (Optional[Callable]): Transform to apply to targets.
- batch_size (Optional[int]): If provided, returns a DataLoader with the
- specified batch size instead of a PyTorchDataset. (default:
- :obj:`None`)
-
- Returns:
- Union[PyTorchDataset, torch.utils.data.DataLoader]: Loaded dataset or
- DataLoader if batch_size is provided.
- """
- dataset = PyTorchDataset.load_from_disk(
- path, transform=transform, target_transform=target_transform
- )
-
- # Return DataLoader if batch_size is provided
- if batch_size is not None:
- return dataset.get_dataloader(batch_size=batch_size)
-
- return dataset
+ try:
+ with file_path.open("w", encoding="utf-8") as f:
+ for datapoint in self._data:
+ json.dump(datapoint.to_dict(), f)
+ f.write("\n") # Ensure each entry is on a new line
+ logger.info(f"Dataset saved successfully to {file_path}")
+ except IOError as e:
+ logger.error(f"Error writing to file {file_path}: {e}")
+ raise
diff --git a/camel/environments/base.py b/camel/environments/base.py
index 61530d24b..edd0ce021 100644
--- a/camel/environments/base.py
+++ b/camel/environments/base.py
@@ -20,7 +20,7 @@ from typing import Any, Dict, List, Optional, Tuple
from pydantic import BaseModel, Field
from camel.agents import ChatAgent
-from camel.datasets.base import BaseDataset, GenerativeDataset
+from camel.datasets.base import GenerativeDataset, StaticDataset
from camel.extractors.base import BaseExtractor
from camel.logger import get_logger
from camel.verifiers.base import (
@@ -139,7 +139,7 @@ class BaseEnvironment(ABC):
def __init__(
self,
- dataset: BaseDataset,
+ dataset: StaticDataset,
verifier: BaseVerifier,
extractor: BaseExtractor,
max_steps: Optional[int] = None,
@@ -151,7 +151,7 @@ class BaseEnvironment(ABC):
r"""Initialize the environment.
Args:
- dataset (BaseDataset): Dataset to sample questions from.
+ dataset (StaticDataset): Dataset to sample questions from.
verifier (BaseVerifier): Verifier to check responses.
extractor (BaseExtractor): Extractor to process LLM responses.
max_steps (Optional[int]): Maximum steps per episode. (default:
@@ -393,7 +393,6 @@ class BaseEnvironment(ABC):
question = getattr(datapoint, "question", None)
final_answer = getattr(datapoint, "final_answer", None)
rationale = getattr(datapoint, "rationale", None)
- difficulty = getattr(datapoint, "difficulty", None)
metadata = getattr(datapoint, "metadata", {})
if not question or not final_answer:
@@ -407,7 +406,6 @@ class BaseEnvironment(ABC):
question=question,
context={
"final_answer": final_answer,
- "difficulty": difficulty,
"rationale": rationale,
},
metadata={
diff --git a/examples/dataset/generative_dataset.py b/examples/dataset/generative_dataset.py
new file mode 100644
index 000000000..999582e89
--- /dev/null
+++ b/examples/dataset/generative_dataset.py
@@ -0,0 +1,84 @@
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
+
+import asyncio
+from pathlib import Path
+from typing import ClassVar
+
+from camel.datasets import GenerativeDataset, StaticDataset
+from camel.logger import get_logger
+from camel.verifiers import PythonVerifier
+
+logger = get_logger(__name__)
+
+
+seed_data = [
+ {
+ "question": "What is 2 + 2?",
+ "rationale": "result = 2 + 2\nprint(result)",
+ "final_answer": "4",
+ },
+ {
+ "question": "What is 10 / 2?",
+ "rationale": "result = 10 / 2\nprint(result)",
+ "final_answer": "5.0",
+ },
+]
+
+
+class MockChatAgent:
+ def step(self, prompt, response_format):
+ """Simulates LLM response with executable Python rationale."""
+
+ class MockResponse:
+ msgs: ClassVar = [
+ type(
+ "MockMessage",
+ (),
+ {
+ "parsed": {
+ "question": "What is 3 * 3?",
+ "rationale": "result = 3 * 3\nprint(result)",
+ }
+ },
+ )
+ ]
+
+ return MockResponse()
+
+
+async def main():
+ seed_dataset = StaticDataset(data=seed_data)
+
+ verifier = PythonVerifier(required_packages=["numpy"])
+ await verifier.setup()
+
+ agent = MockChatAgent()
+
+ generative_dataset = GenerativeDataset(
+ seed_dataset=seed_dataset, verifier=verifier, agent=agent
+ )
+
+ new_data = await generative_dataset.generate_new(n=2)
+
+ for dp in new_data:
+ print(dp)
+
+ generative_dataset.save_to_jsonl(Path("generated_data.jsonl"))
+
+ await verifier.cleanup()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
diff --git a/test/datasets/test_base_dataset.py b/test/datasets/test_base_dataset.py
index 3b4f611da..5c4f865b9 100644
--- a/test/datasets/test_base_dataset.py
+++ b/test/datasets/test_base_dataset.py
@@ -18,22 +18,14 @@ from pathlib import Path
from unittest.mock import AsyncMock, MagicMock
import pytest
-import torch
from datasets import Dataset as HFDataset
from pydantic import ValidationError
from torch.utils.data import Dataset
from camel.datasets.base import (
- BaseDataset,
DataPoint,
GenerativeDataset,
- PyTorchDataset,
- SeedDataset,
- SyntheticDataset,
- convert_hf_to_pytorch,
- convert_synthetic_to_pytorch,
- load_pytorch_dataset,
- save_synthetic_dataset,
+ StaticDataset,
)
@@ -51,7 +43,6 @@ def test_datapoint_creation():
assert datapoint.question == 'What is 2+2?'
assert datapoint.rationale == 'Addition of two numbers'
assert datapoint.final_answer == '4'
- assert datapoint.difficulty is None
assert datapoint.metadata is None
@@ -60,8 +51,11 @@ def test_datapoint_validation():
with pytest.raises(ValidationError):
DataPoint(question='What is 2+2?')
- with pytest.raises(ValidationError):
- DataPoint(question='What is 2+2?', rationale='Addition')
+ # This should now work since rationale is optional
+ datapoint = DataPoint(question='What is 2+2?', final_answer='4')
+ assert datapoint.question == 'What is 2+2?'
+ assert datapoint.rationale is None
+ assert datapoint.final_answer == '4'
def test_datapoint_to_dict():
@@ -70,7 +64,6 @@ def test_datapoint_to_dict():
'question': 'What is 2+2?',
'rationale': 'Addition of two numbers',
'final_answer': '4',
- 'difficulty': 'easy',
'metadata': {'topic': 'math'},
}
@@ -80,12 +73,12 @@ def test_datapoint_to_dict():
assert data_dict['question'] == 'What is 2+2?'
assert data_dict['rationale'] == 'Addition of two numbers'
assert data_dict['final_answer'] == '4'
- assert data_dict['difficulty'] == 'easy'
assert data_dict['metadata'] == {'topic': 'math'}
def test_datapoint_from_dict():
r"""Test creating DataPoint from dictionary."""
+ # Test with rationale
data = {
'question': 'What is 2+2?',
'rationale': 'Addition of two numbers',
@@ -116,87 +109,9 @@ def sample_data():
]
-async def test_base_dataset_setup(sample_data):
- r"""Test BaseDataset setup process."""
- with tempfile.TemporaryDirectory() as temp_dir:
- dataset = BaseDataset(data=sample_data, cache_dir=temp_dir)
- await dataset.setup()
-
- assert dataset._is_setup
- assert len(dataset.data) == 2
- assert isinstance(dataset.data[0], DataPoint)
- assert dataset.data[0].question == 'What is 2+2?'
- assert dataset.data[1].final_answer == '9'
-
-
-async def test_base_dataset_empty_setup():
- r"""Test BaseDataset setup with empty data."""
- dataset = BaseDataset(data=None)
- await dataset.setup()
-
- assert dataset._is_setup
- assert len(dataset.data) == 0
-
-
-async def test_base_dataset_cleanup():
- r"""Test BaseDataset cleanup process."""
- dataset = BaseDataset(data=[])
- await dataset.setup()
- assert dataset._is_setup
-
- await dataset.cleanup()
- assert not dataset._is_setup
-
-
-def test_base_dataset_sample(sample_data):
- r"""Test sampling from BaseDataset."""
- dataset = BaseDataset(data=sample_data)
- dataset._is_setup = True
- dataset.data = [DataPoint(**item) for item in sample_data]
-
- sample = dataset.sample()
- assert isinstance(sample, DataPoint)
- assert sample in dataset.data
-
-
-def test_base_dataset_len(sample_data):
- r"""Test BaseDataset length."""
- dataset = BaseDataset(data=sample_data)
- dataset._is_setup = True
- dataset.data = [DataPoint(**item) for item in sample_data]
-
- assert len(dataset) == 2
-
-
-def test_base_dataset_getitem(sample_data):
- r"""Test BaseDataset item access."""
- dataset = BaseDataset(data=sample_data)
- dataset._is_setup = True
- dataset.data = [DataPoint(**item) for item in sample_data]
-
- item = dataset[0]
- assert isinstance(item, DataPoint)
- assert item.question == 'What is 2+2?'
-
- with pytest.raises(IndexError):
- _ = dataset[100] # Out of bounds
-
-
-def test_base_dataset_metadata():
- r"""Test BaseDataset metadata."""
- dataset = BaseDataset(data=[], cache_dir='/tmp', custom_param='value')
-
- metadata = dataset.metadata
- assert metadata['cache_dir'] == '/tmp'
- assert metadata['custom_param'] == 'value'
-
-
-def test_seed_dataset_init_from_hf_dataset():
+def test_static_dataset_init_from_hf_dataset():
r"""
- Test SeedDataset initialization from a
+ Test StaticDataset initialization from a
Hugging Face Dataset with various scenarios.
"""
@@ -215,7 +130,7 @@ def test_seed_dataset_init_from_hf_dataset():
]
hf_valid = HFDataset.from_list(valid_data)
- dataset = SeedDataset(data=hf_valid, min_samples=1, strict=True)
+ dataset = StaticDataset(data=hf_valid, min_samples=1, strict=True)
# Verify the dataset initialized correctly
assert len(dataset) == 2, "Dataset should contain 2 items."
@@ -238,9 +153,6 @@ def test_seed_dataset_init_from_hf_dataset():
assert (
dataset[1].final_answer == "yes"
), "Second final answer should match input."
- assert (
- dataset[0].difficulty is None
- ), "Difficulty should be None when not provided."
assert (
dataset[0].metadata is None
), "Metadata should be None when not provided."
@@ -256,10 +168,10 @@ def test_seed_dataset_init_from_hf_dataset():
match="Sample at index 0 has invalid 'rationale': "
"expected str, got <class 'NoneType'>",
):
- SeedDataset(data=hf_invalid_missing, min_samples=1, strict=True)
+ StaticDataset(data=hf_invalid_missing, min_samples=1, strict=True)
# Sub-test 2b: Missing required field with strict=False
- dataset_non_strict = SeedDataset(
+ dataset_non_strict = StaticDataset(
data=hf_invalid_missing, min_samples=0, strict=False
)
assert (
@@ -272,10 +184,10 @@ def test_seed_dataset_init_from_hf_dataset():
ValueError,
match="The dataset does not contain enough samples. Need 1, got 0",
):
- SeedDataset(data=hf_empty, min_samples=1, strict=True)
+ StaticDataset(data=hf_empty, min_samples=1, strict=True)
# Sub-test 2d: Empty dataset with min_samples=0
- dataset_empty = SeedDataset(data=hf_empty, min_samples=0, strict=True)
+ dataset_empty = StaticDataset(data=hf_empty, min_samples=0, strict=True)
assert (
len(dataset_empty) == 0
), "Empty dataset should have zero length when min_samples=0."
@@ -286,13 +198,12 @@ def test_seed_dataset_init_from_hf_dataset():
"question": "What is the capital of France?",
"rationale": "France is a country in Europe.",
"final_answer": "Paris",
- "difficulty": "easy",
"metadata": {"source": "geography", "id": 123},
"extra_field": "this should be ignored", # Not in DataPoint
}
]
hf_optional = HFDataset.from_list(data_with_optional)
- dataset_optional = SeedDataset(
+ dataset_optional = StaticDataset(
data=hf_optional, min_samples=1, strict=True
)
@@ -310,9 +221,6 @@ def test_seed_dataset_init_from_hf_dataset():
assert (
dataset_optional[0].final_answer == "Paris"
), "Final answer should match."
- assert (
- dataset_optional[0].difficulty == "easy"
- ), "Difficulty should be set correctly."
assert dataset_optional[0].metadata == {
"source": "geography",
"id": 123,
@@ -322,9 +230,9 @@ def test_seed_dataset_init_from_hf_dataset():
), "Extra field should not be present in DataPoint."
-def test_seed_dataset_init_from_pytorch_dataset():
+def test_static_dataset_init_from_pytorch_dataset():
r"""
- Test SeedDataset initialization from a
+ Test StaticDataset initialization from a
PyTorch Dataset with various scenarios.
"""
@@ -352,7 +260,7 @@ def test_seed_dataset_init_from_pytorch_dataset():
},
]
pytorch_valid = MockPyTorchDataset(valid_data)
- dataset = SeedDataset(data=pytorch_valid, min_samples=1, strict=True)
+ dataset = StaticDataset(data=pytorch_valid, min_samples=1, strict=True)
assert len(dataset) == 2, "Dataset should contain 2 items."
assert isinstance(
@@ -374,9 +282,6 @@ def test_seed_dataset_init_from_pytorch_dataset():
assert (
dataset[1].final_answer == "yes"
), "Second final answer should match input."
- assert (
- dataset[0].difficulty is None
- ), "Difficulty should be None when not provided."
assert (
dataset[0].metadata is None
), "Metadata should be None when not provided."
@@ -395,10 +300,10 @@ def test_seed_dataset_init_from_pytorch_dataset():
match="Sample at index 0 has invalid 'rationale': "
"expected str, got <class 'NoneType'>",
):
- SeedDataset(data=pytorch_invalid_missing, min_samples=1, strict=True)
+ StaticDataset(data=pytorch_invalid_missing, min_samples=1, strict=True)
# Sub-test 2b: Missing required field with strict=False
- dataset_non_strict = SeedDataset(
+ dataset_non_strict = StaticDataset(
data=pytorch_invalid_missing, min_samples=0, strict=False
)
assert (
@@ -411,10 +316,12 @@ def test_seed_dataset_init_from_pytorch_dataset():
ValueError,
match="The dataset does not contain enough samples. Need 1, got 0",
):
- SeedDataset(data=pytorch_empty, min_samples=1, strict=True)
+ StaticDataset(data=pytorch_empty, min_samples=1, strict=True)
# Sub-test 2d: Empty dataset with min_samples=0
- dataset_empty = SeedDataset(data=pytorch_empty, min_samples=0, strict=True)
+ dataset_empty = StaticDataset(
+ data=pytorch_empty, min_samples=0, strict=True
+ )
assert (
len(dataset_empty) == 0
), "Empty dataset should have zero length when min_samples=0."
@@ -425,13 +332,12 @@ def test_seed_dataset_init_from_pytorch_dataset():
"question": "What is the capital of France?",
"rationale": "France is a country in Europe.",
"final_answer": "Paris",
- "difficulty": "easy",
"metadata": {"source": "geography", "id": 123},
"extra_field": "this should be ignored", # Not in DataPoint
}
]
pytorch_optional = MockPyTorchDataset(data_with_optional)
- dataset_optional = SeedDataset(
+ dataset_optional = StaticDataset(
data=pytorch_optional, min_samples=1, strict=True
)
@@ -449,9 +355,6 @@ def test_seed_dataset_init_from_pytorch_dataset():
assert (
dataset_optional[0].final_answer == "Paris"
), "Final answer should match."
- assert (
- dataset_optional[0].difficulty == "easy"
- ), "Difficulty should be set correctly."
assert dataset_optional[0].metadata == {
"source": "geography",
"id": 123,
@@ -471,7 +374,7 @@ def test_seed_dataset_init_from_pytorch_dataset():
}
with pytest.raises(TypeError) as excinfo:
- SeedDataset(NoLenDataset(), min_samples=1, strict=True)
+ StaticDataset(NoLenDataset(), min_samples=1, strict=True)
assert "does not implement `__len__()`." in str(excinfo.value)
# Sub-test 4b: Dataset with non-dict items
@@ -485,12 +388,12 @@ def test_seed_dataset_init_from_pytorch_dataset():
with pytest.raises(
TypeError, match="Item at index 0 is not a dict: got str"
):
- SeedDataset(data=NonDictDataset(), min_samples=1, strict=True)
+ StaticDataset(data=NonDictDataset(), min_samples=1, strict=True)
-def test_seed_dataset_init_from_json_file():
+def test_static_dataset_init_from_json_file():
r"""
- Test SeedDataset initialization from a JSON file with various scenarios.
+ Test StaticDataset initialization from a JSON file with various scenarios.
"""
# **Test 1: Initialization with a valid JSON file (only required fields)**
@@ -512,7 +415,7 @@ def test_seed_dataset_init_from_json_file():
json.dump(valid_data, tmp_file)
tmp_file_path = Path(tmp_file.name)
- dataset = SeedDataset(data=tmp_file_path, min_samples=1, strict=True)
+ 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
@@ -533,9 +436,6 @@ def test_seed_dataset_init_from_json_file():
assert (
dataset[1].final_answer == "yes"
), "Second final answer should match input."
- assert (
- dataset[0].difficulty is None
- ), "Difficulty should be None when not provided."
assert (
dataset[0].metadata is None
), "Metadata should be None when not provided."
@@ -550,7 +450,7 @@ def test_seed_dataset_init_from_json_file():
tmp_file_path = Path(tmp_file.name)
with pytest.raises(ValueError, match="Invalid JSON in file"):
- SeedDataset(data=tmp_file_path, min_samples=1, strict=True)
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
# **Test 3: Initialization with JSON file containing non-list data**
with tempfile.NamedTemporaryFile(
@@ -565,7 +465,7 @@ def test_seed_dataset_init_from_json_file():
with pytest.raises(
ValueError, match="JSON file must contain a list of dictionaries"
):
- SeedDataset(data=tmp_file_path, min_samples=1, strict=True)
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
# **Test 4: Initialization with JSON file containing non-dict items**
with tempfile.NamedTemporaryFile(
@@ -581,12 +481,12 @@ def test_seed_dataset_init_from_json_file():
with pytest.raises(
ValueError, match="Expected a dictionary at index 1, got str"
):
- SeedDataset(data=tmp_file_path, min_samples=1, strict=True)
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
# **Test 5: Initialization with a missing JSON file**
missing_file_path = Path("non_existent_file.json")
with pytest.raises(FileNotFoundError, match="JSON file not found:"):
- SeedDataset(data=missing_file_path, min_samples=1, strict=True)
+ StaticDataset(data=missing_file_path, min_samples=1, strict=True)
# **Test 6: Initialization with an empty JSON file**
with tempfile.NamedTemporaryFile(
@@ -600,10 +500,12 @@ def test_seed_dataset_init_from_json_file():
ValueError,
match="The dataset does not contain enough samples. Need 1, got 0",
):
- SeedDataset(data=tmp_file_path, min_samples=1, strict=True)
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
# Sub-test 6b: Empty dataset with min_samples=0
- dataset_empty = SeedDataset(data=tmp_file_path, min_samples=0, strict=True)
+ 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."
@@ -617,7 +519,6 @@ def test_seed_dataset_init_from_json_file():
"question": "What is the capital of France?",
"rationale": "France is a country in Europe.",
"final_answer": "Paris",
- "difficulty": "easy",
"metadata": {"source": "geography", "id": 123},
"extra_field": "this should be ignored",
}
@@ -625,7 +526,7 @@ def test_seed_dataset_init_from_json_file():
json.dump(data_with_optional, tmp_file)
tmp_file_path = Path(tmp_file.name)
- dataset_optional = SeedDataset(
+ dataset_optional = StaticDataset(
data=tmp_file_path, min_samples=1, strict=True
)
assert len(dataset_optional) == 1, "Dataset should contain 1 item."
@@ -641,9 +542,6 @@ def test_seed_dataset_init_from_json_file():
assert (
dataset_optional[0].final_answer == "Paris"
), "Final answer should match."
- assert (
- dataset_optional[0].difficulty == "easy"
- ), "Difficulty should be set correctly."
assert dataset_optional[0].metadata == {
"source": "geography",
"id": 123,
@@ -671,7 +569,7 @@ def test_seed_dataset_init_from_json_file():
match="Sample at index 0 has invalid 'rationale': "
"expected str, got <class 'NoneType'>",
):
- SeedDataset(data=tmp_file_path, min_samples=1, strict=True)
+ StaticDataset(data=tmp_file_path, min_samples=1, strict=True)
# Sub-test 8b: Invalid data with strict=False
with tempfile.NamedTemporaryFile(
@@ -686,7 +584,7 @@ def test_seed_dataset_init_from_json_file():
json.dump(invalid_data, tmp_file)
tmp_file_path = Path(tmp_file.name)
- dataset_non_strict = SeedDataset(
+ dataset_non_strict = StaticDataset(
data=tmp_file_path, min_samples=0, strict=False
)
assert (
@@ -694,9 +592,9 @@ def test_seed_dataset_init_from_json_file():
), "Invalid items should be filtered out in non-strict mode."
-def test_seed_dataset_init_from_list():
+def test_static_dataset_init_from_list():
r"""
- Test SeedDataset initialization from a list of
+ Test StaticDataset initialization from a list of
dictionaries with various scenarios.
"""
@@ -713,7 +611,7 @@ def test_seed_dataset_init_from_list():
"final_answer": "yes",
},
]
- dataset = SeedDataset(data=valid_data, min_samples=1, strict=True)
+ dataset = StaticDataset(data=valid_data, min_samples=1, strict=True)
assert len(dataset) == 2, "Dataset should contain 2 items."
assert isinstance(
dataset[0], DataPoint
@@ -734,9 +632,6 @@ def test_seed_dataset_init_from_list():
assert (
dataset[1].final_answer == "yes"
), "Second final answer should match input."
- assert (
- dataset[0].difficulty is None
- ), "Difficulty should be None when not provided."
assert (
dataset[0].metadata is None
), "Metadata should be None when not provided."
@@ -749,7 +644,7 @@ def test_seed_dataset_init_from_list():
with pytest.raises(
ValueError, match="Expected a dictionary at index 1, got str"
):
- SeedDataset(data=invalid_list_data, min_samples=1, strict=True)
+ StaticDataset(data=invalid_list_data, min_samples=1, strict=True)
# **Test 3: Initialization with an empty list**
empty_data = []
@@ -758,9 +653,9 @@ def test_seed_dataset_init_from_list():
ValueError,
match="The dataset does not contain enough samples. Need 1, got 0",
):
- SeedDataset(data=empty_data, min_samples=1, strict=True)
+ StaticDataset(data=empty_data, min_samples=1, strict=True)
# Sub-test 3b: Empty list with min_samples=0
- dataset_empty = SeedDataset(data=empty_data, min_samples=0, strict=True)
+ dataset_empty = StaticDataset(data=empty_data, min_samples=0, strict=True)
assert (
len(dataset_empty) == 0
), "Empty dataset should have zero length when min_samples=0."
@@ -778,9 +673,9 @@ def test_seed_dataset_init_from_list():
match="Sample at index 0 has invalid 'rationale': "
"expected str, got <class 'NoneType'>",
):
- SeedDataset(data=invalid_data_missing, min_samples=1, strict=True)
+ StaticDataset(data=invalid_data_missing, min_samples=1, strict=True)
# Sub-test 4b: Missing required field with strict=False
- dataset_non_strict = SeedDataset(
+ dataset_non_strict = StaticDataset(
data=invalid_data_missing, min_samples=0, strict=False
)
assert (
@@ -793,12 +688,11 @@ def test_seed_dataset_init_from_list():
"question": "What is the capital of France?",
"rationale": "France is a country in Europe.",
"final_answer": "Paris",
- "difficulty": "easy",
"metadata": {"source": "geography", "id": 123},
"extra_field": "this should be ignored",
}
]
- dataset_optional = SeedDataset(
+ dataset_optional = StaticDataset(
data=data_with_optional, min_samples=1, strict=True
)
assert len(dataset_optional) == 1, "Dataset should contain 1 item."
@@ -814,9 +708,6 @@ def test_seed_dataset_init_from_list():
assert (
dataset_optional[0].final_answer == "Paris"
), "Final answer should match."
- assert (
- dataset_optional[0].difficulty == "easy"
- ), "Difficulty should be set correctly."
assert dataset_optional[0].metadata == {
"source": "geography",
"id": 123,
@@ -837,7 +728,7 @@ def test_seed_dataset_init_from_list():
"final_answer": "Invalid",
}, # Missing rationale
]
- dataset_mixed = SeedDataset(data=mixed_data, min_samples=1, strict=False)
+ dataset_mixed = StaticDataset(data=mixed_data, min_samples=1, strict=False)
assert (
len(dataset_mixed) == 1
), "Only valid items should be included in non-strict mode."
@@ -846,10 +737,10 @@ def test_seed_dataset_init_from_list():
), "Only the valid item should be present."
-def test_seed_dataset_methods():
+def test_static_dataset_methods():
r"""
Test the __len__, __getitem__, and sample methods
- of SeedDataset with a mock dataset.
+ of StaticDataset with a mock dataset.
"""
mock_data = [
{
@@ -873,7 +764,7 @@ def test_seed_dataset_methods():
"final_answer": "3",
},
]
- dataset = SeedDataset(data=mock_data, min_samples=1, strict=True)
+ dataset = StaticDataset(data=mock_data, min_samples=1, strict=True)
assert len(dataset) == 4, "Dataset should have 4 items."
assert (
@@ -915,262 +806,301 @@ def test_seed_dataset_methods():
), "Sampled item should be in the dataset."
# Test __len__ and sample with empty dataset
- empty_dataset = SeedDataset(data=[], min_samples=0, strict=True)
+ empty_dataset = StaticDataset(data=[], min_samples=0, strict=True)
assert len(empty_dataset) == 0, "Empty dataset should have length 0."
with pytest.raises(RuntimeError, match="Dataset is empty, cannot sample."):
empty_dataset.sample()
-def test_synthetic_dataset_init():
- r"""Test SyntheticDataset initialization."""
- dataset = SyntheticDataset()
- assert dataset._raw_data == []
- assert dataset.data == []
-
-
-def test_synthetic_dataset_add():
- r"""Test adding items to SyntheticDataset."""
- dataset = SyntheticDataset()
- datapoint = DataPoint(
- question='What is 2+2?',
- rationale='Addition of two numbers',
- final_answer='4',
+async def test_generative_dataset():
+ r"""Test GenerativeDataset with mocked components."""
+ mock_static_dataset = MagicMock(spec=StaticDataset)
+ mock_static_dataset.__len__.return_value = 5
+ mock_static_dataset.__getitem__.side_effect = lambda i: DataPoint(
+ question=f"Question {i}",
+ rationale=f"Rationale {i}",
+ final_answer=f"Answer {i}",
)
- dataset.add(datapoint)
- assert len(dataset.data) == 1
- assert dataset.data[0] == datapoint
+ mock_verifier = MagicMock()
+ mock_verifier.verify = AsyncMock()
+ mock_verifier.verify.return_value = MagicMock(result="Verified Answer")
+ mock_agent = MagicMock()
+ mock_agent.step.return_value = MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ 'question': 'Generated Question',
+ 'rationale': 'Generated Rationale',
+ }
+ )
+ ]
+ )
-def test_synthetic_dataset_add_batch():
- r"""Test adding batch of items to SyntheticDataset."""
- dataset = SyntheticDataset()
- datapoints = [
- DataPoint(
- question='What is 2+2?',
- rationale='Addition of two numbers',
- final_answer='4',
- ),
- DataPoint(
- question='What is 3×3?',
- rationale='Multiplication of two numbers',
- final_answer='9',
- ),
- ]
+ # Create GenerativeDataset with mocks
+ dataset = GenerativeDataset(
+ seed_dataset=mock_static_dataset,
+ verifier=mock_verifier,
+ agent=mock_agent,
+ )
- dataset.add_batch(datapoints)
- assert len(dataset.data) == 2
- assert dataset.data == datapoints
+ # Test generate_new method
+ await dataset.generate_new(2)
+ # Verify interactions
+ assert mock_agent.step.call_count >= 2
+ assert mock_verifier.verify.await_count >= 2
+ assert len(dataset._data) == 2
-def test_synthetic_dataset_filter():
- r"""Test filtering SyntheticDataset."""
- dataset = SyntheticDataset()
- datapoints = [
- DataPoint(
- question='What is 2+2?',
- rationale='Addition of two numbers',
- final_answer='4',
- difficulty='easy',
- ),
- DataPoint(
- question='What is 3×3?',
- rationale='Multiplication of two numbers',
- final_answer='9',
- difficulty='medium',
- ),
- ]
- dataset.add_batch(datapoints)
+ # Verify generated data
+ assert all(isinstance(dp, DataPoint) for dp in dataset._data)
+ assert all(dp.final_answer == "Verified Answer" for dp in dataset._data)
- # Filter for easy questions
- filtered = dataset.filter(lambda dp: dp.difficulty == 'easy')
- assert len(filtered.data) == 1
- assert filtered.data[0].question == 'What is 2+2?'
+async def test_generate_new():
+ r"""Test GenerativeDataset with mocked components."""
+ mock_static_dataset = MagicMock(spec=StaticDataset)
+ mock_static_dataset.__len__.return_value = 5
+ mock_static_dataset.__getitem__.side_effect = lambda i: DataPoint(
+ question=f"Question {i}",
+ rationale=f"Rationale {i}",
+ final_answer=f"Answer {i}",
+ )
-def test_pytorch_dataset_init(sample_data):
- r"""Test PyTorchDataset initialization."""
- dataset = PyTorchDataset(sample_data)
- assert len(dataset) == 2
- assert dataset[0]['question'] == 'What is 2+2?'
+ # Create a mock verifier instead of using a real PythonVerifier
+ mock_verifier = MagicMock()
+ mock_verifier.verify = AsyncMock()
+ mock_verifier.verify.side_effect = [
+ MagicMock(result="11"),
+ MagicMock(result=None),
+ MagicMock(result="15"),
+ ]
-def test_pytorch_dataset_from_datapoints():
- r"""Test creating PyTorchDataset from DataPoints."""
- datapoints = [
- DataPoint(
- question='What is 2+2?',
- rationale='Addition of two numbers',
- final_answer='4',
+ mock_agent = MagicMock()
+ mock_agent.step.return_value = MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ 'question': 'Generated Question',
+ 'rationale': 'Generated Rationale',
+ }
+ )
+ ]
+ )
+ mock_agent.step.side_effect = [
+ MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ "question": "What is 5 + 6?",
+ "rationale": "print(5 + 6)",
+ }
+ )
+ ]
),
- DataPoint(
- question='What is 3×3?',
- rationale='Multiplication of two numbers',
- final_answer='9',
+ # syntactically incorrect
+ MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ "question": "What is 3 + 5?",
+ "rationale": "print(2 + )",
+ }
+ )
+ ]
+ ),
+ MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ "question": "What is 7 + 8?",
+ "rationale": "print(7 + 8)",
+ }
+ )
+ ]
),
]
- dataset = PyTorchDataset.from_datapoints(datapoints)
- assert len(dataset) == 2
- assert dataset[0]['question'] == 'What is 2+2?'
- assert dataset[1]['final_answer'] == '9'
-
-
-def test_pytorch_dataset_transform():
- r"""Test PyTorchDataset with transform function."""
- data = [
- {
- 'question': 'What is 2+2?',
- 'rationale': 'Addition of two numbers',
- 'final_answer': '4',
- }
- ]
-
- def transform(sample):
- sample['question'] = sample['question'].upper()
- return sample
-
- dataset = PyTorchDataset(data, transform=transform)
- assert dataset[0]['question'] == 'WHAT IS 2+2?'
-
-
-def test_pytorch_dataset_collate_fn():
- r"""Test PyTorchDataset collate function."""
- batch = [
- {
- 'question': 'What is 2+2?',
- 'rationale': 'Addition of two numbers',
- 'final_answer': '4',
- 'numeric_value': 4,
- },
- {
- 'question': 'What is 3×3?',
- 'rationale': 'Multiplication of two numbers',
- 'final_answer': '9',
- 'numeric_value': 9,
- },
- ]
+ # Create GenerativeDataset with mocks
+ dataset = GenerativeDataset(
+ seed_dataset=mock_static_dataset,
+ verifier=mock_verifier,
+ agent=mock_agent,
+ )
- result = PyTorchDataset.collate_fn(batch)
+ new_datapoints = await dataset.generate_new(2)
- # String fields should be lists
- assert isinstance(result['question'], list)
- assert result['question'] == ['What is 2+2?', 'What is 3×3?']
+ assert (
+ len(new_datapoints) == 2
+ ), "Should generate exactly 2 valid datapoints"
+ assert mock_agent.step.call_count == 3, "Should retry past invalid output"
- # Numeric fields should be tensors
- assert isinstance(result['numeric_value'], torch.Tensor)
- assert torch.equal(result['numeric_value'], torch.tensor([4, 9]))
+ # Check first correct datapoint (5 + 6 = 11)
+ dp1 = new_datapoints[0]
+ assert dp1.question == "What is 5 + 6?"
+ assert dp1.rationale == "print(5 + 6)"
+ assert (
+ dp1.final_answer == "11"
+ ), "Verifier should output '11' from print(5 + 6)"
+ assert dp1.metadata["synthetic"] == "True"
+ # Check second correct datapoint (7 + 8 = 15)
+ dp2 = new_datapoints[1]
+ assert dp2.question == "What is 7 + 8?"
+ assert dp2.rationale == "print(7 + 8)"
+ assert (
+ dp2.final_answer == "15"
+ ), "Verifier should output '15' from print(7 + 8)"
+ assert dp2.metadata["synthetic"] == "True"
-def test_convert_hf_to_pytorch():
- r"""Test converting HuggingFace dataset to PyTorchDataset."""
- hf_data = [
- {
- 'q': 'What is 2+2?',
- 'r': 'Addition of two numbers',
- 'a': '4',
- },
- {
- 'q': 'What is 3×3?',
- 'r': 'Multiplication of two numbers',
- 'a': '9',
- },
- ]
+ # Verify that the mock verifier was called the expected number of times
+ assert (
+ mock_verifier.verify.call_count == 3
+ ), "Verifier should be called 3 times"
- hf_dataset = HFDataset.from_list(hf_data)
- # Test with column mapping
- column_mapping = {'q': 'question', 'r': 'rationale', 'a': 'final_answer'}
- pytorch_dataset = convert_hf_to_pytorch(
- hf_dataset, column_mapping=column_mapping
+async def test_generate_new_with_max_retries():
+ r"""Test GenerativeDataset retry mechanism with
+ max_retries=2 and a sequence of one correct, three wrong,
+ and one correct output, expecting failure."""
+
+ mock_static_dataset = MagicMock(spec=StaticDataset)
+ mock_static_dataset.__len__.return_value = 5
+ mock_static_dataset.__getitem__.side_effect = lambda i: DataPoint(
+ question=f"Question {i}",
+ rationale=f"Rationale {i}",
+ final_answer=f"Answer {i}",
)
- assert len(pytorch_dataset) == 2
- assert pytorch_dataset[0]['question'] == 'What is 2+2?'
- assert pytorch_dataset[1]['final_answer'] == '9'
+ # Create a mock verifier instead of using a real PythonVerifier
+ mock_verifier = MagicMock()
+ mock_verifier.verify = AsyncMock()
+ mock_verifier.verify.side_effect = [
+ MagicMock(result="7"),
+ MagicMock(result=None),
+ MagicMock(result=None),
+ MagicMock(result=None),
+ MagicMock(result="23"),
+ ]
-def test_convert_synthetic_to_pytorch():
- r"""Test converting SyntheticDataset to PyTorchDataset."""
- synthetic_dataset = SyntheticDataset()
- datapoints = [
- DataPoint(
- question='What is 2+2?',
- rationale='Addition of two numbers',
- final_answer='4',
+ # Set up mock agent with specific output sequence
+ mock_agent = MagicMock()
+ mock_agent.step.side_effect = [
+ # Correct
+ MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ "question": "What is 3 + 4?",
+ "rationale": "print(3 + 4)",
+ }
+ )
+ ]
),
- DataPoint(
- question='What is 3×3?',
- rationale='Multiplication of two numbers',
- final_answer='9',
+ # Wrong: Syntactically incorrect
+ MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ "question": "What is 5 + 6?",
+ "rationale": "print(5 + )",
+ }
+ )
+ ]
+ ),
+ # Wrong: Syntactically incorrect
+ MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ "question": "What is 7 + 8?",
+ "rationale": "print(7 + )",
+ }
+ )
+ ]
+ ),
+ # Wrong: Syntactically incorrect
+ MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ "question": "What is 9 + 10?",
+ "rationale": "print(9 + )",
+ }
+ )
+ ]
+ ),
+ # Correct
+ MagicMock(
+ msgs=[
+ MagicMock(
+ parsed={
+ "question": "What is 11 + 12?",
+ "rationale": "print(11 + 12)",
+ }
+ )
+ ]
),
]
- synthetic_dataset.add_batch(datapoints)
-
- pytorch_dataset = convert_synthetic_to_pytorch(synthetic_dataset)
-
- assert len(pytorch_dataset) == 2
- assert pytorch_dataset[0]['question'] == 'What is 2+2?'
- assert pytorch_dataset[1]['final_answer'] == '9'
-
-
-def test_save_and_load_pytorch_dataset():
- r"""Test saving and loading PyTorchDataset."""
- with tempfile.NamedTemporaryFile(suffix='.pt') as temp_file:
- # Create and save dataset
- data = [
- {
- 'question': 'What is 2+2?',
- 'rationale': 'Addition of two numbers',
- 'final_answer': '4',
- }
- ]
- dataset = PyTorchDataset(data)
- dataset.save_to_disk(temp_file.name)
-
- # Load dataset
- loaded_dataset = load_pytorch_dataset(temp_file.name)
- assert len(loaded_dataset) == 1
- assert loaded_dataset[0]['question'] == 'What is 2+2?'
+ dataset = GenerativeDataset(
+ seed_dataset=mock_static_dataset,
+ verifier=mock_verifier,
+ agent=mock_agent,
+ )
+ # Expect RuntimeError due to insufficient valid
+ # datapoints within retry limit
+ with pytest.raises(
+ RuntimeError,
+ match="Failed to generate 2 valid datapoints after 2 retries.",
+ ):
+ await dataset.generate_new(2, max_retries=2)
-def test_save_synthetic_dataset():
- r"""Test saving SyntheticDataset."""
- with tempfile.NamedTemporaryFile(suffix='.pt') as temp_file:
- # Create and save dataset
- synthetic_dataset = SyntheticDataset()
- datapoint = DataPoint(
- question='What is 2+2?',
- rationale='Addition of two numbers',
- final_answer='4',
- )
- synthetic_dataset.add(datapoint)
+ # Verify that the agent was called 3 times before failing
+ assert (
+ mock_agent.step.call_count == 3
+ ), "Should attempt 3 times: correct, wrong, wrong"
- save_synthetic_dataset(synthetic_dataset, temp_file.name)
+ # Verify that the mock verifier was called the expected number of times
+ assert (
+ mock_verifier.verify.call_count == 3
+ ), "Verifier should be called 3 times"
- # Load dataset
- loaded_dataset = load_pytorch_dataset(temp_file.name)
- assert len(loaded_dataset) == 1
- assert loaded_dataset[0]['question'] == 'What is 2+2?'
+async def test_generative_dataset_save_to_jsonl(tmp_path):
+ r"""Test GenerativeDataset's save_to_jsonl method with mocked data.
+ This test verifies that:
+ 1. Data can be successfully saved to a JSONL file.
+ 2. The method handles empty datasets appropriately.
+ 3. IO errors are caught and raised as expected.
-async def test_generative_dataset():
- r"""Test GenerativeDataset with mocked components."""
- mock_seed_dataset = MagicMock(spec=SeedDataset)
- mock_seed_dataset.__len__.return_value = 5
- mock_seed_dataset.__getitem__.side_effect = lambda i: DataPoint(
+ Args:
+ tmp_path: Pytest fixture providing a temporary directory.
+ """
+ # Mock the seed dataset
+ mock_static_dataset = MagicMock(spec=StaticDataset)
+ mock_static_dataset.__len__.return_value = 5
+ mock_static_dataset.__getitem__.side_effect = lambda i: DataPoint(
question=f"Question {i}",
rationale=f"Rationale {i}",
final_answer=f"Answer {i}",
)
+ # Mock the verifier
mock_verifier = MagicMock()
mock_verifier.verify = AsyncMock()
mock_verifier.verify.return_value = MagicMock(result="Verified Answer")
+ # Mock the agent
mock_agent = MagicMock()
mock_agent.step.return_value = MagicMock(
msgs=[
@@ -1185,19 +1115,81 @@ async def test_generative_dataset():
# Create GenerativeDataset with mocks
dataset = GenerativeDataset(
- seed_dataset=mock_seed_dataset,
+ seed_dataset=mock_static_dataset,
verifier=mock_verifier,
agent=mock_agent,
)
- # Test generate_new method
- await dataset.generate_new(2)
-
- # Verify interactions
- assert mock_agent.step.call_count >= 2
- assert mock_verifier.verify.await_count >= 2
- assert len(dataset.data) == 2
+ # Mock data
+ dataset._data = [
+ DataPoint(
+ question="What is 2 + 2?",
+ rationale="Adding 2 and 2 gives 4.",
+ final_answer="4",
+ metadata={"created": "2025-03-12T10:00:00"},
+ ),
+ DataPoint(
+ question="What color is the sky?",
+ rationale="The sky appears blue due to Rayleigh scattering.",
+ final_answer="Blue",
+ metadata={"created": "2025-03-12T10:01:00"},
+ ),
+ DataPoint(
+ question="How many sides does a triangle have?",
+ rationale="A triangle is defined as a shape with three sides.",
+ final_answer="3",
+ metadata={"created": "2025-03-12T10:02:00"},
+ ),
+ DataPoint(
+ question="What is the capital of France?",
+ rationale="France is a country in Europe, and its capital "
+ "is well-known.",
+ final_answer="Paris",
+ metadata={"created": "2025-03-12T10:03:00"},
+ ),
+ ]
- # Verify generated data
- assert all(isinstance(dp, DataPoint) for dp in dataset.data)
- assert all(dp.final_answer == "Verified Answer" for dp in dataset.data)
+ # Test 1: Successful save to JSONL
+ file_path = tmp_path / "test_dataset.jsonl"
+ dataset.save_to_jsonl(file_path)
+
+ # Verify file exists and content is correct
+ assert file_path.exists(), "JSONL file was not created"
+ with file_path.open("r", encoding="utf-8") as f:
+ lines = f.readlines()
+ assert len(lines) == 4, "Incorrect number of lines in JSONL file"
+
+ # Parse and verify all lines
+ for i, (line, expected_dp) in enumerate(zip(lines, dataset._data)):
+ parsed_line = json.loads(line)
+ assert (
+ parsed_line["question"] == expected_dp.question
+ ), f"Question mismatch at line {i+1}"
+ assert (
+ parsed_line["rationale"] == expected_dp.rationale
+ ), f"Rationale mismatch at line {i+1}"
+ assert (
+ parsed_line["final_answer"] == expected_dp.final_answer
+ ), f"Final answer mismatch at line {i+1}"
+ assert "metadata" in parsed_line, f"Metadata missing at line {i+1}"
+ assert (
+ parsed_line["metadata"]["created"]
+ == expected_dp.metadata["created"]
+ ), f"Metadata mismatch at line {i+1}"
+
+ # Test 2: Empty dataset raises ValueError
+ dataset._data = []
+ with pytest.raises(ValueError, match="Dataset is empty. No data to save."):
+ dataset.save_to_jsonl(file_path)
+
+ # Test 3: IO Error handling (simulate by using a directory path)
+ invalid_path = tmp_path / "nonexistent" / "test.jsonl"
+ dataset._data = [
+ DataPoint(
+ question="Test",
+ rationale="Test rationale",
+ final_answer="Test answer",
+ )
+ ]
+ with pytest.raises(IOError, match="No such file or directory"):
+ dataset.save_to_jsonl(invalid_path)