|
| 1 | +"""Tests for the direct Galileo LLM-as-judge evaluator and client.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +from unittest.mock import AsyncMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from agent_control_models import Step |
| 10 | + |
| 11 | +LLM_ENV = { |
| 12 | + "GALILEO_API_SECRET_KEY": "test-secret", |
| 13 | + "GALILEO_LUNA_INVOKE_URL": "http://luna-invoke:8090", |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +class TestGalileoLLMClient: |
| 18 | + """Tests for ``GalileoLLMClient`` and ``ScorerInvokeRequest``.""" |
| 19 | + |
| 20 | + def test_execution_context_serializes_missing_optional_claims_as_null(self) -> None: |
| 21 | + from agent_control_evaluator_galileo.llm.client import ( |
| 22 | + GalileoExecutionContext, |
| 23 | + ScorerInvokeInputs, |
| 24 | + ScorerInvokeRequest, |
| 25 | + ) |
| 26 | + |
| 27 | + request = ScorerInvokeRequest( |
| 28 | + scorer_id="scorer-123", |
| 29 | + inputs=ScorerInvokeInputs(query="hello"), |
| 30 | + execution_context=GalileoExecutionContext(organization_id="org-1"), |
| 31 | + ) |
| 32 | + |
| 33 | + assert request.to_dict()["execution_context"] == { |
| 34 | + "organization_id": "org-1", |
| 35 | + "user_id": None, |
| 36 | + "project_id": None, |
| 37 | + "run_id": None, |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | +class TestLlmEvaluator: |
| 42 | + """Tests for ``LlmEvaluator`` execution context handling.""" |
| 43 | + |
| 44 | + @patch.dict(os.environ, LLM_ENV) |
| 45 | + @pytest.mark.asyncio |
| 46 | + @pytest.mark.parametrize("target_type", ["log_stream", "agent_stream"]) |
| 47 | + async def test_evaluator_consumes_verified_opaque_extensions(self, target_type: str) -> None: |
| 48 | + from agent_control_evaluator_galileo.llm import LlmEvaluator |
| 49 | + from agent_control_evaluator_galileo.llm.client import ( |
| 50 | + GalileoExecutionContext, |
| 51 | + GalileoLLMClient, |
| 52 | + ScorerInvokeResponse, |
| 53 | + ) |
| 54 | + |
| 55 | + evaluator = LlmEvaluator.from_dict( |
| 56 | + {"scorer_id": "scorer-123", "threshold": 0.5, "operator": "gte"} |
| 57 | + ) |
| 58 | + extensions = { |
| 59 | + "namespace_key": "org-1", |
| 60 | + "caller_id": "verified-user-2", |
| 61 | + "target_type": target_type, |
| 62 | + "target_id": "run-4", |
| 63 | + "metadata": {"project_id": "project-3"}, |
| 64 | + } |
| 65 | + |
| 66 | + with patch.object(GalileoLLMClient, "invoke", new_callable=AsyncMock) as mock_invoke: |
| 67 | + mock_invoke.return_value = ScorerInvokeResponse(score=0.8, status="success") |
| 68 | + result = await evaluator.evaluate_with_extensions( |
| 69 | + "selected input", Step(type="llm", name="answer", input="prompt"), extensions |
| 70 | + ) |
| 71 | + |
| 72 | + assert result.matched is True |
| 73 | + mock_invoke.assert_awaited_once_with( |
| 74 | + scorer_id="scorer-123", |
| 75 | + step=Step(type="llm", name="answer", input="prompt"), |
| 76 | + execution_context=GalileoExecutionContext( |
| 77 | + organization_id="org-1", |
| 78 | + user_id="verified-user-2", |
| 79 | + project_id="project-3", |
| 80 | + run_id="run-4", |
| 81 | + ), |
| 82 | + input="selected input", |
| 83 | + output=None, |
| 84 | + config=None, |
| 85 | + timeout=10.0, |
| 86 | + ) |
| 87 | + |
| 88 | + @patch.dict(os.environ, LLM_ENV) |
| 89 | + @pytest.mark.asyncio |
| 90 | + async def test_evaluator_allows_missing_caller_id(self) -> None: |
| 91 | + from agent_control_evaluator_galileo.llm import LlmEvaluator |
| 92 | + from agent_control_evaluator_galileo.llm.client import ( |
| 93 | + GalileoExecutionContext, |
| 94 | + GalileoLLMClient, |
| 95 | + ScorerInvokeResponse, |
| 96 | + ) |
| 97 | + |
| 98 | + evaluator = LlmEvaluator.from_dict({"scorer_id": "scorer-123"}) |
| 99 | + extensions = { |
| 100 | + "namespace_key": "org-1", |
| 101 | + "target_type": "log_stream", |
| 102 | + "target_id": "run-4", |
| 103 | + "metadata": {}, |
| 104 | + } |
| 105 | + |
| 106 | + with patch.object(GalileoLLMClient, "invoke", new_callable=AsyncMock) as mock_invoke: |
| 107 | + mock_invoke.return_value = ScorerInvokeResponse(score=0.8, status="success") |
| 108 | + result = await evaluator.evaluate_with_extensions( |
| 109 | + "selected input", Step(type="llm", name="answer", input="prompt"), extensions |
| 110 | + ) |
| 111 | + |
| 112 | + assert result.error is None |
| 113 | + mock_invoke.assert_awaited_once_with( |
| 114 | + scorer_id="scorer-123", |
| 115 | + step=Step(type="llm", name="answer", input="prompt"), |
| 116 | + execution_context=GalileoExecutionContext( |
| 117 | + organization_id="org-1", |
| 118 | + user_id=None, |
| 119 | + project_id=None, |
| 120 | + run_id="run-4", |
| 121 | + ), |
| 122 | + input="selected input", |
| 123 | + output=None, |
| 124 | + config=None, |
| 125 | + timeout=10.0, |
| 126 | + ) |
| 127 | + |
| 128 | + @patch.dict(os.environ, LLM_ENV) |
| 129 | + @pytest.mark.asyncio |
| 130 | + async def test_evaluator_omits_run_id_for_unsupported_target_type(self) -> None: |
| 131 | + from agent_control_evaluator_galileo.llm import LlmEvaluator |
| 132 | + from agent_control_evaluator_galileo.llm.client import ( |
| 133 | + GalileoExecutionContext, |
| 134 | + GalileoLLMClient, |
| 135 | + ScorerInvokeResponse, |
| 136 | + ) |
| 137 | + |
| 138 | + evaluator = LlmEvaluator.from_dict({"scorer_id": "scorer-123"}) |
| 139 | + extensions = { |
| 140 | + "namespace_key": "org-1", |
| 141 | + "caller_id": "verified-user-2", |
| 142 | + "target_type": "environment", |
| 143 | + "target_id": "prod", |
| 144 | + "metadata": {"project_id": "project-3"}, |
| 145 | + } |
| 146 | + |
| 147 | + with patch.object(GalileoLLMClient, "invoke", new_callable=AsyncMock) as mock_invoke: |
| 148 | + mock_invoke.return_value = ScorerInvokeResponse(score=0.8, status="success") |
| 149 | + result = await evaluator.evaluate_with_extensions( |
| 150 | + "selected input", Step(type="llm", name="answer", input="prompt"), extensions |
| 151 | + ) |
| 152 | + |
| 153 | + assert result.error is None |
| 154 | + mock_invoke.assert_awaited_once_with( |
| 155 | + scorer_id="scorer-123", |
| 156 | + step=Step(type="llm", name="answer", input="prompt"), |
| 157 | + execution_context=GalileoExecutionContext( |
| 158 | + organization_id="org-1", |
| 159 | + user_id="verified-user-2", |
| 160 | + project_id="project-3", |
| 161 | + run_id=None, |
| 162 | + ), |
| 163 | + input="selected input", |
| 164 | + output=None, |
| 165 | + config=None, |
| 166 | + timeout=10.0, |
| 167 | + ) |
| 168 | + |
| 169 | + @patch.dict(os.environ, LLM_ENV) |
| 170 | + @pytest.mark.asyncio |
| 171 | + async def test_evaluator_rejects_missing_organization_id(self) -> None: |
| 172 | + from agent_control_evaluator_galileo.llm import LlmEvaluator |
| 173 | + from agent_control_evaluator_galileo.llm.client import GalileoLLMClient |
| 174 | + |
| 175 | + evaluator = LlmEvaluator.from_dict({"scorer_id": "scorer-123"}) |
| 176 | + extensions = { |
| 177 | + "target_type": "log_stream", |
| 178 | + "target_id": "run-4", |
| 179 | + "metadata": {"project_id": "project-3"}, |
| 180 | + } |
| 181 | + |
| 182 | + with patch.object(GalileoLLMClient, "invoke", new_callable=AsyncMock) as mock_invoke: |
| 183 | + result = await evaluator.evaluate_with_extensions( |
| 184 | + "selected input", |
| 185 | + Step(type="llm", name="answer", input="prompt"), |
| 186 | + extensions, |
| 187 | + ) |
| 188 | + |
| 189 | + assert result.error is not None |
| 190 | + assert "organization_id" in result.error |
| 191 | + mock_invoke.assert_not_called() |
0 commit comments