Callbacks
Callback classes for logging, saving, and tracking optimization progress.
BestPromptCallback
Bases: Callback
Callback for tracking the best prompt during optimization.
This callback keeps track of the prompt with the highest score.
Attributes:
Name | Type | Description |
---|---|---|
best_prompt |
str
|
The prompt with the highest score so far. |
best_score |
float
|
The highest score achieved so far. |
Source code in promptolution/callbacks.py
__init__()
get_best_prompt()
Get the best prompt and score achieved during optimization.
Returns: Tuple[str, float]: The best prompt and score.
on_step_end(optimizer)
Update the best prompt and score if a new high score is achieved.
Args: optimizer: The optimizer object that called the callback.
Source code in promptolution/callbacks.py
CSVCallback
Bases: Callback
Callback for saving optimization progress to a CSV file.
This callback saves prompts and scores at each step to a CSV file.
Attributes:
Name | Type | Description |
---|---|---|
dir |
str
|
Directory the CSV file is saved to. |
step |
int
|
The current step number. |
Source code in promptolution/callbacks.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
__init__(dir)
Initialize the CSVCallback.
Args: dir (str): Directory the CSV file is saved to.
Source code in promptolution/callbacks.py
on_step_end(optimizer)
Save prompts and scores to csv.
Args: optimizer: The optimizer object that called the callback
Source code in promptolution/callbacks.py
on_train_end(optimizer)
Called at the end of training.
Args: optimizer: The optimizer object that called the callback.
Source code in promptolution/callbacks.py
Callback
Base class for optimization callbacks.
Source code in promptolution/callbacks.py
on_epoch_end(optimizer)
Called at the end of each optimization epoch.
Args: optimizer: The optimizer object that called the callback.
Returns:
Name | Type | Description |
---|---|---|
Bool |
True if the optimization should continue, False if it should stop. |
Source code in promptolution/callbacks.py
on_step_end(optimizer)
Called at the end of each optimization step.
Args: optimizer: The optimizer object that called the callback.
Returns:
Name | Type | Description |
---|---|---|
Bool |
True if the optimization should continue, False if it should stop. |
Source code in promptolution/callbacks.py
on_train_end(optimizer)
Called at the end of the entire optimization process.
Args: optimizer: The optimizer object that called the callback.
Returns:
Name | Type | Description |
---|---|---|
Bool |
True if the optimization should continue, False if it should stop. |
Source code in promptolution/callbacks.py
LoggerCallback
Bases: Callback
Callback for logging optimization progress.
This callback logs information about each step, epoch, and the end of training.
Attributes:
Name | Type | Description |
---|---|---|
logger |
The logger object to use for logging. |
|
step |
int
|
The current step number. |
Source code in promptolution/callbacks.py
__init__(logger)
on_step_end(optimizer)
Log information about the current step.
Source code in promptolution/callbacks.py
on_train_end(optimizer, logs=None)
Log information at the end of training.
Args: optimizer: The optimizer object that called the callback. logs: Additional information to log.
Source code in promptolution/callbacks.py
ProgressBarCallback
Bases: Callback
Callback for displaying a progress bar during optimization.
This callback uses tqdm to display a progress bar that updates at each step.
Attributes:
Name | Type | Description |
---|---|---|
pbar |
tqdm
|
The tqdm progress bar object. |
Source code in promptolution/callbacks.py
__init__(total_steps)
Initialize the ProgressBarCallback.
Args: total_steps (int): The total number of steps in the optimization process.
on_step_end(optimizer)
Update the progress bar at the end of each step.
Args: optimizer: The optimizer object that called the callback.
on_train_end(optimizer)
Close the progress bar at the end of training.
Args: optimizer: The optimizer object that called the callback.
TokenCountCallback
Bases: Callback
Callback for stopping optimization based on the total token count.
Source code in promptolution/callbacks.py
__init__(max_tokens_for_termination, token_type_for_termination)
Initialize the TokenCountCallback.
Args: max_tokens_for_termination (int): Maximum number of tokens which is allowed befor the algorithm is stopped. token_type_for_termination (str): Can be one of either "input_tokens", "output_tokens" or "total_tokens".
Source code in promptolution/callbacks.py
on_step_end(optimizer)
Check if the total token count exceeds the maximum allowed. If so, stop the optimization.