Parameter Optimizer: Bayesian Parameter Tuning
Optimize LLM parameters like temperature and top_p with Bayesian techniques.
The ParameterOptimizer uses Bayesian optimization to tune LLM call parameters such as temperature, top_p, frequency_penalty, and other sampling parameters. Unlike other optimizers that modify the prompt itself, this optimizer keeps your prompt unchanged and focuses solely on finding the best parameter configuration for your specific task.
When to Use: Optimize LLM parameters (temperature, top_p) without changing your prompt. Best when you have a good prompt but need to tune model behavior.
Key Trade-offs: Requires defining parameter search space; doesn’t modify prompt text; uses two-phase Bayesian search.
Have questions about ParameterOptimizer? Our Optimizer & SDK FAQ
answers common questions, including when to use this optimizer, how parameters like default_n_trials and local_search_ratio
work, and how to define custom parameter search spaces.
How It Works
This optimizer uses Optuna, a hyperparameter optimization framework, to search for the best LLM parameters:
-
Baseline Evaluation: First evaluates your prompt with its current parameters (or default parameters) to establish a baseline score.
-
Parameter Space Definition: You define which parameters to optimize and their valid ranges using a
ParameterSearchSpace. For example:temperature: float between 0.0 and 2.0top_p: float between 0.0 and 1.0frequency_penalty: float between -2.0 and 2.0
-
Global Search Phase:
- Optuna explores the full parameter space using Bayesian optimization (TPESampler by default).
- Tries various parameter combinations to find promising regions.
- Evaluates each combination against your dataset using the specified metric.
-
Local Search Phase (optional):
- After global search, focuses on the best parameter region found.
- Performs fine-grained optimization around the best parameters.
- Controlled by
local_search_ratioandlocal_search_scale.
-
Parameter Importance Analysis:
- Calculates which parameters had the most impact on performance.
- Uses FANOVA importance (requires scikit-learn) or falls back to correlation-based sensitivity analysis.
-
Result: Returns the best parameter configuration found, along with detailed optimization history and parameter importance rankings.
The optimizer intelligently balances exploration (trying diverse parameters) with exploitation (refining promising configurations) to efficiently find optimal settings.
The core of this optimizer relies on robust evaluation where each parameter configuration is assessed using your metric against the dataset. Understanding Opik’s evaluation platform is key to effective use:
Configuration Options
Basic Configuration
Advanced Configuration
The key parameters are:
model: The LLM used for evaluation with different parameter configurations.default_n_trials: Default number of optimization trials (can be overridden inoptimize_parameter).n_threads: Number of parallel threads for evaluation (balance with API rate limits).local_search_ratio: Ratio of trials dedicated to local search around best parameters (0.0-1.0).local_search_scale: Scale factor for local search range (0.0 = no local search, higher = wider range).seed: Random seed for reproducibility.verbose: Logging level (0=warnings only, 1=info, 2=debug).
Example Usage
Basic Example
Advanced Example with Custom Parameters
Parameter Search Space
The ParameterSearchSpace defines which parameters to optimize and their valid ranges. It supports:
Float Parameters
Integer Parameters
Categorical Parameters
Boolean Parameters
Targeting Nested Parameters
You can optimize nested parameters in model_kwargs:
Model Support
The ParameterOptimizer supports all models available through LiteLLM. This provides broad compatibility with providers like OpenAI, Azure OpenAI, Anthropic, Google, and many others, including locally hosted models.
Configuration Example using LiteLLM model string
Best Practices
-
Start Simple
- Begin with 1-2 key parameters (e.g., temperature, top_p)
- Add more parameters once you understand their impact
- Too many parameters increases search space and trial requirements
-
Define Reasonable Ranges
- Use tighter ranges based on domain knowledge
- For temperature: 0.0-1.0 for factual tasks, 0.5-1.5 for creative tasks
- For top_p: 0.8-1.0 for most tasks
-
Trial Budget
- Start with 20-30 trials for 2-3 parameters
- Increase to 50-100 trials for 4+ parameters
- Monitor convergence - stop if improvements plateau
-
Local Search
- Use
local_search_ratio=0.3(default) for refinement - Increase to 0.4-0.5 if global search found good region quickly
- Decrease to 0.1-0.2 for more exploration
- Use
-
Parallel Evaluation
- Set
n_threadsbased on API rate limits - More threads = faster optimization but may hit limits
- Balance speed with cost and rate limit constraints
- Set
-
Parameter Importance
- Check
parameter_importancein results - Focus future optimization on high-impact parameters
- Consider fixing low-impact parameters to reduce search space
- Check
-
Validation
- Test final parameters on held-out validation set
- Verify improvements generalize beyond training data
- Consider parameter stability across different datasets
Research and References
Next Steps
- Explore specific Optimizers for algorithm details.
- Refer to the FAQ for common questions and troubleshooting.
- Refer to the API Reference for detailed configuration options.