No project description provided
Project description
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}")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file genetic_prompt_compiler-0.2.1.tar.gz.
File metadata
- Download URL: genetic_prompt_compiler-0.2.1.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f6bd56914c74bbcd60609340b0335659370061736dd2fea8fea7649409ad09d
|
|
| MD5 |
8e252c16ade72b2f10d8270852a88c40
|
|
| BLAKE2b-256 |
a101080d75daeb7201eee4ca7e586f728ada7a2dee92ebd575055e6b8179a3e8
|
File details
Details for the file genetic_prompt_compiler-0.2.1-py3-none-any.whl.
File metadata
- Download URL: genetic_prompt_compiler-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7dc7b26a77b74b2977fca632702601e067497b1dc0e4c674d507d70b69808c24
|
|
| MD5 |
9114381fd85d4065801322de4eda67dd
|
|
| BLAKE2b-256 |
d109d0398f0ec9c317e3c85b041adf8811d4692f1978c7fcdbc3fbd253f295bc
|