Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ All notable changes to SkillOpt are documented here. This project adheres to
- Ignore sentence-ending periods when normalizing OfficeQA answers, so a
correct answer such as `42.` matches the gold `42` instead of scoring zero
exact match and F1 (thanks @Shizoqua).
- Score the last `<answer>` tag in an OfficeQA response, as SearchQA and
LiveMathematicianBench already do, so a model that revises its answer is
graded on the revision instead of the draft (thanks @Shizoqua).

### Security
- Prevent managed-identity credentials from being sent to non-Azure or
Expand Down
8 changes: 5 additions & 3 deletions skillopt/envs/officeqa/rollout.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,11 @@ def _build_user(
parts.append("Use the model's built-in web search tool when needed. Return the final answer inside <answer>...</answer>.")
return "\n\n".join(parts)
def _extract_answer(text: str) -> str:
match = _FINAL_RE.search(text)
if match:
return match.group(1).strip()
# Score the last tagged answer, like SearchQA and LiveMathematicianBench, so a
# model that revises its answer within one response is graded on the revision.
matches = _FINAL_RE.findall(text)
if matches:
return matches[-1].strip()
lines = [line.strip() for line in text.splitlines() if line.strip()]
return lines[-1] if lines else text.strip()
def _extract_search_queries(text: str) -> list[str]:
Expand Down
55 changes: 55 additions & 0 deletions tests/test_officeqa_answer_extraction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""OfficeQA final-answer extraction when a response contains more than one <answer> tag."""

from __future__ import annotations

from types import SimpleNamespace

import pytest

from skillopt.envs.officeqa import rollout

ITEM = {"id": "treasury", "question": "When was the declaration signed?", "ground_truth": "1776"}


@pytest.fixture(autouse=True)
def offline_backend(monkeypatch):
for name in ("OFFICEQA_CUSTOM_SEARCH_AUTH", "OFFICEQA_SEARCH_API_URL", "PARALLEL_API_KEY"):
monkeypatch.delenv(name, raising=False)
monkeypatch.setattr(rollout, "get_target_backend", lambda: "openai_chat")


def _run(tmp_path, monkeypatch, response: str) -> dict:
docs = tmp_path / "docs"
docs.mkdir()
monkeypatch.setattr(rollout, "chat_target_messages", lambda **kwargs: (SimpleNamespace(content=response), {}))
return rollout.process_one(
ITEM, str(tmp_path / "out"), "", search_mode="offline", use_local_tools=False, data_dirs=[str(docs)]
)


def test_revised_answer_is_scored_not_the_draft(tmp_path, monkeypatch):
row = _run(tmp_path, monkeypatch, "<answer>1774</answer>\nChecking the date again.\n<answer>1776</answer>")

assert row["predicted_answer"] == "1776"
assert row["hard"] == 1


def test_draft_answer_is_not_credited_when_the_revision_is_wrong(tmp_path, monkeypatch):
row = _run(tmp_path, monkeypatch, "<answer>1776</answer> On reflection: <answer>1774</answer>")

assert row["predicted_answer"] == "1774"
assert row["hard"] == 0


@pytest.mark.parametrize(
("text", "expected"),
[
("<answer>42</answer>", "42"),
("<ANSWER> 42 </ANSWER>", "42"),
("<answer>\n12.5%\n</answer>", "12.5%"),
("draft <answer>1</answer> final <answer>2</answer>", "2"),
("no tags\nlast line", "last line"),
],
)
def test_extract_answer(text, expected):
assert rollout._extract_answer(text) == expected