Experiment API¶
AI4RAGExperiment ¶
AI4RAGExperiment(documents: list[DoclingDocument], benchmark_data: DataFrame, search_space: AI4RAGSearchSpace, optimizer_settings: OptimizerSettings, event_handler: BaseEventHandler, vector_store_config: BaseVectorStoreConfig, optimization_metric: RAGMetric = Metrics.OVERALL_SCORE, **kwargs)
Class responsible for conducting AutoRAG experiment, that consists of finding the best hyperparameters for several steps/stages.
AI4RAGExperiment is essentially an orchestrator for the RAG Patterns hyperparameters optimization for the desired metric. It requires from user to provide fully defined search space on which the experiment will be executed.
AI4RAG uses 'BaseRAGTemplate' inheriting classes as definitions on how to build and utilize RAG Pattern with the given search space nodes.
Parameters:
-
documents(list[DoclingDocument]) –List of parsed docling documents to embed in vector db and use as context in RAG.
-
benchmark_data(DataFrame | BenchmarkData) –Structure with 3 columns: 'question', 'correct_answers' and 'correct_answer_document_keys'.
-
search_space(AI4RAGSearchSpace) –Grid of parameters used during hyperparameter optimization.
-
optimizer_settings(OptimizerSettings) –Settings for the optimizer to be used during the experiment.
-
vector_store_config(BaseVectorStoreConfig) –Connection config for the vector store backend. Its type (via
config.provider) determines which vector store implementation is used for indexing and retrieval. -
event_handler(BaseEventHandler) –Instance satisfying BaseEventHandler's interface to stream pattern evaluation results and intermediate status updates. EventHandler is an entrypoint to configure custom logging and assets handling.
-
optimization_metric(RAGMetric, default:Metrics.OVERALL_SCORE) –Metric used for calculating the final score that drives optimization. Must be a
RAGMetricinstance selected from :class:Metrics.
Other Parameters:
-
metrics(Sequence[RAGMetric]) –Metrics evaluated during the AutoRAG experiment, each a
RAGMetricinstance selected from :class:Metrics. Not all of these metrics are used to calculate the final score, but they are included in the evaluation results. When omitted, defaults are derived from the configured evaluators. -
evaluators(list[BaseEvaluator] | None) –Evaluator instances used to score RAG patterns during optimization. When
None, defaults to[UnitxtEvaluator()]. To enable LLM-as-a-Judge evaluation, pass both aUnitxtEvaluatorand aLLMaJEvaluatorconfigured with a judge model. -
n_mps_foundation_models(int) –Amount of foundation models to be further used in experiment post pre-selection.
-
n_mps_embedding_models(int) –Amount of embedding models to be further used in experiment post pre-selection.
-
inference_max_threads(int) –Defines the number of threads to use during generation model inference.
Attributes:
-
results(ExperimentResults) –Instance holding information about each iteration during the experiment. It consists of statuses, RAG pattern objects, scores and settings.
Source code in ai4rag/core/experiment/experiment.py
Attributes¶
optimization_metric property writable ¶
Get optimization metrics used for the experiment.
Methods:¶
run_pre_selection ¶
run_pre_selection(foundation_models: list[BaseFoundationModel], embedding_models: list[BaseEmbeddingModel], n_records: int = 5, random_seed: int = 17) -> dict[str, list[BaseEmbeddingModel | BaseFoundationModel]]
Run models pre-selection using ModelsPreSelector and sample of the data.
Parameters:
-
embedding_models(list[BaseEmbeddingModel]) –Embedding models to be considered during pre-selection process.
-
foundation_models(list[BaseFoundationModel]) –Foundation models to be evaluated during pre-selection process.
-
n_records(int, default:5) –Amount of records that should be used during models pre-selection.
-
random_seed(int, default:17) –Random seed value used for sampling benchmark data records.
Returns:
-
dict[str, list[BaseFoundationModel | EmbeddingModel]]–Best embedding models and foundation models found in pre-selection.
Source code in ai4rag/core/experiment/experiment.py
run_single_evaluation ¶
Evaluate a single RAG configuration and return its score using provided documents.
Parameters:
-
rag_params(RAGParamsType) –A dictionary containing rag parameters as keys and their values.
-
publish_pattern(bool, default:True) –Whether to send the evaluated pattern to the event handler immediately. GAM optimization suppresses this only for warm-start candidates; GAM candidates are published as they complete.
Returns:
-
float–A single evaluation score obtained by the executed rag pattern.
Source code in ai4rag/core/experiment/experiment.py
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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
search ¶
Prepare and execute experiment to find the best RAG parameters.
Result of the search() can be reviewed via self.results as this object stores results of each evaluation or via self.event_handler with custom implementation.