> ## Documentation Index
> Fetch the complete documentation index at: https://futureagi.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Random Search Optimizer

> Understand the Random Search optimizer, a simple and effective gradient-free method for establishing a baseline in prompt optimization by exploring random variations.

Random Search is a gradient-free method that generates a set of random variations of an initial prompt using a powerful "teacher" LLM. It then evaluates each variation against a dataset and selects the best-performing one. It's a fast, straightforward, and often surprisingly effective way to explore different prompt phrasings and establish a strong performance baseline.

***

## **When to Use Random Search**

<CardGroup cols={2}>
  <Card title="✅ Best For" icon="check">
    * Establishing a quick baseline
    * Simple tasks like summarization or classification
    * Broad, unbiased exploration of the prompt space
    * Projects with a low computational budget
  </Card>

  <Card title="❌ Not Ideal For" icon="xmark">
    * Complex, nuanced, or multi-step reasoning tasks
    * Directed, efficient optimization when failure modes are known
    * Tasks requiring highly structured or constrained prompts
    * Finding the absolute, state-of-the-art best prompt
  </Card>
</CardGroup>

***

## **How It Works**

The Random Search process is simple and effective, involving three main steps:

<Steps>
  <Step title="1. Generate Variations">
    You provide an initial prompt. The optimizer then uses a powerful `teacher_model` (like GPT-4o) to generate a specified `num_variations` of diverse rewrites of that prompt.
  </Step>

  <Step title="2. Evaluate All Variations">
    The optimizer iterates through each generated variation. For each one, it generates outputs for all examples in your dataset and scores them using the provided evaluator.
  </Step>

  <Step title="3. Select the Best">
    The variation that achieves the highest average score across the entire dataset is chosen as the best prompt. The process concludes, and this top-performing prompt is returned.
  </Step>
</Steps>

***

## **Basic Usage**

```python theme={null}
from fi.opt.optimizers import RandomSearchOptimizer
from fi.opt.generators import LiteLLMGenerator
from fi.opt.datamappers import BasicDataMapper
from fi.opt.base.evaluator import Evaluator

# 1. Define the generator with the initial prompt to be optimized
initial_generator = LiteLLMGenerator(
    model="gpt-4o-mini",
    prompt_template="Summarize this article: {article}"
)

# 2. Setup the evaluator to score prompt performance
evaluator = Evaluator(
    eval_template="summary_quality",
    eval_model_name="turing_flash",
    fi_api_key="your_key",
    fi_secret_key="your_secret"
)

# 3. Setup the data mapper
data_mapper = BasicDataMapper(
    key_map={"input": "article", "output": "generated_output"}
)

# 4. Initialize the Random Search optimizer
# It needs the generator to optimize, a powerful teacher model, and the number of variations to try.
optimizer = RandomSearchOptimizer(
    generator=initial_generator,
    teacher_model="gpt-4o",
    num_variations=10
)

# 5. Run the optimization
result = optimizer.optimize(
    evaluator=evaluator,
    data_mapper=data_mapper,
    dataset=my_dataset
)

print(f"Best prompt found: {result.best_generator.get_prompt_template()}")
print(f"Final score: {result.final_score:.4f}")
```

***

## **Configuration Parameters**

<ParamField path="generator" type="BaseGenerator" required>
  The generator instance that you want to optimize. The optimizer will modify the prompt template within this object.
</ParamField>

<ParamField path="teacher_model" type="str" default="gpt-5">
  The powerful language model used to generate the prompt variations. The quality of the random search depends heavily on this model's ability to create diverse and sensible rewrites. Recommended: `gpt-4o`, `claude-3-opus`.
</ParamField>

<ParamField path="num_variations" type="int" default="5">
  The number of different prompt variations the teacher model will generate. This parameter controls the trade-off between the breadth of the search and the computational cost/time of the optimization.
</ParamField>

<ParamField path="teacher_model_kwargs" type="Dict" default="{}">
  A dictionary of additional arguments to pass to the teacher model during variation generation. This is useful for controlling parameters like `temperature` to influence the creativity of the variations.

  ```python theme={null}
  # Example: Increase temperature for more creative variations
  optimizer = RandomSearchOptimizer(
      ...,
      teacher_model_kwargs={"temperature": 1.2}
  )
  ```
</ParamField>

***

## **Underlying Research**

Random search is a foundational technique in hyperparameter tuning, valued for its simplicity and surprising effectiveness, often outperforming more structured methods like grid search.

* **Baseline Strength**: Research like "[Random Sampling as a Strong Baseline for Prompt Optimisation](https://arxiv.org/abs/2311.09569)" demonstrates that even simple random sampling can be a highly competitive method for improving prompts.
* **Broad Applicability**: It is frequently used as the first step in prompt optimization toolkits to get a sense of the landscape. Its ability to avoid getting stuck in local optima makes it a valuable tool in the discrete and high-dimensional space of prompt engineering.

***

## **Next Steps**

<CardGroup cols={2}>
  <Card title="Try Bayesian Search" icon="chart-line" href="/future-agi/get-started/optimization/optimizers/bayesian-search">
    For more intelligent, learning-based exploration
  </Card>

  <Card title="Compare All Optimizers" icon="scale-balanced" href="/future-agi/get-started/optimization/optimizers/overview">
    See which optimizer fits your needs
  </Card>
</CardGroup>
