Skip to main content

No project description provided

Project description

PyPI version

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

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?",
]


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,
    ),
    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

genetic_prompt_compiler-0.2.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

genetic_prompt_compiler-0.2.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file genetic_prompt_compiler-0.2.0.tar.gz.

File metadata

  • Download URL: genetic_prompt_compiler-0.2.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for genetic_prompt_compiler-0.2.0.tar.gz
Algorithm Hash digest
SHA256 078706f1e3256e87aaad2b0e0c18fb0a3c78e22b4a56eb3c218961a099181e38
MD5 3b80f2f5e1de65030d892c3605a1ceb9
BLAKE2b-256 c6ca3ee26ac67f8b9a6032928b16c50bb47d3a18a5095710e16822b58bd62f8b

See more details on using hashes here.

File details

Details for the file genetic_prompt_compiler-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for genetic_prompt_compiler-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 baecf9cd9621127403db7198bf92e1da445d59a771f0875fbddb9a72d4ae4a98
MD5 24dca03b0577a2906977ea4cb8b3ddac
BLAKE2b-256 12627e838e6d17297564cdb00e47319a7199866f04883994071799d493cc773b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page