Few-Shot Bayesian Optimizer

Optimize few-shot examples for chat prompts with Bayesian techniques.

The FewShotBayesianOptimizer is a sophisticated prompt optimization tool adds relevant examples from your sample questions to the system prompt using Bayesian optimization techniques.

The FewShotBayesianOptimizer is a strong choice when your primary goal is to find the optimal number and combination of few-shot examples (demonstrations) to accompany your main instruction prompt, particularly for chat models. If your task performance heavily relies on the quality and relevance of in-context examples, this optimizer is ideal.

How It Works

The FewShotBayesianOptimizer uses Bayesian optimization to find the optimal set and number of few-shot examples to include with your base instruction prompt for chat models. It Uses Optuna, a hyperparameter optimization framework, to guide the search for the optimal set and number of few-shot examples.

FewShot Bayesian Optimizer

Quickstart

You can use the FewShotBayesianOptimizer to optimize a prompt by following these steps:

1from opik_optimizer import FewShotBayesianOptimizer
2from opik.evaluation.metrics import LevenshteinRatio
3from opik_optimizer import datasets, ChatPrompt
4
5# Initialize optimizer
6optimizer = FewShotBayesianOptimizer(
7 model="openai/gpt-4",
8 model_parameters={
9 "temperature": 0.1,
10 "max_tokens": 5000
11 },
12)
13
14# Prepare dataset
15dataset = datasets.hotpot_300()
16
17# Define metric and prompt (see docs for more options)
18def levenshtein_ratio(dataset_item, llm_output):
19 return LevenshteinRatio().score(reference=dataset_item["answer"], output=llm_output)
20
21prompt = ChatPrompt(
22 messages=[
23 {"role": "system", "content": "Provide an answer to the question."},
24 {"role": "user", "content": "{question}"}
25 ]
26)
27
28# Run optimization
29results = optimizer.optimize_prompt(
30 prompt=prompt,
31 dataset=dataset,
32 metric=levenshtein_ratio,
33 n_samples=100
34)
35
36# Access results
37results.display()

Configuration Options

Optimizer parameters

The optimizer has the following parameters:

model
strDefaults to gpt-4o
LiteLLM model name for optimizer’s internal reasoning (generating few-shot templates)
model_parameters
dict[str, typing.Any] | None
Optional dict of LiteLLM parameters for optimizer’s internal LLM calls. Common params: temperature, max_tokens, max_completion_tokens, top_p.
min_examples
intDefaults to 2
Minimum number of examples to include in the prompt
max_examples
intDefaults to 8
Maximum number of examples to include in the prompt
n_threads
intDefaults to 8
Number of threads for parallel evaluation
verbose
intDefaults to 1
Controls internal logging/progress bars (0=off, 1=on)
seed
intDefaults to 42
Random seed for reproducibility

optimize_prompt parameters

The optimize_prompt method has the following parameters:

prompt
ChatPrompt
The prompt to optimize
dataset
Dataset
Opik Dataset to optimize on
metric
Callable
Metric function to evaluate on
experiment_config
dict | None
Optional configuration for the experiment, useful to log additional metadata
n_samples
int | None
Optional number of items to test in the dataset
auto_continue
boolDefaults to False
Whether to auto-continue optimization
agent_class
type[opik_optimizer.optimizable_agent.OptimizableAgent] | None
Optional agent class to use
project_name
strDefaults to Optimization
Opik project name for logging traces (default: “Optimization”)
max_trials
intDefaults to 10
Number of trials for Bayesian Optimization (default: 10)
args
Any
kwargs
Any

Model Support

There are two models to consider when using the FewShotBayesianOptimizer:

  • FewShotBayesianOptimizer.model: The model used to generate the few-shot template and placeholder.
  • ChatPrompt.model: The model used to evaluate the prompt.

The model parameter accepts any LiteLLM-supported model string (e.g., "gpt-4o", "azure/gpt-4", "anthropic/claude-3-opus", "gemini/gemini-1.5-pro"). You can also pass in extra model parameters using the model_parameters parameter:

1optimizer = FewShotBayesianOptimizer(
2 model="anthropic/claude-3-opus-20240229",
3 model_parameters={
4 "temperature": 0.7,
5 "max_tokens": 4096
6 }
7)

Next Steps

  1. Explore specific Optimizers for algorithm details.
  2. Refer to the FAQ for common questions and troubleshooting.
  3. Refer to the API Reference for detailed configuration options.