CAMEL’s data generation modules for high-quality, instruction-tuned, and reasoning-rich datasets.
This page introduces CAMEL’s data generation modules for creating high-quality training data with explicit reasoning, diverse instructions, and advanced automated refinement.
Chain of Thought (CoT): Generates explicit reasoning paths
Self-Instruct: Produces instruction-following data from both humans and machines
Source2Synth: Synthesizes multi-hop QA from source text or code
Self-Improving CoT: Iteratively improves reasoning through agent self-critique
Chain of Thought (CoT) data generation creates step-by-step reasoning paths for problem solving, leveraging dual agents and advanced search/verification logic.
Key Features
Monte Carlo Tree Search (MCTS) for solution exploration
Binary Search Error Detection for precise error localization
Dual-Agent Verification System for quality assurance
Solution Tree Management for tracking reasoning paths
Core Components
CoTDataGenerator ClassThe main class that implements the CoT generation system with the following capabilities:
Dual-Agent Architecture: Supports both single-agent (legacy) and dual-agent modes
Answer Generation: Sophisticated answer generation with MCTS
Answer Verification: Robust verification system using golden answers
Error Detection: Binary search-based error detection in solutions
Solution Management: Comprehensive solution tree management and export
Quick Start: CoT Data Generation
Spin up chain-of-thought data generation with dual agents, golden answers, and CoT solution generation:
Copy
from camel.agents import ChatAgentfrom camel.datagen import CoTDataGenerator# Initialize agentsgenerator_agent = ChatAgent("System message for generator")verifier_agent = ChatAgent("System message for verifier")# Define golden answersgolden_answers = { "question1": "answer1", "question2": "answer2"}# Create generatorcot_generator = CoTDataGenerator( generator_agent=generator_agent, verifier_agent=verifier_agent, golden_answers=golden_answers, search_limit=100)# Generate solutionsolution = cot_generator.solve("question1")
Data Import/Export for CoT
Easily import question-answer pairs or export generated solutions for further use:
Copy
# Import QA pairs from JSONcot_generator.import_qa_from_json("qa_pairs.json")# Export solutionscot_generator.export_solutions("solutions.json")
Solution Generation Process
1
Direct Solution Attempt
First, the agent attempts to solve the problem directly and checks the result against the golden answer for correctness.
2
MCTS-Based Exploration
If the direct attempt fails, a Monte Carlo Tree Search (MCTS) explores alternative reasoning paths, building a solution tree from previous attempts.
3
Error Detection & Correction
Binary search is used to efficiently pinpoint and isolate errors in the solution. New solutions are then generated, reusing verified-correct parts.
4
Solution Verification
All candidate solutions are strictly verified using a dual-agent system or comparison against golden answers to ensure high quality and accuracy.
Configuration Options
search_limit: Maximum number of search iterations (default: 100)
generator_agent: Specialized agent for answer generation
verifier_agent: Specialized agent for answer verification
golden_answers: Pre-defined correct answers for validation
Output Format
The solution tree is exported in JSON format containing:
Self-Instruct is a pipeline for generating high-quality, diverse instructions by combining human-written seed tasks and machine-generated prompts, all filtered for quality and diversity.
Key Features
Combines human-written and machine-generated instructions using configurable ratios
Supports both classification and non-classification task types
Built-in instruction filtering and validation
Automatic instance generation for tasks
JSON-based data input/output
Core Components
SelfInstructPipeline – Orchestrates the end-to-end instruction generation, mixing seeds and machine prompts, filtering, and outputting results.
InstructionFilter – Handles validation and filtering of all generated instructions:
Length-based, keyword, and punctuation checks
Non-English text detection
ROUGE similarity filtering for deduplication
Extensible registry for custom filters
Quick Start: Self-Instruct Generation
Quickly set up an instruction generation pipeline with both human and machine prompts:
Source2Synth generates complex multi-hop QA pairs from source text (or code) via an orchestrated pipeline of AI-driven and rule-based steps, with curation and complexity control.
Core Components
UserDataProcessor: Orchestrates the full pipeline, from raw text through QA generation and curation.
This pipeline implements self-taught reasoning—an iterative process where an AI agent refines its own reasoning traces via self-evaluation, feedback, and reward models for continual improvement.
Key Components
SelfImprovingCoTPipeline: Implements the STaR (Self-Taught Reasoning) methodology, supporting both agent-based and external reward model evaluation, iterative feedback loops, and flexible output formats.
Customizable reasoning and evaluation agents
Support for reward models and custom thresholds
Few-shot learning and rich output options
Architecture Stages
1
Initial Reasoning Trace Generation
The pipeline generates an initial reasoning path for each problem using the designated agent.
2
Self-Evaluation
An evaluator agent (or reward model) critically reviews each reasoning trace for quality, clarity, and correctness.
3
Feedback-Based Improvement
The system refines and re-generates reasoning steps using the evaluation feedback.
4
Iterative Refinement
This evaluation-feedback loop is repeated for a configurable number of iterations to reach optimal performance.
Quick Start: Self-Improving CoT Pipeline
Launch a self-improving reasoning workflow with just a few lines:
Copy
from camel.agents import ChatAgentfrom camel.datagen import SelfImprovingCoTPipeline# Initialize agentsreason_agent = ChatAgent( """Answer my question and give your final answer within \\boxed{}.""")evaluate_agent = ChatAgent( "You are a highly critical teacher who evaluates the student's answers " "with a meticulous and demanding approach.")# Prepare your problemsproblems = [ {"problem": "Your problem text here"}, # Add more problems...]# Create and run the pipelinepipeline = SelfImprovingCoTPipeline( reason_agent=reason_agent, evaluate_agent=evaluate_agent, problems=problems, max_iterations=3, output_path="star_output.json")results = pipeline.generate()
Advanced: External Reward Model Integration
Evaluate and guide reasoning traces with an external reward model, such as Nemotron: