# Few-Shot Bayesian Optimizer

> Learn how to use the Few-Shot Bayesian Optimizer to find optimal few-shot examples for your chat-based prompts using Bayesian optimization techniques.

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

<Note>
  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.
</Note>

## 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](https://optuna.org/), a hyperparameter optimization framework, to guide the search for the
optimal set and number of few-shot examples.

<Frame>
  <img src="https://files.buildwithfern.com/https://opik.docs.buildwithfern.com/docs/opik/2026-01-30T17:33:07.444Z/img/agent_optimization/fewshot_bayesian_optimizer.png" alt="FewShot Bayesian Optimizer" />
</Frame>

## Quickstart

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

```python maxLines=1000
from opik_optimizer import FewShotBayesianOptimizer
from opik.evaluation.metrics import LevenshteinRatio
from opik_optimizer import datasets, ChatPrompt