Utils
Dict
Module evalsense.utils.dict.
Functions:
| Name | Description |
|---|---|
deep_update |
Recursively updates a dictionary with data from another dictionary. |
deep_update
Recursively updates a dictionary with data from another dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
old_dict
|
dict
|
The dictionary to update. |
required |
new_dict
|
dict
|
The new dictionary data. |
required |
Source code in evalsense/utils/dict.py
Files
Module evalsense.utils.files.
Functions:
| Name | Description |
|---|---|
download_file |
Downloads a file from a URL. |
get_remote_file_headers |
Gets the HTTP headers of a remote file. |
to_safe_filename |
Converts a string to a safe filename. |
verify_file |
Verifies the integrity of a file against the provided metadata. |
download_file
download_file(
url: str,
target_path: str | Path,
resume_download: bool = True,
force_download: bool = False,
show_progress: bool = True,
max_attempts: int = 2,
expected_hash: str | None = None,
hash_type: str = DEFAULT_HASH_TYPE,
chunk_size: int = 10 * 1024**2,
) -> None
Downloads a file from a URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL of the file to download. |
required |
target_path
|
str | Path
|
The path to save the downloaded file. |
required |
resume_download
|
bool
|
Whether to resume a partially downloaded file when the server provides a strong ETag. |
True
|
force_download
|
bool
|
Whether to force the download even if the file already exists. |
False
|
show_progress
|
bool
|
Whether to show download progress. |
True
|
max_attempts
|
int
|
The maximum number of download attempts. Defaults to 2. |
2
|
expected_hash
|
str
|
The expected hash of the downloaded file. |
None
|
hash_type
|
str
|
The hash algorithm to use. Defaults to "sha256". |
DEFAULT_HASH_TYPE
|
chunk_size
|
int
|
The size of each download chunk in bytes. Defaults to 10MB. |
10 * 1024 ** 2
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the download fails after the maximum number of attempts. |
ValueError
|
If max_attempts is invalid. |
Source code in evalsense/utils/files.py
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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | |
get_remote_file_headers
Gets the HTTP headers of a remote file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL of the file. |
required |
max_attempts
|
int
|
The maximum number of attempts to get the headers. Defaults to 2. |
2
|
Returns:
| Type | Description |
|---|---|
CaseInsensitiveDict[str]
|
The headers of the file. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the headers cannot be determined in the maximum number of attempts. |
Source code in evalsense/utils/files.py
to_safe_filename
Converts a string to a safe filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The string to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The safe filename. |
Source code in evalsense/utils/files.py
verify_file
verify_file(
file_path: str | Path,
expected_size: int | None = None,
expected_hash: str | None = None,
hash_type: str = DEFAULT_HASH_TYPE,
show_progress: bool = True,
chunk_size: int = 10 * 1024**2,
) -> bool
Verifies the integrity of a file against the provided metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
The path to the file to verify. |
required |
expected_size
|
int
|
The expected size of the file in bytes (skips checking size if None). |
None
|
expected_hash
|
str
|
The expected hash of the file (skips checking hash if None). |
None
|
hash_type
|
str
|
The hash algorithm to use. Defaults to "sha256". |
DEFAULT_HASH_TYPE
|
show_progress
|
bool
|
Whether to show verification progress. |
True
|
chunk_size
|
int
|
The size of each verification chunk in bytes. Defaults to 10MB. |
10 * 1024 ** 2
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the file matches the expected metadata, False otherwise. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the hash type is unsupported. |
Source code in evalsense/utils/files.py
Huggingface
Module evalsense.utils.huggingface.
Functions:
| Name | Description |
|---|---|
disable_dataset_progress_bars |
Context manager to disable progress bars for Hugging Face datasets. |
disable_dataset_progress_bars
Context manager to disable progress bars for Hugging Face datasets.
Source code in evalsense/utils/huggingface.py
Text
Classes:
| Name | Description |
|---|---|
WeightedRating |
A conditional mean and the returned rating probabilities that support it. |
Functions:
| Name | Description |
|---|---|
extract_lines |
Extract lines from the text based on a filter function. |
extract_score |
Parse one complete, in-range, non-negative decimal integer. |
extract_ternary_answer |
Extract a ternary answer (True/False/Unknown) from the text. |
extract_weighted_binary_answer |
Extract a weighted binary answer from the model output. |
extract_weighted_rating |
Average complete rating tokens from a one-token judge request. |
extract_weighted_score |
Return the conditional rating mean from validated one-token evidence. |
format_template |
Format a template string with the provided keyword arguments. |
parse_ternary_answer |
Parse a complete answer, allowing one balanced presentation wrapper. |
strip_list_prefix |
Remove recognised list prefixes without stripping leading numerical values. |
validate_score_range |
Require non-negative integer endpoints and a usable normalisation span. |
WeightedRating
dataclass
A conditional mean and the returned rating probabilities that support it.
Source code in evalsense/utils/text.py
extract_lines
extract_lines(
text: str,
include_filter_fun: Callable[[str], bool] = lambda _: (
True
),
trim_lines: bool = True,
) -> list[str]
Extract lines from the text based on a filter function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to extract lines from. |
required |
include_filter_fun
|
Callable[[str], bool]
|
A function that takes a line and returns True if it should be included, False otherwise. Defaults to a function that includes all lines. |
lambda _: True
|
trim_lines
|
bool
|
Whether to trim bullet points, list numbers and whitespace from the beginning/end of each line. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list of extracted lines. |
Source code in evalsense/utils/text.py
extract_score
Parse one complete, in-range, non-negative decimal integer.
Outer whitespace and leading zeroes are accepted. Signs, fractions, decimals, wrappers and explanatory text are rejected instead of searching for digits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The complete judge response. |
required |
min_score
|
int
|
The minimum valid score. Defaults to 1. |
1
|
max_score
|
int
|
The maximum valid score. Defaults to 10. |
10
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The validated integer rating. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the response is not a complete in-range integer. |
Source code in evalsense/utils/text.py
extract_ternary_answer
extract_ternary_answer(
text: str,
binary_only: bool,
unknown_on_mismatch: bool = True,
) -> bool | None
Extract a ternary answer (True/False/Unknown) from the text.
This legacy helper searches within text and can return unknown on mismatch.
Use parse_ternary_answer for complete-response validation with distinct errors.
Valid answers are 'yes', 'no', 'true', 'false', 'unknown', and 'I don't know'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to extract the answer from. |
required |
binary_only
|
bool
|
If True, only 'yes' or 'no' are valid answers. |
required |
unknown_on_mismatch
|
bool
|
If True, return None for answers not matching any of the valid answers. If False, raise an error. Only relevant if binary_only is False. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
bool | None
|
bool | None: The extracted answer - bool for True/False, None for Unknown. |
Source code in evalsense/utils/text.py
extract_weighted_binary_answer
Extract a weighted binary answer from the model output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output
|
ModelOutput
|
The model output containing logprobs. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The model probability of the answer being True. |
Source code in evalsense/utils/text.py
extract_weighted_rating
extract_weighted_rating(
output: ModelOutput,
min_score: int = 1,
max_score: int = 10,
) -> WeightedRating
Average complete rating tokens from a one-token judge request.
Every allowed rating must occur in the returned alternatives. The result is conditional on the valid rating tokens in that table, whose probabilities are renormalised. It is not a full-sequence probability estimate.
Callers must request at most one output token. Missing ratings, multi-token responses and inconsistent probability evidence raise ValueError.
Source code in evalsense/utils/text.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
extract_weighted_score
Return the conditional rating mean from validated one-token evidence.
See extract_weighted_rating for the token and probability requirements.
Source code in evalsense/utils/text.py
format_template
Format a template string with the provided keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str
|
The template string to format. |
required |
**kwargs
|
dict[str, any]
|
Keyword arguments to replace placeholders in the template. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted string. |
Source code in evalsense/utils/text.py
parse_ternary_answer
Parse a complete answer, allowing one balanced presentation wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
A complete yes/no/true/false/unknown/"I don't know" label, ignoring case and surrounding whitespace. A single quote pair (straight or typographic), Markdown emphasis (, *, _, __) or inline backticks may wrap the label. One final period or exclamation mark is allowed inside or outside the wrapper, but not both. Nested wrappers, explanations, JSON and conflicting labels are rejected. |
required |
binary_only
|
bool
|
Reject explicit unknown answers when True. |
False
|
Returns:
| Type | Description |
|---|---|
bool | None
|
bool | None: True or False for a binary label, or None for an explicit unknown. Invalid text never returns None. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the complete response does not match the accepted format, or an unknown label is used in binary-only mode. Messages omit text. |
Source code in evalsense/utils/text.py
strip_list_prefix
Remove recognised list prefixes without stripping leading numerical values.
Prefixes are -, *, •, 1., 1) or [1]. They must be
followed by whitespace or, for unspaced lists, a letter. Ambiguous prefixes
before numbers or punctuation are retained: 1.5 mg and -2 mg are not
list numbering. Leading/trailing whitespace is removed; internal text is kept.
Source code in evalsense/utils/text.py
validate_score_range
Require non-negative integer endpoints and a usable normalisation span.