Skip to main content

Library for RAG evaluation

Project description

AI DIAL RAG EVAL

PyPI version

Overview

Library designed for RAG (Retrieval-Augmented Generation) evaluation, where retrieval and generation metrics are calculated.

Usage

Install the library using pip:

pip install aidial-rag-eval

Example

The example of how to get retrieval metrics along with answer inference based on the context.

import pandas as pd
from langchain_openai import AzureChatOpenAI
from aidial_rag_eval import create_rag_eval_metrics_report
from aidial_rag_eval.metric_binds import CONTEXT_TO_ANSWER_INFERENCE,\
    ANSWER_TO_GROUND_TRUTH_INFERENCE, GROUND_TRUTH_TO_ANSWER_INFERENCE

llm = AzureChatOpenAI(model="gemini-2.5-flash-lite")

df_ground_truth = pd.DataFrame([
    {
        "question": "What is the diameter of the Earth and the name of the biggest ocean?",
        "documents": ["earth.pdf"],
        "facts": ["The diameter of the Earth is approximately 12,742 kilometers.", "The biggest ocean on Earth is the Pacific Ocean."],
        "answer": "The Earth's diameter measures about 12,742 kilometers, and the Pacific Ocean is the largest ocean on our planet."
    },])
df_answer = pd.DataFrame([
    {
        "question": "What is the diameter of the Earth and the name of the biggest ocean?",
        "documents": ["earth.pdf"],
        "context":  [
            "The Earth, our home planet, is the third planet from the sun. It's the only planet known to have an atmosphere containing free oxygen and oceans of liquid water on its surface. The diameter of the Earth is approximately 12,742 kilometers.",
            "The Pacific Ocean is the largest and deepest of Earth's oceanic divisions, extending from the Arctic Ocean in the north to the Southern Ocean in the south."
        ],
        "answer": "The Earth has a diameter of approximately 12,742 kilometers."
    },
])

df_metrics = create_rag_eval_metrics_report(
    df_ground_truth,
    df_answer,
    llm=llm,
    metric_binds=[
        CONTEXT_TO_ANSWER_INFERENCE,
        ANSWER_TO_GROUND_TRUTH_INFERENCE,
        GROUND_TRUTH_TO_ANSWER_INFERENCE,
    ],
)
print(df_metrics[["facts_ranks", "recall", 'precision', 'mrr', 'f1', 'ctx_ans_inference', 'ans_gt_inference', 'gt_ans_inference']])

It is expected to see the following results:

recall precision mrr f1 ctx_ans_inference ans_gt_inference gt_ans_inference
0.5 0.5 0.5 0.5 1.0 0.5 1.0

In this table:

  • "recall" of 0.5 indicates that only 1 out of 2 ground truth facts were found in the context.
  • "precision" of 0.5 reflects that just 1 context chunk out of 2 includes any ground truth facts.
  • The prefix of the inference metrics signifies the premise and hypothesis in the following format: premise_hypothesis_inference.
    • "ctx" refers to 'context'
    • "ans" refers to 'answer'
    • "gt" refers to 'ground truth answer'
  • "ctx_ans_inference" and "ans_gt_inference" values of 1.0 mean our answer can be derived directly from the context and the ground truth answer, respectively.
  • "gt_ans_inference" of 0.5, denotes that the ground truth answer can only be partially inferred from our answer.

Recommended models

The algorithm is token-intensive. Considering the balance between quality and price, the following models are recommended:

  • gemini-2.5-flash-lite
  • gpt-5-mini
  • gemini-2.0-flash-lite
  • gpt-5-nano

Developer environment

This project uses Python>=3.11 and Poetry>=2.2.1 as a dependency manager.

Check out Poetry's documentation on how to install it on your system before proceeding.

To install requirements:

poetry install

This will install all requirements for running the package, linting, formatting and tests.

Lint

Run the linting before committing:

make lint

To auto-fix formatting issues run:

make format

Test

Run unit tests locally for available python versions:

make test

Run unit tests for the specific python version:

make test PYTHON=3.11

The generation evaluation requires an access to the LLM. The generation evaluation tests (located in tests/llm_tests directory) use cached LLM responses by default. To run the tests with real LLM responses, you need add --llm-mode=real argument to the test command:

make test PYTHON=3.11 ARGS="--llm-mode=real"

The test run with real LLM responses requires the following environment variables to be set:

Variable Description
DIAL_URL The URL of the DIAL server.
DIAL_API_KEY The API key for the DIAL server.

Copy .env.example to .env and customize it for your environment.

Clean

To remove the virtual environment and build artifacts run:

make clean

Build

To build the package run:

make build

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aidial_rag_eval-0.6.0.dev16.tar.gz (29.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aidial_rag_eval-0.6.0.dev16-py3-none-any.whl (45.5 kB view details)

Uploaded Python 3

File details

Details for the file aidial_rag_eval-0.6.0.dev16.tar.gz.

File metadata

  • Download URL: aidial_rag_eval-0.6.0.dev16.tar.gz
  • Upload date:
  • Size: 29.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.15 Linux/6.17.0-1011-azure

File hashes

Hashes for aidial_rag_eval-0.6.0.dev16.tar.gz
Algorithm Hash digest
SHA256 daf2c1a88408eac0de104201483ddb79d78531e3c36034d62b141b0543058d72
MD5 d1c680e482fab9207138cfa04966fc8f
BLAKE2b-256 fefd5ba03ee404d6bf0472d019511dd609c1d73248c84558f4c5ca88a3735d65

See more details on using hashes here.

File details

Details for the file aidial_rag_eval-0.6.0.dev16-py3-none-any.whl.

File metadata

  • Download URL: aidial_rag_eval-0.6.0.dev16-py3-none-any.whl
  • Upload date:
  • Size: 45.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.15 Linux/6.17.0-1011-azure

File hashes

Hashes for aidial_rag_eval-0.6.0.dev16-py3-none-any.whl
Algorithm Hash digest
SHA256 176bb5436bc9fadd22b815a656cc621130efdf16fe983f771e47ffdf06280f62
MD5 23e020d87c30697e2be9e76d123deb46
BLAKE2b-256 1f603f747d9bbac6c84e34626258ce3ea1eb8c1db69acb1940134e78b97d3f23

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page