camel.datagen package#
Subpackages#
- camel.datagen.self_instruct package
- Subpackages
- Submodules
- camel.datagen.self_instruct.self_instruct module
AgentResponse
SelfInstructPipeline
SelfInstructPipeline.construct_data()
SelfInstructPipeline.generate()
SelfInstructPipeline.generate_machine_instance()
SelfInstructPipeline.generate_machine_instances()
SelfInstructPipeline.generate_machine_instruction()
SelfInstructPipeline.identify_instruction()
SelfInstructPipeline.load_seed()
SelfInstructPipeline.parse_classification_output()
SelfInstructPipeline.parse_non_classification_output()
SelfInstructPipeline.sample_human_tasks()
SelfInstructPipeline.sample_machine_tasks()
- camel.datagen.self_instruct.templates module
- Module contents
FilterFunction
InstructionFilter
KeywordFilter
LengthFilter
NonEnglishFilter
PunctuationFilter
RougeSimilarityFilter
SelfInstructPipeline
SelfInstructPipeline.construct_data()
SelfInstructPipeline.generate()
SelfInstructPipeline.generate_machine_instance()
SelfInstructPipeline.generate_machine_instances()
SelfInstructPipeline.generate_machine_instruction()
SelfInstructPipeline.identify_instruction()
SelfInstructPipeline.load_seed()
SelfInstructPipeline.parse_classification_output()
SelfInstructPipeline.parse_non_classification_output()
SelfInstructPipeline.sample_human_tasks()
SelfInstructPipeline.sample_machine_tasks()
- camel.datagen.source2synth package
- Submodules
- camel.datagen.source2synth.data_processor module
- camel.datagen.source2synth.models module
ContextPrompt
MultiHopQA
MultiHopQA.question
MultiHopQA.reasoning_steps
MultiHopQA.answer
MultiHopQA.supporting_facts
MultiHopQA.type
MultiHopQA.Config
MultiHopQA.answer
MultiHopQA.model_computed_fields
MultiHopQA.model_config
MultiHopQA.model_fields
MultiHopQA.question
MultiHopQA.reasoning_steps
MultiHopQA.supporting_facts
MultiHopQA.type
ReasoningStep
- camel.datagen.source2synth.user_data_processor_config module
ProcessorConfig
ProcessorConfig.complexity_threshold
ProcessorConfig.dataset_size
ProcessorConfig.hop_generating_agent
ProcessorConfig.max_length
ProcessorConfig.min_length
ProcessorConfig.model_computed_fields
ProcessorConfig.model_config
ProcessorConfig.model_fields
ProcessorConfig.seed
ProcessorConfig.use_ai_model
- Module contents
DataCurator
ExampleConstructor
MultiHopQA
MultiHopQA.question
MultiHopQA.reasoning_steps
MultiHopQA.answer
MultiHopQA.supporting_facts
MultiHopQA.type
MultiHopQA.Config
MultiHopQA.answer
MultiHopQA.model_computed_fields
MultiHopQA.model_config
MultiHopQA.model_fields
MultiHopQA.question
MultiHopQA.reasoning_steps
MultiHopQA.supporting_facts
MultiHopQA.type
ProcessorConfig
ProcessorConfig.complexity_threshold
ProcessorConfig.dataset_size
ProcessorConfig.hop_generating_agent
ProcessorConfig.max_length
ProcessorConfig.min_length
ProcessorConfig.model_computed_fields
ProcessorConfig.model_config
ProcessorConfig.model_fields
ProcessorConfig.seed
ProcessorConfig.use_ai_model
ReasoningStep
UserDataProcessor
Submodules#
camel.datagen.cot_datagen module#
- class camel.datagen.cot_datagen.AgentResponse(*, score: float)[source]#
Bases:
BaseModel
Model for structured agent responses.
A Pydantic model class that represents structured responses from agents, including a similarity score that measures the quality of the response.
- Parameters:
score (float) β A similarity score between 0 and 1 that compares the current answer to the correct answer. Must be within the range [0, 1].
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'score': FieldInfo(annotation=float, required=True, description='Similarity score between 0 and 1 \n comparing current answer to correct answer', metadata=[typing.Annotated[float, None, Interval(gt=None, ge=0, lt=None, le=1), None, None]])}#
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- score: float#
- class camel.datagen.cot_datagen.CoTDataGenerator(chat_agent: ChatAgent | None = None, *, generator_agent: ChatAgent | None = None, verifier_agent: ChatAgent | None = None, golden_answers: Dict[str, str], search_limit: int = 100)[source]#
Bases:
object
Class for generating and managing data through chat agent interactions.
This module implements a sophisticated Chain of Thought data generation system that combines several key algorithms to produce high-quality reasoning paths. Methods implemented:
Monte Carlo Tree Search (MCTS)
Binary Search Error Detection
Dual-Agent Verification System
Solution Tree Management
- Parameters:
chat_agent (Optional[ChatAgent]) β Optional single agent for both tasks (legacy mode). (default:
None
)generator_agent (Optional[ChatAgent]) β Optional specialized agent for answer generation. (default:
None
)verifier_agent (Optional[ChatAgent]) β Optional specialized agent for answer verification. (default:
None
)golden_answers (Dict[str, str]) β Dictionary containing pre-defined correct answers for validation and comparison. Required for answer verification.
search_limit (int) β Maximum number of search iterations allowed. (default:
100
)
- binary_search_error(question: str, solution: str) int [source]#
Use binary search to locate the first error in the solution. This method splits the solution into sentences using both English and Chinese sentence delimiters and performs binary search to find the first error.
- Parameters:
question (str) β The question being solved.
solution (str) β The complete solution to analyze.
- Returns:
- The position of the first error found in the solution.
Returns -1. If no errors are found (all sentences are correct).
- Return type:
int
- export_solutions(filepath: str = 'solutions.json') None [source]#
Export the solution process and results to a JSON file. Exports the solution tree, golden answers,
and export timestamp to a JSON file.
The exported data includes: - solutions: The solution tree
with intermediate steps
golden_answers: The reference answers used for verification
export_time: ISO format timestamp of the export
- Parameters:
filepath (str, optional) β Path where the JSON file will be saved. (default:
'solutions.json'
)- Returns:
- The method writes to a file and logs the result but does not
return any value.
- Return type:
None
- get_answer(question: str, context: str = '') str [source]#
Get an answer from the chat agent for a given question.
- Parameters:
question (str) β The question to ask.
context (str) β Additional context for the question. (default:
""
)
- Returns:
The generated answer.
- Return type:
str
- import_qa_from_json(data: str | Dict[str, str]) bool [source]#
Import question and answer data from either a JSON file or a dictionary.
- Parameters:
data (Union[str, Dict[str, str]]) β
Either a path to a JSON file containing QA pairs or a dictionary of question-answer pairs. If a string is provided, itβs treated as a file path. The expected format is: {βquestion1β: βanswer1β,
βquestion2β: βanswer2β, β¦}
- Returns:
True if import was successful, False otherwise.
- Return type:
bool
- monte_carlo_tree_search(question: str, partial_solution: str = '') float [source]#
Perform Monte Carlo Tree Search to find the best solution.
Process: a. Selection: Choose promising partial solutions based on previous scores b. Expansion: Generate new solution steps using the generator agent c. Simulation: Evaluate solution quality using similarity scores d. Backpropagation: Update solution tree with new findings
- Parameters:
question (str) β The question to solve.
partial_solution (str) β The current partial solution. (default:
""
)
- Returns:
- The similarity score between the current
solution and golden answer.
- Return type:
float
- solve(question: str) str [source]#
Solve a question using a multi-step approach.
The solution process follows these steps: 1. Try to solve directly - if correct, return the solution 2. If not correct, use Monte Carlo Tree Search to find a good solution 3. If the solution isnβt perfect, use binary search to locate errors 4. Generate a new solution based on the correct part
- Parameters:
question (str) β The question to solve.
- Returns:
The best solution found.
- Return type:
str
- verify_answer(question: str, answer: str) bool [source]#
Verify if a generated answer is semantically equivalent to the golden answer for a given question.
- Parameters:
question (str) β The question being answered.
answer (str) β The answer to verify.
- Returns:
- True if the answer matches the golden answer based on
semantic equivalence (meaning the core content and meaning are the same, even if the exact wording differs). False in the following cases: - If the provided question doesnβt exist in the golden answers - If the answerβs meaning differs from the golden answer
- Return type:
bool
- class camel.datagen.cot_datagen.VerificationResponse(*, is_correct: bool)[source]#
Bases:
BaseModel
Model for structured verification responses.
A Pydantic model class that represents verification results from agents, indicating whether an answer is correct or not.
- Parameters:
is_correct (bool) β Boolean indicating if the answer is correct.
- is_correct: bool#
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'is_correct': FieldInfo(annotation=bool, required=True, description='Boolean indicating if the answer is correct')}#
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
camel.datagen.self_improving_cot module#
- class camel.datagen.self_improving_cot.AgentTraceEvaluation(*, correctness: float, clarity: float, completeness: float, feedback: str)[source]#
Bases:
BaseModel
- clarity: float#
- completeness: float#
- correctness: float#
- feedback: str#
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'clarity': FieldInfo(annotation=float, required=True), 'completeness': FieldInfo(annotation=float, required=True), 'correctness': FieldInfo(annotation=float, required=True), 'feedback': FieldInfo(annotation=str, required=True)}#
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- class camel.datagen.self_improving_cot.ProblemResult(*, id: str | None = None, type: str | None = None, problem: str, solution: str | None = None, final_trace: str, agent_evaluate_success: bool | None = None, boxed_answer_success: bool = False, improvement_history: List[TraceIteration])[source]#
Bases:
BaseModel
- agent_evaluate_success: bool | None#
- boxed_answer_success: bool#
- final_trace: str#
- id: str | None#
- improvement_history: List[TraceIteration]#
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'agent_evaluate_success': FieldInfo(annotation=Union[bool, NoneType], required=False, default=None), 'boxed_answer_success': FieldInfo(annotation=bool, required=False, default=False), 'final_trace': FieldInfo(annotation=str, required=True), 'id': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'improvement_history': FieldInfo(annotation=List[TraceIteration], required=True), 'problem': FieldInfo(annotation=str, required=True), 'solution': FieldInfo(annotation=Union[str, NoneType], required=False, default=None), 'type': FieldInfo(annotation=Union[str, NoneType], required=False, default=None)}#
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- problem: str#
- solution: str | None#
- type: str | None#
- class camel.datagen.self_improving_cot.RewardTraceEvaluation(*, feedback: str, **data)[source]#
Bases:
BaseModel
- feedback: str#
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {'extra': 'allow'}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'feedback': FieldInfo(annotation=str, required=True)}#
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- class camel.datagen.self_improving_cot.SelfImprovingCoTPipeline(reason_agent: ChatAgent, problems: List[Dict], max_iterations: int = 3, score_threshold: float | Dict[str, float] = 0.7, rejection_sampling_n: int | None = None, evaluate_agent: ChatAgent | None = None, reward_model: BaseRewardModel | None = None, output_path: str | None = None, few_shot_examples: str | None = None, batch_size: int | None = None, max_workers: int | None = None, solution_pattern: str = '\\\\boxed{(.*?)}', trace_pattern: str | None = None)[source]#
Bases:
object
Pipeline for generating self-taught reasoning traces using the self-improving methodology.
This implements the STaR paperβs approach of: 1. Initial reasoning trace generation 2. Self-evaluation 3. Feedback-based improvement 4. Iterative refinement
- EVALUATION_TEMPLATE = 'Please evaluate this reasoning trace and \nprovide scores and feedback in valid JSON format.\n\nProblem: {problem}\n\n{solution}\n\nReasoning Trace:\n{trace}\n\nEvaluate for:\n1. Correctness (Is each step logically sound?)\n2. Clarity (Is the explanation clear and well-structured?)\n3. Completeness (Are all necessary steps included?)\n\nRespond ONLY with a JSON object in this exact format:\n{{\n "correctness": <score between 0 and 1>,\n "clarity": <score between 0 and 1>,\n "completeness": <score between 0 and 1>,\n "feedback": "<specific feedback for improvement>"\n}}'#
- IMPROVEMENT_TEMPLATE = 'Based on this feedback, generate an \nimproved reasoning trace:\nProblem: {problem}\n\n{solution}\n\nPrevious Trace:\n{trace}\n\nFeedback:\n{feedback}\n\nGenerate a new, improved reasoning trace that addresses the feedback.'#
- REASONING_TEMPLATE = "Let's solve this step by step:\nProblem: {problem}\n1. First, let's understand what we're asked\n2. Let's break this down into parts\n3. Let's solve each part systematically\n4. Finally, let's verify our solution\n\n{few_shot_examples}\n\nPlease show your complete reasoning process."#
- evaluate_trace(problem: str, trace: str, solution: str | None = None) Dict[str, Any] [source]#
Evaluate the quality of a reasoning trace.
- Parameters:
problem (str) β The original problem text to evaluate against.
trace (str) β The reasoning trace to evaluate.
solution (Optional[str]) β The solution to the problem, if provided. (default:
None
)
- Returns:
- Evaluation results containing:
scores: Dict of evaluation dimensions and their scores
feedback: Detailed feedback for improvement
For Agent self-evaluation, the scores will include: - correctness: Score for logical correctness - clarity: Score for clarity of explanation - completeness: Score for completeness of reasoning
For reward model evaluation, the scores will depend on the modelβs evaluation dimensions.
- Return type:
Dict[str, Any]
- generate(rationalization: bool = False) List[Dict[str, Any]] [source]#
Execute the self-improving cot pipeline on all problems.
Process problems and return results. If output_path is specified, also save results to file.
- Parameters:
rationalization (bool, optional) β Whether to use rationalization. (default:
False
)- Returns:
List of processed results
- Return type:
List[Dict[str, Any]]
- generate_reasoning_trace(problem: str) str [source]#
Generate initial reasoning trace for a given problem.
- Parameters:
problem (str) β The problem text to generate reasoning for.
- Returns:
Generated reasoning trace.
- Return type:
str
- generate_reasoning_trace_rejection(problem: str) str [source]#
Generate multiple candidate reasoning traces for a problem and select the best one based on evaluation.
- Parameters:
problem (str) β The problem text for generating a reasoning trace.
- Returns:
- The best candidate trace that meets quality criteria, or the
first candidate if none qualify.
- Return type:
str
- improve_trace(problem: str, trace: str, feedback: str, solution: str | None = None) str [source]#
Generate improved reasoning trace based on feedback.
- Parameters:
problem (str) β The original problem text.
trace (str) β The current reasoning trace.
feedback (str) β Feedback for improving the trace.
solution (Optional[str]) β The solution to the problem, if provided. (default:
None
)
- Returns:
Improved reasoning trace.
- Return type:
str
- process_problem(problem: Dict, rationalization: bool = False) ProblemResult [source]#
Process a single problem through the self-improving cot pipeline.
- Parameters:
problem (Dict) β Problem dictionary containing the problem text.
rationalization (bool, optional) β Whether to use rationalization. (default:
False
)
- Returns:
Results with final trace and history.
- Return type:
- Raises:
ValueError β If the problem format is invalid.
- class camel.datagen.self_improving_cot.TraceIteration(*, iteration: int, trace: str, evaluation: AgentTraceEvaluation | RewardTraceEvaluation)[source]#
Bases:
BaseModel
- evaluation: AgentTraceEvaluation | RewardTraceEvaluation#
- iteration: int#
- model_computed_fields: ClassVar[Dict[str, ComputedFieldInfo]] = {}#
A dictionary of computed field names and their corresponding ComputedFieldInfo objects.
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- model_fields: ClassVar[Dict[str, FieldInfo]] = {'evaluation': FieldInfo(annotation=Union[AgentTraceEvaluation, RewardTraceEvaluation], required=True), 'iteration': FieldInfo(annotation=int, required=True), 'trace': FieldInfo(annotation=str, required=True)}#
Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo] objects.
This replaces Model.__fields__ from Pydantic V1.
- trace: str#
Module contents#
- class camel.datagen.CoTDataGenerator(chat_agent: ChatAgent | None = None, *, generator_agent: ChatAgent | None = None, verifier_agent: ChatAgent | None = None, golden_answers: Dict[str, str], search_limit: int = 100)[source]#
Bases:
object
Class for generating and managing data through chat agent interactions.
This module implements a sophisticated Chain of Thought data generation system that combines several key algorithms to produce high-quality reasoning paths. Methods implemented:
Monte Carlo Tree Search (MCTS)
Binary Search Error Detection
Dual-Agent Verification System
Solution Tree Management
- Parameters:
chat_agent (Optional[ChatAgent]) β Optional single agent for both tasks (legacy mode). (default:
None
)generator_agent (Optional[ChatAgent]) β Optional specialized agent for answer generation. (default:
None
)verifier_agent (Optional[ChatAgent]) β Optional specialized agent for answer verification. (default:
None
)golden_answers (Dict[str, str]) β Dictionary containing pre-defined correct answers for validation and comparison. Required for answer verification.
search_limit (int) β Maximum number of search iterations allowed. (default:
100
)
- binary_search_error(question: str, solution: str) int [source]#
Use binary search to locate the first error in the solution. This method splits the solution into sentences using both English and Chinese sentence delimiters and performs binary search to find the first error.
- Parameters:
question (str) β The question being solved.
solution (str) β The complete solution to analyze.
- Returns:
- The position of the first error found in the solution.
Returns -1. If no errors are found (all sentences are correct).
- Return type:
int
- export_solutions(filepath: str = 'solutions.json') None [source]#
Export the solution process and results to a JSON file. Exports the solution tree, golden answers,
and export timestamp to a JSON file.
The exported data includes: - solutions: The solution tree
with intermediate steps
golden_answers: The reference answers used for verification
export_time: ISO format timestamp of the export
- Parameters:
filepath (str, optional) β Path where the JSON file will be saved. (default:
'solutions.json'
)- Returns:
- The method writes to a file and logs the result but does not
return any value.
- Return type:
None
- get_answer(question: str, context: str = '') str [source]#
Get an answer from the chat agent for a given question.
- Parameters:
question (str) β The question to ask.
context (str) β Additional context for the question. (default:
""
)
- Returns:
The generated answer.
- Return type:
str
- import_qa_from_json(data: str | Dict[str, str]) bool [source]#
Import question and answer data from either a JSON file or a dictionary.
- Parameters:
data (Union[str, Dict[str, str]]) β
Either a path to a JSON file containing QA pairs or a dictionary of question-answer pairs. If a string is provided, itβs treated as a file path. The expected format is: {βquestion1β: βanswer1β,
βquestion2β: βanswer2β, β¦}
- Returns:
True if import was successful, False otherwise.
- Return type:
bool
- monte_carlo_tree_search(question: str, partial_solution: str = '') float [source]#
Perform Monte Carlo Tree Search to find the best solution.
Process: a. Selection: Choose promising partial solutions based on previous scores b. Expansion: Generate new solution steps using the generator agent c. Simulation: Evaluate solution quality using similarity scores d. Backpropagation: Update solution tree with new findings
- Parameters:
question (str) β The question to solve.
partial_solution (str) β The current partial solution. (default:
""
)
- Returns:
- The similarity score between the current
solution and golden answer.
- Return type:
float
- solve(question: str) str [source]#
Solve a question using a multi-step approach.
The solution process follows these steps: 1. Try to solve directly - if correct, return the solution 2. If not correct, use Monte Carlo Tree Search to find a good solution 3. If the solution isnβt perfect, use binary search to locate errors 4. Generate a new solution based on the correct part
- Parameters:
question (str) β The question to solve.
- Returns:
The best solution found.
- Return type:
str
- verify_answer(question: str, answer: str) bool [source]#
Verify if a generated answer is semantically equivalent to the golden answer for a given question.
- Parameters:
question (str) β The question being answered.
answer (str) β The answer to verify.
- Returns:
- True if the answer matches the golden answer based on
semantic equivalence (meaning the core content and meaning are the same, even if the exact wording differs). False in the following cases: - If the provided question doesnβt exist in the golden answers - If the answerβs meaning differs from the golden answer
- Return type:
bool
- class camel.datagen.SelfImprovingCoTPipeline(reason_agent: ChatAgent, problems: List[Dict], max_iterations: int = 3, score_threshold: float | Dict[str, float] = 0.7, rejection_sampling_n: int | None = None, evaluate_agent: ChatAgent | None = None, reward_model: BaseRewardModel | None = None, output_path: str | None = None, few_shot_examples: str | None = None, batch_size: int | None = None, max_workers: int | None = None, solution_pattern: str = '\\\\boxed{(.*?)}', trace_pattern: str | None = None)[source]#
Bases:
object
Pipeline for generating self-taught reasoning traces using the self-improving methodology.
This implements the STaR paperβs approach of: 1. Initial reasoning trace generation 2. Self-evaluation 3. Feedback-based improvement 4. Iterative refinement
- EVALUATION_TEMPLATE = 'Please evaluate this reasoning trace and \nprovide scores and feedback in valid JSON format.\n\nProblem: {problem}\n\n{solution}\n\nReasoning Trace:\n{trace}\n\nEvaluate for:\n1. Correctness (Is each step logically sound?)\n2. Clarity (Is the explanation clear and well-structured?)\n3. Completeness (Are all necessary steps included?)\n\nRespond ONLY with a JSON object in this exact format:\n{{\n "correctness": <score between 0 and 1>,\n "clarity": <score between 0 and 1>,\n "completeness": <score between 0 and 1>,\n "feedback": "<specific feedback for improvement>"\n}}'#
- IMPROVEMENT_TEMPLATE = 'Based on this feedback, generate an \nimproved reasoning trace:\nProblem: {problem}\n\n{solution}\n\nPrevious Trace:\n{trace}\n\nFeedback:\n{feedback}\n\nGenerate a new, improved reasoning trace that addresses the feedback.'#
- REASONING_TEMPLATE = "Let's solve this step by step:\nProblem: {problem}\n1. First, let's understand what we're asked\n2. Let's break this down into parts\n3. Let's solve each part systematically\n4. Finally, let's verify our solution\n\n{few_shot_examples}\n\nPlease show your complete reasoning process."#
- evaluate_trace(problem: str, trace: str, solution: str | None = None) Dict[str, Any] [source]#
Evaluate the quality of a reasoning trace.
- Parameters:
problem (str) β The original problem text to evaluate against.
trace (str) β The reasoning trace to evaluate.
solution (Optional[str]) β The solution to the problem, if provided. (default:
None
)
- Returns:
- Evaluation results containing:
scores: Dict of evaluation dimensions and their scores
feedback: Detailed feedback for improvement
For Agent self-evaluation, the scores will include: - correctness: Score for logical correctness - clarity: Score for clarity of explanation - completeness: Score for completeness of reasoning
For reward model evaluation, the scores will depend on the modelβs evaluation dimensions.
- Return type:
Dict[str, Any]
- generate(rationalization: bool = False) List[Dict[str, Any]] [source]#
Execute the self-improving cot pipeline on all problems.
Process problems and return results. If output_path is specified, also save results to file.
- Parameters:
rationalization (bool, optional) β Whether to use rationalization. (default:
False
)- Returns:
List of processed results
- Return type:
List[Dict[str, Any]]
- generate_reasoning_trace(problem: str) str [source]#
Generate initial reasoning trace for a given problem.
- Parameters:
problem (str) β The problem text to generate reasoning for.
- Returns:
Generated reasoning trace.
- Return type:
str
- generate_reasoning_trace_rejection(problem: str) str [source]#
Generate multiple candidate reasoning traces for a problem and select the best one based on evaluation.
- Parameters:
problem (str) β The problem text for generating a reasoning trace.
- Returns:
- The best candidate trace that meets quality criteria, or the
first candidate if none qualify.
- Return type:
str
- improve_trace(problem: str, trace: str, feedback: str, solution: str | None = None) str [source]#
Generate improved reasoning trace based on feedback.
- Parameters:
problem (str) β The original problem text.
trace (str) β The current reasoning trace.
feedback (str) β Feedback for improving the trace.
solution (Optional[str]) β The solution to the problem, if provided. (default:
None
)
- Returns:
Improved reasoning trace.
- Return type:
str
- process_problem(problem: Dict, rationalization: bool = False) ProblemResult [source]#
Process a single problem through the self-improving cot pipeline.
- Parameters:
problem (Dict) β Problem dictionary containing the problem text.
rationalization (bool, optional) β Whether to use rationalization. (default:
False
)
- Returns:
Results with final trace and history.
- Return type:
- Raises:
ValueError β If the problem format is invalid.
- reasoning_traces: List[Dict[str, Any]]#
- class camel.datagen.SelfInstructPipeline(agent: ChatAgent, seed: str, num_machine_instructions: int = 5, data_output_path: str | None = './data_output.json', human_to_machine_ratio: tuple = (6, 2), instruction_filter: InstructionFilter | None = None, filter_config: Dict[str, Dict[str, Any]] | None = None, stop_on_first_failure: bool = False)[source]#
Bases:
object
A pipeline to generate and manage machine-generated instructions for tasks, combining human and machine task samples.
- Parameters:
agent (ChatAgent) β The agent used to interact and generate instructions.
seed (str) β The path to the human-written instructions.
num_machine_instructions (int) β Number of machine-generated instructions to generate. (default:
5
)data_output_path (Optional[str]) β Path to save the generated data. (default:
/data_output.json
)human_to_machine_ratio (tuple) β Ratio of human to machine tasks used for instruction generation. (default:
(6, 2)
)instruction_filter (InstructionFilter) β A filter to validate generated instructions. (default:
None
)filter_config (Optional[Dict[str, Dict[str, Any]]]) β configuration for the filter functions registered in FILE_REGISTRY. (default:
None
)stop_on_first_failure (bool) β If True, stops checking filters after the first failure.
- construct_data()[source]#
Save the machine-generated tasks to the specified output path in JSON format.
- generate(timeout_minutes=600)[source]#
Execute the entire pipeline to generate machine instructions and instances.
- Parameters:
timeout_minutes (int) β Maximum time in minutes to run the generation process before timing out. (default:
600
)
- generate_machine_instance(instruction: str, classification: bool) list[dict] [source]#
Generate instances for a given instruction.
- Parameters:
instruction (str) β The instruction to create instances for.
classification (bool) β Whether the instruction is a classification task.
- Returns:
A list of generated instances in input-output format.
- Return type:
List[dict]
- generate_machine_instances()[source]#
Generate instances for each machine task based on its classification status.
- generate_machine_instruction() List [source]#
Generate a machine instruction using the agent.
- Combines human and machine tasks based on the configured ratio to
create a prompt for instruction generation.
- Returns:
The prompt and a machine-generated instruction.
- Return type:
List
- identify_instruction(instruction: str) bool [source]#
Determine if the given instruction is a classification task.
- Parameters:
instruction (str) β The instruction to classify.
- Returns:
- True if the instruction is a classification task,
otherwise False.
- Return type:
bool
- load_seed(path: str)[source]#
Load seed tasks from a file. Defaults to a predefined seed file if no path is provided.
- Parameters:
path (str) β Path to the seed file.
- Raises:
FileNotFoundError β If the seed file does not exist.
- parse_classification_output(generated_text: str) List[Dict[str, str]] [source]#
Parse the generated text for classification tasks into input-output pairs.
- Parameters:
generated_text (str) β The raw text generated by the agent for classification tasks.
- Returns:
- A list of dictionaries with βinputβ and
βoutputβ keys.
- Return type:
List[Dict[str, str]]
- parse_non_classification_output(generated_text: str) List[Dict[str, str]] [source]#
Parse the generated text for non-classification tasks into input-output pairs.
- Parameters:
generated_text (str) β The raw text generated by the agent for non-classification tasks.
- Returns:
- A list of dictionaries with βinputβ and
βoutputβ keys.
- Return type:
List[Dict[str, str]]