|
1 | 1 | """Unit tests for DeepEval LLM Manager.""" |
2 | 2 |
|
| 3 | +import logging |
| 4 | + |
3 | 5 | import pytest |
4 | 6 | from pytest_mock import MockerFixture |
5 | 7 |
|
@@ -95,3 +97,62 @@ def test_drop_params_always_enabled(self, mocker: MockerFixture) -> None: |
95 | 97 | DeepEvalLLMManager("gpt-4", {}) |
96 | 98 |
|
97 | 99 | assert mock_litellm.drop_params is True |
| 100 | + |
| 101 | + def test_patch_deepeval_retries_called_with_configured_value( |
| 102 | + self, mocker: MockerFixture |
| 103 | + ) -> None: |
| 104 | + """Test that _patch_deepeval_retries patches LiteLLMModel retry logic.""" |
| 105 | + # Mock LiteLLMModel with methods that have retry decorators |
| 106 | + mock_generate = mocker.Mock() |
| 107 | + mock_generate.retry = mocker.Mock() |
| 108 | + mock_a_generate = mocker.Mock() |
| 109 | + mock_a_generate.retry = mocker.Mock() |
| 110 | + mock_generate_raw = mocker.Mock() |
| 111 | + mock_generate_raw.retry = mocker.Mock() |
| 112 | + mock_a_generate_raw = mocker.Mock() |
| 113 | + mock_a_generate_raw.retry = mocker.Mock() |
| 114 | + mock_generate_samples = mocker.Mock() |
| 115 | + mock_generate_samples.retry = mocker.Mock() |
| 116 | + |
| 117 | + mock_litellm_class = mocker.patch( |
| 118 | + "lightspeed_evaluation.core.llm.deepeval.LiteLLMModel" |
| 119 | + ) |
| 120 | + mock_litellm_class.generate = mock_generate |
| 121 | + mock_litellm_class.a_generate = mock_a_generate |
| 122 | + mock_litellm_class.generate_raw_response = mock_generate_raw |
| 123 | + mock_litellm_class.a_generate_raw_response = mock_a_generate_raw |
| 124 | + mock_litellm_class.generate_samples = mock_generate_samples |
| 125 | + |
| 126 | + # Mock stop_after_attempt to return a mock stop condition |
| 127 | + mock_stop_condition = mocker.Mock() |
| 128 | + mock_stop_after_attempt = mocker.patch( |
| 129 | + "lightspeed_evaluation.core.llm.deepeval.stop_after_attempt", |
| 130 | + return_value=mock_stop_condition, |
| 131 | + ) |
| 132 | + |
| 133 | + params = {"num_retries": 5} |
| 134 | + DeepEvalLLMManager("gpt-4", params) |
| 135 | + |
| 136 | + # Verify stop_after_attempt was called 5 times (once per method) with the configured retries |
| 137 | + assert mock_stop_after_attempt.call_count == 5 |
| 138 | + mock_stop_after_attempt.assert_called_with(5) |
| 139 | + |
| 140 | + # Verify each method's retry.stop was set to the new stop condition |
| 141 | + assert mock_generate.retry.stop == mock_stop_condition |
| 142 | + assert mock_a_generate.retry.stop == mock_stop_condition |
| 143 | + assert mock_generate_raw.retry.stop == mock_stop_condition |
| 144 | + assert mock_a_generate_raw.retry.stop == mock_stop_condition |
| 145 | + assert mock_generate_samples.retry.stop == mock_stop_condition |
| 146 | + |
| 147 | + def test_patch_deepeval_retries_logs_operation( |
| 148 | + self, mocker: MockerFixture, caplog: pytest.LogCaptureFixture |
| 149 | + ) -> None: |
| 150 | + """Test that retry patching logs the max_retries value.""" |
| 151 | + mocker.patch("lightspeed_evaluation.core.llm.deepeval.LiteLLMModel") |
| 152 | + |
| 153 | + params = {"num_retries": 7} |
| 154 | + |
| 155 | + with caplog.at_level(logging.INFO): |
| 156 | + DeepEvalLLMManager("gpt-4", params) |
| 157 | + |
| 158 | + assert "Patched DeepEval retry logic: max_retries=7" in caplog.text |
0 commit comments