> ## Documentation Index
> Fetch the complete documentation index at: https://docs.vals.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Metrics

## Intro

Custom Metrics allow you to define your own metrics for your runs. This is done by defining a function that takes in the output of a test, and returns a pass rate.

A basic example that calculates the pass rate if the evaluation is greater than 0.5:

```python theme={null}
import pandas as pd

def custom_metric_function(df: pd.DataFrame) -> float:
    if df.empty or "eval" not in df:
        return 0.0

    total = len(df)
    passed = (df["eval"] > 0.5).sum()

    return passed / total  # percentage (between 0 and 1)
```

# Managing Custom Metrics

Custom Metrics can be managed from the Settings page, which you can access by clicking your username in the top right corner.

<img src="https://mintcdn.com/valsai/wQjTO96-HXTL4O8a/images/custom_metrics/table.png?fit=max&auto=format&n=wQjTO96-HXTL4O8a&q=85&s=fa5036c0992196349a9cf93df5a19623" alt="Table" width="2560" height="1024" data-path="images/custom_metrics/table.png" />

# Creating Custom Metrics

Each custom metric is defined by a name and a python function, with an optional description.

<img src="https://mintcdn.com/valsai/wQjTO96-HXTL4O8a/images/custom_metrics/create.png?fit=max&auto=format&n=wQjTO96-HXTL4O8a&q=85&s=1b77ea6da7cf66e3a31d2681091990b8" alt="Create" width="1610" height="1476" data-path="images/custom_metrics/create.png" />

# Format

The function used for the custom metric **must** match the following signature:

```python theme={null}
def custom_metric_function(df: pd.DataFrame) -> float:
```

You may define any other functions and use them, but the entrypoint must be `custom_metric_function`.
The function takes in a pandas DataFrame, and returns a float representing the pass rate.

You have access to all fields in the DataFrame, the format is as follows:

> NOTE: Each row in the DataFrame represents a Check Result

| Field                 | Type            | Description                                                                                 |
| --------------------- | --------------- | ------------------------------------------------------------------------------------------- |
| `id`                  | `string`        | Unique identifier                                                                           |
| `tags`                | `array[string]` | Tags defined inside of the test                                                             |
| `input`               | `string`        | Input provided to the LLM, etc. "What is QSBS?"                                             |
| `input_context`       | `object`        | Context provided alongside the input (ex. conversation history)                             |
| `output`              | `string`        | The LLM output model's generated response.                                                  |
| `output_context`      | `object`        | Context provided alongside the output. (ex. reasoning)                                      |
| `right_answer`        | `string`        | The right answer (user provided)                                                            |
| `refused_to_answer`   | `boolean`       | Whether the model refused to answer the prompt                                              |
| `is_rephrasal`        | `boolean`       | Indicates if this was a rephrased version of another input                                  |
| `been_rephrased`      | `boolean`       | Indicates if this input has been rephrased into other versions                              |
| `file_ids`            | `array[string]` | IDs of associated files                                                                     |
| `operator`            | `string`        | Operator used for evaluation (e.g., `equals`, `contains`).                                  |
| `criteria`            | `string`        | The key concept, keyword, or criteria that evaluation is focused on (e.g., `"Copernicus"`). |
| `eval`                | `number`        | Binary evaluation score: `1` for pass, `0` for fail.                                        |
| `cont`                | `number`        | Confidence score                                                                            |
| `feedback`            | `string`        | Textual explanation or reasoning for the evaluation decision.                               |
| `is_global`           | `boolean`       | Whether the test case was applied globally or within a specific subset.                     |
| `modifiers.extractor` | `string`        | Extraction modifier                                                                         |

Example:

```python theme={null}
"""
  {
    "id":"852b5372-6768-4cb2-8f88-9937bda73fc4",
    "tags":[
      "HISTORY",
      "$200",
      "Jeopardy!"
    ],
    "input":"'For the last 8 years of his life, Galileo was under house arrest for espousing this man's theory'",
    "input_context":{},
    "output":"Galileo Galilei was under house arrest for espousing the heliocentric theory proposed by Nicolaus Copernicus. Copernicus suggested that the Earth and other planets revolve around the Sun, which contradicted the geocentric view that was widely accepted at the time. Galileo's support of this theory, particularly after publishing \"Dialogue Concerning the Two Chief World Systems,\" led to his trial and subsequent house arrest by the Roman Catholic Church.",
    "output_context":{},
    "right_answer":"",
    "refused_to_answer":false,
    "is_rephrasal":false,
    "been_rephrased":false,
    "file_ids":[],
    "operator":"equals",
    "criteria":"Copernicus",
    "eval":0,
    "cont":0,
    "feedback":"Text 1 provides detailed information about Galileo's support for the heliocentric theory and his subsequent house arrest, while Text 2 only mentions Copernicus without any context or details. The two texts do not cover the same core concepts or convey the same essential meaning.",
    "is_global":false,
    "modifiers.extractor":""
  },
"""
```

# Testing Custom Metrics

Before creating or updating your custom metric, you should test it on a successful run result.
You can do this by clicking `Run` button located right below the code area, then selecting a run result.

This will run your custom metric as if it were being used in an actual run, and output the pass rate as well as any errors.

<img src="https://mintcdn.com/valsai/wQjTO96-HXTL4O8a/images/custom_metrics/test.png?fit=max&auto=format&n=wQjTO96-HXTL4O8a&q=85&s=54099563fc2b34c3d52f2fc2a8b712e8" alt="Test" width="1608" height="1476" data-path="images/custom_metrics/test.png" />

# Using Custom Metrics

To use your custom metric, simply select it inside your test suite. Any subsequent runs will use all custom metrics selected.

<img src="https://mintcdn.com/valsai/wQjTO96-HXTL4O8a/images/custom_metrics/use.png?fit=max&auto=format&n=wQjTO96-HXTL4O8a&q=85&s=78c0320a1bed7953827360c237acbba1" alt="Use" width="2560" height="1024" data-path="images/custom_metrics/use.png" />

The custom metric pass rate will be displayed in the run result page, under Run Statistics

<img src="https://mintcdn.com/valsai/wQjTO96-HXTL4O8a/images/custom_metrics/statistic.png?fit=max&auto=format&n=wQjTO96-HXTL4O8a&q=85&s=9665ab0be2c503e70f411d43a1be83a5" alt="Statistic" width="936" height="1360" data-path="images/custom_metrics/statistic.png" />

# Updating past runs

When updating a custom metric, you can choose to apply the changes to all previous runs that used this metric. If selected, the system will re-run the updated metric on those past runs and recalculate the pass rate for each, displaying the revised results.

<img src="https://mintcdn.com/valsai/wQjTO96-HXTL4O8a/images/custom_metrics/update_past.png?fit=max&auto=format&n=wQjTO96-HXTL4O8a&q=85&s=d9f504d86a73cf08312ffbece45034ad" alt="Update Past" width="1604" height="1780" data-path="images/custom_metrics/update_past.png" />
