File: test_aoai_python_grader.py

package info (click to toggle)
python-azure 20251104%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 770,224 kB
  • sloc: python: 6,357,217; ansic: 804; javascript: 287; makefile: 198; sh: 193; xml: 109
file content (54 lines) | stat: -rw-r--r-- 1,827 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pytest
from unittest.mock import MagicMock, patch

from azure.ai.evaluation import AzureOpenAIPythonGrader
from azure.ai.evaluation._model_configurations import AzureOpenAIModelConfiguration


class TestAzureOpenAIPythonGrader:
    """Test cases for AzureOpenAIPythonGrader."""

    def test_init_valid(self):
        """Test valid initialization."""
        model_config = AzureOpenAIModelConfiguration(
            azure_endpoint="https://test.openai.azure.com",
            api_key="test-key",
            azure_deployment="test-deployment",
        )

        source_code = """
def grade(sample: dict, item: dict) -> float:
    output = sample.get("output_text")
    label = item.get("label")
    return 1.0 if output == label else 0.0
"""

        grader = AzureOpenAIPythonGrader(
            model_config=model_config,
            name="python_test",
            image_tag="2025-05-08",
            pass_threshold=0.5,
            source=source_code,
        )

        assert grader.pass_threshold == 0.5
        assert grader.id == "azureai://built-in/evaluators/azure-openai/python_grader"

    def test_invalid_pass_threshold(self):
        """Test invalid pass_threshold values."""
        model_config = AzureOpenAIModelConfiguration(
            azure_endpoint="https://test.openai.azure.com",
            api_key="test-key",
            azure_deployment="test-deployment",
        )

        source_code = "def grade(sample: dict, item: dict) -> float:\n    return 1.0"

        with pytest.raises(ValueError, match="pass_threshold must be between 0.0 and 1.0"):
            AzureOpenAIPythonGrader(
                model_config=model_config,
                name="python_test",
                image_tag="2025-05-08",
                pass_threshold=1.5,
                source=source_code,
            )