-
Notifications
You must be signed in to change notification settings - Fork 821
Expand file tree
/
Copy pathtest_writer.py
More file actions
519 lines (402 loc) · 15.9 KB
/
test_writer.py
File metadata and controls
519 lines (402 loc) · 15.9 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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import logging
import unittest.mock
from typing import Any
import pytest
import strands
from strands.models.writer import WriterModel
@pytest.fixture
def writer_client_cls():
with unittest.mock.patch.object(strands.models.writer.writerai, "AsyncClient") as mock_client_cls:
yield mock_client_cls
@pytest.fixture
def writer_client(writer_client_cls):
return writer_client_cls.return_value
@pytest.fixture
def client_args():
return {"api_key": "writer_api_key"}
@pytest.fixture
def model_id():
return "palmyra-x5"
@pytest.fixture
def stream_options():
return {"include_usage": True}
@pytest.fixture
def model(writer_client, model_id, stream_options, client_args):
_ = writer_client
return WriterModel(client_args, model_id=model_id, stream_options=stream_options)
@pytest.fixture
def messages():
return [{"role": "user", "content": [{"text": "test"}]}]
@pytest.fixture
def system_prompt():
return "System prompt"
def test__init__(writer_client_cls, model_id, stream_options, client_args):
model = WriterModel(client_args=client_args, model_id=model_id, stream_options=stream_options)
config = model.get_config()
exp_config = {"stream_options": stream_options, "model_id": model_id}
assert config == exp_config
writer_client_cls.assert_called_once_with(api_key=client_args.get("api_key", ""))
def test_update_config(model):
model.update_config(model_id="palmyra-x4")
model_id = model.get_config().get("model_id")
assert model_id == "palmyra-x4"
def test_format_request_basic(model, messages, model_id, stream_options):
request = model.format_request(messages)
exp_request = {
"stream": True,
"messages": [{"role": "user", "content": [{"type": "text", "text": "test"}]}],
"model": model_id,
"stream_options": stream_options,
}
assert request == exp_request
def test_format_request_with_params(model, messages, model_id, stream_options):
model.update_config(temperature=0.19)
request = model.format_request(messages)
exp_request = {
"messages": [{"role": "user", "content": [{"type": "text", "text": "test"}]}],
"model": model_id,
"stream_options": stream_options,
"temperature": 0.19,
"stream": True,
}
assert request == exp_request
def test_format_request_with_system_prompt(model, messages, model_id, stream_options, system_prompt):
request = model.format_request(messages, system_prompt=system_prompt)
exp_request = {
"messages": [
{"content": "System prompt", "role": "system"},
{"content": [{"text": "test", "type": "text"}], "role": "user"},
],
"model": model_id,
"stream_options": stream_options,
"stream": True,
}
assert request == exp_request
def test_format_request_with_tool_use(model, model_id, stream_options):
messages = [
{
"role": "assistant",
"content": [
{
"toolUse": {
"toolUseId": "c1",
"name": "calculator",
"input": {"expression": "2+2"},
},
},
],
},
]
request = model.format_request(messages)
exp_request = {
"messages": [
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"function": {"arguments": '{"expression": "2+2"}', "name": "calculator"},
"id": "c1",
"type": "function",
}
],
},
],
"model": model_id,
"stream_options": stream_options,
"stream": True,
}
assert request == exp_request
def test_format_request_with_tool_results(model, model_id, stream_options):
messages = [
{
"role": "user",
"content": [
{
"toolResult": {
"toolUseId": "c1",
"status": "success",
"content": [
{"text": "answer is 4"},
],
}
}
],
}
]
request = model.format_request(messages)
exp_request = {
"messages": [
{
"role": "tool",
"content": [{"text": "answer is 4", "type": "text"}],
"tool_call_id": "c1",
},
],
"model": model_id,
"stream_options": stream_options,
"stream": True,
}
assert request == exp_request
def test_format_request_with_image(model, model_id, stream_options):
messages = [
{
"role": "user",
"content": [
{
"image": {
"format": "png",
"source": {"bytes": b"lovely sunny day"},
},
},
],
},
]
request = model.format_request(messages)
exp_request = {
"messages": [
{
"role": "user",
"content": [
{
"image_url": {
"url": "data:image/png;base64,bG92ZWx5IHN1bm55IGRheQ==",
},
"type": "image_url",
},
],
},
],
"model": model_id,
"stream": True,
"stream_options": stream_options,
}
assert request == exp_request
def test_format_request_with_empty_content(model, model_id, stream_options):
messages = [
{
"role": "user",
"content": [],
},
]
tru_request = model.format_request(messages)
exp_request = {
"messages": [],
"model": model_id,
"stream_options": stream_options,
"stream": True,
}
assert tru_request == exp_request
@pytest.mark.parametrize(
("content", "content_type"),
[
({"video": {}}, "video"),
({"document": {}}, "document"),
({"other": {}}, "other"),
],
)
def test_format_request_with_unsupported_type(model, content, content_type):
messages = [
{
"role": "user",
"content": [content],
},
]
with pytest.raises(TypeError, match=f"content_type=<{content_type}> | unsupported type"):
model.format_request(messages)
def test_format_request_skips_reasoning_content(model):
"""Test that reasoningContent blocks from other providers are silently skipped."""
messages = [
{
"role": "user",
"content": [
{"text": "Hello"},
{"reasoningContent": {"reasoningText": {"text": "Let me think...", "signature": "sig"}}},
],
},
]
# Should not raise — reasoningContent is silently dropped
model.format_request(messages)
class AsyncStreamWrapper:
def __init__(self, items: list[Any]):
self.items = items
def __aiter__(self):
return self._generator()
async def _generator(self):
for item in self.items:
yield item
async def mock_streaming_response(items: list[Any]):
return AsyncStreamWrapper(items)
@pytest.mark.asyncio
async def test_stream(writer_client, model, model_id):
mock_tool_call_1_part_1 = unittest.mock.Mock(index=0)
mock_tool_call_2_part_1 = unittest.mock.Mock(index=1)
mock_delta_1 = unittest.mock.Mock(
content="I'll calculate", tool_calls=[mock_tool_call_1_part_1, mock_tool_call_2_part_1]
)
mock_tool_call_1_part_2 = unittest.mock.Mock(index=0)
mock_tool_call_2_part_2 = unittest.mock.Mock(index=1)
mock_delta_2 = unittest.mock.Mock(
content="that for you", tool_calls=[mock_tool_call_1_part_2, mock_tool_call_2_part_2]
)
mock_delta_3 = unittest.mock.Mock(content="", tool_calls=None)
mock_event_1 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason=None, delta=mock_delta_1)])
mock_event_2 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason=None, delta=mock_delta_2)])
mock_event_3 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason="tool_calls", delta=mock_delta_3)])
mock_event_4 = unittest.mock.Mock()
writer_client.chat.chat.return_value = mock_streaming_response(
[mock_event_1, mock_event_2, mock_event_3, mock_event_4]
)
messages = [{"role": "user", "content": [{"type": "text", "text": "calculate 2+2"}]}]
response = model.stream(messages, None, None)
# Consume the response
[event async for event in response]
# The events should be formatted through format_chunk, so they should be StreamEvent objects
expected_request = {
"model": model_id,
"messages": [{"role": "user", "content": [{"type": "text", "text": "calculate 2+2"}]}],
"stream": True,
"stream_options": {"include_usage": True},
}
writer_client.chat.chat.assert_called_once_with(**expected_request)
@pytest.mark.asyncio
async def test_stream_empty(writer_client, model, model_id):
mock_delta = unittest.mock.Mock(content=None, tool_calls=None)
mock_usage = unittest.mock.Mock(prompt_tokens=0, completion_tokens=0, total_tokens=0)
mock_event_1 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason=None, delta=mock_delta)])
mock_event_2 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason="stop", delta=mock_delta)])
mock_event_3 = unittest.mock.Mock()
mock_event_4 = unittest.mock.Mock(usage=mock_usage)
writer_client.chat.chat.return_value = mock_streaming_response(
[mock_event_1, mock_event_2, mock_event_3, mock_event_4]
)
messages = [{"role": "user", "content": []}]
response = model.stream(messages, None, None)
# Consume the response
[event async for event in response]
expected_request = {
"model": model_id,
"messages": [],
"stream": True,
"stream_options": {"include_usage": True},
}
writer_client.chat.chat.assert_called_once_with(**expected_request)
@pytest.mark.asyncio
async def test_stream_with_empty_choices(writer_client, model, model_id, captured_warnings):
mock_delta = unittest.mock.Mock(content="content", tool_calls=None)
mock_usage = unittest.mock.Mock(prompt_tokens=10, completion_tokens=20, total_tokens=30)
mock_event_1 = unittest.mock.Mock(spec=[])
mock_event_2 = unittest.mock.Mock(choices=[])
mock_event_3 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason=None, delta=mock_delta)])
mock_event_4 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason="stop", delta=mock_delta)])
mock_event_5 = unittest.mock.Mock(usage=mock_usage)
writer_client.chat.chat.return_value = mock_streaming_response(
[mock_event_1, mock_event_2, mock_event_3, mock_event_4, mock_event_5]
)
messages = [{"role": "user", "content": [{"text": "test"}]}]
response = model.stream(messages, None, None)
# Consume the response
[event async for event in response]
expected_request = {
"model": model_id,
"messages": [{"role": "user", "content": [{"text": "test", "type": "text"}]}],
"stream": True,
"stream_options": {"include_usage": True},
}
writer_client.chat.chat.assert_called_once_with(**expected_request)
# Ensure no warnings emitted
assert len(captured_warnings) == 0
@pytest.mark.asyncio
async def test_tool_choice_not_supported_warns(writer_client, model, model_id, captured_warnings, alist):
mock_delta = unittest.mock.Mock(content="content", tool_calls=None)
mock_usage = unittest.mock.Mock(prompt_tokens=10, completion_tokens=20, total_tokens=30)
mock_event_1 = unittest.mock.Mock(spec=[])
mock_event_2 = unittest.mock.Mock(choices=[])
mock_event_3 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason=None, delta=mock_delta)])
mock_event_4 = unittest.mock.Mock(choices=[unittest.mock.Mock(finish_reason="stop", delta=mock_delta)])
mock_event_5 = unittest.mock.Mock(usage=mock_usage)
writer_client.chat.chat.return_value = mock_streaming_response(
[mock_event_1, mock_event_2, mock_event_3, mock_event_4, mock_event_5]
)
messages = [{"role": "user", "content": [{"text": "test"}]}]
response = model.stream(messages, None, None, tool_choice={"auto": {}})
# Consume the response
await alist(response)
expected_request = {
"model": model_id,
"messages": [{"role": "user", "content": [{"text": "test", "type": "text"}]}],
"stream": True,
"stream_options": {"include_usage": True},
}
writer_client.chat.chat.assert_called_once_with(**expected_request)
# Ensure expected warning is invoked
assert len(captured_warnings) == 1
assert "ToolChoice was provided to this provider but is not supported" in str(captured_warnings[0].message)
def test_config_validation_warns_on_unknown_keys(writer_client, captured_warnings):
"""Test that unknown config keys emit a warning."""
WriterModel({"api_key": "test"}, model_id="test-model", invalid_param="test")
assert len(captured_warnings) == 1
assert "Invalid configuration parameters" in str(captured_warnings[0].message)
assert "invalid_param" in str(captured_warnings[0].message)
def test_update_config_validation_warns_on_unknown_keys(model, captured_warnings):
"""Test that update_config warns on unknown keys."""
model.update_config(wrong_param="test")
assert len(captured_warnings) == 1
assert "Invalid configuration parameters" in str(captured_warnings[0].message)
assert "wrong_param" in str(captured_warnings[0].message)
def test_format_request_filters_s3_source_image(model, caplog):
"""Test that images with Location sources are filtered out with warning."""
caplog.set_level(logging.WARNING, logger="strands.models.writer")
messages = [
{
"role": "user",
"content": [
{"text": "look at this image"},
{
"image": {
"format": "png",
"source": {"location": {"type": "s3", "uri": "s3://my-bucket/image.png"}},
},
},
],
},
]
request = model.format_request(messages)
# Image with S3 source should be filtered, text should remain
formatted_messages = request["messages"]
user_content = formatted_messages[0]["content"]
assert len(user_content) == 1
assert user_content[0]["type"] == "text"
assert "Location sources are not supported by Writer" in caplog.text
def test_format_request_filters_location_source_document(model, caplog):
"""Test that documents with Location sources are filtered out with warning."""
caplog.set_level(logging.WARNING, logger="strands.models.writer")
messages = [
{
"role": "user",
"content": [
{"text": "analyze this document"},
{
"document": {
"format": "pdf",
"name": "report.pdf",
"source": {"location": {"type": "s3", "uri": "s3://my-bucket/report.pdf"}},
},
},
{
"document": {
"format": "pdf",
"name": "report.pdf",
"source": {"location": {"type": "s3", "uri": "s3://my-bucket/report.pdf"}},
},
},
],
},
]
request = model.format_request(messages)
# Document with S3 source should be filtered, text should remain
formatted_messages = request["messages"]
user_content = formatted_messages[0]["content"]
assert len(user_content) == 1
assert user_content[0]["type"] == "text"
assert "Location sources are not supported by Writer" in caplog.text