-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathtest_tool.py
435 lines (384 loc) · 16.4 KB
/
test_tool.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
import copy
import uuid
import langchain
import pydantic_core
import pytest
from langchain.tools import tool
from unittest.mock import patch
from testing_support.fixtures import reset_core_stats_engine, validate_attributes
from testing_support.ml_testing_utils import (
disabled_ai_monitoring_record_content_settings,
disabled_ai_monitoring_settings,
events_with_context_attrs,
set_trace_info,
)
from testing_support.validators.validate_custom_event import validate_custom_event_count
from testing_support.validators.validate_custom_events import validate_custom_events
from testing_support.validators.validate_error_trace_attributes import validate_error_trace_attributes
from testing_support.validators.validate_transaction_error_event_count import validate_transaction_error_event_count
from testing_support.validators.validate_transaction_metrics import validate_transaction_metrics
from newrelic.api.background_task import background_task
from newrelic.api.llm_custom_attributes import WithLlmCustomAttributes
from newrelic.common.object_names import callable_name
@pytest.fixture
def single_arg_tool():
@tool
def _single_arg_tool(query: str):
"""A test tool that returns query string"""
return query
return _single_arg_tool
@pytest.fixture
def multi_arg_tool():
@tool
def _multi_arg_tool(first_num: int, second_num: int):
"""A test tool that adds two integers together"""
return first_num + second_num
return _multi_arg_tool
def events_sans_content(event):
new_event = copy.deepcopy(event)
for _event in new_event:
del _event[1]["input"]
if "output" in _event[1]:
del _event[1]["output"]
return new_event
single_arg_tool_recorded_events = [
(
{"type": "LlmTool"},
{
"id": None, # UUID that varies with each run
"run_id": None,
"output": "Python Agent",
"name": "_single_arg_tool",
"description": "A test tool that returns query string",
"span_id": None,
"trace_id": "trace-id",
"input": "{'query': 'Python Agent'}",
"vendor": "langchain",
"ingest_source": "Python",
"duration": None,
},
)
]
@reset_core_stats_engine()
@validate_custom_events(events_with_context_attrs(single_arg_tool_recorded_events))
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_single_arg_tool",
scoped_metrics=[("Llm/tool/LangChain/run", 1)],
rollup_metrics=[("Llm/tool/LangChain/run", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@validate_attributes("agent", ["llm"])
@background_task()
def test_langchain_single_arg_tool(set_trace_info, single_arg_tool):
set_trace_info()
with WithLlmCustomAttributes({"context": "attr"}):
single_arg_tool.run({"query": "Python Agent"})
@reset_core_stats_engine()
@disabled_ai_monitoring_record_content_settings
@validate_custom_events(events_sans_content(single_arg_tool_recorded_events))
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_single_arg_tool_no_content",
scoped_metrics=[("Llm/tool/LangChain/run", 1)],
rollup_metrics=[("Llm/tool/LangChain/run", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@validate_attributes("agent", ["llm"])
@background_task()
def test_langchain_single_arg_tool_no_content(set_trace_info, single_arg_tool):
set_trace_info()
single_arg_tool.run({"query": "Python Agent"})
@reset_core_stats_engine()
@validate_custom_events(events_with_context_attrs(single_arg_tool_recorded_events))
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_single_arg_tool_async",
scoped_metrics=[("Llm/tool/LangChain/arun", 1)],
rollup_metrics=[("Llm/tool/LangChain/arun", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@validate_attributes("agent", ["llm"])
@background_task()
def test_langchain_single_arg_tool_async(set_trace_info, single_arg_tool, loop):
set_trace_info()
with WithLlmCustomAttributes({"context": "attr"}):
loop.run_until_complete(single_arg_tool.arun({"query": "Python Agent"}))
@reset_core_stats_engine()
@disabled_ai_monitoring_record_content_settings
@validate_custom_events(events_sans_content(single_arg_tool_recorded_events))
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_single_arg_tool_async_no_content",
scoped_metrics=[("Llm/tool/LangChain/arun", 1)],
rollup_metrics=[("Llm/tool/LangChain/arun", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@validate_attributes("agent", ["llm"])
@background_task()
def test_langchain_single_arg_tool_async_no_content(set_trace_info, single_arg_tool, loop):
set_trace_info()
loop.run_until_complete(single_arg_tool.arun({"query": "Python Agent"}))
multi_arg_tool_recorded_events = [
(
{"type": "LlmTool"},
{
"id": None, # UUID that varies with each run
"run_id": None,
"output": "81",
"name": "_multi_arg_tool",
"description": "A test tool that adds two integers together",
"span_id": None,
"trace_id": "trace-id",
"input": "{'first_num': 53, 'second_num': 28}",
"vendor": "langchain",
"ingest_source": "Python",
"duration": None,
"tags": "['python', 'test_tags']",
"metadata.test": "langchain",
"metadata.test_run": True,
},
)
]
@reset_core_stats_engine()
@validate_custom_events(multi_arg_tool_recorded_events)
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_multi_arg_tool",
scoped_metrics=[("Llm/tool/LangChain/run", 1)],
rollup_metrics=[("Llm/tool/LangChain/run", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@background_task()
def test_langchain_multi_arg_tool(set_trace_info, multi_arg_tool):
set_trace_info()
multi_arg_tool.metadata = {"test_run": True}
multi_arg_tool.tags = ["test_tags"]
multi_arg_tool.run({"first_num": 53, "second_num": 28}, tags=["python"], metadata={"test": "langchain"})
@reset_core_stats_engine()
@validate_custom_events(multi_arg_tool_recorded_events)
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_multi_arg_tool_async",
scoped_metrics=[("Llm/tool/LangChain/arun", 1)],
rollup_metrics=[("Llm/tool/LangChain/arun", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@background_task()
def test_langchain_multi_arg_tool_async(set_trace_info, multi_arg_tool, loop):
set_trace_info()
multi_arg_tool.metadata = {"test_run": True}
multi_arg_tool.tags = ["test_tags"]
loop.run_until_complete(
multi_arg_tool.arun({"first_num": 53, "second_num": 28}, tags=["python"], metadata={"test": "langchain"})
)
multi_arg_error_recorded_events = [
(
{"type": "LlmTool"},
{
"id": None, # UUID that varies with each run
"run_id": None, # No run ID created on error
"name": "_multi_arg_tool",
"description": "A test tool that adds two integers together",
"span_id": None,
"trace_id": "trace-id",
"input": "{'first_num': 53}",
"vendor": "langchain",
"ingest_source": "Python",
"duration": None,
"tags": "['test_tags', 'python']",
"metadata.test": "langchain",
"metadata.test_run": True,
"error": True,
},
)
]
@reset_core_stats_engine()
@validate_transaction_error_event_count(1)
@validate_error_trace_attributes(
callable_name(pydantic_core._pydantic_core.ValidationError), exact_attrs={"agent": {}, "intrinsic": {}, "user": {}}
)
@validate_custom_events(events_with_context_attrs(multi_arg_error_recorded_events))
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_error_in_run",
scoped_metrics=[("Llm/tool/LangChain/run", 1)],
rollup_metrics=[("Llm/tool/LangChain/run", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@background_task()
def test_langchain_error_in_run(set_trace_info, multi_arg_tool):
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
set_trace_info()
# Only one argument is provided while the tool expects two to create an error
with WithLlmCustomAttributes({"context": "attr"}):
multi_arg_tool.run(
{"first_num": 53}, tags=["test_tags", "python"], metadata={"test_run": True, "test": "langchain"}
)
@reset_core_stats_engine()
@disabled_ai_monitoring_record_content_settings
@validate_transaction_error_event_count(1)
@validate_error_trace_attributes(
callable_name(pydantic_core._pydantic_core.ValidationError), exact_attrs={"agent": {}, "intrinsic": {}, "user": {}}
)
@validate_custom_events(events_sans_content(multi_arg_error_recorded_events))
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_error_in_run_no_content",
scoped_metrics=[("Llm/tool/LangChain/run", 1)],
rollup_metrics=[("Llm/tool/LangChain/run", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@background_task()
def test_langchain_error_in_run_no_content(set_trace_info, multi_arg_tool):
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
set_trace_info()
# Only one argument is provided while the tool expects two to create an error
multi_arg_tool.run(
{"first_num": 53}, tags=["test_tags", "python"], metadata={"test_run": True, "test": "langchain"}
)
@reset_core_stats_engine()
@validate_transaction_error_event_count(1)
@validate_error_trace_attributes(
callable_name(pydantic_core._pydantic_core.ValidationError), exact_attrs={"agent": {}, "intrinsic": {}, "user": {}}
)
@validate_custom_events(events_with_context_attrs(multi_arg_error_recorded_events))
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_error_in_run_async",
scoped_metrics=[("Llm/tool/LangChain/arun", 1)],
rollup_metrics=[("Llm/tool/LangChain/arun", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@background_task()
def test_langchain_error_in_run_async(set_trace_info, multi_arg_tool, loop):
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
with WithLlmCustomAttributes({"context": "attr"}):
set_trace_info()
# Only one argument is provided while the tool expects two to create an error
loop.run_until_complete(
multi_arg_tool.arun(
{"first_num": 53}, tags=["test_tags", "python"], metadata={"test_run": True, "test": "langchain"}
)
)
@reset_core_stats_engine()
@disabled_ai_monitoring_record_content_settings
@validate_transaction_error_event_count(1)
@validate_error_trace_attributes(
callable_name(pydantic_core._pydantic_core.ValidationError), exact_attrs={"agent": {}, "intrinsic": {}, "user": {}}
)
@validate_custom_events(events_sans_content(multi_arg_error_recorded_events))
@validate_custom_event_count(count=1)
@validate_transaction_metrics(
name="test_tool:test_langchain_error_in_run_async_no_content",
scoped_metrics=[("Llm/tool/LangChain/arun", 1)],
rollup_metrics=[("Llm/tool/LangChain/arun", 1)],
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@background_task()
def test_langchain_error_in_run_async_no_content(set_trace_info, multi_arg_tool, loop):
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
set_trace_info()
# Only one argument is provided while the tool expects two to create an error
loop.run_until_complete(
multi_arg_tool.arun(
{"first_num": 53}, tags=["test_tags", "python"], metadata={"test_run": True, "test": "langchain"}
)
)
@reset_core_stats_engine()
@validate_custom_event_count(count=0)
def test_langchain_tool_outside_txn(single_arg_tool):
single_arg_tool.run(
{"query": "Python Agent"}, tags=["test_tags", "python"], metadata={"test_run": True, "test": "langchain"}
)
@reset_core_stats_engine()
@validate_custom_event_count(count=0)
def test_langchain_tool_outside_txn_async(single_arg_tool, loop):
loop.run_until_complete(
single_arg_tool.arun(
{"query": "Python Agent"}, tags=["test_tags", "python"], metadata={"test_run": True, "test": "langchain"}
)
)
@disabled_ai_monitoring_settings
@reset_core_stats_engine()
@validate_custom_event_count(count=0)
@background_task()
def test_langchain_tool_disabled_ai_monitoring_events_sync(set_trace_info, single_arg_tool):
set_trace_info()
single_arg_tool.run(
{"query": "Python Agent"}, tags=["test_tags", "python"], metadata={"test_run": True, "test": "langchain"}
)
@disabled_ai_monitoring_settings
@reset_core_stats_engine()
@validate_custom_event_count(count=0)
@background_task()
def test_langchain_tool_disabled_ai_monitoring_events_async(set_trace_info, single_arg_tool, loop):
set_trace_info()
loop.run_until_complete(
single_arg_tool.arun(
{"query": "Python Agent"}, tags=["test_tags", "python"], metadata={"test_run": True, "test": "langchain"}
)
)
def test_langchain_multiple_async_calls(set_trace_info, single_arg_tool, multi_arg_tool, loop):
call1 = single_arg_tool_recorded_events.copy()
call1[0][1]["run_id"] = "b1883d9d-10d6-4b67-a911-f72849704e92"
call2 = multi_arg_tool_recorded_events.copy()
call2[0][1]["run_id"] = "a58aa0c0-c854-4657-9e7b-4cce442f3b61"
expected_events = call1 + call2
@reset_core_stats_engine()
@validate_custom_events(expected_events)
@validate_custom_event_count(count=2)
@validate_transaction_metrics(
name="test_tool:test_langchain_multiple_async_calls.<locals>._test",
custom_metrics=[(f"Supportability/Python/ML/LangChain/{langchain.__version__}", 1)],
background_task=True,
)
@background_task()
def _test():
set_trace_info()
with patch("langchain_core.callbacks.manager.uuid", autospec=True) as mock_uuid:
mock_uuid.uuid4.side_effect = [
uuid.UUID("b1883d9d-10d6-4b67-a911-f72849704e92"), # first call
uuid.UUID("a58aa0c0-c854-4657-9e7b-4cce442f3b61"),
uuid.UUID("a58aa0c0-c854-4657-9e7b-4cce442f3b61"), # second call
uuid.UUID("a58aa0c0-c854-4657-9e7b-4cce442f3b63"),
uuid.UUID("b1883d9d-10d6-4b67-a911-f72849704e93"),
uuid.UUID("a58aa0c0-c854-4657-9e7b-4cce442f3b64"),
uuid.UUID("a58aa0c0-c854-4657-9e7b-4cce442f3b65"),
uuid.UUID("a58aa0c0-c854-4657-9e7b-4cce442f3b66"),
]
loop.run_until_complete(
asyncio.gather(
single_arg_tool.arun({"query": "Python Agent"}),
multi_arg_tool.arun(
{"first_num": 53, "second_num": 28},
tags=["python", "test_tags"],
metadata={"test": "langchain", "test_run": True},
),
)
)
_test()