Experiment API¶
AI4RAGExperiment ¶
AI4RAGExperiment(
documents: list[Document],
benchmark_data: DataFrame,
search_space: AI4RAGSearchSpace,
vector_store_type: Literal["chroma", "ls_milvus"],
optimizer_settings: OptimizerSettings,
event_handler: BaseEventHandler,
client: LlamaStackClient | Any = None,
optimization_metric: str = MetricType.FAITHFULNESS,
**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[Document | tuple[str, str]]) –List of documents to embed in vector db and use as context in RAG. When given as list of langchain's Document instances, both content and document ids must be provided: Document(page_content=..., metadata={document_id: 'some_id'}) When given as list of tuples it should be (content, document_id)
-
benchmark_data(DataFrame | BenchmarkData) –Structure with 3 columns: 'question', 'correct_answers' and - if applicable - 'correct_answer_document_ids'.
-
search_space(AI4RAGSearchSpace) –Grid of parameters used during hyperparameter optimization.
-
vector_store_type(Literal['chroma', 'ls_milvus']) –Specific type of Vector Data Base that will be used during the experiment.
-
optimizer_settings(OptimizerSettings) –Settings for the optimizer to be used during the experiment.
-
client(LlamaStackClient | Any, default:None) –Instance of the llama stack client or other client allowing to communicate with the available vector store providers.
-
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(str, default:MetricType.FAITHFULNESS) –Metrics that should be used for calculating final score value that will be minimized. This sequence should contain 1 value for first release.
Other Parameters:
-
job_id(str) –Unique identifier for a job.
-
metrics(Sequence[str]) –Metrics that will be evaluated during AutoRAG experiment. Not all of these metrics will be used to calculate final score, but they will be included in the evaluation results.
-
evaluator(BaseEvaluator) –An implementation of the BaseEvaluator class, that will be used by the AI4RAGExperiment To evaluate the RAG pattern performance and will be utilized during the optimization process.
-
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.
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.
Functions¶
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.
Returns:
-
float–A single evaluation score obtained by the executed rag pattern.
Source code in ai4rag/core/experiment/experiment.py
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 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 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | |
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.