-
Notifications
You must be signed in to change notification settings - Fork 817
Expand file tree
/
Copy pathtest_model_bedrock.py
More file actions
498 lines (377 loc) · 15.5 KB
/
test_model_bedrock.py
File metadata and controls
498 lines (377 loc) · 15.5 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
import time
import uuid
import pydantic
import pytest
import strands
from strands import Agent
from strands.models import BedrockModel
from strands.types.content import ContentBlock
@pytest.fixture
def system_prompt():
return "You are an AI assistant that uses & instead of ."
@pytest.fixture
def streaming_model():
return BedrockModel(
streaming=True,
)
@pytest.fixture
def non_streaming_model():
return BedrockModel(
streaming=False,
)
@pytest.fixture
def streaming_agent(streaming_model, system_prompt):
return Agent(
model=streaming_model,
system_prompt=system_prompt,
load_tools_from_directory=False,
)
@pytest.fixture
def non_streaming_agent(non_streaming_model, system_prompt):
return Agent(
model=non_streaming_model,
system_prompt=system_prompt,
load_tools_from_directory=False,
)
@pytest.fixture
def yellow_color():
class Color(pydantic.BaseModel):
"""Describes a color."""
name: str
@pydantic.field_validator("name", mode="after")
@classmethod
def lower(_, value):
return value.lower()
return Color(name="yellow")
def test_streaming_agent(streaming_agent):
"""Test agent with streaming model."""
result = streaming_agent("Hello!")
assert len(str(result)) > 0
def test_non_streaming_agent(non_streaming_agent):
"""Test agent with non-streaming model."""
result = non_streaming_agent("Hello!")
assert len(str(result)) > 0
@pytest.mark.asyncio
async def test_streaming_model_events(streaming_model, alist):
"""Test streaming model events."""
messages = [{"role": "user", "content": [{"text": "Hello"}]}]
# Call stream and collect events
events = await alist(streaming_model.stream(messages))
# Verify basic structure of events
assert any("messageStart" in event for event in events)
assert any("contentBlockDelta" in event for event in events)
assert any("messageStop" in event for event in events)
@pytest.mark.asyncio
async def test_non_streaming_model_events(non_streaming_model, alist):
"""Test non-streaming model events."""
messages = [{"role": "user", "content": [{"text": "Hello"}]}]
# Call stream and collect events
events = await alist(non_streaming_model.stream(messages))
# Verify basic structure of events
assert any("messageStart" in event for event in events)
assert any("contentBlockDelta" in event for event in events)
assert any("messageStop" in event for event in events)
def test_tool_use_streaming(streaming_model):
"""Test tool use with streaming model."""
tool_was_called = False
@strands.tool
def calculator(expression: str) -> float:
"""Calculate the result of a mathematical expression."""
nonlocal tool_was_called
tool_was_called = True
return eval(expression)
agent = Agent(model=streaming_model, tools=[calculator], load_tools_from_directory=False)
result = agent("What is 123 + 456?")
# Print the full message content for debugging
print("\nFull message content:")
import json
print(json.dumps(result.message["content"], indent=2))
assert tool_was_called
def test_tool_use_non_streaming(non_streaming_model):
"""Test tool use with non-streaming model."""
tool_was_called = False
@strands.tool
def calculator(expression: str) -> float:
"""Calculate the result of a mathematical expression."""
nonlocal tool_was_called
tool_was_called = True
return eval(expression)
agent = Agent(model=non_streaming_model, tools=[calculator], load_tools_from_directory=False)
agent("What is 123 + 456?")
assert tool_was_called
def test_structured_output_streaming(streaming_model):
"""Test structured output with streaming model."""
class Weather(pydantic.BaseModel):
time: str
weather: str
agent = Agent(model=streaming_model)
result = agent.structured_output(Weather, "The time is 12:00 and the weather is sunny")
assert isinstance(result, Weather)
assert result.time == "12:00"
assert result.weather == "sunny"
def test_structured_output_non_streaming(non_streaming_model):
"""Test structured output with non-streaming model."""
class Weather(pydantic.BaseModel):
time: str
weather: str
agent = Agent(model=non_streaming_model)
result = agent.structured_output(Weather, "The time is 12:00 and the weather is sunny")
assert isinstance(result, Weather)
assert result.time == "12:00"
assert result.weather == "sunny"
def test_invoke_multi_modal_input(streaming_agent, yellow_img):
content = [
{"text": "what is in this image"},
{
"image": {
"format": "png",
"source": {
"bytes": yellow_img,
},
},
},
]
result = streaming_agent(content)
text = result.message["content"][0]["text"].lower()
assert "yellow" in text
def test_document_citations(non_streaming_agent, letter_pdf):
content: list[ContentBlock] = [
{
"document": {
"name": "letter to shareholders",
"source": {"bytes": letter_pdf},
"citations": {"enabled": True},
"context": "This is a letter to shareholders",
"format": "pdf",
},
},
{"text": "What does the document say about artificial intelligence? Use citations to back up your answer."},
]
non_streaming_agent(content)
assert any("citationsContent" in content for content in non_streaming_agent.messages[-1]["content"])
# Validate message structure is valid in multi-turn
non_streaming_agent("What is your favorite part?")
def test_document_citations_streaming(streaming_agent, letter_pdf):
content: list[ContentBlock] = [
{
"document": {
"name": "letter to shareholders",
"source": {"bytes": letter_pdf},
"citations": {"enabled": True},
"context": "This is a letter to shareholders",
"format": "pdf",
},
},
{"text": "What does the document say about artificial intelligence? Use citations to back up your answer."},
]
streaming_agent(content)
assert any("citationsContent" in content for content in streaming_agent.messages[-1]["content"])
# Validate message structure is valid in multi-turn
streaming_agent("What is your favorite part?")
def test_structured_output_multi_modal_input(streaming_agent, yellow_img, yellow_color):
content = [
{"text": "Is this image red, blue, or yellow?"},
{
"image": {
"format": "png",
"source": {
"bytes": yellow_img,
},
},
},
]
tru_color = streaming_agent.structured_output(type(yellow_color), content)
exp_color = yellow_color
assert tru_color == exp_color
def test_redacted_content_handling():
"""Test redactedContent handling with thinking mode."""
bedrock_model = BedrockModel(
model_id="us.anthropic.claude-3-7-sonnet-20250219-v1:0",
additional_request_fields={
"thinking": {
"type": "enabled",
"budget_tokens": 2000,
}
},
)
agent = Agent(name="test_redact", model=bedrock_model)
# https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#example-working-with-redacted-thinking-blocks
result = agent(
"ANTHROPIC_MAGIC_STRING_TRIGGER_REDACTED_THINKING_46C9A13E193C177646C7398A98432ECCCE4C1253D5E2D82641AC0E52CC2876CB"
)
assert "reasoningContent" in result.message["content"][0]
assert "redactedContent" in result.message["content"][0]["reasoningContent"]
assert isinstance(result.message["content"][0]["reasoningContent"]["redactedContent"], bytes)
def test_reasoning_content_in_messages_with_thinking_disabled():
"""Test that messages with reasoningContent are accepted when thinking is explicitly disabled."""
# First, get a real reasoning response with thinking enabled
thinking_model = BedrockModel(
model_id="us.anthropic.claude-sonnet-4-20250514-v1:0",
additional_request_fields={
"thinking": {
"type": "enabled",
"budget_tokens": 1024,
}
},
)
agent_with_thinking = Agent(model=thinking_model)
result_with_thinking = agent_with_thinking("What is 2+2?")
# Verify we got reasoning content
assert "reasoningContent" in result_with_thinking.message["content"][0]
# Now create a model with thinking disabled and use the messages from the thinking session
disabled_model = BedrockModel(
model_id="us.anthropic.claude-sonnet-4-20250514-v1:0",
additional_request_fields={
"thinking": {
"type": "disabled",
}
},
)
# Use the conversation history that includes reasoning content
messages = agent_with_thinking.messages
agent_disabled = Agent(model=disabled_model, messages=messages)
result = agent_disabled("What about 3+3?")
assert result.stop_reason == "end_turn"
def test_multi_prompt_system_content():
"""Test multi-prompt system content blocks."""
system_prompt_content = [
{"text": "You are a helpful assistant."},
{"text": "Always be concise."},
{"text": "End responses with 'Done.'"},
]
agent = Agent(system_prompt=system_prompt_content, load_tools_from_directory=False)
# just verifying there is no failure
agent("Hello!")
def test_prompt_caching_with_5m_ttl():
"""Test prompt caching with 5 minute TTL and verify cache metrics.
This test verifies:
1. First call creates cache (cacheWriteInputTokens > 0)
2. Second call reads from cache (cacheReadInputTokens > 0)
Uses Claude Haiku 4.5 which supports TTL in CachePointBlock on Bedrock.
Older models (e.g. Claude Sonnet 4) reject the TTL field with a ValidationException.
"""
model = BedrockModel(
model_id="us.anthropic.claude-haiku-4-5-20251001-v1:0",
streaming=False,
)
# Use unique identifier to avoid cache conflicts between test runs
unique_id = str(uuid.uuid4())
# Minimum 4096 tokens required for caching with Haiku 4.5
large_context = f"Background information for test {unique_id}: " + ("This is important context. " * 1000)
system_prompt_with_cache = [
{"text": large_context},
{"cachePoint": {"type": "default", "ttl": "5m"}},
{"text": "You are a helpful assistant."},
]
agent = Agent(
model=model,
system_prompt=system_prompt_with_cache,
load_tools_from_directory=False,
)
# First call should create the cache (cache write)
result1 = agent("What is 2+2?")
assert len(str(result1)) > 0
# Verify cache write occurred on first call
assert result1.metrics.accumulated_usage.get("cacheWriteInputTokens", 0) > 0, (
"Expected cacheWriteInputTokens > 0 on first call"
)
# Second call should use the cached content (cache read)
result2 = agent("What is 3+3?")
assert len(str(result2)) > 0
# Verify cache read occurred on second call
assert result2.metrics.accumulated_usage.get("cacheReadInputTokens", 0) > 0, (
"Expected cacheReadInputTokens > 0 on second call"
)
def test_prompt_caching_with_1h_ttl():
"""Test prompt caching with 1 hour TTL and verify cache metrics.
Uses Claude Haiku 4.5 which supports 1hr TTL.
Uses unique content per test run to avoid cache conflicts with concurrent CI runs.
Even with 1hr TTL, unique content ensures cache entries don't interfere across tests.
"""
model = BedrockModel(
model_id="us.anthropic.claude-haiku-4-5-20251001-v1:0",
streaming=False,
)
# Use timestamp to ensure unique content per test run (avoids CI conflicts)
unique_id = str(int(time.time() * 1000000)) # microsecond timestamp
# Minimum 4096 tokens required for caching with Haiku 4.5
large_context = f"Background information for test {unique_id}: " + ("This is important context. " * 1000)
system_prompt_with_cache = [
{"text": large_context},
{"cachePoint": {"type": "default", "ttl": "1h"}},
{"text": "You are a helpful assistant."},
]
agent = Agent(
model=model,
system_prompt=system_prompt_with_cache,
load_tools_from_directory=False,
)
# First call should create the cache
result1 = agent("What is 2+2?")
assert len(str(result1)) > 0
# Verify cache write occurred
assert result1.metrics.accumulated_usage.get("cacheWriteInputTokens", 0) > 0, (
"Expected cacheWriteInputTokens > 0 on first call with 1h TTL"
)
# Second call should use the cached content
result2 = agent("What is 3+3?")
assert len(str(result2)) > 0
# Verify cache read occurred
assert result2.metrics.accumulated_usage.get("cacheReadInputTokens", 0) > 0, (
"Expected cacheReadInputTokens > 0 on second call with 1h TTL"
)
def test_prompt_caching_with_ttl_in_messages():
"""Test prompt caching with TTL in message content and verify cache metrics.
Uses Claude Haiku 4.5 which supports TTL in CachePointBlock on Bedrock.
Older models (e.g. Claude Sonnet 4) reject the TTL field with a ValidationException.
"""
model = BedrockModel(
model_id="us.anthropic.claude-haiku-4-5-20251001-v1:0",
streaming=False,
)
agent = Agent(model=model, load_tools_from_directory=False)
unique_id = str(uuid.uuid4())
# Minimum 4096 tokens required for caching with Haiku 4.5
large_text = f"Important context for test {unique_id}: " + ("This is critical information. " * 1000)
content_with_cache = [
{"text": large_text},
{"cachePoint": {"type": "default", "ttl": "5m"}},
{"text": "Based on the context above, what is 5+5?"},
]
# First call creates cache
result1 = agent(content_with_cache)
assert len(str(result1)) > 0
# Verify cache write in message content
assert result1.metrics.accumulated_usage.get("cacheWriteInputTokens", 0) > 0, (
"Expected cacheWriteInputTokens > 0 when caching message content"
)
# Subsequent call should use cache
result2 = agent("What about 10+10?")
assert len(str(result2)) > 0
# Verify cache read on subsequent call
assert result2.metrics.accumulated_usage.get("cacheReadInputTokens", 0) > 0, (
"Expected cacheReadInputTokens > 0 on subsequent call"
)
def test_prompt_caching_backward_compatibility_no_ttl(non_streaming_model):
"""Test that prompt caching works without TTL (backward compatibility).
Verifies that cache points work correctly when TTL is not specified,
maintaining backward compatibility with existing code.
"""
unique_id = str(uuid.uuid4())
large_context = f"Background information for test {unique_id}: " + ("This is important context. " * 200)
system_prompt_with_cache = [
{"text": large_context},
{"cachePoint": {"type": "default"}}, # No TTL specified
{"text": "You are a helpful assistant."},
]
agent = Agent(
model=non_streaming_model,
system_prompt=system_prompt_with_cache,
load_tools_from_directory=False,
)
result = agent("Hello!")
assert len(str(result)) > 0
# Verify cache write occurred even without TTL
assert result.metrics.accumulated_usage.get("cacheWriteInputTokens", 0) > 0, (
"Expected cacheWriteInputTokens > 0 even without TTL specified"
)