🐫 CAMEL: The first and the best multi-agent framework. Finding the Scaling Law of Agents. https://www.camel-ai.org
Python agentai-societiesartificial-intelligencecommunicative-aicooperative-aideep-learninglarge-language-modelsmulti-agent-systemsnatural-language-processing
feat: add Evol-Instruct alike methods to camel/datagen (#1990)
Zeyu Zhang6c5d76c57683
diff --git a/camel/datagen/evol_instruct/__init__.py b/camel/datagen/evol_instruct/__init__.pynew file mode 100644index 000000000..3fc5e068a--- /dev/null+++ b/camel/datagen/evol_instruct/__init__.py@@ -0,0 +1,20 @@+# ========= 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. =========++from .evol_instruct import EvolInstructPipeline++__all__ = [+ 'EvolInstructPipeline',+ 'MathEvolInstructTemplates',+]diff --git a/camel/datagen/evol_instruct/evol_instruct.py b/camel/datagen/evol_instruct/evol_instruct.pynew file mode 100644index 000000000..44f028021--- /dev/null+++ b/camel/datagen/evol_instruct/evol_instruct.py@@ -0,0 +1,424 @@+# ========= 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 random+import time+from concurrent.futures import ThreadPoolExecutor+from math import ceil+from typing import Any, Dict, List, Optional, Tuple, Type, Union, cast++from tqdm import tqdm++from camel.agents import ChatAgent+from camel.datagen.evol_instruct.scorer import BaseScorer, GeneralScorer+from camel.datagen.evol_instruct.templates import EvolInstructTemplates+from camel.logger import get_logger++logger = get_logger(__name__)+++class EvolInstructPipeline:+ r"""Pipeline for evolving prompts using the Evol-Instruct methodology.++ Supports custom templates defining evolution strategies and methods. The+ pipeline leverages language models to iteratively refine prompts through+ specified evolution strategies.++ Args:+ templates (Type[EvolInstructTemplates]): Template class containing+ evolution strategy and method definitions. Must provide+ `EVOL_METHODS` and `STRATEGY` attributes.+ (default: :obj:`EvolInstructTemplates`)+ agent (Optional[ChatAgent]): Chat agent instance for LLM interaction.+ If :obj:`None`, initializes with a default ChatAgent.+ (default: :obj:`None`)+ """++ def __init__(+ self,+ templates: Type = EvolInstructTemplates,+ agent: Optional[ChatAgent] = None,+ ) -> None:+ r"""Initialize pipeline with templates and language model agent.++ Args:+ templates (Type[EvolInstructTemplates]): Template class containing+ evolution strategy configurations.+ (default: :obj:`EvolInstructTemplates`)+ agent (Optional[ChatAgent]): Preconfigured chat agent instance.+ Creates a default ChatAgent if not provided.+ (default: :obj:`None`)+ """+ self.templates = templates+ self.agent = agent or ChatAgent()++ def _resolve_evolution_method(self, method_key: str) -> str:+ r"""Resolve evolution method key to concrete implementation.++ Args:+ method_key (str): Input method identifier. Can be:+ - Direct method key from templates.EVOL_METHODS+ - Strategy name from templates.STRATEGY keys++ Returns:+ str: Resolved method key from EVOL_METHODS+ """+ if method_key in self.templates.EVOL_METHODS:+ return method_key+ if method_key.upper() in self.templates.STRATEGY:+ strategy = self.templates.STRATEGY[method_key.upper()]+ strategy_methods = strategy["methods"]+ return random.choice(strategy_methods)++ logger.warning(+ f"Invalid evolution method: {method_key}. "+ f"Using random selection."+ )+ return random.choice(list(self.templates.EVOL_METHODS))++ def _get_evolution_methods(+ self,+ method: Union[str, List[str]],+ num_generations: int = 2,+ ) -> List[str]:+ r"""Get list of evolution methods based on input specification.++ Args:+ method (Union[str, List[str]]): Specification for method selection.+ Can be:+ - Strategy name for methods from that strategy+ - Specific method name+ - List of method specifications+ num_generations (int): Number of methods to return.++ Returns:+ List[str]: List of resolved method names+ """+ candidate_methods = []++ if isinstance(method, list):+ for method_spec in method:+ candidate_methods.append(+ self._resolve_evolution_method(method_spec)+ )+ elif isinstance(method, str):+ if method.upper() in self.templates.STRATEGY:+ strategy = self.templates.STRATEGY[method.upper()]+ candidate_methods = strategy["methods"]+ else:+ candidate_methods = [self._resolve_evolution_method(method)]++ # Remove duplicates while preserving order+ unique_candidates = []+ for method_name in candidate_methods:+ if method_name not in unique_candidates:+ unique_candidates.append(method_name)++ if len(unique_candidates) >= num_generations:+ methods = random.sample(unique_candidates, num_generations)+ else:+ methods = unique_candidates.copy()+ while len(methods) < num_generations:+ methods.append(random.choice(unique_candidates))++ return methods++ def _generate_single_evolution(+ self,+ prompt: str,+ method: str,+ return_method: bool = False,+ ) -> Tuple[str, str]:+ r"""Generate a single evolved prompt from a seed prompt.++ Args:+ prompt (str): The seed prompt to evolve.+ method (str): The evolution method key to use.+ return_method (bool): If True, returns method along with prompt.++ Returns:+ Tuple[str, str]: Evolved prompt and method+ """+ resolved_method = self._resolve_evolution_method(method)++ # Find strategy containing the resolved method+ strategy_key = None+ for strategy, group in self.templates.STRATEGY.items():+ if resolved_method in group["methods"]:+ strategy_key = strategy+ break++ if strategy_key is None:+ strategy_key = random.choice(list(self.templates.STRATEGY.keys()))++ strategy = self.templates.STRATEGY[strategy_key]+ instruction_template = strategy["meta_instruction"]+ instruction = instruction_template.format(+ method=self.templates.EVOL_METHODS.get(+ resolved_method,+ random.choice(list(self.templates.EVOL_METHODS.values())),+ ),+ prompt=prompt,+ )++ self.agent.reset()+ response = self.agent.step(instruction)+ evolved_prompt = response.msgs[0].content.strip()++ if return_method:+ return (evolved_prompt, resolved_method)+ else:+ return (evolved_prompt, "")++ def _generate_multiple_evolutions(+ self,+ prompt: str,+ method: Union[str, List[str]],+ num_generations: int = 2,+ keep_original: bool = True,+ num_threads: int = 10,+ ) -> List[Tuple[str, str]]:+ r"""Generate multiple evolved versions of a prompt.++ Args:+ prompt (str): Seed prompt to evolve.+ method (Union[str, List[str]]): Evolution method specification.+ num_generations (int): Candidates to generate per iteration.+ keep_original (bool): Whether to keep the original prompt.+ num_threads (int): Number of threads for parallel processing.++ Returns:+ List[Tuple[str, str]]: List of (evolved_prompt, method) pairs+ """+ results = [(prompt, "original")] if keep_original else []++ if isinstance(method, list) and len(method) == num_generations:+ candidate_methods = method+ else:+ candidate_methods = self._get_evolution_methods(+ method=method, num_generations=num_generations+ )++ def _process_single_method(method_name: str) -> Tuple[str, str]:+ return self._generate_single_evolution(+ prompt, method_name, return_method=True+ )++ with ThreadPoolExecutor(max_workers=num_threads) as executor:+ evolved_results = list(+ executor.map(_process_single_method, candidate_methods)+ )++ results.extend(evolved_results)+ return results++ def _generate_iterative_evolutions(+ self,+ prompt: str,+ evolution_spec: Union[str, List[Union[str, List[str]]]],+ num_generations: int = 2,+ num_iterations: Optional[int] = None,+ keep_original: bool = True,+ scorer: Optional[BaseScorer] = None,+ num_threads: int = 10,+ ) -> Dict[int, List[Dict[str, Any]]]:+ r"""Generate iterative evolutions of a prompt with scoring.++ Args:+ prompt (str): Seed prompt to evolve.+ evolution_spec (Union[str, List[Union[str, List[str]]]]):+ Evolution method specification.+ If a list is provided and num_iterations is None, then+ num_iterations is set to the length of the list.+ num_generations (int): Candidates to generate per iteration.+ num_iterations (Optional[int]): Number of evolution iterations.+ Defaults to the length of evolution_spec.+ keep_original (bool): Include original prompt in results.+ scorer (Optional[BaseScorer]): Scoring model for candidate.+ num_threads (int): Number of threads for parallel processing.++ Returns:+ Dict[int, List[Dict[str, Any]]]: Evolution results per iteration,+ where each candidate is represented as a dict with keys:+ "instruction", "method", and "scores".+ """+ if num_iterations is None:+ if isinstance(evolution_spec, list):+ num_iterations = len(evolution_spec)+ else:+ num_iterations = 1++ results = {}+ current_prompt = prompt+ scorer = scorer or GeneralScorer()++ for iteration in range(num_iterations):+ if isinstance(evolution_spec, list):+ if iteration < len(evolution_spec):+ iteration_spec = evolution_spec[iteration]+ else:+ iteration_spec = evolution_spec[-1]+ else:+ iteration_spec = evolution_spec++ batch_results = self._generate_multiple_evolutions(+ prompt=current_prompt,+ method=iteration_spec,+ num_generations=num_generations,+ keep_original=False,+ num_threads=num_threads,+ )++ scored_results = []+ for candidate, method_used in batch_results:+ scores = scorer.score(current_prompt, candidate)+ scored_results.append(+ {+ "instruction": candidate,+ "method": method_used,+ "scores": scores,+ }+ )++ best_index = max(+ range(len(scored_results)),+ key=lambda i: sum(+ cast(Dict[str, int], scored_results[i]["scores"]).values()+ ),+ )++ best_candidate = cast(+ str, scored_results[best_index]["instruction"]+ )++ if keep_original:+ results[iteration] = [+ {+ "instruction": current_prompt,+ "method": "original",+ "scores": {},+ },+ *scored_results,+ ]+ else:+ results[iteration] = scored_results++ current_prompt = best_candidate++ return results++ def generate(+ self,+ prompts: List[str],+ evolution_spec: Union[str, List[Union[str, List[str]]]],+ num_generations: int = 2,+ num_iterations: Optional[int] = None,+ keep_original: bool = True,+ scorer: Optional[BaseScorer] = None,+ num_chunks: int = 1,+ retry_limit: int = 3,+ retry_delay: float = 1.0,+ num_threads: int = 10,+ ) -> List[Dict[int, List[Dict[str, Any]]]]:+ r"""Evolve a batch of prompts through iterative refinement.++ Args:+ prompts (List[str]): Seed prompts to evolve.+ evolution_spec (Union[str, List[Union[str, List[str]]]]):+ Evolution method specification.+ If a list is provided and num_iterations is None, then+ num_iterations is set to the length of the list.+ num_generations (int): Candidates to generate per iteration.+ num_iterations (Optional[int]): Number of evolution iterations.+ Defaults to the length of evolution_spec.+ keep_original (bool): Include original prompts in results.+ scorer (Optional[BaseScorer]): Scoring model for candidate.+ num_chunks (int): Number of parallel processing chunks.+ retry_limit (int): Max retries for failed generations.+ retry_delay (float): Delay between retries in seconds.+ num_threads (int): Number of threads for parallel processing.++ Returns:+ List[Dict[int, List[Dict[str, Any]]]]: Evolution results.+ """+ if num_iterations is None:+ if isinstance(evolution_spec, list):+ num_iterations = len(evolution_spec)+ else:+ num_iterations = 1++ evolution_plan: List[List[List[str]]] = []+ for _ in prompts:+ prompt_plan = []+ for iteration in range(num_iterations):+ if isinstance(evolution_spec, list):+ if iteration < len(evolution_spec):+ raw_spec = evolution_spec[iteration]+ else:+ raw_spec = evolution_spec[-1]+ else:+ raw_spec = evolution_spec+ prompt_plan.append(+ self._get_evolution_methods(raw_spec, num_generations)+ )+ evolution_plan.append(prompt_plan)++ def _process_prompt(+ args: Tuple[str, List[List[str]]],+ ) -> Dict[int, List[Dict[str, Any]]]:+ prompt, methods = args+ retries = 0+ while retries <= retry_limit:+ try:+ return self._generate_iterative_evolutions(+ prompt=prompt,+ evolution_spec=evolution_spec,+ num_generations=num_generations,+ num_iterations=num_iterations,+ keep_original=keep_original,+ scorer=scorer,+ num_threads=num_threads,+ )+ except Exception as e:+ retries += 1+ if retries <= retry_limit:+ logger.warning(+ f"Error processing prompt "+ f"(attempt {retries}/{retry_limit}): {e!s}"+ )+ time.sleep(retry_delay)+ else:+ logger.error("Failed to process prompt.")+ return {}++ raise RuntimeError("_process_prompt() did not return.")++ num_chunks = max(1, min(num_chunks, len(prompts)))+ chunk_size = ceil(len(prompts) / num_chunks)+ results = []++ for chunk_idx in range(0, len(prompts), chunk_size):+ chunk = prompts[chunk_idx : chunk_idx + chunk_size]+ plan_chunk = evolution_plan[chunk_idx : chunk_idx + chunk_size]++ with ThreadPoolExecutor(max_workers=num_threads) as executor:+ chunk_results = list(+ tqdm(+ executor.map(_process_prompt, zip(chunk, plan_chunk)),+ total=len(chunk),+ )+ )+ results.extend(chunk_results)++ return resultsdiff --git a/camel/datagen/evol_instruct/scorer.py b/camel/datagen/evol_instruct/scorer.pynew file mode 100644index 000000000..6c80260ec--- /dev/null+++ b/camel/datagen/evol_instruct/scorer.py@@ -0,0 +1,166 @@+# ========= 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 json+from abc import ABC, abstractmethod+from typing import Dict, Optional++from pydantic import BaseModel, Field++from camel.agents import ChatAgent+from camel.logger import get_logger++logger = get_logger(__name__)+++class BaseScorer(ABC):+ @abstractmethod+ def score(+ self, reference_prompt: str, candidate_prompt: str+ ) -> Dict[str, int]:+ r"""Compare a candidate prompt against a reference prompt and+ return a tuple of scores. The higher the score, the better.+ For example, (diversity, difficulty, feasibility).+ """+ pass+++class MathScorer(BaseScorer):+ def __init__(self, agent: Optional[ChatAgent] = None):+ self.system_msg = (+ "You are an evaluator for math problems. Your task is to compare "+ "a new math problem against a reference math problem, and rate it "+ "in **four dimensions**, each scored from 1 to 5.\n\n"+ "1. Diversity (1-5): How novel is the new problem compared to the "+ "reference? 1 = very similar, 5 = completely different.\n"+ "2. Difficulty (1-5): Rate the relative difficulty compared to the"+ " reference problem. 1 = much less difficult, "+ "3 = similar difficulty, 5 = much more difficult.\n"+ "3. Validity (1-5): How well-defined and sound is the problem?"+ "1 = very vague or flawed, 5 = very clear and rigorous.\n"+ "4. Solvability (1-5): How likely is the problem solvable using "+ "standard math techniques? 1 = very unsolvable or ambiguous, "+ "5 = very clearly solvable.\n\n"+ "Respond with a JSON object like: "+ "{ \"diversity\": ..., \"difficulty\": ..., "+ "\"validity\": ..., \"solvability\": ... }"+ )+ self.agent = agent or ChatAgent(self.system_msg)++ class MathScoreSchema(BaseModel):+ diversity: int = Field(+ ...,+ description=(+ "Score for the diversity of the math problem "+ "compared to the reference"+ ),+ )+ difficulty: int = Field(+ ..., description="Score for the relative difficulty"+ )+ validity: int = Field(+ ...,+ description="Score for how well-defined and sound the problem is",+ )+ solvability: int = Field(+ ...,+ description="Score for the solvability of the problem",+ )++ def score(+ self, reference_problem: str, new_problem: str+ ) -> Dict[str, int]:+ r"""Evaluates the new math problem relative to the reference math+ problem.++ Args:+ reference_problem (str): The reference math problem.+ new_problem (str): The new or evolved math problem.++ Returns:+ Dict[str, int]: A dictionary with scores for diversity, difficulty,+ validity, and solvability.+ """+ query = (+ f"Reference problem:\n{reference_problem}\n\n"+ f"New problem:\n{new_problem}\n\n"+ "Provide scores in JSON format."+ )+ response = self.agent.step(query, response_format=self.MathScoreSchema)+ score_data = json.loads(response.msg.content)+ return score_data+++class GeneralScorer(BaseScorer):+ def __init__(self, agent: Optional[ChatAgent] = None):+ self.system_msg = (+ "You are an evaluator for problems in various domains. Your task "+ "is to compare a new problem against a reference problem, and rate"+ " it in **three dimensions**, each scored from 1 to 5.\n\n"+ "1. Diversity (1-5): How novel is the new problem compared to the "+ "reference? 1 = very similar, 5 = completely different.\n"+ "2. Complexity (1-5): Relative to the reference problem. "+ "1 = much less complex, 3 = similar complexity, "+ "5 = much more complex.\n"+ "3. Validity (1-5): How well-defined, meaningful, the problem is."+ "1 = vague/flawed, 5 = precise and fully meaningful.\n"+ "Respond with a JSON object like: "+ "{ \"diversity\": ..., \"complexity\": ..., \"validity\": ... }"+ )+ self.agent = agent or ChatAgent(self.system_msg)++ class GeneralScoreSchema(BaseModel):+ diversity: int = Field(+ ...,+ description=(+ "Score for the diversity of the problem "+ "compared to the reference."+ ),+ )+ complexity: int = Field(+ ...,+ description=("Score for the relative complexity of the problem."),+ )+ validity: int = Field(+ ...,+ description=(+ "Score estimating the likelihood that the problem is "+ "well-defined."+ ),+ )++ def score(+ self, reference_problem: str, new_problem: str+ ) -> Dict[str, int]:+ r"""Evaluates the new problem against the reference problem using+ structured scoring.++ Args:+ reference_problem (str): The original problem.+ new_problem (str): The evolved or new problem.++ Returns:+ Dict[str, int]: A dictionary with scores for diversity, complexity,+ and validity.+ """+ query = (+ f"Reference problem:\n{reference_problem}\n\n"+ f"New problem:\n{new_problem}\n\n"+ "Provide scores in JSON format."+ )+ response = self.agent.step(+ query, response_format=self.GeneralScoreSchema+ )+ score_data = json.loads(response.msg.content)+ return score_datadiff --git a/camel/datagen/evol_instruct/templates.py b/camel/datagen/evol_instruct/templates.pynew file mode 100644index 000000000..939710a5e--- /dev/null+++ b/camel/datagen/evol_instruct/templates.py@@ -0,0 +1,268 @@+# ========= 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. =========++from abc import ABC, abstractmethod+from dataclasses import dataclass+from typing import Dict, List, Union+++# flake8: noqa+@dataclass(frozen=True)+class BaseEvolInstructTemplates(ABC):+ r"""Abstract base class for evolution instruction templates.++ This class defines a required structure for prompt transformation templates+ - `EVOL_METHODS`: A dictionary mapping method keys to their descriptions.+ - `STRATEGY`: A dictionary defining strategies and associated methods.++ Subclasses should define concrete templates for specific domains.+ """++ @property+ @abstractmethod+ def EVOL_METHODS(self) -> Dict[str, str]:+ r"""A dictionary mapping evolution method keys to their descriptions."""+ pass++ @property+ @abstractmethod+ def STRATEGY(self) -> Dict[str, Dict[str, Union[str, List[str]]]]:+ r"""A dictionary defining strategies and their corresponding methods."""+ pass+++# flake8: noqa+@dataclass(frozen=True)+class EvolInstructTemplates(BaseEvolInstructTemplates):+ r"""Contains templates for EvolInstruct prompt transformations.++ References:+ - WizardLM: Empowering Large Language Models to Follow Complex+ Instructions+ https://arxiv.org/pdf/2304.12244+ - eva: Evolving Alignment via Asymmetric Self-Play+ https://arxiv.org/abs/2411.00062+ """++ # High-level instructions on in-depth/in-breadth evolving+ INST_IN_DEPTH = (+ "Please act as an expert Prompt Creator.\n"+ "Your objective is to rewrite a given prompt into a more complex "+ "version to make those large language models (e.g., gemini) a bit "+ "harder to handle.\n"+ "But the rewritten prompt must be reasonable and must be understood "+ "and responded by humans.\n"+ "Your rewriting cannot omit the non-text parts such as the table and "+ "code in #Given Prompt#, if there is any."+ "You should try your best not to make the #Rewritten Prompt# become "+ "verbose, "+ "The #Rewritten Prompt# should be roughly the similar length or a "+ "little bit more than that of #Given Prompt#.\n"+ "The #Rewritten Prompt# must sound like a real human user's prompt; "+ "DON'T make it like sound machine-generated."+ "Specifically, you SHOULD complicate the given prompt using the "+ "following method: "+ "\n{method}\n"+ "The rewritten prompt should reflect meaningful changes across its "+ "structure, ensuring the entire sentence feels sufficiently different "+ "from the original. "+ "Again, make sure the rewritten prompt is more CHALLENGING."+ "Respond with your rewritten prompt directly. "+ "#Given Prompt#:\n{prompt}\n"+ "#Rewritten Prompt#:\n"+ ).lstrip()++ INST_IN_BREADTH = (+ "Please act as an expert Prompt Creator.\n"+ "Your objective is to generate a brand-new prompt based on the #Given "+ "Prompt#. "+ "The purpose of this task is to promote diversity and generality of "+ "training prompts for language models, helping it practice with "+ "varied challenges and perspectives.\n"+ "The LENGTH and complexity of the #Created Prompt# should be similar "+ "to that of the #Given Prompt#.\n"+ "The #Created Prompt# must be reasonable, interpretable, and solvable "+ "by humans.\n"+ "The #Created Prompt# must sound like a real human user's prompt; "+ "DON'T make it sound like machine-generated."+ "Follow the method described below to guide your creation:\n"+ "{method}\n"+ "The created prompt should reflect meaningful changes across its "+ "structure, ensuring the entire sentence feels sufficiently different "+ "from the original. "+ "Respond with your created prompt directly.\n"+ "#Given Prompt#:\n{prompt}\n"+ "#Created Prompt#:\n"+ ).lstrip()++ # Sub-method instructions (following the eva paper setting)+ IN_BREADTH_KEYS = [+ 'persona',+ 'shift-in',+ 'shift-out',+ 'mix',+ 'abstract',+ ]++ IN_DEPTH_KEYS = [+ 'constraints',+ 'deepening',+ 'concretizing',+ 'reasoning',+ 'expansion',+ ]++ STRATEGY = {+ "IN-DEPTH": {+ 'meta_instruction': INST_IN_DEPTH,+ 'methods': IN_DEPTH_KEYS,+ },+ "IN-BREADTH": {+ 'meta_instruction': INST_IN_BREADTH,+ 'methods': IN_BREADTH_KEYS,+ },+ }++ EVOL_METHODS = {+ "persona": (+ "Reframe the #Given Prompt# as if written by a user with a "+ "completely different persona, background, or expertise. Adjust "+ "the tone, style, phrasing, or anything you feel proper to "+ "reflect this change. The changes should make the prompt feel "+ "like it was authored by someone entirely new."+ ),+ "shift-in": (+ "Shift the high-level idea of the #Given Prompt# to explore a "+ "different subdomain or context within the same domain. Ensure "+ "the new topic still challenges the model to reason or provide "+ "knowledge relevant to the domain."+ ),+ "shift-out": (+ "Shift the high-level idea of the #Given Prompt# to a completely "+ "different topic in a different setting. The new topic may "+ "challenge the model with similar reasoning or contextual "+ "understanding but in a novel way."+ ),+ "mix": (+ "Combine the high-level concept of the #Given Prompt# with "+ "elements from a different domain. Introduce novel scenarios or "+ "contexts to create diversity while maintaining relevance to the "+ "original idea."+ ),+ "abstract": (+ "Turn the #Given Prompt# into a more abstract or generalized "+ "version, removing specific details while preserving its intent. "+ "Ensure the new prompt encourages broader, principle-driven "+ "reasoning."+ ),+ "constraints": (+ "Add one or more significant constraints or requirements into the "+ "'#Given Prompt#'. The added constraints must meaningfully alter "+ "how the model would respond. For example, specify additional "+ "rules, contexts, or limitations that demand creative adjustments."+ ),+ "deepening": (+ "If the #Given Prompt# contains inquiries about certain issues, "+ "increase the depth and breadth of the inquiry. Make the question "+ "require a more detailed, multi-layered, or comprehensive response"+ ". For instance, break the problem into sub-problems or require "+ "connections between unrelated concepts."+ ),+ "concretizing": (+ "Replace general concepts in the #Given Prompt# with more specific"+ " and detailed concepts. Ensure that the change makes the problem "+ "more defined and concrete, leaving less room for ambiguity. For "+ "example, replace 'a device' with 'a wearable fitness tracker "+ "with GPS'."+ ),+ "reasoning": (+ "Add one or more reasoning steps into the '#Given Prompt#'. "+ "Explicitly rewrite it to demand multi-step reasoning or justify "+ "intermediate steps in the solution. For instance, if the original"+ " prompt is a simple query, make the response require a "+ "step-by-step breakdown of logic or calculations."+ ),+ "expansion": (+ "Expand the #Given Prompt# by including additional perspectives, "+ "domains, or layers of complexity. For example, if the original "+ "prompt focuses on a single scenario, add related scenarios or ask"+ " the model to compare different situations."+ ),+ }+++# flake8: noqa+@dataclass(frozen=True)+class MathEvolInstructTemplates(BaseEvolInstructTemplates):+ r"""Contains templates for MathEvolInstruct prompt transformations."""++ # Meta-instructions for in-depth evolving+ INST_IN_DEPTH = (+ "Please act as a math expert. Your objective is to create a new math "+ "problem that is more challenging yet concise than the given math "+ "problem. Ensure that the mathematical content (including any "+ "equations or figures) is preserved, and rephrase the problem to "+ "increase its complexity and depth. The generated problem should be "+ "clearly stated, strictly mathematical, and suitable for solving with "+ "symbolic computation (e.g., using sympy). You will be given a method "+ "to guide your creation. Make sure to follow the method strictly. "+ "Consolidate any multiple parts into one integrated question that "+ "ask for one definitive answer. Respond with your generated problem "+ "directly. "+ "#Original Problem#:\n{prompt}\n"+ "#Generated Problem#:\n"+ ).lstrip()++ EVOL_METHODS = {+ "constraints": (+ "Add one or more significant constraints or requirements into the "+ "'#Given Prompt#'. The added constraints must meaningfully alter "+ "how the model would respond. For example, specify additional "+ "rules, contexts, or limitations that demand creative adjustments."+ ),+ "deepening": (+ "Increase the difficulty of the #Given Prompt# by integrating "+ "additional layers of reasoning and rigor. Refine the problem so "+ "that all added difficulty is consolidated into a single coherent "+ "question requiring one final answer, avoiding fragmentation into "+ "multiple sub-problems."+ ),+ "expansion": (+ "Expand the #Given Prompt# by incorporating additional "+ "perspectives or layers of complexity into the problem statement. "+ "Ensure that the revised problem remains a single, unified "+ "question with one final answer, rather than a series of separate "+ "sub-questions."+ ),+ "condense": (+ "Reformulate the given math problem into a well-structured and "+ "formally stated mathematical question.\n"+ "- Present the problem in a structured and rigorous mathematical "+ "format.\n"+ "- Removing unnecessary instructions, explanations, or hints.\n"+ "- If the given problem contains several sub-questions, make "+ "necessary changes to let the problem could be answered with one "+ "number or expression by removing the sub-questions or combining "+ "them into one."+ ),+ }++ IN_DEPTH_KEYS = ['constraints', 'deepening', 'expansion']++ STRATEGY = {+ "IN-DEPTH": {+ 'meta_instruction': INST_IN_DEPTH,+ 'methods': IN_DEPTH_KEYS,+ },+ }diff --git a/examples/datagen/evol_instruct/evol_instruct.py b/examples/datagen/evol_instruct/evol_instruct.pynew file mode 100644index 000000000..77f945165--- /dev/null+++ b/examples/datagen/evol_instruct/evol_instruct.py@@ -0,0 +1,82 @@+# ========= 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 json+import logging+import os++from camel.agents import ChatAgent+from camel.datagen.evol_instruct import EvolInstructPipeline+from camel.datagen.evol_instruct.scorer import MathScorer+from camel.datagen.evol_instruct.templates import MathEvolInstructTemplates+from camel.logger import enable_logging, get_logger, set_log_level+from camel.models import ModelFactory+from camel.types import ModelPlatformType, ModelType++os.environ["CAMEL_LOGGING_DISABLED"] = "false"+++def main():+ r"""Example usage of EvolInstructPipeline for iterative and parallel+ evolution.+ """+ # Load data+ file_path = "./examples/datagen/evol_instruct/input.json"+ prompts = json.loads(open(file_path, "r", encoding="utf-8").read())++ # Define parameters+ model = ModelFactory.create(+ model_platform=ModelPlatformType.OPENAI,+ model_type=ModelType.GPT_4O_MINI,+ model_config_dict={"temperature": 0.7, "max_tokens": 4096},+ )+ agent = ChatAgent(model=model)+ num_generations = 2+ evol_spec = [+ "in-depth",+ "in-depth",+ "in-depth",+ "condense",+ ]++ # Initialize the data generation pipeline with the specified template+ pipeline = EvolInstructPipeline(+ agent=agent,+ templates=MathEvolInstructTemplates,+ )++ # Execute the data generation pipeline+ results = pipeline.generate(+ prompts=prompts,+ evolution_spec=evol_spec,+ num_iterations=4,+ num_generations=num_generations,+ scorer=MathScorer(),+ )++ # Save the generated results to a file+ results_path = "./examples/datagen/evol_instruct/results.json"+ with open(results_path, mode="w", encoding="utf-8") as file:+ json.dump(results, file, indent=4, ensure_ascii=False)++ logger.info(f"Results saved to '{results_path}'.")+++if __name__ == "__main__":+ enable_logging()+ set_log_level(logging.WARNING)+ logger = get_logger("evol-instruct")+ logger.info("Begin evolution.")+ main()+ logger.info("Evolution complete.")diff --git a/examples/datagen/evol_instruct/input.json b/examples/datagen/evol_instruct/input.jsonnew file mode 100644index 000000000..f5a5e3bdc--- /dev/null+++ b/examples/datagen/evol_instruct/input.json@@ -0,0 +1,7 @@+[+ "Let \\[f(x) = \\left\\{\n\\begin{array}{cl} ax+3, &\\text{ if }x>2, \\\\\nx-5 &\\text{ if } -2 \\le x \\le 2, \\\\\n2x-b &\\text{ if } x <-2.\n\\end{array}\n\\right.\\]Find $a+b$ if the piecewise function is continuous (which means that its graph can be drawn without lifting your pencil from the paper).",+ "A rectangular band formation is a formation with $m$ band members in each of $r$ rows, where $m$ and $r$ are integers. A particular band has less than 100 band members. The director arranges them in a rectangular formation and finds that he has two members left over. If he increases the number of members in each row by 1 and reduces the number of rows by 2, there are exactly enough places in the new formation for each band member. What is the largest number of members the band could have?",+ "Find the center of the circle with equation $x^2 - 6x + y^2 + 2y = 9$.",+ "What are all values of $p$ such that for every $q>0$, we have $$\\frac{3(pq^2+p^2q+3q^2+3pq)}{p+q}>2p^2q?$$ Express your answer in interval notation in decimal form.",+ "Tim wants to invest some money in a bank which compounds quarterly with an annual interest rate of $7\\%$. To the nearest dollar, how much money should he invest if he wants a total of $\\$60,\\!000$ at the end of $5$ years?"+]\ No newline at end of filediff --git a/examples/datagen/evol_instruct/results.json b/examples/datagen/evol_instruct/results.jsonnew file mode 100644index 000000000..d4e8537e0--- /dev/null+++ b/examples/datagen/evol_instruct/results.json@@ -0,0 +1,552 @@+[+ {+ "0": [+ {+ "instruction": "Let \\[f(x) = \\left\\{\n\\begin{array}{cl} ax+3, &\\text{ if }x>2, \\\\\nx-5 &\\text{ if } -2 \\le x \\le 2, \\\\\n2x-b &\\text{ if } x <-2.\n\\end{array}\n\\right.\\]Find $a+b$ if the piecewise function is continuous (which means that its graph can be drawn without lifting your pencil from the paper).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Let \\[ f(x) = \\left\\{\n\\begin{array}{cl} ax + 3, & \\text{if } x > 3, \\\\\nx^2 - 4, & \\text{if } -3 \\le x \\le 3, \\\\\nbx + 1, & \\text{if } x < -3.\n\\end{array}\n\\right.\\]\nDetermine the values of \\( a \\) and \\( b \\) such that the function \\( f(x) \\) is continuous across its entire domain, and compute the value of \\( a^2 + b^2 \\).",+ "method": "expansion",+ "scores": {+ "diversity": 3,+ "difficulty": 3,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Let \\[ f(x) = \\left\\{\n\\begin{array}{cl} ax + 4, & \\text{ if } x > 3, \\\\\n2x - c, & \\text{ if } -3 \\leq x \\leq 3, \\\\\nbx + 1, & \\text{ if } x < -3.\n\\end{array}\n\\right. \\] \nDetermine the values of \\( a \\), \\( b \\), and \\( c \\) such that the piecewise function is continuous at the transition points \\( x = 3 \\) and \\( x = -3 \\). Then, calculate the value of \\( a + b + c \\).",+ "method": "constraints",+ "scores": {+ "diversity": 3,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "1": [+ {+ "instruction": "Let \\[ f(x) = \\left\\{\n\\begin{array}{cl} ax + 4, & \\text{ if } x > 3, \\\\\n2x - c, & \\text{ if } -3 \\leq x \\leq 3, \\\\\nbx + 1, & \\text{ if } x < -3.\n\\end{array}\n\\right. \\] \nDetermine the values of \\( a \\), \\( b \\), and \\( c \\) such that the piecewise function is continuous at the transition points \\( x = 3 \\) and \\( x = -3 \\). Then, calculate the value of \\( a + b + c \\).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Let \\[ f(x) = \\left\\{\n\\begin{array}{cl} ax + 4, & \\text{ if } x > 4, \\\\\nkx^2 - 2, & \\text{ if } -4 \\leq x \\leq 4, \\\\\nbx + 1, & \\text{ if } x < -4.\n\\end{array}\n\\right. \\] \nDetermine the values of \\( a \\), \\( b \\), and \\( k \\) such that the piecewise function is continuous at the transition points \\( x = 4 \\) and \\( x = -4 \\). Additionally, evaluate the expression \\( a^2 + b^2 + k^2 \\).",+ "method": "constraints",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Let \\[ f(x) = \\left\\{\n\\begin{array}{cl} ax^2 + 4, & \\text{ if } x > 3, \\\\\n2x + d, & \\text{ if } -3 \\leq x \\leq 3, \\\\\nbx - 5, & \\text{ if } x < -3.\n\\end{array}\n\\right. \\] \n\nDetermine the values of \\( a \\), \\( b \\), and \\( d \\) such that the piecewise function is continuous at the transition points \\( x = 3 \\) and \\( x = -3 \\). Additionally, find the value of \\( a^2 + b^2 + d \\).",+ "method": "deepening",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "2": [+ {+ "instruction": "Let \\[ f(x) = \\left\\{\n\\begin{array}{cl} ax + 4, & \\text{ if } x > 4, \\\\\nkx^2 - 2, & \\text{ if } -4 \\leq x \\leq 4, \\\\\nbx + 1, & \\text{ if } x < -4.\n\\end{array}\n\\right. \\] \nDetermine the values of \\( a \\), \\( b \\), and \\( k \\) such that the piecewise function is continuous at the transition points \\( x = 4 \\) and \\( x = -4 \\). Additionally, evaluate the expression \\( a^2 + b^2 + k^2 \\).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Let \n\\[\nf(x) = \\left\\{\n\\begin{array}{cl} \nax + 4, & \\text{ if } x > 4, \\\\\nkx^2 - 2, & \\text{ if } -4 \\leq x \\leq 4, \\\\\nbx + 1, & \\text{ if } x < -4.\n\\end{array}\n\\right. \n\\]\nDetermine the values of \\( a \\), \\( b \\), and \\( k \\) such that the piecewise function is continuous at the transition points \\( x = 4 \\) and \\( x = -4 \\), and additionally, ensure that the derivative \\( f'(x) \\) is also continuous at these points. Finally, compute the value of the expression \\( (a + b + k)^2 - (ab + ak + bk) \\).",+ "method": "expansion",+ "scores": {+ "diversity": 3,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Let \n\n\\[\nf(x) = \\begin{cases} \nax + 4, & \\text{ if } x > 4, \\\\\nkx^2 - 2, & \\text{ if } -4 \\leq x \\leq 4, \\\\\nbx + 1, & \\text{ if } x < -4.\n\\end{cases}\n\\]\n\nTo ensure the continuity of the piecewise function at the transition points \\( x = 4 \\) and \\( x = -4 \\), derive the conditions on the coefficients \\( a \\), \\( b \\), and \\( k \\) such that:\n\n1. The limit from the left at \\( x = 4 \\) equals the limit from the right at \\( x = 4 \\).\n2. The limit from the left at \\( x = -4 \\) equals the limit from the right at \\( x = -4 \\).\n\nFurthermore, compute the value of the expression \\( (a + b + k)^2 + (a - b)^2 + (k - 4)^2 \\) given these continuity conditions.",+ "method": "deepening",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "3": [+ {+ "instruction": "Let \n\\[\nf(x) = \\left\\{\n\\begin{array}{cl} \nax + 4, & \\text{ if } x > 4, \\\\\nkx^2 - 2, & \\text{ if } -4 \\leq x \\leq 4, \\\\\nbx + 1, & \\text{ if } x < -4.\n\\end{array}\n\\right. \n\\]\nDetermine the values of \\( a \\), \\( b \\), and \\( k \\) such that the piecewise function is continuous at the transition points \\( x = 4 \\) and \\( x = -4 \\), and additionally, ensure that the derivative \\( f'(x) \\) is also continuous at these points. Finally, compute the value of the expression \\( (a + b + k)^2 - (ab + ak + bk) \\).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Let \n\\[\nf(x) = \\left\\{\n\\begin{array}{cl} \nax + 4, & \\text{ if } x > 4, \\\\\nkx^2 - 2, & \\text{ if } -4 \\leq x \\leq 4, \\\\\nbx + 1, & \\text{ if } x < -4.\n\\end{array}\n\\right. \n\\]\nDetermine the values of \\( a \\), \\( b \\), and \\( k \\) such that the piecewise function is both continuous and differentiable at the transition points \\( x = 4 \\) and \\( x = -4 \\). Specifically, ensure that the function and its first derivative \\( f'(x) \\) are equal at these points. Finally, calculate the value of the expression \\( (a + b + k)^3 - 3(a + b + k)(ab + ak + bk) + 3(ab + ak + bk)^2 \\).",+ "method": "condense",+ "scores": {+ "diversity": 4,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Let \n\\[\nf(x) = \\left\\{\n\\begin{array}{cl} \nax + 6, & \\text{ if } x > 5, \\\\\nkx^3 - 3x, & \\text{ if } -5 \\leq x \\leq 5, \\\\\nbx + 2, & \\text{ if } x < -5.\n\\end{array}\n\\right. \n\\]\nDetermine the values of \\( a \\), \\( b \\), and \\( k \\) such that the piecewise function is continuous at the transition points \\( x = 5 \\) and \\( x = -5 \\), and ensure that both the first derivative \\( f'(x) \\) and the second derivative \\( f''(x) \\) are continuous at these points. Finally, compute the value of the expression \\( (a + b + k)^3 - 3(a + b + k)(ab + ak + bk) \\).",+ "method": "condense",+ "scores": {+ "diversity": 3,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ }+ ]+ },+ {+ "0": [+ {+ "instruction": "A rectangular band formation is a formation with $m$ band members in each of $r$ rows, where $m$ and $r$ are integers. A particular band has less than 100 band members. The director arranges them in a rectangular formation and finds that he has two members left over. If he increases the number of members in each row by 1 and reduces the number of rows by 2, there are exactly enough places in the new formation for each band member. What is the largest number of members the band could have?",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "A band consists of a total of \\(N\\) members, where \\(N\\) is a positive integer less than 100. The director initially arranges the band members in a rectangular formation with \\(m\\) members in each of \\(r\\) rows, leaving 3 members unassigned. When he increases the number of members per row by 2 and decreases the number of rows by 1, the new arrangement accommodates exactly all the band members without any leftover. \n\nDetermine the largest possible value of \\(N\\) that satisfies these conditions, and express your solution in terms of \\(N\\) as a quadratic equation in \\(m\\) and \\(r\\).",+ "method": "expansion",+ "scores": {+ "diversity": 4,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "A rectangular band formation consists of \\( m \\) band members in each of \\( r \\) rows, where \\( m \\) and \\( r \\) are positive integers and the total number of band members \\( N \\) satisfies \\( N < 100 \\). Initially, the director finds that when arranged in this rectangular formation, there are 2 members left over, leading to the equation:\n\n\\[\nN = mr + 2\n\\]\n\nWhen the director increases the number of members in each row by 2 and reduces the number of rows by 3, the new arrangement perfectly accommodates all band members. This gives the equation:\n\n\\[\nN = (m + 2)(r - 3)\n\\]\n\nDetermine the largest possible value of \\( N \\) that satisfies both conditions.",+ "method": "deepening",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "1": [+ {+ "instruction": "A band consists of a total of \\(N\\) members, where \\(N\\) is a positive integer less than 100. The director initially arranges the band members in a rectangular formation with \\(m\\) members in each of \\(r\\) rows, leaving 3 members unassigned. When he increases the number of members per row by 2 and decreases the number of rows by 1, the new arrangement accommodates exactly all the band members without any leftover. \n\nDetermine the largest possible value of \\(N\\) that satisfies these conditions, and express your solution in terms of \\(N\\) as a quadratic equation in \\(m\\) and \\(r\\).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "A musical ensemble consists of \\(N\\) members, where \\(N\\) is a positive integer constrained by \\(1 \\leq N < 100\\). Initially, the ensemble is arranged in a rectangular grid of \\(m\\) members per row and \\(r\\) rows, with 3 members remaining unassigned. When the arrangement is modified by increasing the number of members per row by 3 and reducing the number of rows by 2, the new configuration accommodates all members precisely.\n\nGiven this scenario, derive the maximum feasible value of \\(N\\) that meets these criteria, and express your findings as a quadratic equation in terms of \\(m\\) and \\(r\\). Additionally, provide a detailed solution for \\(N\\) in terms of \\(m\\) and \\(r\\).",+ "method": "constraints",+ "scores": {+ "diversity": 3,+ "difficulty": 3,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "A music ensemble consists of \\(N\\) members, where \\(N\\) is a positive integer less than 100. The conductor initially arranges the members in a rectangular formation of \\(m\\) members in each of \\(r\\) rows, resulting in 3 members remaining unassigned. Upon increasing the number of members per row by 3 and decreasing the number of rows by 2, the new arrangement perfectly accommodates all \\(N\\) members without any leftover.\n\nFormulate a quadratic equation involving \\(m\\) and \\(r\\) that represents the conditions given, and subsequently determine the largest possible value of \\(N\\) that meets these criteria.",+ "method": "deepening",+ "scores": {+ "diversity": 2,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "2": [+ {+ "instruction": "A musical ensemble consists of \\(N\\) members, where \\(N\\) is a positive integer constrained by \\(1 \\leq N < 100\\). Initially, the ensemble is arranged in a rectangular grid of \\(m\\) members per row and \\(r\\) rows, with 3 members remaining unassigned. When the arrangement is modified by increasing the number of members per row by 3 and reducing the number of rows by 2, the new configuration accommodates all members precisely.\n\nGiven this scenario, derive the maximum feasible value of \\(N\\) that meets these criteria, and express your findings as a quadratic equation in terms of \\(m\\) and \\(r\\). Additionally, provide a detailed solution for \\(N\\) in terms of \\(m\\) and \\(r\\).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "A musical ensemble consists of \\(N\\) members, where \\(N\\) is a positive integer constrained by \\(1 \\leq N < 100\\). The ensemble is initially arranged in a rectangular grid of \\(m\\) members per row and \\(r\\) rows, with \\(k\\) members remaining unassigned, where \\(k\\) is a positive integer less than \\(m\\). Upon modifying the configuration by increasing the number of members per row by \\(p\\) and reducing the number of rows by \\(q\\), the new arrangement accommodates all members exactly.\n\nGiven this scenario, derive the maximum feasible value of \\(N\\) that satisfies these conditions, and express your findings as a quadratic equation in terms of \\(m\\), \\(r\\), \\(k\\), \\(p\\), and \\(q\\). Furthermore, provide a detailed solution for \\(N\\) in terms of \\(m\\) and \\(r\\), while incorporating the parameters \\(k\\), \\(p\\), and \\(q\\).",+ "method": "expansion",+ "scores": {+ "diversity": 4,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "A musical ensemble consists of \\(N\\) members, where \\(N\\) is a positive integer constrained by \\(1 \\leq N < 100\\). Initially, the ensemble is arranged in a rectangular grid of \\(m\\) members per row and \\(r\\) rows, with a certain number \\(k\\) of members remaining unassigned. When the arrangement is modified by increasing the number of members per row by \\(p\\) and reducing the number of rows by \\(q\\), the new configuration accommodates all members precisely.\n\nGiven this scenario, formulate the relationship between \\(N\\), \\(m\\), \\(r\\), \\(k\\), \\(p\\), and \\(q\\) leading to a quadratic equation. Specifically, derive the maximum feasible value of \\(N\\) under these constraints, and express your findings as a function of \\(m\\) and \\(r\\) in terms of \\(k\\), \\(p\\), and \\(q\\). Please provide a detailed solution for \\(N\\) in terms of \\(m\\) and \\(r\\), including the parameters \\(k\\), \\(p\\), and \\(q\\).",+ "method": "constraints",+ "scores": {+ "diversity": 4,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "3": [+ {+ "instruction": "A musical ensemble consists of \\(N\\) members, where \\(N\\) is a positive integer constrained by \\(1 \\leq N < 100\\). Initially, the ensemble is arranged in a rectangular grid of \\(m\\) members per row and \\(r\\) rows, with a certain number \\(k\\) of members remaining unassigned. When the arrangement is modified by increasing the number of members per row by \\(p\\) and reducing the number of rows by \\(q\\), the new configuration accommodates all members precisely.\n\nGiven this scenario, formulate the relationship between \\(N\\), \\(m\\), \\(r\\), \\(k\\), \\(p\\), and \\(q\\) leading to a quadratic equation. Specifically, derive the maximum feasible value of \\(N\\) under these constraints, and express your findings as a function of \\(m\\) and \\(r\\) in terms of \\(k\\), \\(p\\), and \\(q\\). Please provide a detailed solution for \\(N\\) in terms of \\(m\\) and \\(r\\), including the parameters \\(k\\), \\(p\\), and \\(q\\).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "A musical ensemble consists of \\(N\\) members, where \\(N\\) is a positive integer constrained by \\(1 \\leq N < 100\\). The members are initially arranged in a rectangular grid of \\(m\\) members per row and \\(r\\) rows, with \\(k\\) members remaining unassigned, such that \\(N = mr + k\\). \n\nWhen the arrangement is modified by increasing the number of members per row by \\(p\\) and reducing the number of rows by \\(q\\), the new configuration accommodates all members precisely, leading to the equation \\(N = (m + p)(r - q)\\).\n\nGiven these configurations, derive the quadratic equation that relates \\(N\\), \\(m\\), \\(r\\), \\(k\\), \\(p\\), and \\(q\\). Furthermore, determine the maximum feasible value of \\(N\\) under these constraints and express \\(N\\) as a function of \\(m\\), \\(r\\), \\(k\\), \\(p\\), and \\(q\\). Provide a thorough solution detailing the steps taken to arrive at this function and confirm the conditions under which it holds.",+ "method": "condense",+ "scores": {+ "diversity": 4,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Consider a musical ensemble consisting of \\( N \\) members, where \\( N \\) is a positive integer constrained by \\( 1 \\leq N < 100 \\). The ensemble is initially arranged in a rectangular grid with \\( m \\) members per row and \\( r \\) rows, leaving \\( k \\) members unassigned such that \\( k < m \\) and \\( k \\) is a non-negative integer. When the arrangement changes by increasing the number of members per row by \\( p \\) (where \\( p \\) is a positive integer) and decreasing the number of rows by \\( q \\) (where \\( q \\) is a positive integer), the new configuration accommodates all members exactly.\n\nYour task is to formulate a relationship that connects \\( N \\), \\( m \\), \\( r \\), \\( k \\), \\( p \\), and \\( q \\) leading to a quadratic equation. Specifically, derive the maximum feasible value of \\( N \\) given these constraints. Express \\( N \\) as a function of \\( m \\) and \\( r \\) in terms of \\( k \\), \\( p \\), and \\( q \\), and provide a detailed solution that includes:\n\n1. The initial arrangement equation: \\( N = m \\cdot r + k \\)\n2. The modified arrangement equation: \\( N = (m + p)(r - q) \\)\n3. A quadratic equation derived from equating the two expressions for \\( N \\).\n4. The explicit form of \\( N \\) in terms of \\( m \\), \\( r \\), \\( k \\), \\( p \\), and \\( q \\).\n\nFind the maximum \\( N \\) under the specified constraints, and prove that your derived function is valid for the given range of \\( N \\).",+ "method": "condense",+ "scores": {+ "diversity": 4,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ }+ ]+ },+ {+ "0": [+ {+ "instruction": "Find the center of the circle with equation $x^2 - 6x + y^2 + 2y = 9$.",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Determine the coordinates of the center of the circle defined by the equation \\( 4(x^2 - 3x) + 4(y^2 + y) = 36 \\), and express your answer in simplified form.",+ "method": "deepening",+ "scores": {+ "diversity": 3,+ "difficulty": 3,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Determine the coordinates of the center of the circle defined by the equation \\( 3x^2 + 3y^2 - 18x + 12y - 27 = 0 \\) by rewriting the equation in standard form and identifying the center directly from it.",+ "method": "expansion",+ "scores": {+ "diversity": 2,+ "difficulty": 3,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "1": [+ {+ "instruction": "Determine the coordinates of the center of the circle defined by the equation \\( 4(x^2 - 3x) + 4(y^2 + y) = 36 \\), and express your answer in simplified form.",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Given the equation of a circle in the form \\( 4(x^2 - 3x) + 4(y^2 + y) = 36 \\), first transform this equation into standard form by completing the square for both the \\(x\\) and \\(y\\) variables. After finding the center of the circle, express the coordinates of the center as a function of the parameters \\(a\\) and \\(b\\) defined by \\(a = -\\frac{b}{2}\\) for the \\(x\\) component and \\(b = -\\frac{c}{2}\\) for the \\(y\\) component. Finally, compute the exact coordinates of the center and represent your answer as a simplified ordered pair \\((h, k)\\).",+ "method": "expansion",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Given the equation of the circle \\( 4(x^2 - 3x) + 4(y^2 + y) = 36 \\), transform it into standard form by completing the square for both \\( x \\) and \\( y \\). Then, calculate the coordinates of the center of the circle, and express your answer as an ordered pair, \\((h, k)\\), where \\( h \\) and \\( k \\) are in their simplest form.",+ "method": "constraints",+ "scores": {+ "diversity": 2,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "2": [+ {+ "instruction": "Given the equation of a circle in the form \\( 4(x^2 - 3x) + 4(y^2 + y) = 36 \\), first transform this equation into standard form by completing the square for both the \\(x\\) and \\(y\\) variables. After finding the center of the circle, express the coordinates of the center as a function of the parameters \\(a\\) and \\(b\\) defined by \\(a = -\\frac{b}{2}\\) for the \\(x\\) component and \\(b = -\\frac{c}{2}\\) for the \\(y\\) component. Finally, compute the exact coordinates of the center and represent your answer as a simplified ordered pair \\((h, k)\\).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Given the equation of a circle in the form \\( 9(x^2 + 4x) + 9(y^2 - 2y) = 81 \\), first transform this equation into standard form by completing the square for both the \\(x\\) and \\(y\\) variables. After deriving the center of the circle, express the coordinates of the center as functions of the parameters \\(a\\) and \\(b\\), where \\(a = -\\frac{b}{2}\\) for the \\(x\\) coordinate and \\(b = -\\frac{c}{2}\\) for the \\(y\\) coordinate, with \\(c\\) being the constant term after completing the square. Finally, compute the exact coordinates of the center in terms of \\(a\\) and \\(b\\), and provide your answer as a simplified ordered pair \\((h, k)\\) where \\(h\\) and \\(k\\) are explicitly defined in terms of \\(a\\) and \\(b\\).",+ "method": "expansion",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Given the equation of a circle in the form \\( 9(x^2 - \\frac{8}{3}x) + 9(y^2 + 4y) = 81 \\), transform this equation into standard form by completing the square for both the \\(x\\) and \\(y\\) variables. Define the center of the circle as \\((h, k)\\) and express \\(h\\) and \\(k\\) in terms of the parameters \\(p\\) and \\(q\\) defined by \\(p = -\\frac{b}{2}\\) for the \\(x\\) component and \\(q = -\\frac{c}{2}\\) for the \\(y\\) component. Finally, calculate the exact coordinates of the center and represent your answer as a simplified ordered pair \\((h, k)\\). Additionally, determine the radius of the circle and provide your answer as \\(r = \\sqrt{R}\\), where \\(R\\) is the squared radius obtained from the standard form equation.",+ "method": "constraints",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "3": [+ {+ "instruction": "Given the equation of a circle in the form \\( 9(x^2 + 4x) + 9(y^2 - 2y) = 81 \\), first transform this equation into standard form by completing the square for both the \\(x\\) and \\(y\\) variables. After deriving the center of the circle, express the coordinates of the center as functions of the parameters \\(a\\) and \\(b\\), where \\(a = -\\frac{b}{2}\\) for the \\(x\\) coordinate and \\(b = -\\frac{c}{2}\\) for the \\(y\\) coordinate, with \\(c\\) being the constant term after completing the square. Finally, compute the exact coordinates of the center in terms of \\(a\\) and \\(b\\), and provide your answer as a simplified ordered pair \\((h, k)\\) where \\(h\\) and \\(k\\) are explicitly defined in terms of \\(a\\) and \\(b\\).",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Given the equation of a circle represented as \\( 4(x^2 + 6x) + 4(y^2 - 8y) = 64 \\), transform this equation into standard form by completing the square for both the \\(x\\) and \\(y\\) variables. Derive the center coordinates of the circle and express these coordinates as functions of the parameters \\(a\\) and \\(b\\), defined as \\(a = -\\frac{d}{2}\\) for the \\(x\\) coordinate and \\(b = -\\frac{e}{2}\\) for the \\(y\\) coordinate, where \\(d\\) and \\(e\\) are the linear coefficients from the completed square form of the \\(x\\) and \\(y\\) terms, respectively. Finally, calculate the exact coordinates of the center in terms of \\(a\\) and \\(b\\), and provide your answer as a simplified ordered pair \\((h, k)\\), where \\(h\\) and \\(k\\) are expressed explicitly in terms of \\(a\\) and \\(b\\).",+ "method": "condense",+ "scores": {+ "diversity": 3,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Given the equation of a circle represented as \\( 4(x^2 - 6x) + 4(y^2 + 8y) = 64 \\), first transform this equation into its standard form by completing the square for both the \\( x \\) and \\( y \\) variables. After identifying the center of the circle, express the coordinates of the center as functions of the parameters \\( p \\) and \\( q \\), where \\( p = -\\frac{b}{2} \\) for the \\( x \\) coordinate and \\( q = -\\frac{d}{2} \\) for the \\( y \\) coordinate, with \\( b \\) and \\( d \\) being the coefficients of the linear terms in the completed square expressions. Finally, compute the exact coordinates of the center in terms of \\( p \\) and \\( q \\), and present your answer as a simplified ordered pair \\((h, k)\\) where \\( h \\) and \\( k \\) are explicitly defined in terms of \\( p \\) and \\( q \\).",+ "method": "condense",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ]+ },+ {+ "0": [+ {+ "instruction": "What are all values of $p$ such that for every $q>0$, we have $$\\frac{3(pq^2+p^2q+3q^2+3pq)}{p+q}>2p^2q?$$ Express your answer in interval notation in decimal form.",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Determine all values of \\( p \\) such that the inequality \n\n\\[\n\\frac{3(pq^2 + p^2q + 3q^2 + 3pq)}{p + q} > 2p^2q\n\\]\n\nholds for all \\( q > 0 \\). Additionally, find the conditions under which this inequality simplifies to a quadratic in \\( q \\) that is positive for all \\( q > 0 \\) and express your answer in interval notation in decimal form.",+ "method": "deepening",+ "scores": {+ "diversity": 2,+ "difficulty": 3,+ "validity": 5,+ "solvability": 4+ }+ },+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality holds:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq)}{p + q} > 2p^2q + 5q^2.\n\\]\n\nExpress your answer in interval notation in decimal form.",+ "method": "expansion",+ "scores": {+ "diversity": 2,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "1": [+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality holds:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq)}{p + q} > 2p^2q + 5q^2.\n\\]\n\nExpress your answer in interval notation in decimal form.",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality is satisfied:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq)}{p + q} \\geq 2p^2q + 5q^2 - \\frac{(p^2 + 3p + 2)q^3}{(p + q)^2}.\n\\]\n\nExpress your answer in interval notation in decimal form.",+ "method": "deepening",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality is satisfied:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq)}{p + q} \\geq 2p^2q + 5q^2 + \\frac{1}{q},\n\\]\n\nand identify if there exists any \\( p \\) for which the inequality holds true as \\( q \\) approaches infinity. Express your answer in interval notation in decimal form.",+ "method": "constraints",+ "scores": {+ "diversity": 2,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "2": [+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality is satisfied:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq)}{p + q} \\geq 2p^2q + 5q^2 - \\frac{(p^2 + 3p + 2)q^3}{(p + q)^2}.\n\\]\n\nExpress your answer in interval notation in decimal form.",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality holds true:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq)}{p + q} - 2p^2q - 5q^2 + \\frac{(p^2 + 3p + 2)q^3}{(p + q)^2} \\geq 0.\n\\]\n\nFurthermore, find the values of \\( p \\) for which the minimum of the left-hand side expression, as a function of \\( q \\), is non-negative for all \\( q > 0 \\). Express your answer in interval notation in decimal form.",+ "method": "deepening",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality holds:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq) + (p + q)^2}{p + q} \\geq 2p^2q + 5q^2 - \\frac{(p^2 + 3p + 2)q^3}{(p + q)^2} + \\frac{3pq^3}{(p + q)^3}.\n\\]\n\nExpress your answer in interval notation in decimal form.",+ "method": "constraints",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "3": [+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality holds true:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq)}{p + q} - 2p^2q - 5q^2 + \\frac{(p^2 + 3p + 2)q^3}{(p + q)^2} \\geq 0.\n\\]\n\nFurthermore, find the values of \\( p \\) for which the minimum of the left-hand side expression, as a function of \\( q \\), is non-negative for all \\( q > 0 \\). Express your answer in interval notation in decimal form.",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality is satisfied:\n\n\\[\n\\frac{3(pq^2 + p^2 q + 3q^2 + 3pq) + (p^2 + 3p + 2)q^3}{(p + q)^2} - 2p^2q - 5q^2 \\geq 0.\n\\]\n\nIn addition, identify the values of \\( p \\) for which the minimum of the left-hand side expression, viewed as a function of \\( q \\), remains non-negative for all \\( q > 0 \\). Present your answer in interval notation with two decimal points.",+ "method": "condense",+ "scores": {+ "diversity": 4,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Determine all values of \\( p \\) such that for every \\( q > 0 \\), the following inequality holds true:\n\n\\[\n\\frac{3(pq^3 + p^2 q^2 + 3q^3 + 3pq^2)}{p + q} - 2p^3q^2 - 5q^3 + \\frac{(p^2 + 3p + 4)q^4}{(p + q)^3} \\geq 0.\n\\]\n\nAdditionally, find the values of \\( p \\) for which the minimum of the left-hand side expression, treated as a function of \\( q \\), is non-negative for all \\( q > 0 \\). Present your answer in interval notation in decimal form.",+ "method": "condense",+ "scores": {+ "diversity": 4,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ }+ ]+ },+ {+ "0": [+ {+ "instruction": "Tim wants to invest some money in a bank which compounds quarterly with an annual interest rate of $7\\%$. To the nearest dollar, how much money should he invest if he wants a total of $\\$60,\\!000$ at the end of $5$ years?",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Tim plans to invest an amount \\( P \\) in a bank that compounds interest quarterly at an annual rate of \\( 7\\% \\). If he aims to accumulate a total of \\( \\$60,000 \\) after \\( 5 \\) years, determine the required principal \\( P \\) to the nearest dollar. Additionally, express \\( P \\) in terms of the compound interest formula \\( A = P \\left(1 + \\frac{r}{n}\\right)^{nt} \\) where \\( A \\) is the final amount, \\( r \\) is the annual interest rate, \\( n \\) is the number of times interest is compounded per year, and \\( t \\) is the number of years.",+ "method": "deepening",+ "scores": {+ "diversity": 2,+ "difficulty": 2,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Let \\( P \\) be the principal amount that Tim needs to invest, compounded quarterly at an annual interest rate of \\( r = 7\\% = 0.07 \\), over a period of \\( t = 5 \\) years. The formula for the future value \\( A \\) of an investment compounded quarterly is given by:\n\n\\[\nA = P \\left(1 + \\frac{r}{n}\\right)^{nt}\n\\]\n\nwhere \\( n \\) is the number of compounding periods per year. In this case, \\( n = 4 \\). \n\nTim desires a future value of \\( A = 60,000 \\). \n\nReformulate the problem to find the minimum principal \\( P \\) that satisfies the following equation:\n\n\\[\n60,000 = P \\left(1 + \\frac{0.07}{4}\\right)^{4 \\times 5}\n\\]\n\nWhat is the exact amount Tim should invest, rounded to the nearest dollar?",+ "method": "constraints",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "1": [+ {+ "instruction": "Let \\( P \\) be the principal amount that Tim needs to invest, compounded quarterly at an annual interest rate of \\( r = 7\\% = 0.07 \\), over a period of \\( t = 5 \\) years. The formula for the future value \\( A \\) of an investment compounded quarterly is given by:\n\n\\[\nA = P \\left(1 + \\frac{r}{n}\\right)^{nt}\n\\]\n\nwhere \\( n \\) is the number of compounding periods per year. In this case, \\( n = 4 \\). \n\nTim desires a future value of \\( A = 60,000 \\). \n\nReformulate the problem to find the minimum principal \\( P \\) that satisfies the following equation:\n\n\\[\n60,000 = P \\left(1 + \\frac{0.07}{4}\\right)^{4 \\times 5}\n\\]\n\nWhat is the exact amount Tim should invest, rounded to the nearest dollar?",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Let \\( P \\) represent the principal amount that Tim invests, compounded continuously at an annual interest rate of \\( r = 7\\% = 0.07 \\), over a period of \\( t = 5 \\) years. The formula for the future value \\( A \\) of an investment compounded continuously is given by:\n\n\\[\nA = Pe^{rt}\n\\]\n\nwhere \\( e \\) is the base of the natural logarithm. Tim requires a future value of \\( A = 60,000 \\).\n\nReformulate the problem to determine the minimum principal \\( P \\) that satisfies the following equation:\n\n\\[\n60,000 = Pe^{0.07 \\times 5}\n\\]\n\nWhat is the exact amount Tim should invest, rounded to the nearest dollar?",+ "method": "expansion",+ "scores": {+ "diversity": 4,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Let \\( P \\) represent the principal amount that Tim must invest to achieve a future value of \\( A = 60,000 \\) after \\( t = 5 \\) years, compounded quarterly at an annual interest rate of \\( r = 7\\% = 0.07 \\). The formula for the future value \\( A \\) when compounded \\( n \\) times per year is described by:\n\n\\[\nA = P \\left(1 + \\frac{r}{n}\\right)^{nt}\n\\]\n\nTo further explore the dynamics of investment growth, consider an additional complication where Tim is required to make a one-time additional contribution of \\( C = 5,000 \\) at the end of the second year. The future value \\( A' \\) with this additional contribution can be expressed as:\n\n\\[\nA' = P \\left(1 + \\frac{0.07}{4}\\right)^{4 \\cdot 5} + 5,000 \\left(1 + \\frac{0.07}{4}\\right)^{4 \\cdot 3}\n\\]\n\nDetermine the minimum principal \\( P \\) such that:\n\n\\[\n60,000 = P \\left(1 + \\frac{0.07}{4}\\right)^{20} + 5,000 \\left(1 + \\frac{0.07}{4}\\right)^{12}\n\\]\n\nWhat is the exact amount Tim should invest, rounded to the nearest dollar?",+ "method": "constraints",+ "scores": {+ "diversity": 3,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "2": [+ {+ "instruction": "Let \\( P \\) represent the principal amount that Tim invests, compounded continuously at an annual interest rate of \\( r = 7\\% = 0.07 \\), over a period of \\( t = 5 \\) years. The formula for the future value \\( A \\) of an investment compounded continuously is given by:\n\n\\[\nA = Pe^{rt}\n\\]\n\nwhere \\( e \\) is the base of the natural logarithm. Tim requires a future value of \\( A = 60,000 \\).\n\nReformulate the problem to determine the minimum principal \\( P \\) that satisfies the following equation:\n\n\\[\n60,000 = Pe^{0.07 \\times 5}\n\\]\n\nWhat is the exact amount Tim should invest, rounded to the nearest dollar?",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Let \\( P \\) be the principal amount Tim invests, compounded continuously at an annual interest rate of \\( r = 7\\% = 0.07 \\), over a variable period \\( t \\) years. The desired future value \\( A \\) is given as \\( A = 60,000 \\). The relationship governing the growth of this investment is expressed by the formula:\n\n\\[\nA = Pe^{rt}\n\\]\n\nTim aims to achieve this future value under the condition that \\( t \\) follows an exponential growth model, specifically: \\( t = 2n \\) for some integer \\( n \\). Reformulate the problem to derive the minimum principal \\( P \\) that satisfies the following equation for the smallest integer \\( n \\) such that \\( t \\geq 5 \\):\n\n\\[\n60,000 = Pe^{0.07(2n)}\n\\]\n\nDetermine the exact principal amount \\( P \\) that Tim must invest, rounded to the nearest dollar, when \\( n \\) is at its minimum value.",+ "method": "expansion",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Let \\( P \\) denote the principal amount that Tim invests, compounded continuously at an annual interest rate of \\( r = 7\\% = 0.07 \\), over a period of \\( t \\) years, where \\( t \\) is a variable representing the number of years. The relationship for the future value \\( A \\) of the investment compounded continuously is expressed as:\n\n\\[\nA = Pe^{rt}\n\\]\n\nTim desires a future value of \\( A = 60,000 \\) and needs to determine the minimum principal \\( P \\) that allows for a desired investment horizon of \\( t \\) years, given by the equation:\n\n\\[\n60,000 = Pe^{0.07t}\n\\]\n\nTo challenge the complexity further, solve for \\( P \\) in terms of \\( t \\) and determine the minimum principal \\( P \\) required when \\( t \\) is set to 10 years. What is the exact amount Tim should invest, rounded to the nearest dollar?",+ "method": "constraints",+ "scores": {+ "diversity": 3,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ],+ "3": [+ {+ "instruction": "Let \\( P \\) be the principal amount Tim invests, compounded continuously at an annual interest rate of \\( r = 7\\% = 0.07 \\), over a variable period \\( t \\) years. The desired future value \\( A \\) is given as \\( A = 60,000 \\). The relationship governing the growth of this investment is expressed by the formula:\n\n\\[\nA = Pe^{rt}\n\\]\n\nTim aims to achieve this future value under the condition that \\( t \\) follows an exponential growth model, specifically: \\( t = 2n \\) for some integer \\( n \\). Reformulate the problem to derive the minimum principal \\( P \\) that satisfies the following equation for the smallest integer \\( n \\) such that \\( t \\geq 5 \\):\n\n\\[\n60,000 = Pe^{0.07(2n)}\n\\]\n\nDetermine the exact principal amount \\( P \\) that Tim must invest, rounded to the nearest dollar, when \\( n \\) is at its minimum value.",+ "method": "original",+ "scores": {}+ },+ {+ "instruction": "Let \\( P \\) represent the principal amount that Tim invests, compounded continuously at an annual interest rate of \\( r = 7\\% = 0.07 \\). The future value \\( A \\) is given as \\( A = 60,000 \\). The relationship governing the growth of this investment is expressed by the formula:\n\n\\[\nA = Pe^{rt}\n\\]\n\nHowever, Tim faces a constraint where the time \\( t \\) is modeled as a quadratic function of an integer \\( n \\), specifically given by \\( t = n^2 + 2n \\). Your task is to reformulate the problem to find the minimum principal \\( P \\) such that \\( t \\) satisfies the condition \\( t \\geq 5 \\).\n\nSet up the equation:\n\n\\[\n60,000 = Pe^{0.07(n^2 + 2n)}\n\\]\n\nDetermine the minimum principal amount \\( P \\), rounded to the nearest dollar, when \\( n \\) is the smallest integer such that \\( t \\geq 5 \\).",+ "method": "condense",+ "scores": {+ "diversity": 3,+ "difficulty": 5,+ "validity": 5,+ "solvability": 5+ }+ },+ {+ "instruction": "Let \\( P \\) be the principal amount Tim invests, compounded continuously at an annual interest rate of \\( r = 7\\% = 0.07 \\). The desired future value \\( A \\) is \\( A = 60,000 \\). The growth of this investment is described by the equation:\n\n\\[\nA = Pe^{rt}\n\\]\n\nTim aims to achieve this future value under the condition that \\( t \\) is a piecewise function defined as follows: \n\n\\[\nt = \n\\begin{cases} \n3n & \\text{if } n \\text{ is even} \\\\\n3n + 1 & \\text{if } n \\text{ is odd}\n\\end{cases}\n\\]\n\nfor some integer \\( n \\). Reformulate the problem to derive the minimum principal \\( P \\) needed to satisfy the equation for the smallest integer \\( n \\) such that \\( t \\geq 5 \\).\n\nSpecifically, determine \\( P \\) when \\( n \\) is the smallest integer that meets this condition, ensuring that \\( A = 60,000 \\) holds true in the equation:\n\n\\[\n60,000 = Pe^{0.07t}\n\\]\n\nCalculate the exact principal amount \\( P \\) needed, rounded to the nearest dollar, based on the minimum qualifying value of \\( n \\).",+ "method": "condense",+ "scores": {+ "diversity": 4,+ "difficulty": 4,+ "validity": 5,+ "solvability": 5+ }+ }+ ]+ }+]\ No newline at end of filediff --git a/test/datagen/test_evol_instruct.py b/test/datagen/test_evol_instruct.pynew file mode 100644index 000000000..cc0106e0e--- /dev/null+++ b/test/datagen/test_evol_instruct.py@@ -0,0 +1,198 @@+# ========= 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 unittest+from typing import Any, ClassVar, Dict+from unittest.mock import MagicMock, patch++from camel.agents import ChatAgent+from camel.datagen.evol_instruct import EvolInstructPipeline+++# Dummy template class providing EVOL_METHODS and STRATEGY configuration.+class DummyTemplates:+ EVOL_METHODS: ClassVar[Dict[str, str]] = {+ "method1": "Method1 instruction",+ "method2": "Method2 instruction",+ "method3": "Method3 instruction",+ }+ STRATEGY: ClassVar[Dict[str, Dict[str, Any]]] = {+ "DEPTH": {+ "meta_instruction": "Use {method} to evolve: {prompt}",+ "methods": ["method1", "method2"],+ },+ "BREADTH": {+ "meta_instruction": "Apply {method} for expansion: {prompt}",+ "methods": ["method3"],+ },+ }+++# Dummy scorer for testing purposes.+class DummyScorer:+ def score(self, current_prompt: str, candidate: str) -> Dict[str, int]:+ # Score based on the length of the candidate prompt.+ return {"score": len(candidate)}+++class TestEvolInstructPipeline(unittest.TestCase):+ def setUp(self):+ # Create a mocked ChatAgent.+ self.mock_agent = MagicMock(spec=ChatAgent)+ self.mock_agent.reset.return_value = None+ # Simulate agent.step returning a response.+ fake_response = MagicMock()+ fake_response.msgs = [MagicMock(content="Evolved prompt")]+ self.mock_agent.step.return_value = fake_response++ self.templates = DummyTemplates+ self.pipeline = EvolInstructPipeline(+ templates=self.templates, agent=self.mock_agent+ )++ def test_resolve_evolution_method_direct(self):+ # When a key in EVOL_METHODS is provided,+ # it should return the key directly.+ self.assertEqual(+ self.pipeline._resolve_evolution_method("method1"), "method1"+ )++ def test_resolve_evolution_method_strategy(self):+ # When a strategy name is provided,+ # it should return one of the methods from that strategy.+ with patch("random.choice", return_value="method2") as mock_choice:+ resolved = self.pipeline._resolve_evolution_method("DEPTH")+ self.assertEqual(resolved, "method2")+ mock_choice.assert_called_once()++ def test_resolve_evolution_method_invalid(self):+ # When an invalid method is provided,+ # it should return a random valid method.+ with patch("random.choice", return_value="method1") as mock_choice:+ resolved = self.pipeline._resolve_evolution_method("invalid")+ self.assertEqual(resolved, "method1")+ mock_choice.assert_called_once()++ def test_get_evolution_methods_str(self):+ # When method is a strategy name,+ # it should return the strategy's defined methods.+ methods = self.pipeline._get_evolution_methods("DEPTH", 2)+ self.assertTrue(set(methods).issubset({"method1", "method2"}))+ self.assertEqual(len(methods), 2)++ def test_get_evolution_methods_list(self):+ # When method is a list,+ # it should return the parsed methods and sample to the number.+ methods = self.pipeline._get_evolution_methods(+ ["BREADTH", "method1"], 3+ )+ self.assertEqual(len(methods), 3)+ self.assertTrue(all(m in {"method1", "method3"} for m in methods))++ def test_generate_single_evolution(self):+ prompt = "Original prompt"+ # Test _generate_single_evolution returns an evolved prompt and method.+ evolved_prompt, method_used = self.pipeline._generate_single_evolution(+ prompt, "method1", return_method=True+ )+ expected_instruction = (+ "Use Method1 instruction to evolve: " "Original prompt"+ )+ self.mock_agent.reset.assert_called() # Check that reset was called.+ self.mock_agent.step.assert_called_with(expected_instruction)+ self.assertEqual(evolved_prompt, "Evolved prompt")+ self.assertEqual(method_used, "method1")++ def test_generate_multiple_evolutions_keep_original(self):+ prompt = "Test prompt"+ results = self.pipeline._generate_multiple_evolutions(+ prompt,+ "DEPTH",+ num_generations=2,+ keep_original=True,+ num_threads=1,+ )+ # When keep_original is True,+ # the first result should be the original prompt.+ self.assertEqual(len(results), 3)+ self.assertEqual(results[0][0], "Test prompt")+ self.assertEqual(results[0][1], "original")++ def test_generate_multiple_evolutions_no_original(self):+ prompt = "Test prompt"+ results = self.pipeline._generate_multiple_evolutions(+ prompt,+ "DEPTH",+ num_generations=2,+ keep_original=False,+ num_threads=1,+ )+ self.assertEqual(len(results), 2)++ def test_generate_iterative_evolutions(self):+ prompt = "Start prompt"+ scorer = DummyScorer()+ # evolution_spec as a list, with two iterations:+ # first DEPTH, then BREADTH.+ results = self.pipeline._generate_iterative_evolutions(+ prompt,+ evolution_spec=["DEPTH", "BREADTH"],+ num_generations=2,+ num_iterations=2,+ keep_original=True,+ scorer=scorer,+ num_threads=1,+ )+ # Check the number of iterations.+ self.assertEqual(len(results), 2)+ # The first element of each iteration should be the original prompt+ # or the best candidate.+ self.assertEqual(results[0][0]["instruction"], "Start prompt")+ self.assertIsInstance(results[1][0]["instruction"], str)++ def test_generate_batch(self):+ # Test the generate method handling multiple prompts.+ prompts = ["Prompt 1", "Prompt 2"]+ # To ensure test stability, patch _generate_iterative_evolutions to+ # return a fixed result.+ dummy_result = {+ 0: [+ {+ "instruction": "Evo",+ "method": "method1",+ "scores": {"score": 10},+ }+ ]+ }+ with patch.object(+ self.pipeline,+ "_generate_iterative_evolutions",+ return_value=dummy_result,+ ) as mock_iter:+ results = self.pipeline.generate(+ prompts,+ "DEPTH",+ num_generations=2,+ num_iterations=1,+ keep_original=False,+ num_threads=1,+ )+ self.assertEqual(len(results), 2)+ for res in results:+ self.assertEqual(res, dummy_result)+ self.assertEqual(mock_iter.call_count, len(prompts))+++if __name__ == "__main__":+ unittest.main()