Coder Social home page Coder Social logo

antonin-deniau / genetic_prompt_compiler Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 77 KB

Optimize a prompt for a language model using a genetic algorithm.

License: MIT License

Python 100.00%
genetic-algorithm litellm llm prompt-engineering prompt-generator prompt-toolkit

genetic_prompt_compiler's Introduction

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
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}")

genetic_prompt_compiler's People

Contributors

antonin-deniau avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.