Interface IPromptOptimizer<T>
- Namespace
- AiDotNet.Interfaces
- Assembly
- AiDotNet.dll
Defines the contract for optimizing prompts to improve language model performance.
public interface IPromptOptimizer<T>
Type Parameters
TThe type of numeric data used for scoring.
Remarks
A prompt optimizer automatically refines prompts to achieve better performance on a specific task. Optimization strategies include discrete search, gradient-based methods, ensemble approaches, and evolutionary algorithms.
For Beginners: A prompt optimizer automatically improves your prompts.
Think of it like automatic recipe refinement:
- You start with a basic recipe
- The optimizer tries variations (more salt, less sugar, different temperature)
- It measures which variations taste better
- It keeps refining until it finds the best version
For prompts:
- You provide a basic prompt
- Optimizer generates variations
- Tests each variation's performance
- Returns the best-performing prompt
Example: Initial prompt: "Classify this review" After optimization: "Carefully analyze the sentiment and tone of the following product review. Classify it as positive, negative, or neutral based on the overall customer satisfaction." Result: 15% accuracy improvement
Benefits:
- Better results without manual trial-and-error
- Discover optimal phrasings you wouldn't think of
- Systematic improvement process
- Measurable performance gains
Methods
GetOptimizationHistory()
Gets the optimization history showing performance over iterations.
IReadOnlyList<OptimizationHistoryEntry<T>> GetOptimizationHistory()
Returns
Remarks
Returns a list of prompts and their scores from the optimization process, allowing analysis of how optimization progressed.
For Beginners: Shows how the prompt improved during optimization.
Like a training log showing progress:
- Iteration 1: "Classify sentiment" → Score: 0.65
- Iteration 5: "Classify the sentiment of" → Score: 0.72
- Iteration 10: "Analyze sentiment and classify as" → Score: 0.78
- Iteration 20: "Carefully analyze the sentiment..." → Score: 0.85
Use this to:
- Visualize improvement over time
- Understand what changes helped
- Debug optimization issues
- Decide if more iterations would help
Example:
var history = optimizer.GetOptimizationHistory();
foreach (var entry in history)
{
Console.WriteLine($"Iteration {entry.Iteration}: Score {entry.Score}");
Console.WriteLine($"Prompt: {entry.Prompt}");
}
// Plot improvement curve
PlotScores(history.Select(h => h.Score));
Optimize(string, Func<string, T>, int)
IPromptTemplate Optimize(string initialPrompt, Func<string, T> evaluationFunction, int maxIterations = 100)
Parameters
Returns
OptimizeAsync(string, Func<string, Task<T>>, int, CancellationToken)
Task<IPromptTemplate> OptimizeAsync(string initialPrompt, Func<string, Task<T>> evaluationFunction, int maxIterations = 100, CancellationToken cancellationToken = default)
Parameters
initialPromptstringevaluationFunctionFunc<string, Task<T>>maxIterationsintcancellationTokenCancellationToken