-
Notifications
You must be signed in to change notification settings - Fork 385
Expand file tree
/
Copy pathai_testlib.py
More file actions
219 lines (178 loc) · 8.32 KB
/
ai_testlib.py
File metadata and controls
219 lines (178 loc) · 8.32 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import functools
import inspect
import json
import os
from collections.abc import Callable, Coroutine
from typing import Any, override
from unittest.mock import patch
from urllib import parse
from warnings import warn
import vcr
from vcr.config import RecordMode
from vcr.request import Request
from splunklib.ai.messages import AIMessage, ContentBlock, TextBlock
from splunklib.ai.model import PredefinedModel
from tests.ai_test_model import InternalAIModel, TestLLMSettings, create_model
from tests.testlib import SDKTestCase
REDACTED_APP_KEY = "[[[--APPKEY-REDACTED-]]]"
class AITestCase(SDKTestCase):
_model: PredefinedModel | None = None
@override
def setUp(self) -> None:
super().setUp()
# Our tests don't expect this app to be installed, if needed it is
# installed on demand.
for app in self.service.apps.list(): # pyright: ignore[reportUnknownVariableType]
if app.name.lower() == "splunk_mcp_server":
app.delete()
self.restart_splunk()
def _parse_content_block(self, block: str | ContentBlock) -> str | None:
match block:
case TextBlock():
return block.text
case str():
return block
case _:
warn("Skipping OpaqueBlock when parsing the AIMessage.content")
return None
def parse_content(self, message: AIMessage) -> str:
"""Parses the content from AIMessage and builds a single string our of it"""
if isinstance(message.content, str):
return message.content
return " ".join(
parsed_block
for block in message.content
if (parsed_block := self._parse_content_block(block))
)
@property
def test_llm_settings(self) -> TestLLMSettings:
client_id: str = self.opts.kwargs["internal_ai_client_id"]
client_secret: str = self.opts.kwargs["internal_ai_client_secret"]
app_key: str = self.opts.kwargs["internal_ai_app_key"]
token_url: str = self.opts.kwargs["internal_ai_token_url"]
base_url: str = self.opts.kwargs["internal_ai_base_url"]
return TestLLMSettings(
internal_ai=InternalAIModel(
client_id=client_id,
client_secret=client_secret,
app_key=app_key,
token_url=token_url,
base_url=base_url,
)
)
async def model(self) -> PredefinedModel:
if self._model is not None:
return self._model
model = await create_model(self.test_llm_settings)
self._model = model
return model
def ai_snapshot_test() -> Callable[
[Callable[..., Coroutine[Any, Any, None]]], Callable[..., Coroutine[Any, Any, None]]
]:
def decorator(
fn: Callable[..., Coroutine[Any, Any, None]],
) -> Callable[..., Coroutine[Any, Any, None]]:
source_file = inspect.getfile(fn)
test_dir = os.path.dirname(source_file)
test_file = os.path.splitext(os.path.basename(source_file))[0]
snapshot_dir = os.path.join(test_dir, "snapshots", test_file)
snapshot_filename = f"{fn.__qualname__}.json"
@functools.wraps(fn)
async def wrapper(self: AITestCase, *args: Any, **kwargs: Any) -> None:
settings = self.test_llm_settings
assert settings.internal_ai is not None
internal_ai_hostname = parse.urlparse(settings.internal_ai.base_url).hostname
assert internal_ai_hostname is not None
class _JSONFriendlySerializer:
def deserialize(self, serialized: str) -> Any:
assert settings.internal_ai is not None
serialized = serialized.replace(REDACTED_APP_KEY, settings.internal_ai.app_key)
data = json.loads(serialized)
for interaction in data.get("interactions", []):
interaction["request"]["uri"] = interaction["request"]["uri"].replace(
"internal-ai-host", internal_ai_hostname, 1
)
interaction["request"]["body"] = json.dumps(interaction["request"]["body"])
body = interaction["response"]["body"]
interaction["response"]["body"] = {}
interaction["response"]["body"]["string"] = json.dumps(body)
return data
def serialize(self, dict: Any) -> str:
for interaction in dict.get("interactions", []):
interaction["request"]["uri"] = interaction["request"]["uri"].replace(
internal_ai_hostname, "internal-ai-host", 1
)
body = interaction["request"]["body"]
interaction["request"]["body"] = json.loads(body)
resp_body = interaction["response"]["body"]["string"]
interaction["response"]["body"] = json.loads(resp_body)
out = json.dumps(dict, indent=4) + "\n"
assert settings.internal_ai is not None
out = out.replace(settings.internal_ai.app_key, REDACTED_APP_KEY)
# Assert that nothing is leaking into the public snapshots.
assert internal_ai_hostname not in out.lower()
assert settings.internal_ai.app_key.lower() not in out.lower()
assert settings.internal_ai.base_url.lower() not in out.lower()
assert settings.internal_ai.token_url.lower() not in out.lower()
assert settings.internal_ai.client_id.lower() not in out.lower()
assert settings.internal_ai.client_secret.lower() not in out.lower()
return out
def _before_record_request(request: Request) -> Request | None:
url = parse.urlparse(request.uri) # pyright: ignore[reportUnknownArgumentType, reportUnknownVariableType]
if url.hostname == internal_ai_hostname:
request.headers = {}
return request
return None
def _before_record_response(response: Any) -> Any:
response["headers"] = {}
return response
def _json_body_matcher(r1: Any, r2: Any) -> None:
b1 = json.loads(r1.body)
b2 = json.loads(r2.body)
if b1 != b2:
raise AssertionError(f"Body mismatch:\n{b1}\n!=\n{b2}")
my_vcr = vcr.VCR(
cassette_library_dir=snapshot_dir,
serializer="json-friendly",
record_mode=RecordMode.ONCE,
match_on=[
"method",
"scheme",
"host",
"port",
"path",
"query",
"jsonbody",
],
before_record_request=_before_record_request,
before_record_response=_before_record_response,
record_on_exception=False,
drop_unused_requests=True,
)
my_vcr.register_serializer("json-friendly", _JSONFriendlySerializer())
my_vcr.register_matcher("jsonbody", _json_body_matcher)
with my_vcr.use_cassette(snapshot_filename): # pyright: ignore[reportGeneralTypeIssues]
await fn(self, *args, **kwargs)
return wrapper
return decorator
def deterministic_thread_ids() -> Callable[
[Callable[..., Coroutine[Any, Any, None]]], Callable[..., Coroutine[Any, Any, None]]
]:
def decorator(
fn: Callable[..., Coroutine[Any, Any, None]],
) -> Callable[..., Coroutine[Any, Any, None]]:
@functools.wraps(fn)
async def wrapper(self: AITestCase, *args: Any, **kwargs: Any) -> None:
counter = 0
def _deterministic_uuid() -> str:
nonlocal counter
result = f"00000000-0000-0000-0000-{counter:012d}"
counter += 1
return result
with patch(
"splunklib.ai.engines.langchain._thread_id_new_uuid",
side_effect=_deterministic_uuid,
):
await fn(self, *args, **kwargs)
return wrapper
return decorator