### Install Dependencies Source: https://github.com/beeevita/evoprompt/blob/main/README.md Install project dependencies using pip. ```bash pip install -r requirements.txt ``` -------------------------------- ### Get Dataset Verbalizers Source: https://context7.com/beeevita/evoprompt/llms.txt This snippet demonstrates how to retrieve verbalizers (label words) for a given dataset using `get_dataset_verbalizers`. It shows examples for 'sst2' and 'sst-5' datasets. ```python from utils import get_dataset_verbalizers # Get verbalizers (label words) for a dataset verbalizers = get_dataset_verbalizers("sst2") print(verbalizers) # Output: [' negative', ' positive'] verbalizers = get_dataset_verbalizers("sst-5") print(verbalizers) # Output: [' terrible', ' bad', ' okay', ' good', ' great'] ``` -------------------------------- ### Initialize LLM Client and Query Source: https://context7.com/beeevita/evoprompt/llms.txt Initialize the LLM client using configuration from 'auth.yaml' and query for task completion or prompt evolution. Supports batch queries. ```python from llm_client import llm_init, llm_query, paraphrase # Initialize LLM configuration from auth.yaml config = llm_init( auth_file="./auth.yaml", llm_type="turbo", # Options: 'davinci', 'turbo', 'gpt4' setting="default" ) # Query LLM for task completion response = llm_query( data="Classify the following text: 'This movie was amazing!'", client=None, type="turbo", task=True, # True for task execution, False for evolution temperature=0, **config ) print(response) # Output: "positive" ``` ```python # Batch query for multiple inputs batch_data = [ "Classify: 'Great product!'", "Classify: 'Terrible experience.'" ] responses = llm_query( data=batch_data, client=None, type="davinci", task=True, temperature=0, **config ) print(responses) # Output: ["positive", "negative"] ``` ```python # Generate prompt variations using paraphrase original_prompt = "Classify the sentiment of the text." variations = paraphrase( sentence=original_prompt, client=None, type="turbo", temperature=0.5, **config ) print(variations) # Output: "Determine the emotional tone of the given text." ``` ```python # Batch paraphrase for multiple prompts prompts = [ "Simplify this sentence.", "Make this text easier to read." ] paraphrased = paraphrase( sentence=prompts, client=None, type="davinci", temperature=0.5, **config ) # Output: ["Reword this sentence simply.", "Transform this text for easier comprehension."] ``` -------------------------------- ### Initialize and Run Genetic Algorithm Evolution Source: https://context7.com/beeevita/evoprompt/llms.txt This snippet demonstrates initializing a Genetic Algorithm (GA) evoluter, initializing its population from prompts, running the evolution process, and accessing the best evolved prompt and its score. It also shows how to switch to Differential Evolution (DE) by changing the `evo_mode` and re-initializing the evoluter. ```python args = parse_args() args.dataset = "sst2" args.task = "cls" args.evo_mode = "ga" args.popsize = 10 args.budget = 10 args.sel_mode = "wheel" args.ga_mode = "topk" # Initialize evaluator evaluator = CLSEvaluator(args) # Initialize GA evoluter ga_evoluter = GAEvoluter(args, evaluator) # Initialize population from prompts evaluated_prompts, cur_budget = ga_evoluter.init_pop() print(f"Initial population size: {len(ga_evoluter.population)}") print(f"Best initial prompt: {ga_evoluter.population[0]}") # Run evolution ga_evoluter.evolute() # Access results print(f"Best evolved prompt: {ga_evoluter.population[0]}") print(f"Best score: {ga_evoluter.scores[0]}") # For Differential Evolution args.evo_mode = "de" args.template = "v1" de_evoluter = DEEvoluter(args, evaluator) de_evoluter.init_pop() de_evoluter.evolute() # Results are written to output directory: # - step{N}_pop.txt: Population state at step N # - dev_result.txt: Final sorted results ``` -------------------------------- ### Select initial population subset Source: https://context7.com/beeevita/evoprompt/llms.txt Initializes a population of prompts using either top-k selection or random sampling. ```python all_prompts = ["prompt1", "prompt2", "prompt3", "prompt4", "prompt5"] top_prompts = k_init_pop("topk", all_prompts, k=3) print(top_prompts) # Output: ["prompt1", "prompt2", "prompt3"] random_prompts = k_init_pop("randomk", all_prompts, k=3) print(random_prompts) # Output: 3 randomly selected prompts ``` -------------------------------- ### Initialize Summarization Evaluator Source: https://context7.com/beeevita/evoprompt/llms.txt Set up a summarization evaluator to test prompt effectiveness for generating summaries. It handles input data, reference texts, and calculates relevant metrics. ```python # Initialize summarization evaluator args.task = "sum" args.dataset = "sam" sum_evaluator = SumEvaluator(args) # Evaluate summarization prompt result = sum_evaluator.forward( prompt_pre="Write a one-sentence summary.", eval_src=sum_evaluator.dev_src[:10], ref_texts=sum_evaluator.dev_tgt[:10] ) print(result) # Output: {"hypos": [...], "scores": [0.35, 0.12, 0.28, 0.25]} # ROUGE-1, ROUGE-2, ROUGE-L, mean ``` -------------------------------- ### Run Genetic Algorithm for Simplification Task (Alpaca) Source: https://github.com/beeevita/evoprompt/blob/main/README.md Execute the genetic algorithm for prompt evolution on the Alpaca dataset for simplification tasks. Customize --llm_type for different LLMs. ```bash bash scripts/sim/run_ga_alpaca.sh ```