MLflow Helper¶
MLflow integration utilities for experiment tracking and result management.
Recommendation
Create dedicated experiments for evaluations. Don't mix with production traces.
Async (preferred)¶
ragpill.evaluate_testset_with_mlflow
async
¶
Evaluate a testset with comprehensive MLflow logging and tracking.
This function orchestrates the complete evaluation workflow:
- Sets up MLflow experiment and starts a run
- Wraps the task with MLflow tracing
- Evaluates all test cases using the provided task
- Cleans up LLMJudge traces (which clutter the UI)
- Maps traces to evaluation results
- Logs metrics, parameters, and assessments to MLflow
- Tags traces with case metadata for filtering and analysis
The function automatically:
- Logs overall accuracy
- Logs accuracy per tag for granular analysis
- Attaches feedback/assessments to each trace
- Preserves trace IDs for later inspection
- Logs model parameters for reproducibility
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
testset
|
Dataset[Any, Any, CaseMetadataT]
|
The dataset to evaluate, created via
|
required |
task
|
TaskType
|
The task to evaluate - can be either synchronous or asynchronous callable.
Should accept inputs of type |
required |
mlflow_settings
|
MLFlowSettings | None
|
MLflow configuration settings. If None, loads from environment variables:
- |
None
|
model_params
|
dict[str, str] | None
|
Optional dictionary of model/system parameters to log for reproducibility.
Examples: |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: Evaluation results with columns:
- |
Example
import mlflow
from ragpill.csv.testset import load_testset, default_evaluator_classes
from ragpill.mlflow_helper import evaluate_testset_with_mlflow
from ragpill.settings import MLFlowSettings
# Load test dataset
testset = load_testset(
csv_path="testset.csv",
evaluator_classes=default_evaluator_classes,
)
# Define your task
async def my_agent(question: str) -> str:
# Your agent logic here
return f"Answer to: {question}"
# Run evaluation with MLflow tracking
results_df = evaluate_testset_with_mlflow(
testset=testset,
task=my_agent,
model_params={
"model": "gpt-4o-mini",
"temperature": "0.7",
"system_prompt": "You are a helpful assistant",
}
)
# Analyze results
print(f"Overall accuracy: {results_df['evaluator_result'].mean():.2%}")
Note
This function will start and end an MLflow run. Make sure MLflow tracking is properly configured before calling this function.
See Also
load_testset:
Create test datasets from CSV files
MLFlowSettings:
MLflow configuration settings
Source code in src/ragpill/mlflow_helper.py
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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 | |
Sync wrapper¶
Use this when await is not available (plain scripts, CLI tools, synchronous test suites).
It runs the async version in a dedicated thread, so it is safe to call from both sync and
async contexts — including Jupyter notebooks and FastAPI route handlers.
ragpill.evaluate_testset_with_mlflow_sync
¶
Synchronous wrapper around evaluate_testset_with_mlflow.
Prefer the async version when possible. Use this wrapper when you cannot use await —
for example in plain scripts, CLI tools, or synchronous test suites.
Internally, this runs the async function via asyncio.run() inside a fresh thread from a
ThreadPoolExecutor. That thread has no running event loop, so asyncio.run() always
succeeds — even when the caller is already inside a running event loop (e.g. Jupyter,
FastAPI, or an asyncio-based test runner).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
testset
|
Dataset[Any, Any, CaseMetadataT]
|
The dataset to evaluate. |
required |
task
|
TaskType
|
The task to evaluate — sync or async callable. |
required |
mlflow_settings
|
MLFlowSettings | None
|
MLflow configuration. If None, loaded from environment variables. |
None
|
model_params
|
dict[str, str] | None
|
Optional model/system parameters to log for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pandas.DataFrame: Same evaluation results as the async version. |
See Also
evaluate_testset_with_mlflow:
The async version of this function.