QA plan: Answer similarity eval — BERTScore and SentenceTransformers (#78)
Story: Answer similarity eval: BERTScore + SentenceTransformers (#78)
Feature: Retrieval-quality eval harness (#75)
Capability: Retrieval & generation API (#11)
Spec: specs/retrieval.md
Test scope
Section titled “Test scope”| Type | Coverage |
|---|---|
| Unit | /query is called for each eval row the script does not skip, up to 3 attempts per row; the response’s answer and cited document ids are extracted correctly |
| Unit | A question longer than /query’s 2,048-character limit is skipped without a call |
| Unit | BERTScore F1 is computed per question; bert_f1_mean and bert_f1_median are floats in [0.0, 1.0] |
| Unit | SentenceTransformers cosine similarity is computed per question; sentence_bert_cosine_mean and sentence_bert_cosine_median are floats in [0.0, 1.0] (raw scores below 0 are clamped to 0) |
| Unit | Output JSON contains at least the fields commit_sha, timestamp, bert_f1_mean, bert_f1_median, sentence_bert_cosine_mean, sentence_bert_cosine_median, evaluated_count, and per_question_results (per-question breakdown array) |
| Unit | Per-question breakdown includes question, ground_truth_answer, generated_answer, bert_f1, sentence_bert_cosine for each row |
| Unit | collect exits 1 with a descriptive message when /query returns a non-200 status other than 502/503/504 (a 429 or 500 is a hard failure, not retried), or an HTTP 200 whose body is not a QueryResponse |
| Unit | A question whose /query call times out, fails to connect, or returns 502/503/504 on every attempt is left unscored; score exits 0 when at least one question is scored |
| Unit | After 3 consecutive unavailable questions, the remaining questions are marked unavailable without a /query call and a WARNING is logged; any question that gets an HTTP 200 from /query (an answer or a no-match result) resets the count, and a question skipped as too long does not |
| Unit | score exits 1 when no question is scored |
| Unit | The script runs as two steps: collect writes the rows and answers to an answers file, and score reads that file and writes the report; score exits 1 on a malformed answers file, and either step exits 1 when it cannot write its output file |
| Unit | --api-url must be https:// (plain http:// only for localhost or 127.0.0.1), and --count is bounded to 1-100 |
| Unit | The script mints an API key for the run with a quota covering every attempt and a one-day expiry, and revokes it when the queries finish, including after a failure |
| Unit | collect exits 1 when the eval table is empty |
| Integration | Against a stubbed /query returning deterministic answers: output JSON is valid, bert_f1_mean, bert_f1_median, and sentence_bert_cosine_mean are in [0.0, 1.0], and two runs give reports that differ only in timestamp |
| Integration | collect then score against local Postgres: the report file is valid JSON for the run’s commit, and the per-run key is live with its quota during collect and gone afterwards |
| Integration | With the pinned models on CPU, scoring the same pairs twice gives identical scores in [0.0, 1.0] |
| CI check | No job with id-token: write installs or runs the eval group (Workflow Checks workflow, check_credential_free_builds.py); the score job has no environment and no id-token grant |
Note: backend/eval/eval_answers.py is a CI script outside backend/src/rag_sample/. Coverage floors from RAG006 do not apply. Tests assert output format, metric range bounds, and repeatability, not specific score values.
Entry criteria
Section titled “Entry criteria”- Story #76 (corpus setup) merged; eval table seeded with ground-truth answers
- QA document corpus ingested, including the eval rows’ contexts, so
/querycan retrieve them - Story #77 (retrieval eval) merged (establishes the
reports/<commit-sha>/<timestamp>-<kind>.jsonkey layout reused here) /queryendpoint deployed and accessible in QA env, or stubbed for local testsbert-base-uncasedandall-MiniLM-L6-v2model weights reachable from the CI runner (Hugging Face at the pinned revisions, or the CI cache)- The QA eval CI role
rag-sample-qa-eval-roleand therag-sample-evalbucket applied throughinfra/bootstrap, and theqa-evalGitHub environment holding secretsAWS_QA_EVAL_ROLE_ARNandQA_DB_SECRET_ARNand variablesQA_DB_CLUSTER_ARN,QA_DB_NAME, andQA_API_URL(the QAapi_endpoint)
Exit criteria
Section titled “Exit criteria”- All unit tests pass
- The
models-marked tests pass, not skip, when run by hand as part of a model upgrade, per the eval-answers runbook in rag-sample-app - Integration: script produces a valid JSON report with its metric fields in range against a stubbed
/query - The
Workflow Checkscredential-free-builds check passes collectexits 1 on an empty eval table and on a/querynon-200 status other than 502/503/504;scoreexits 1 when no question is scored- QA env run: the
collect,score, anduploadjobs ofeval-answers.ymlrun end to end against the deployed/queryendpoint, and the report lands unders3://rag-sample-eval/reports/<commit-sha>/ending-answers.jsonwith content typeapplication/json - Model download in CI completes within the job timeout
Environments
Section titled “Environments”Unit and integration: local (integration tests also need local Docker Postgres, make db-up, for the eval rows and a real api_keys row), with a fake /query transport returning deterministic answers and the models either mocked or loaded from cache. The QA env run uses the live /query endpoint and real S3. No CI hard-fail on eval score.
Tooling
Section titled “Tooling”pytest, unittest.mock for /query HTTP calls and the models. bert-score, sentence-transformers, and CPU-only torch in the backend’s eval dependency group; tests that load the real models carry the models marker and skip when the group is not installed. Docker Postgres is not required for unit tests (eval rows come from fixtures). In the QA env run, the collect job uses the QA eval CI role over OIDC for the eval rows and the run’s API key, the score job holds no credentials, and the upload job uses the role for the S3 report write with the runner’s AWS CLI. No job makes a direct Bedrock call; /query does.
Fixtures and data
Section titled “Fixtures and data”- Golden eval rows: three
EvalRowinstances, one whose ground-truth document is cited, one whose context is ingested but not cited, and one whose context is not ingested - Fake
/querytransport: a patched HTTP request function returning{"answer": "Fake answer.", "citations": [...]}per question, oranswer: nullwithreason: "no_relevant_results" - Fake scorer: a mock
AnswerScorerreturning a fixedPairScoreper pair - Answers file: the golden rows and answers written with
collected_document - Commit SHA constant
"abc1234", set asCOMMIT_SHAforscore
How tests are written
Section titled “How tests are written”def test_score_writes_report_with_required_fields(score_wired) -> None: # Given score wired to the golden answers file and a fake scorer
# When score runs code = ea.main(score_wired["args"])
# Then it exits 0 and writes a report with every required field assert code == 0 report = json.loads(score_wired["output"].read_text(encoding="utf-8")) assert REQUIRED_FIELDS <= set(report) assert len(report["per_question_results"]) == 3 assert 0.0 <= report["bert_f1_mean"] <= 1.0 assert 0.0 <= report["sentence_bert_cosine_mean"] <= 1.0- BERTScore and SentenceTransformers download about 510 MB of model weights on first use. The models are pinned to Hugging Face commit revisions and cached in CI under a key that changes with the pins; a cache miss adds the download to the job time.
- Scores are repeatable on CPU at the pinned revisions for fixed answers, but a pair’s score can shift by about 1e-7 when batch composition changes. Do not use a specific score as a regression gate in CI.
- Generated answers are only as repeatable as the generation model’s sampling settings, so live runs are compared by their means and medians, not per-question values.
- The
/queryendpoint requires a valid bearer token. The script mints a per-run key; a rejected key (HTTP 401/403) must cause a clear non-zero exit, not a silent empty-answer result.
© 2026 Benjamin Arunski