UniPrompt looks at prompt optimization as that of learning multiple facets of a task from a set of training examples. UniPrompt, consists of a generative model to generate initial candidates for each prompt section; and a feedback mechanism that aggregates suggested edits from multiple mini-batches into a conceptual description for the section. In particular, it can generate long, complex prompts that baseline algorithms are unable to generate.
- Python 3 (>= 3.8)
-
Install the latest version of
pip
andsetuptools
python3 -m pip install --upgrade pip setuptools
-
To setup the package locally, run
python3 -m pip install .
-
Set Environment Variables
OpenAI Endpoint: You need to set the
OPENAI_API_KEY
environment variable in the config before running the code. -
Update the Config File
Modify the
config/dataset_name.json
file as per your use case.If you are using an internal endpoint, make sure to set
api_type
toazure
,api_base
to your endpoint URL andapi_version
in your dataset config file. If you are using an OpenAI endpoint, then just set api_type tooai
.The configuration includes the following parameters:
"dataset_path": "data/ethos.jsonl", "mini_batch_size": 5, "batch_size": 7, "iterations": 1, "epochs": 5, "logging_file_path": "logs/ethos.jsonl", "epsilon": 0.5, "beam_width": 3, "group_frequency": 2, "cache_path": "cache/ethos.db", "initial_prompt": "<initial_prompt>", "metric_kwargs": { "type": "accuracy" }, "solver_llm": { "model_kwargs": { "model": "gpt-4o", "temperature": 0, "max_tokens": 512, "stream": false }, "api_kwargs": { "api_type": "", "api_base": "", "api_version": "", "api_key":"", } }, "expert_llm": { "model_kwargs": { "model": "gpt-4", "temperature": 0, "max_tokens": 512, "stream": false }, "api_kwargs": { "api_type": "", "api_base": "", "api_version": "", "api_key":"", } }, "grouping_llm": { "model_kwargs": { "model": "gpt-4", "temperature": 0, "max_tokens": 512, "stream": false }, "api_kwargs": { "api_type": "", "api_base": "", "api_version": "", "api_key":"", } }
Metric
type
can be one of['accuracy', 'weighted_accuracy', 'hinge_accuracy']
Example config files can be found at config/ethos.json and config/bbh_navigate.json. Make sure to setapi_kwargs
before using them.A brief explanations on the config parameters:
dataset_path
: Path to the dataset filemini_batch_size
: Number of examples processed in each mini-batchbatch_size
: Number of mini-batches processed before updating the promptiterations
: Number of times to iterate over the dataset in each epochepochs
: Total number of training epochslogging_file_path
: Path to save the log fileepsilon
: An exploration parameter with range [0, 1]beam_width
: Number of top-performing prompts to maintain in the beam searchgroup_frequency
: Group questions every nth epochcache_path
: Path to store/retrieve cached resultsinitial_prompt
: The starting prompt for optimization
-
Prepare the Dataset
The dataset format is very important. Ensure your dataset is a JSONL file with the following format:
split
: (train, test, validation)question
: Full question that you want to get answered, including any prefix or postfix statementschoices
: If the answer has choices, it should be a list, like[monkey, zebra, lion, tiger]
answer
: The answer from the options
Example:
{"split": "train", "question": "What is the largest land animal?", "choices": ["monkey", "zebra", "lion", "tiger"], "answer": "tiger"}
For a working example, run
python examples/uniprompt_default.py --config=config/ethos.json
pip install -e "./[dev]"
To ensure your code follows the style guidelines, install ruff ~= 4.0
ruff check --fix