forked from bentoml/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_package.py
123 lines (103 loc) · 3.61 KB
/
test_package.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# pylint: disable=unused-argument
from __future__ import annotations
import typing as t
from typing import TYPE_CHECKING
import pytest
import arize.api
from arize.utils.types import ModelTypes
if TYPE_CHECKING:
from _pytest.monkeypatch import MonkeyPatch
class Client:
LOG_ARGS: list[dict[str, t.Any]] = []
INIT_ARGS: list[dict[str, t.Any]] = []
def __init__(
self,
api_key: str,
space_key: str,
uri: t.Optional[str] = "https://api.arize.com/v1",
max_workers: t.Optional[int] = 8,
max_queue_bound: t.Optional[int] = 5000,
timeout: t.Optional[int] = 200,
):
self.INIT_ARGS.append(locals())
def log(
self,
prediction_id: t.Union[str, int, float],
model_id: str,
model_type,
environment,
model_version=None,
prediction_timestamp=None,
prediction_label=None,
actual_label=None,
features=None,
embedding_features=None,
shap_values=None,
tags=None,
batch_id=None,
):
self.LOG_ARGS.append(locals())
MY_SPACE_KEY = "my_space_key"
MY_API_KEY = "my_api_key"
URI = "http://localhost:5000/v1"
BENTO_NAME = "my_bento_name"
BENTO_VERSION = "my_bento_version"
REQUEST_ID = 214121
@pytest.fixture(autouse=True, name="init_context")
def fixture_init_context(monkeypatch: MonkeyPatch) -> None:
from bentoml._internal.context import trace_context
from bentoml._internal.context import component_context
trace_context.request_id = REQUEST_ID
component_context.bento_name = BENTO_NAME
component_context.bento_version = BENTO_VERSION
monkeypatch.setattr(arize.api, "Client", Client)
def test_mapping(init_context: None) -> None:
from bentoml_plugins.arize import ArizeMonitor
monitor_name = "my_model_1"
monitor = ArizeMonitor(
monitor_name,
space_key=MY_SPACE_KEY,
api_key=MY_API_KEY,
uri=URI,
)
monitor.start_record()
monitor.log(1, name="pred", role="prediction", data_type="categorical")
monitor.stop_record()
assert monitor.model_type == ModelTypes.SCORE_CATEGORICAL
assert Client.INIT_ARGS[-1]["space_key"] == MY_SPACE_KEY
assert Client.LOG_ARGS[-1]["model_id"] == f"{BENTO_NAME}:{monitor_name}"
assert Client.LOG_ARGS[-1]["model_version"] == BENTO_VERSION
assert Client.LOG_ARGS[-1]["prediction_id"] == REQUEST_ID
assert Client.LOG_ARGS[-1]["model_type"] == ModelTypes.SCORE_CATEGORICAL
monitor = ArizeMonitor(
monitor_name,
space_key="my_space_key",
api_key="my_api_key",
uri="http://localhost:5000/v1",
)
monitor.start_record()
monitor.log(1, name="pred", role="prediction", data_type="categorical")
monitor.log(1, name="pred2", role="prediction", data_type="categorical")
monitor.stop_record()
assert monitor.model_type == ModelTypes.SCORE_CATEGORICAL
monitor = ArizeMonitor(
"my_monitor_2",
space_key="my_space_key",
api_key="my_api_key",
uri="http://localhost:5000/v1",
)
monitor.start_record()
monitor.log(1, name="pred", role="prediction", data_type="numerical")
monitor.stop_record()
assert monitor.model_type == ModelTypes.NUMERIC
monitor = ArizeMonitor(
"my_monitor_2",
space_key="my_space_key",
api_key="my_api_key",
uri="http://localhost:5000/v1",
)
monitor.start_record()
monitor.log(1, name="pred", role="prediction", data_type="numerical")
monitor.log(1, name="pred2", role="prediction", data_type="numerical")
monitor.stop_record()
assert monitor.model_type == ModelTypes.NUMERIC