Genetic Prompt Compiler
Optimize a prompt for a language model using a genetic algorithm.
Installation
pip install genetic-prompt-compiler
Usage
You can find complete examples in the examples folder.
import genetic_prompt_compiler
from genetic_prompt_compiler import GeneticCompilerArgs
from genetic_prompt_compiler.mutate import rule_based_mutate, RuleBasedMutateConfig, Technique
from genetic_prompt_compiler.ranking import top_n_ranking, TopNRankingConfig
from genetic_prompt_compiler.fitness import rule_based_fitness, RuleBasedFitnessConfig
initial_prompt = "Answer my question about the universe"
rules = [
"It should be a good answer",
"It should be factually correct",
"It should be in english",
]
test_data [
"Why is the sky blue?",
"Who is the president of the United States?",
"What is the capital of France?",
]
# The default techniques to use to mutate the prompts
DEFAULT_TECHNIQUES = [
Technique(
prompt="Use the expert technique `You are an expert in {topic}`",
presence=0.3,
),
Technique(
prompt="Use the Chain of Thought technique `Let's think step by step...`",
presence=0.3,
),
Technique(
prompt="Use some examples `Here are some examples of answers: {examples}`",
presence=0.3,
),
]
args = GeneticCompilerArgs(
# Mutation function to use
mutate=rule_based_mutate,
# Ranking function to use, will be used to select the prompts to keep in each generation
ranking=top_n_ranking,
# Fitness function to use, will be used to rank the prompts in each generation
fitness=rule_based_fitness,
# Ranking function arguments
ranking_config=TopNRankingConfig(
# Top n prompts to keep in each generation
top_n=5,
),
mutation_config=RuleBasedMutateConfig(
# The llm function to use to mutate the prompts
mutation_llm=lambda q: "",
# Rules to generate the mutated prompts on
rules=rules,
# This is the default techniques used to mutate the prompts, you can omit this argument
techniques=DEFAULT_TECHNIQUES,
),
fitnes_config=RuleBasedFitnessConfig(
# The llm function to use to rank the prompts
fitness_llm=lambda q: "",
# The llm function that you need to optimize
student=lambda q: "",
# Rules to test the prompts on
rules=rules,
# The rating notation to use (X/10, X/5 etc.)
rating_notation=10,
# Test data to test the prompts on
train_examples=test_data,
# Amount of examples to test on prompts in each generation
example_amount=3,
),
# Amount of prompts to generate in each generation
popultation_size=10,
# Amount of generations to run
iterations=5,
# Initial prompts to start with.
# This prompts will be kept for the first generation, alongside propulation_size - len(initial_prompts) mutated versions of it
initial_prompts=[initial_prompt],
)
for population in genetic_prompt_compiler.run(args):
print(f"Top prompts:")
for prompt in population:
print(f"\t - {prompt}")
Metadata
Release files for genetic-prompt-compiler 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| genetic_prompt_compiler-0.2.1.tar.gz | 4.3 kB | Without provenance | Details for genetic_prompt_compiler-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| genetic_prompt_compiler-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for genetic_prompt_compiler-0.2.1-py3-none-any.whl |
Total release size: 11.0 kB
Release files / genetic_prompt_compiler-0.2.1.tar.gz
| Download URL | genetic_prompt_compiler-0.2.1.tar.gz |
|---|---|
| Size | 4.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7f6bd56914c74bbcd60609340b0335659370061736dd2fea8fea7649409ad09d
|
|
BLAKE2b-256 checksum How to use checksums |
a101080d75daeb7201eee4ca7e586f728ada7a2dee92ebd575055e6b8179a3e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Release files / genetic_prompt_compiler-0.2.1-py3-none-any.whl
| Download URL | genetic_prompt_compiler-0.2.1-py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7dc7b26a77b74b2977fca632702601e067497b1dc0e4c674d507d70b69808c24
|
|
BLAKE2b-256 checksum How to use checksums |
d109d0398f0ec9c317e3c85b041adf8811d4692f1978c7fcdbc3fbd253f295bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.18
|
Security for genetic-prompt-compiler 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.