|
| 1 | +import asyncio |
| 2 | +from types import SimpleNamespace |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from eval_protocol.models import EvaluationRow, Message |
| 7 | +from eval_protocol.pytest import SingleTurnRolloutProcessor |
| 8 | + |
| 9 | + |
| 10 | +class _DummyConfig: |
| 11 | + def __init__(self): |
| 12 | + self.completion_params = {"model": "fake-model", "temperature": 0} |
| 13 | + self.semaphore = asyncio.Semaphore(10) |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.asyncio |
| 17 | +async def test_single_turn_drops_trailing_assistant_by_default(monkeypatch): |
| 18 | + # Arrange dataset row with trailing assistant message |
| 19 | + row = EvaluationRow( |
| 20 | + messages=[ |
| 21 | + Message(role="user", content="What is 2+2?"), |
| 22 | + Message(role="assistant", content="Old response"), |
| 23 | + ] |
| 24 | + ) |
| 25 | + |
| 26 | + # Capture the messages payload passed to the LLM call |
| 27 | + captured = {} |
| 28 | + |
| 29 | + # Patch module-level imports in the processor module |
| 30 | + import eval_protocol.pytest.default_single_turn_rollout_process as mod |
| 31 | + |
| 32 | + class StubChoices: |
| 33 | + pass |
| 34 | + |
| 35 | + class StubModelResponse: |
| 36 | + def __init__(self, text: str): |
| 37 | + self.choices = [StubChoices()] |
| 38 | + # Emulate OpenAI-like response.message fields |
| 39 | + self.choices[0].message = SimpleNamespace(content=text, tool_calls=None) |
| 40 | + # Minimal usage payload |
| 41 | + self.usage = SimpleNamespace(prompt_tokens=1, completion_tokens=1, total_tokens=2) |
| 42 | + |
| 43 | + async def fake_acompletion(**kwargs): |
| 44 | + # Verify that trailing assistant was dropped before sending |
| 45 | + msgs = kwargs.get("messages", []) |
| 46 | + assert msgs, "Expected non-empty messages payload" |
| 47 | + captured["messages"] = msgs |
| 48 | + assert msgs[-1]["role"] != "assistant", "Trailing assistant should be dropped by default" |
| 49 | + return StubModelResponse(text="4") |
| 50 | + |
| 51 | + # Monkeypatch the processor module's symbols to avoid dependency on litellm types |
| 52 | + monkeypatch.setattr(mod, "ModelResponse", StubModelResponse, raising=True) |
| 53 | + monkeypatch.setattr(mod, "Choices", StubChoices, raising=True) |
| 54 | + monkeypatch.setattr(mod, "acompletion", fake_acompletion, raising=True) |
| 55 | + |
| 56 | + processor = SingleTurnRolloutProcessor() |
| 57 | + config = _DummyConfig() |
| 58 | + |
| 59 | + # Act |
| 60 | + tasks = processor([row], config) |
| 61 | + out = await tasks[0] |
| 62 | + |
| 63 | + # Assert: request trimmed the trailing assistant |
| 64 | + sent_msgs = captured["messages"] |
| 65 | + assert len(sent_msgs) == 1 |
| 66 | + assert sent_msgs[0]["role"] == "user" |
| 67 | + assert out.messages[-1].role == "assistant" |
| 68 | + assert out.messages[-1].content == "4" |
| 69 | + # Ensure previous trailing assistant was not duplicated |
| 70 | + assert [m.role for m in out.messages] == ["user", "assistant"] |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.asyncio |
| 74 | +async def test_single_turn_keeps_trailing_assistant_when_disabled(monkeypatch): |
| 75 | + # Arrange dataset row with trailing assistant message |
| 76 | + row = EvaluationRow( |
| 77 | + messages=[ |
| 78 | + Message(role="user", content="Say hi"), |
| 79 | + Message(role="assistant", content="Hi!"), |
| 80 | + ] |
| 81 | + ) |
| 82 | + |
| 83 | + captured = {} |
| 84 | + |
| 85 | + import eval_protocol.pytest.default_single_turn_rollout_process as mod |
| 86 | + |
| 87 | + class StubChoices: |
| 88 | + pass |
| 89 | + |
| 90 | + class StubModelResponse: |
| 91 | + def __init__(self, text: str): |
| 92 | + self.choices = [StubChoices()] |
| 93 | + self.choices[0].message = SimpleNamespace(content=text, tool_calls=None) |
| 94 | + self.usage = SimpleNamespace(prompt_tokens=1, completion_tokens=1, total_tokens=2) |
| 95 | + |
| 96 | + async def fake_acompletion(**kwargs): |
| 97 | + msgs = kwargs.get("messages", []) |
| 98 | + captured["messages"] = msgs |
| 99 | + # With opt-out, trailing assistant is preserved |
| 100 | + assert msgs[-1]["role"] == "assistant" |
| 101 | + return StubModelResponse(text="Hello again") |
| 102 | + |
| 103 | + monkeypatch.setattr(mod, "ModelResponse", StubModelResponse, raising=True) |
| 104 | + monkeypatch.setattr(mod, "Choices", StubChoices, raising=True) |
| 105 | + monkeypatch.setattr(mod, "acompletion", fake_acompletion, raising=True) |
| 106 | + |
| 107 | + processor = SingleTurnRolloutProcessor(drop_trailing_assistant_messages=False) |
| 108 | + config = _DummyConfig() |
| 109 | + |
| 110 | + # Act |
| 111 | + tasks = processor([row], config) |
| 112 | + out = await tasks[0] |
| 113 | + |
| 114 | + # Assert: both original messages plus new assistant |
| 115 | + sent_msgs = captured["messages"] |
| 116 | + assert [m["role"] for m in sent_msgs] == ["user", "assistant"] |
| 117 | + assert [m.role for m in out.messages] == ["user", "assistant", "assistant"] |
| 118 | + assert out.messages[-1].content == "Hello again" |
0 commit comments