|
| 1 | +import pytest |
| 2 | +from unittest.mock import MagicMock |
| 3 | + |
| 4 | +from agentevals.config import OpenAIEvalDef |
| 5 | +from agentevals.openai_eval_backend import ( |
| 6 | + _build_jsonl_items, |
| 7 | + _build_testing_criteria, |
| 8 | + evaluate_openai_eval, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def _label_grader(**overrides): |
| 13 | + base = { |
| 14 | + "type": "label_model", |
| 15 | + "model": "gpt-4o-mini", |
| 16 | + "input": [{"role": "user", "content": "Rate: {{ item.actual_response }}"}], |
| 17 | + "labels": ["good", "bad"], |
| 18 | + "passing_labels": ["good"], |
| 19 | + } |
| 20 | + base.update(overrides) |
| 21 | + return base |
| 22 | + |
| 23 | + |
| 24 | +def _invocation(text: str): |
| 25 | + inv = MagicMock() |
| 26 | + inv.final_response.parts = [MagicMock(text=text)] |
| 27 | + return inv |
| 28 | + |
| 29 | + |
| 30 | +class TestOpenAIEvalDefValidation: |
| 31 | + def test_text_similarity_valid(self): |
| 32 | + d = OpenAIEvalDef(name="sim", grader={"type": "text_similarity", "evaluation_metric": "bleu"}) |
| 33 | + assert d.grader["type"] == "text_similarity" |
| 34 | + |
| 35 | + def test_text_similarity_missing_metric(self): |
| 36 | + with pytest.raises(Exception, match="evaluation_metric"): |
| 37 | + OpenAIEvalDef(name="sim", grader={"type": "text_similarity"}) |
| 38 | + |
| 39 | + def test_text_similarity_bad_metric(self): |
| 40 | + with pytest.raises(Exception, match="Unknown evaluation_metric"): |
| 41 | + OpenAIEvalDef(name="sim", grader={"type": "text_similarity", "evaluation_metric": "invalid"}) |
| 42 | + |
| 43 | + def test_label_model_valid(self): |
| 44 | + d = OpenAIEvalDef(name="lm", grader=_label_grader()) |
| 45 | + assert d.grader["type"] == "label_model" |
| 46 | + |
| 47 | + @pytest.mark.parametrize("field", ["model", "input", "labels", "passing_labels"]) |
| 48 | + def test_label_model_missing_required_field(self, field): |
| 49 | + with pytest.raises(Exception, match=field): |
| 50 | + OpenAIEvalDef(name="lm", grader=_label_grader(**{field: None})) |
| 51 | + |
| 52 | + def test_unsupported_grader_type(self): |
| 53 | + with pytest.raises(Exception, match="Unsupported grader type"): |
| 54 | + OpenAIEvalDef(name="x", grader={"type": "unknown"}) |
| 55 | + |
| 56 | + |
| 57 | +class TestBuildTestingCriteria: |
| 58 | + def test_text_similarity_shape(self): |
| 59 | + d = OpenAIEvalDef(name="sim", grader={"type": "text_similarity", "evaluation_metric": "bleu"}, threshold=0.7) |
| 60 | + c = _build_testing_criteria(d) |
| 61 | + assert c["type"] == "text_similarity" |
| 62 | + assert c["evaluation_metric"] == "bleu" |
| 63 | + assert c["pass_threshold"] == 0.7 |
| 64 | + assert "{{ item.actual_response }}" in c["input"] |
| 65 | + assert "{{ item.expected_response }}" in c["reference"] |
| 66 | + |
| 67 | + def test_label_model_shape(self): |
| 68 | + grader = _label_grader() |
| 69 | + d = OpenAIEvalDef(name="quality", grader=grader) |
| 70 | + c = _build_testing_criteria(d) |
| 71 | + assert c["type"] == "label_model" |
| 72 | + assert c["model"] == "gpt-4o-mini" |
| 73 | + assert c["labels"] == ["good", "bad"] |
| 74 | + assert c["passing_labels"] == ["good"] |
| 75 | + assert c["input"] == grader["input"] |
| 76 | + |
| 77 | + |
| 78 | +class TestBuildJsonlItems: |
| 79 | + def test_text_similarity_includes_expected(self): |
| 80 | + items = _build_jsonl_items([_invocation("hello")], [_invocation("world")], include_expected=True) |
| 81 | + assert "expected_response" in items[0]["item"] |
| 82 | + |
| 83 | + def test_label_model_excludes_expected(self): |
| 84 | + items = _build_jsonl_items([_invocation("hello")], [], include_expected=False) |
| 85 | + assert "expected_response" not in items[0]["item"] |
| 86 | + |
| 87 | + def test_missing_expected_falls_back_to_empty(self): |
| 88 | + items = _build_jsonl_items([_invocation("hello")], [], include_expected=True) |
| 89 | + assert items[0]["item"]["expected_response"] == "" |
| 90 | + |
| 91 | + |
| 92 | +class TestEvaluateOpenAIEval: |
| 93 | + async def test_no_api_key_returns_error(self, monkeypatch): |
| 94 | + monkeypatch.delenv("OPENAI_API_KEY", raising=False) |
| 95 | + d = OpenAIEvalDef(name="sim", grader={"type": "text_similarity", "evaluation_metric": "bleu"}) |
| 96 | + result = await evaluate_openai_eval(d, [], []) |
| 97 | + assert "OPENAI_API_KEY" in (result.error or "") |
| 98 | + |
| 99 | + async def test_text_similarity_requires_expected(self, monkeypatch): |
| 100 | + monkeypatch.setenv("OPENAI_API_KEY", "test-key") |
| 101 | + d = OpenAIEvalDef(name="sim", grader={"type": "text_similarity", "evaluation_metric": "bleu"}) |
| 102 | + result = await evaluate_openai_eval(d, [_invocation("hi")], None) |
| 103 | + assert "expected invocations" in (result.error or "") |
| 104 | + |
| 105 | + async def test_label_model_does_not_require_expected(self, monkeypatch): |
| 106 | + monkeypatch.setenv("OPENAI_API_KEY", "test-key") |
| 107 | + monkeypatch.setattr("agentevals.openai_eval_backend._get_openai_client", lambda: None) |
| 108 | + d = OpenAIEvalDef(name="lm", grader=_label_grader()) |
| 109 | + result = await evaluate_openai_eval(d, [_invocation("hi")], None) |
| 110 | + assert "expected invocations" not in (result.error or "") |
0 commit comments