AmanPriyanshu/GeneticPromptLab

★ 36⑂ 4

GeneticPromptLab uses genetic algorithms for automated prompt engineering (for LLMs), enhancing quality and diversity through iterative selection, crossover, and mutation

About AmanPriyanshu/GeneticPromptLab

AmanPriyanshu/GeneticPromptLab is an open-source project on GitHub, mainly written in Python. GeneticPromptLab uses genetic algorithms for automated prompt engineering (for LLMs), enhancing quality and diversity through iterative selection, crossover It currently holds 36 stars and 4 forks with 0 open issues, and was last pushed on an unknown date (repository created unknown).

Project Overview

AI Homed tracks it on the AI Prompt Engineering board.

GitHub Repository Details

Repository AmanPriyanshu/GeneticPromptLab · default branch - · size 0 KB · watchers 0 · source: GitHub REST API and repository README

README

GeneticPromptLab

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.

Overview

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.

Pip Install

pip install genetic-prompt-lab

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.

graph TD;
    A[Initialize OpenAI Client] --> B[Load Dataset Configuration]
    B --> C[Setup QuestionsAnswersOptimizer]
    C --> D[Run Genetic Algorithm]

subgraph Genetic Algorithm Process D --> GA[Start Genetic Algorithm] GA --> Init[Generate Initial Prompts] Init --> FE1 subgraph Fitness Evaluation FE1[Current Population] FE1 --> FE2[Evaluate Individual Fitness] FE2 --> FE3[Calculate Average Fitness] end

FE1 --> STP[Select Top Prompts] STP --> Cross[Crossover] subgraph Crossover Mechanism Cross --> Cross1[Select Parent Prompts] Cross1 --> Cross2[Apply Crossover Logic] Cross2 --> Cross3[Generate Offspring Prompts] end

Cross3 --> Mut[Mutation] subgraph Mutation Mechanism Mut --> Mut1[Select Prompts for Mutation] Mut1 --> Mut2[Apply Mutation Logic] Mut2 --> Mut3[Generate Mutated Prompts] end

Mut3 --> Check[Check Convergence] Check -- No --> FE1 Check -- Yes --> Finish[End Genetic Algorithm] end

Finish --> Out[Output]

style D fill:#f9f,stroke:#333,stroke-width:4px style Genetic Algorithm Process fill:#ccf,stroke:#333,stroke-width:2px style Fitness Evaluation fill:#fdd,stroke:#333,stroke-width:2px style Crossover Mechanism fill:#dfd,stroke:#333,stroke-width:2px style Mutation Mechanism fill:#ddf,stroke:#333,stroke-width:2px

Features

Installation

To install GeneticPromptLab, clone this repository and install the required packages:

git clone https://github.com/username/GeneticPromptLab.git
cd GeneticPromptLab
pip install -r requirements.txt

Benchmarks

Performance on TREC Dataset

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.

Training Progression exploring only 10% of the training data

Performance on TREC Dataset

Best Prompt within 10 generations (100 samples explored) achieves 76.1% accuracy on test-set.

Performance on AG News Dataset

The AG News dataset contains over 1 million news articles classified into categories like World, Sports, Business, and Sci/Tech. Below is the performance graph for the AG News dataset.

Training Progression exploring only 10% of the training data

Performance on AG News Dataset

Best Prompt within 10 generations (100 samples explored) achieves 80.5% accuracy on test-set.

Quick Start

To run GeneticPromptLab on the AG News dataset:

from GeneticPromptLab import QuestionsAnswersOptimizer, agnews
from openai import OpenAI

Load API key

with open("openai_api.key", "r") as f: key = f.read().strip()

Initialize client

client = OpenAI(api_key=key)

Get AG News dataset & Initialize the optimizer

lab = QuestionsAnswersOptimizer( client=client, problem_description=problem_description, train_questions_list=train_questions_list, train_answers_label=train_answers_label, test_questions_list=test_questions_list, test_answers_label=test_answers_label, label_dict=label_dict, model_name=model_name, sample_p=sample_p, init_and_fitness_sample=population_size, window_size_init=2, num_retries=num_retries)

Run the genetic algorithm

optimized_prompts = lab.genetic_algorithm() print(optimized_prompts)

Detailed Explanation of Arguments:

1. client

2. problem_description

3. train_questions_list

4. train_answers_label

5. test_questions_list

6. test_answers_label

