### Create Initial Population Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Initialize a population of evolution units by combining selected thinking styles and mutation prompts with a specific task description and one-shot example. ```python from run_prompt_breeder import create_population from prompts import THINKING_STYLES, MUTATION_PROMPTS import random # Select thinking styles and mutation prompts thinking_styles = random.sample(THINKING_STYLES, 2) mutation_prompts = random.sample(MUTATION_PROMPTS, 2) # Define problem and one-shot example problem_description = "Solve the math word problem, giving your answer as an arabic numeral." one_shot_example = { "question": "Janet has 3 apples. She buys 2 more. How many apples does she have?", "answer": "5" } # Create population (2 thinking styles × 2 mutation prompts = 4 units) population = create_population( tp_set=thinking_styles, mutator_set=mutation_prompts, problem_description=problem_description, one_shot_example=one_shot_example ) print(f"Population size: {population.size}") # Output: Population size: 4 print(f"Population age: {population.age}") # Output: Population age: 0 ``` -------------------------------- ### Run Prompt Breeder Script Source: https://github.com/shivamsuchak/promptbreeder/blob/main/README.md Execute the main script to start the prompt generation and evolution process. Specify parameters like the number of thinking styles, mutation prompts, problem statement, evaluations per unit, and number of generations. ```bash python run_prompt.py -ts 2 -mp 2 -p "Solve the math word problem, giving your answer as an arabic numeral." -e 10 -n 5 ``` -------------------------------- ### Initialize and Run Population Evolution Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Sets up the initial population and executes the evolutionary process over a specified number of generations. ```python # Create and initialize population population = create_population(thinking_styles, mutation_prompts, problem_description, one_shot_example) population = init_run(population, gsm8k_dataset, num_evals=10) # Run evolution for 5 generations final_population = run_for_n( n=5, population=population, gsm8k_examples=gsm8k_dataset, num_evals=10 ) # Output shows generation progress: # Running Generation 0 # Generation 0 Summary: Max Fitness: 0.60 # Running Generation 1 # Generation 1 Summary: Max Fitness: 0.70 # ... # Access final best prompt best_unit = max(final_population.units, key=lambda u: u.fitness) print(f"Best prompt after evolution: {best_unit.P}") print(f"Best fitness: {best_unit.fitness:.2f}") ``` -------------------------------- ### Initialize Evolutionary Run Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Generate initial task prompts for population units using the OpenAI API and establish baseline fitness scores against a benchmark dataset. ```python from run_prompt_breeder import create_population, init_run from gsm8k_utils import read_jsonl import openai # Setup openai.api_key = "your-api-key" gsm8k_dataset = read_jsonl("./gsm8k_sampled.jsonl") # Create and initialize population population = create_population(thinking_styles, mutation_prompts, problem_description, one_shot_example) # Initialize run - generates prompts and evaluates fitness initialized_population = init_run( population=population, gsm8k_examples=gsm8k_dataset, num_evals=10 # Evaluate each unit on 10 random examples ) # Each unit now has a generated task prompt (P) and fitness score for unit in initialized_population.units: print(f"Task Prompt: {unit.P[:100]}...") print(f"Fitness: {unit.fitness:.2f}") ``` -------------------------------- ### Run Prompt Breeder via CLI Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Execute the evolutionary optimization process using command-line arguments to configure population size, generations, and task parameters. ```bash # Basic usage with default parameters python run_prompt_breeder.py # Full configuration with all parameters python run_prompt_breeder.py \ -ts 2 \ -mp 2 \ -p "Solve the math word problem, giving your answer as an arabic numeral." \ -e 10 \ -n 5 # Parameters: # -ts, --num_thinking_styles: Number of thinking styles (default: 2) # -mp, --num_mutation_prompts: Number of mutation prompts (default: 2) # -p, --problem_statement: Problem description for the task # -e, --num_evals: Evaluations per unit per generation (default: 10) # -n, --simulations: Number of generations to run (default: 5) ``` -------------------------------- ### Zero-Order Prompt Generation Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Generates a new task prompt by creating an ordered list of hints based on the unit's mutation prompt. ```python from mutations import zero_order_prompt_gen from data_structures import EvolutionUnit # Create an evolution unit unit = EvolutionUnit( T="Think step by step", M="mathematical reasoning", P="", fitness=0, history=[] ) # Generate new task prompt mutated_unit = zero_order_prompt_gen(unit) print(f"Generated prompt: {mutated_unit.P}") print(f"History: {mutated_unit.history}") # Contains the new prompt ``` -------------------------------- ### zero_order_prompt_gen Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Generates a new task prompt using a zero-order strategy by creating an ordered list of hints based on the unit's mutation prompt. ```APIDOC ## zero_order_prompt_gen ### Description Generates a new task prompt using a zero-order strategy by creating an ordered list of hints based on the unit's mutation prompt. ### Parameters #### Request Body - **unit** (EvolutionUnit) - Required - The evolution unit to mutate. ### Response - **mutated_unit** (EvolutionUnit) - The unit with the newly generated prompt. ``` -------------------------------- ### Run Evolutionary Generations Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Execute the evolutionary algorithm for a specified number of generations to iteratively improve prompt quality through mutation and selection. ```python from run_prompt_breeder import run_for_n, create_population, init_run ``` -------------------------------- ### lineage_based_mutation Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Creates a new task prompt based on the evolutionary history of elite prompts. ```APIDOC ## lineage_based_mutation ### Description Creates a new task prompt based on the evolutionary history of elite prompts, leveraging the lineage of successful prompts to guide generation. ### Parameters #### Request Body - **unit** (EvolutionUnit) - Required - The unit to mutate. - **population** (Population) - Required - The population containing elite history. ### Response - **mutated_unit** (EvolutionUnit) - The unit with the new prompt generated from elite lineage. ``` -------------------------------- ### Lineage-Based Mutation Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Generates a new task prompt by incorporating the evolutionary history of elite prompts from the population. ```python from mutations import lineage_based_mutation from data_structures import EvolutionUnit, Population # Create unit and population with elite history unit = EvolutionUnit(T="Step by step", M="logical", P="Current prompt", fitness=0.6, history=[]) population = Population(size=4, age=3, problem_description="Solve math problems", units=[]) # Add elite history (best prompts from previous generations) population.elites = [ EvolutionUnit(T="", M="", P="First elite prompt", fitness=0.7, history=[]), EvolutionUnit(T="", M="", P="Second elite prompt", fitness=0.8, history=[]) ] # Generate new prompt based on elite lineage mutated_unit = lineage_based_mutation(unit, population=population) print(f"New prompt based on elite history: {mutated_unit.P}") ``` -------------------------------- ### Evaluate Population Fitness Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Assess the performance of prompt units against dataset samples and identify the elite performers for the current generation. ```python from run_prompt_breeder import evaluate_fitness import random # Sample evaluation examples examples = random.sample(gsm8k_dataset, 10) # Evaluate population fitness evaluated_population = evaluate_fitness( population=population, gsm8k_examples=gsm8k_dataset, num_evals=10 ) # Access results max_fitness = max(unit.fitness for unit in evaluated_population.units) print(f"Max fitness: {max_fitness:.2f}") # Output: Max fitness: 0.70 # Access elite units (best performers from each generation) print(f"Number of elites: {len(evaluated_population.elites)}") if evaluated_population.elites: print(f"Best elite prompt: {evaluated_population.elites[-1].P}") ``` -------------------------------- ### Read JSONL Dataset Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Utility function to load benchmark datasets formatted as JSON Lines. ```python from utils import read_jsonl # Load GSM8K dataset gsm8k_examples = read_jsonl("./gsm8k_sampled.jsonl") # Each example contains question and answer for example in gsm8k_examples[:2]: print(f"Question: {example['question']}") print(f"Answer: {example['answer']}") print("---") # Output: # Question: Janet's ducks lay 16 eggs per day... # Answer: 18 # --- ``` -------------------------------- ### read_jsonl Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Reads a JSONL file and returns a list of dictionaries. ```APIDOC ## read_jsonl ### Description Reads a JSONL (JSON Lines) file and returns a list of dictionaries, used for loading benchmark datasets. ### Parameters #### Request Body - **file_path** (string) - Required - The path to the JSONL file. ### Response - **data** (list) - A list of dictionaries containing the parsed JSON lines. ``` -------------------------------- ### Apply Population Mutations Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Performs random mutations on the population using tournament selection to replace weaker units with mutated versions of stronger ones. ```python from mutations import mutate # Apply mutations to population # Uses tournament selection: pairs units, mutates the better one, replaces the worse one mutated_population = mutate(population) # Mutation strategies applied randomly: # 1. zero_order_prompt_gen - generates new task prompt from hints # 2. zero_order_hypermutation - evolves the thinking style # 3. lineage_based_mutation - creates prompt based on elite history for unit in mutated_population.units: print(f"Mutated prompt: {unit.P[:80]}...") print(f"History length: {len(unit.history)}") ``` -------------------------------- ### mutate Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Applies random mutations to the population using one of three mutation strategies, replacing weaker units with mutated versions of stronger units through tournament selection. ```APIDOC ## mutate ### Description Applies random mutations to the population using one of three mutation strategies, replacing weaker units with mutated versions of stronger units through tournament selection. ### Parameters #### Request Body - **population** (Population) - Required - The population object containing units to be mutated. ### Response - **mutated_population** (Population) - The population after applying mutation strategies. ``` -------------------------------- ### Extract and Normalize Answer using gsm_extract_answer Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Extracts and normalizes the answer from a completion text by stripping whitespace. Used for preprocessing ground truth answers from the dataset. ```python from utils import gsm_extract_answer # Extract answer from dataset completion raw_answer = " 42 \n" extracted = gsm_extract_answer(raw_answer) print(f"Extracted answer: '{extracted}'") # Output: Extracted answer: '42' ``` -------------------------------- ### Check Answer in Response Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Verifies if an expected answer exists within a model's response using case-insensitive substring matching. ```python from utils import check_answer_in_response # Check if model's response contains the correct answer model_response = "After calculating step by step, the answer is 42 apples." expected_answer = "42" is_correct = check_answer_in_response(model_response, expected_answer) print(f"Answer correct: {is_correct}") # Output: Answer correct: True # Handles case insensitivity model_response = "The Answer Is FORTY-TWO or 42" is_correct = check_answer_in_response(model_response, "42") print(f"Answer correct: {is_correct}") # Output: Answer correct: True ``` -------------------------------- ### Zero-Order Hypermutation Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Mutates the thinking style attribute of an evolution unit by elaborating on its current style. ```python from mutations import zero_order_hypermutation from data_structures import EvolutionUnit unit = EvolutionUnit( T="Think step by step", M="analytical thinking", P="Solve carefully", fitness=0.5, history=[] ) # Mutate the thinking style (M attribute) mutated_unit = zero_order_hypermutation(unit) print(f"Original M: analytical thinking") print(f"Mutated M: {mutated_unit.M}") # New elaborated thinking style ``` -------------------------------- ### check_answer_in_response Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Checks if the expected answer is present in the model's response using case-insensitive substring matching. ```APIDOC ## check_answer_in_response ### Description Checks if the expected answer is present in the model's response using case-insensitive substring matching for robust answer verification. ### Parameters #### Request Body - **model_response** (string) - Required - The text generated by the model. - **expected_answer** (string) - Required - The target answer to search for. ### Response - **is_correct** (boolean) - True if the answer is found, False otherwise. ``` -------------------------------- ### zero_order_hypermutation Source: https://context7.com/shivamsuchak/promptbreeder/llms.txt Mutates the thinking style of a unit by elaborating on a randomly selected thinking style. ```APIDOC ## zero_order_hypermutation ### Description Mutates the thinking style of a unit by elaborating on a randomly selected thinking style, potentially discovering more effective reasoning approaches. ### Parameters #### Request Body - **unit** (EvolutionUnit) - Required - The evolution unit whose thinking style (M) will be mutated. ### Response - **mutated_unit** (EvolutionUnit) - The unit with the updated thinking style. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.