Magic_Words

Magic_Words

Code for the paper "What's the Magic Word? A Control Theory of LLM Prompting"

Stars: 61

Visit
 screenshot

Magic_Words is a repository containing code for the paper 'What's the Magic Word? A Control Theory of LLM Prompting'. It implements greedy back generation and greedy coordinate gradient (GCG) to find optimal control prompts (magic words). Users can set up a virtual environment, install the package and dependencies, and run example scripts for pointwise control and optimizing prompts for datasets. The repository provides scripts for finding optimal control prompts for question-answer pairs and dataset optimization using the GCG algorithm.

README:

Magic_Words

Code for the paper What's the Magic Word? A Control Theory of LLM Prompting.

Implements greedy back generation and greedy coordinate gradient (GCG) to find optimal control prompts (magic words).

Setup

# create a virtual environment
python3 -m venv venv

# activate the virtual environment
source venv/bin/activate

# install the package and dependencies
pip install -e .
pip install -r requirements.txt

Example Script (Pointwise Control)

Run the script in scripts/backoff_hack.py for a demo of finding the magic words (optimal control prompt) for a given question-answer pair using greedy search and greedy coordinate gradient (GCG). It applies the same algorithms as in the LLM Control Theory paper:

python3 scripts/backoff_hack_demo.py

See the comments in the script for further details. This issue thread is also a good resource for getting up and running.

Example Script (Optimizing Prompts for Dataset)

Here we apply the GCG algorithm from the LLM attacks paper to optimizing prompts on a dataset, similar to the AutoPrompt paper.

python3 scripts/sgcg.py \
    --dataset datasets/100_squad_train_v2.0.jsonl \
    --model meta-llama/Meta-Llama-3-8B-Instruct \
    --k 20 \
    --max_parallel 30 \
    --grad_batch_size 50 \
    --num_iters 30
    

Testing

# run all tests: 
coverage run -m unittest discover

# get coverage report:
coverage report --include=prompt_landscapes/*

# run a specific test:
coverage run -m unittest tests/test_compute_score.py

For Tasks:

Click tags to check more tools for each tasks

For Jobs:

Alternative AI tools for Magic_Words

Similar Open Source Tools

For similar tasks

For similar jobs