Chain-of-Thought (CoT) Generation is a process where AI agents create detailed, step-by-step reasoning paths for solving problems. This involves generating questions, evaluating them, producing detailed solution steps, and making the data available for training and analysis.
from praisonaiagents import Agent, Task, AgentTeamfrom praisonaiagents import cot_save, cot_upload_to_huggingfacefrom pydantic import BaseModelimport os# Define Pydantic model for structured outputclass DecisionModel(BaseModel): response: str decision: strdef write_csv(file_path, data): """Write data to CSV file.""" if not os.path.exists(file_path): with open(file_path, 'w') as file: file.write(data + '\n') else: with open(file_path, 'a') as file: file.write(data + '\n') return f"Data appended to {file_path}"def count_questions(file_path): """Count lines in file.""" with open(file_path, 'r') as file: return sum(1 for _ in file)# Create specialized agentsqa_generator = Agent( name="Generator", role="Question Creator", goal="Create challenging math and logic questions", backstory="Expert in educational content creation", llm="gpt-4o-mini", tools=[write_csv, count_questions])total_questions_evaluator = Agent( name="TotalQuestionsEvaluator", role="Total Questions Evaluator", goal="Evaluate the total number of questions in qa_pairs.csv file", backstory="Expert in evaluating the total number of questions in a file", llm="gpt-4o-mini", tools=[count_questions], output="silent")cot_generator = Agent( name="COTGenerator", role="Chain of Thought Specialist", goal="Generate and manage chain of thought solutions for Q&A pairs", backstory="Expert in breaking down problems and generating detailed solution steps", tools=[cot_save], llm="gpt-4o-mini", output="silent")upload_to_huggingface = Agent( name="UploadToHuggingface", role="Upload to Huggingface", goal="Upload the generated chain of thought solutions to a Huggingface dataset", backstory="Expert in saving data to Huggingface", tools=[cot_upload_to_huggingface], llm="gpt-4o-mini", output="silent")# Create workflow with repeat pattern for generationfrom praisonaiagents import AgentFlow, Task, WorkflowContext, StepResultfrom praisonaiagents import repeat, loop# Step handlers using agentsdef generate_qa(ctx: WorkflowContext) -> StepResult: result = qa_generator.chat("""Generate question and answer in csv format: question, answer Generate 10 unique questions and answers. Example: What is the sum of numbers from 1 to 10?, 55 Number of r's in the word strawberry, 3""") write_csv("qa_pairs.csv", result) return StepResult(output=result)def evaluate_count(ctx: WorkflowContext) -> StepResult: count = count_questions("qa_pairs.csv") return StepResult( output=f"count: {count}", variables={"question_count": count} )def generate_cot(ctx: WorkflowContext) -> StepResult: result = cot_generator.chat(f"Generate chain of thought for: {ctx.variables.get('current_item')}") cot_save(result) return StepResult(output=result)def upload_dataset(ctx: WorkflowContext) -> StepResult: result = upload_to_huggingface.chat("Upload cot_solutions.csv to mervinpraison/cot-dataset") return StepResult(output=result)# Create workflowworkflow = AgentFlow( steps=[ generate_qa, evaluate_count, loop(generate_cot, over="qa_pairs", from_csv="qa_pairs.csv"), upload_dataset ])result = workflow.start("Generate reasoning data")
4
Run the application
Execute the Python script to start generating chain-of-thought data: