Skip to content

Starts With

The StartsWithMetric checks whether the actual output starts with a specified prefix string.

How It Works

  1. Optionally converts both the output and prefix to lowercase
  2. Checks if the output starts with the given prefix
  3. Returns 1.0 if the prefix matches, 0.0 otherwise

Parameters

Parameter Type Default Description
threshold float 1.0 Minimum score to pass
prefix str required The prefix string to check for
case_sensitive bool True Whether matching is case-sensitive

Required Fields

Field Required
input No
actual_output Yes
expected_output No

Usage

from eval_lib.metrics import StartsWithMetric
from eval_lib.test_case import EvalTestCase
import asyncio

test_case = EvalTestCase(
    input="Write a formal greeting",
    actual_output="Dear Sir or Madam, I am writing to inquire about..."
)

metric = StartsWithMetric(threshold=1.0, prefix="Dear", case_sensitive=False)
result = asyncio.run(metric.evaluate(test_case))
print(result.score)  # 1.0

Example Scenarios

Pass (1.0)

metric = StartsWithMetric(prefix="```json", case_sensitive=True)
EvalTestCase(
    input="Return JSON",
    actual_output='```json\n{"key": "value"}\n```'
)
# Output starts with the expected prefix

Fail (0.0)

metric = StartsWithMetric(prefix="Yes", case_sensitive=True)
EvalTestCase(
    input="Can you help?",
    actual_output="Of course! I'd be happy to help."
)
# Output does not start with "Yes"