Skip to content

datarobot_genai.eval.benchmarks.response_similarity

response_similarity

Response Similarity: lexical or embedding similarity to example answers (judge-free).

The fuzzy sibling of answer_correctness. Where answer_correctness asks "did the agent say exactly this?", this asks "how close did the agent get to one of these?". Each case lists one or more acceptable responses, and the score is the response's similarity to the closest one.

Lexical metrics compare words, not meaning, so a paraphrase with no shared words ("30 days" vs "a month") scores low. embedding_cosine tolerates rewording but measures topic, not facts. Use response_acceptability (the judge-based twin, same dataset shape) when correctness matters.

Lexical metrics, all in [0, 1], computed on lowercased words with punctuation and common stopwords removed: token_recall fraction of the example's words that appear in the response (the default; tolerant of verbose agents) token_f1 balances covering the example against adding extra words (SQuAD-style); penalizes long responses to short examples rouge_l longest run of words the two share in the same order (ROUGE-L F-measure); rewards matching word order

Embedding metric, in [0, 1] (negative cosines are clamped to 0): embedding_cosine cosine similarity of the two texts' embeddings, from a local fastembed model (benchmark.params.embedding_model, default BAAI/bge-small-en-v1.5, ~70MB downloaded on first use to FASTEMBED_CACHE_PATH). With the default model, unrelated text scores about 0.4 and a near-miss wrong answer ("90 degrees" vs "100 degrees") can score as high as a correct paraphrase, so raise run.pass_threshold well above 0.5 and read it as "on topic", not "correct".

Scoring (judge-free): the score is the raw similarity to the closest acceptable example, not a pass/fail. passed compares it against the pipeline's run.pass_threshold (default 0.5), which users tune to their data. The reason says in plain words what the number means and which example matched.

unacceptable_responses do not change the score. When given, the reason also reports the closest unacceptable example and flags the case when the response is at least as close to it as to any acceptable one.

Pick the default metric in the pipeline YAML; a case can override it: benchmark: params: metric: token_recall embedding_model: BAAI/bge-small-en-v1.5 # embedding_cosine only

Dataset fields

input (required) the prompt sent to the agent acceptable_responses (required) str | list[str] example good answers unacceptable_responses (optional) str | list[str] example bad answers metric (optional) per-case override of the pipeline metric

A case with no acceptable_responses is scored inconclusive, not failed.

evaluate_response

evaluate_response(response: str, metadata: dict[str, Any], params: dict[str, Any] | None = None) -> dict[str, Any]

Pure scoring logic, importable for unit tests, no judge, no I/O.

Source code in datarobot_genai/eval/benchmarks/response_similarity.py
def evaluate_response(
    response: str, metadata: dict[str, Any], params: dict[str, Any] | None = None
) -> dict[str, Any]:
    """Pure scoring logic, importable for unit tests, no judge, no I/O."""
    acceptable = _as_list(metadata.get("acceptable_responses"))
    if not acceptable:
        return {"reason": "no acceptable_responses provided: cannot score"}

    params = PARAMS if params is None else params
    metric = str(metadata.get("metric") or params.get("metric") or DEFAULT_METRIC).lower()
    if metric not in METRICS:
        return {
            "reason": f"unknown metric '{metric}' (expected one of: "
            f"{', '.join(sorted(METRICS))}): cannot score"
        }

    unacceptable = _as_list(metadata.get("unacceptable_responses"))
    model_name = str(params.get("embedding_model") or DEFAULT_EMBEDDING_MODEL)
    try:
        # One call for both lists, so embedding_cosine embeds the response once.
        scores = _similarities(metric, response or "", acceptable + unacceptable, model_name)
    except Exception as e:  # noqa: BLE001
        # Only the embedding path can raise (model download or load failure).
        return {"reason": f"{metric} could not run ({type(e).__name__}: {e}): cannot score"}

    best, best_ref = _best_match(scores[: len(acceptable)], acceptable)
    explanation = _EXPLANATIONS[metric]
    if metric == EMBEDDING_METRIC:
        explanation += f"; model {model_name}"
    reason = (
        f"{metric} {best:.2f} ({explanation}); "
        f"closest of {len(acceptable)} acceptable: '{_truncate(best_ref)}'"
    )

    if unacceptable:
        worst, worst_ref = _best_match(scores[len(acceptable) :], unacceptable)
        reason += f"; closest unacceptable {worst:.2f}: '{_truncate(worst_ref)}'"
        if worst >= best:
            reason += " (WARNING: at least as close as any acceptable example)"

    value = round(best, 4)
    return {"score": value, metric: value, "reason": reason}