$git clone https://github.com/AmanPriyanshu/GeneticPromptLabGeneticPromptLab is a Python library designed to harness the power of genetic algorithms for automated prompt engineering for large language models (LLMs). By iteratively applying selection, crossover, and mutation processes, GeneticPromptLab enhances the quality and diversity of
| 1 | # GeneticPromptLab |
| 2 | GeneticPromptLab uses genetic algorithms for automated prompt engineering (for LLMs), enhancing quality and diversity through iterative selection, crossover, and mutation, while efficiently exploring minimal yet diverse samples from the training set. |
| 3 | |
| 4 | ## Overview |
| 5 | |
| 6 | GeneticPromptLab is a Python library designed to harness the power of genetic algorithms for automated prompt engineering for large language models (LLMs). By iteratively applying selection, crossover, and mutation processes, GeneticPromptLab enhances the quality and diversity of prompts, leading to improved performance in automated question-answering and classification tasks. |
| 7 | |
| 8 | ### Pip Install |
| 9 | `pip install genetic-prompt-lab` |
| 10 | |
| 11 | This library specifically leverages Sentence Transformers for embedding generation and k-means clustering to sample minimal yet diverse data from training sets. This strategic sampling ensures efficient exploration and optimization of prompts over multiple generations. |
| 12 | |
| 13 | ```mermaid |
| 14 | graph TD; |
| 15 | A[Initialize OpenAI Client] --> B[Load Dataset Configuration] |
| 16 | B --> C[Setup QuestionsAnswersOptimizer] |
| 17 | C --> D[Run Genetic Algorithm] |
| 18 | |
| 19 | subgraph Genetic Algorithm Process |
| 20 | D --> GA[Start Genetic Algorithm] |
| 21 | GA --> Init[Generate Initial Prompts] |
| 22 | Init --> FE1 |
| 23 | |
| 24 | subgraph Fitness Evaluation |
| 25 | FE1[Current Population] |
| 26 | FE1 --> FE2[Evaluate Individual Fitness] |
| 27 | FE2 --> FE3[Calculate Average Fitness] |
| 28 | end |
| 29 | |
| 30 | FE1 --> STP[Select Top Prompts] |
| 31 | STP --> Cross[Crossover] |
| 32 | subgraph Crossover Mechanism |
| 33 | Cross --> Cross1[Select Parent Prompts] |
| 34 | Cross1 --> Cross2[Apply Crossover Logic] |
| 35 | Cross2 --> Cross3[Generate Offspring Prompts] |
| 36 | end |
| 37 | |
| 38 | Cross3 --> Mut[Mutation] |
| 39 | subgraph Mutation Mechanism |
| 40 | Mut --> Mut1[Select Prompts for Mutation] |
| 41 | Mut1 --> Mut2[Apply Mutation Logic] |
| 42 | Mut2 --> Mut3[Generate Mutated Prompts] |
| 43 | end |
| 44 | |
| 45 | Mut3 --> Check[Check Convergence] |
| 46 | Check -- No --> FE1 |
| 47 | Check -- Yes --> Finish[End Genetic Algorithm] |
| 48 | end |
| 49 | |
| 50 | Finish --> Out[Output] |
| 51 | |
| 52 | style D fill:#f9f,stroke:#333,stroke-width:4px |
| 53 | style Genetic Algorithm Process fill:#ccf,stroke:#333,stroke-width:2px |
| 54 | style Fitness Evaluation fill:#fdd,stroke:#333,stroke-width:2px |
| 55 | style Crossover Mechanism fill:#dfd,stroke:#333,stroke-width:2px |
| 56 | style Mutation Mechanism fill:#ddf,stroke:#333,stroke-width:2px |
| 57 | ``` |
| 58 | |
| 59 | ## Features |
| 60 | |
| 61 | - **Genetic Algorithm Implementation**: Complete genetic algorithm cycle including initialization, fitness evaluation, selection, crossover, and mutation for prompt engineering. |
| 62 | - **Integration with Sentence Transformers**: Utilizes Sentence Transformers for generating embeddings, enabling effective clustering and sampling. |
| 63 | - **Diverse Sampling**: Uses k-means clustering to select a representative subset of data, ensuring diverse genetic material for algorithm initialization and evolution. |
| 64 | - **Dynamic Mutation and Crossover**: Incorporates custom mutation and crossover operations tailored to prompt characteristics and task requirements. |
| 65 | - **Multi-Dataset Support**: Pre-configured to run experiments on the TREC and AG News datasets, demonstrating its adaptability to different types of text classification tasks. |
| 66 | |
| 67 | ## Installation |
| 68 | |
| 69 | To install GeneticPromptLab, clone this repository and install the required packages: |
| 70 | |
| 71 | ```bash |
| 72 | git clone https://github.com/username/GeneticPromptLab.git |
| 73 | cd GeneticPromptLab |
| 74 | pip install -r requirements.txt |
| 75 | ``` |
| 76 | |
| 77 | ## Benchmarks |
| 78 | |
| 79 | ### Performance on TREC Dataset |
| 80 | |
| 81 | The TREC dataset comprises 4,500 English questions categorized into various labels. Below is the performance improvement graph showing the evolution of prompt effectiveness over 10 generations. |
| 82 | #### Training Progression exploring only 10% of the training data |
| 83 | ![Performance on TREC Dataset](https://raw.githubusercontent.com/AmanPriyanshu/GeneticPromptLab/main/images/ |