Utils
EvaluationCallManager
Manages the evaluation calls for a specific project and entity in Weave.
This class is responsible for initializing and managing evaluation calls associated with a specific project and entity. It provides functionality to collect guardrail guard calls from evaluation predictions and scores, and render these calls into a structured format suitable for display in Streamlit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity
|
str
|
The entity name. |
required |
project
|
str
|
The project name. |
required |
call_id
|
str
|
The call id. |
required |
max_count
|
int
|
The maximum number of guardrail guard calls to collect from the evaluation. |
10
|
Source code in safeguards/utils.py
collect_guardrail_guard_calls_from_eval()
Collects guardrail guard calls from evaluation predictions and scores.
This function iterates through the children calls of the base evaluation call, extracting relevant guardrail guard calls and their associated scores. It stops collecting calls if it encounters an "Evaluation.summarize" operation or if the maximum count of guardrail guard calls is reached. The collected calls are stored in a list of dictionaries, each containing the input prompt, outputs, and score.
Returns:
Name | Type | Description |
---|---|---|
list |
A list of dictionaries, each containing: - input_prompt (str): The input prompt for the guard call. - outputs (dict): The outputs of the guard call. - score (dict): The score of the guard call. |
Source code in safeguards/utils.py
render_calls_to_streamlit()
Renders the collected guardrail guard calls into a pandas DataFrame suitable for display in Streamlit.
This function processes the collected guardrail guard calls stored in self.call_list
and
organizes them into a dictionary format that can be easily converted into a pandas DataFrame.
The DataFrame contains columns for the input prompts, the safety status of the outputs, and
the correctness of the predictions for each guardrail.
The structure of the DataFrame is as follows: - The first column contains the input prompts. - Subsequent columns contain the safety status and prediction correctness for each guardrail.
Returns:
Type | Description |
---|---|
pd.DataFrame: A DataFrame containing the input prompts, safety status, and prediction correctness for each guardrail. |
Source code in safeguards/utils.py
StreamlitProgressbarCallback
Bases: TrainerCallback
StreamlitProgressbarCallback is a custom callback for the Hugging Face Trainer that integrates a progress bar into a Streamlit application. This class updates the progress bar at each training step, providing real-time feedback on the training process within the Streamlit interface.
Attributes:
Name | Type | Description |
---|---|---|
progress_bar |
DeltaGenerator
|
A Streamlit progress bar object initialized to 0 with the text "Training". |
Methods:
Name | Description |
---|---|
on_step_begin |
Updates the progress bar at the beginning of each training step. The progress is calculated as the percentage of completed steps out of the total steps. The progress bar text is updated to show the current step and the total steps. |
Source code in safeguards/utils.py
initialize_guardrails_on_playground()
Initializes guardrails for the Streamlit application based on the user's selection from the sidebar. This function dynamically imports and configures various guardrail classes from the 'guardrails_genie.guardrails' module, depending on the guardrail names specified in the Streamlit session state.
The function iterates over each guardrail name in 'st.session_state.guardrail_names' and performs the following actions based on the guardrail type:
- For "PromptInjectionLLMGuardrail", it allows the user to select a language model from a dropdown and initializes the guardrail with the selected model.
- For "PromptInjectionClassifierGuardrail", it initializes the guardrail with a predefined model name.
- For "PromptInjectionLlamaGuardrail", it takes a checkpoint name input from the user and initializes the guardrail with the specified checkpoint.
- For entity recognition guardrails like "PresidioEntityRecognitionGuardrail", "RegexEntityRecognitionGuardrail", and "TransformersEntityRecognitionGuardrail", it provides a checkbox for the user to decide whether to anonymize entities and initializes the guardrail accordingly.
- For "RestrictedTermsJudge", it provides a checkbox for anonymization and initializes the guardrail based on the user's choice.
- For any other guardrail names, it initializes the guardrail with default settings.
After initializing all guardrails, it creates a 'GuardrailManager' instance with the configured guardrails and stores it in the session state for further use in the application.
Source code in safeguards/utils.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
|