7. label_dict

8. model_name

9. sample_p

10. init_and_fitness_sample

11. window_size_init

12. num_retries

Documentation

References

[1] H. Tanaka, N. Mori, and M. Okada, "Genetic Algorithm for Prompt Engineering with Novel Genetic Operators," IEEE, 2023.

[2] Q. Guo et al., "Connecting Large Language Models with Evolutionary Algorithms Yields Powerful Prompt Optimizers," arXiv preprint arXiv:2305.15611, 2023.

[3] C. Feng, Y. Sun, K. Li, P. Zhou, J. Lv, and A. Lu, "Genetic Auto-prompt Learning for Pre-trained Code Intelligence Language Models," arXiv preprint arXiv:2306.00185, 2023.

[4] R. Pryzant et al., "Automatic Prompt Optimization with "Gradient Descent" and Beam Search," Microsoft Azure AI, 2023.

[5] V.-T. Do et al., "Automatic Prompt Selection for Large Language Models," arXiv preprint arXiv:2306.05802, 2023.

[6] G. Sabbatella et al., "Bayesian Optimization for Prompt Tuning of Black-box Large Language Models," Mathematics, vol. 12, no. 6, p. 929, 2024.

[7] H. Sun et al., "Towards an Automatic Prompt Optimization Framework for AI Image Generation," Conference paper, 2023.

[8] A. Priyanshu and S. Vijay, "Adaptkeybert: An attention-based approach towards few-shot & zero-shot domain adaptation of keybert," arXiv preprint arXiv:2211.07499, 2022.

[9] R. Ma et al., "RLPrompt: Optimizing Discrete Text Prompts with Reinforcement Learning," arXiv preprint arXiv:2205.12548, 2022.

[10] X. Wang et al., "PromptAgent: Strategic Planning with Language Models Enables Expert-level Prompt Optimization," arXiv preprint arXiv:2305.10880, 2023.

[11] T. Kojima et al., "Large Language Models are Zero-Shot Reasoners," Advances in Neural Information Processing Systems, vol. 35, 2022.

[12] A. Bhattacharjee et al., "Zero-shot LLM-guided Counterfactual Generation for Text," arXiv preprint arXiv:2306.06704, 2023.

[13] S. Vijay and A. Priyanshu, "NERDA-Con: Extending NER models for Continual Learning--Integrating Distinct Tasks and Updating Distribution Shifts," arXiv preprint arXiv:2206.14607, 2022.

[14] X. Lin et al., "Prompt Optimization with Human Feedback," arXiv preprint arXiv:2305.18290, 2023.

Cite This Work

@inproceedings{genetic-prompt-lab,
  title = {GeneticPromptLab: A Framework for Automated Prompt Optimization using Genetic Algorithms and Diverse Sampling Strategies},
  author = {Aman Priyanshu, Supriti Vijay},
  year = {2024},
  publisher = {{GitHub}},
  url = {https://github.com/AmanPriyanshu/GeneticPromptLab}
}

Contributions and Feedback

Contributions, suggestions, and feedback are welcome! If you have any ideas to enhance the app or encounter any issues, please feel free to open an issue or submit a pull request on the GitHub repository. Thank you for your interest in our research work.

Other Usage Details

Attribution

_"GeneticPromptLab" by Aman Priyanshu and Supriti Vijay, licensed under the MIT License _This work is adapted from "GeneticPromptLab" by Aman Priyanshu and Supriti Vijay, licensed under the MIT License. Original work available at: https://github.com/AmanPriyanshu/GeneticPromptLab_

GitHub Stars & Activity

36Stars
4Forks
0Open issues
PythonLanguage

GitHub Popularity

GitHub stars36
Forks4
Open issues0
Primary languagePython
License-
Stars gained today0
Created-
Last pushed-

Trending History

Trending statusnot on today's boards

Related AI Projects

1

microsoft / promptflow

Python★ 11,244⑂ 1,123
2

promptslab / Promptify

Python★ 4,635⑂ 364
3
4

hegelai / prompttools

Python★ 3,055⑂ 256
5

Eladlev / AutoPrompt

Python★ 3,019⑂ 264
6

microsoftarchive / promptbench

Python★ 2,821⑂ 222
7

yaojingang / yao-open-prompts

Python★ 2,817⑂ 461
8

YiVal / YiVal

Python★ 2,134⑂ 328

More AI Rankings