-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_process_response.py
More file actions
132 lines (114 loc) · 5.1 KB
/
test_process_response.py
File metadata and controls
132 lines (114 loc) · 5.1 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
from typing_extensions import TypedDict
from pydantic import BaseModel
from instructor.processing.response import handle_response_model
from instructor.providers.bedrock.utils import _prepare_bedrock_converse_kwargs_internal
def test_typed_dict_conversion() -> None:
class User(TypedDict): # type: ignore
name: str
age: int
_, user_tool_definition = handle_response_model(User)
class User(BaseModel):
name: str
age: int
_, pydantic_user_tool_definition = handle_response_model(User)
assert user_tool_definition == pydantic_user_tool_definition
def test_openai_to_bedrock_conversion() -> None:
"""OpenAI-style input should be fully converted to Bedrock format."""
call_kwargs = {
"model": "anthropic.claude-3-haiku-20240307-v1:0",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Extract: Jason is 22 years old"},
{"role": "assistant", "content": "Sure! Jason is 22."},
],
}
result = _prepare_bedrock_converse_kwargs_internal(call_kwargs)
assert "model" not in result
assert result["modelId"] == "anthropic.claude-3-haiku-20240307-v1:0"
assert result["system"] == [{"text": "You are a helpful assistant."}]
assert len(result["messages"]) == 2
assert result["messages"][0]["role"] == "user"
assert result["messages"][0]["content"] == [
{"text": "Extract: Jason is 22 years old"}
]
assert result["messages"][1]["role"] == "assistant"
assert result["messages"][1]["content"] == [{"text": "Sure! Jason is 22."}]
def test_bedrock_native_preserved() -> None:
"""Bedrock-native input should be preserved as-is."""
call_kwargs = {
"modelId": "anthropic.claude-3-haiku-20240307-v1:0",
"system": [{"text": "You are a helpful assistant."}],
"messages": [
{"role": "user", "content": [{"text": "Extract: Jason is 22 years old"}]},
{"role": "assistant", "content": [{"text": "Sure! Jason is 22."}]},
],
}
result = _prepare_bedrock_converse_kwargs_internal(call_kwargs)
assert result["system"] == [{"text": "You are a helpful assistant."}]
assert len(result["messages"]) == 2
assert result["messages"][0]["content"] == [
{"text": "Extract: Jason is 22 years old"}
]
assert result["messages"][1]["content"] == [{"text": "Sure! Jason is 22."}]
def test_mixed_openai_and_bedrock() -> None:
"""Mixed input: OpenAI-style is converted, Bedrock-native is preserved."""
call_kwargs = {
"modelId": "anthropic.claude-3-haiku-20240307-v1:0",
"system": [{"text": "You are a helpful assistant."}],
"messages": [
{
"role": "user",
"content": "Extract: Jason is 22 years old",
}, # OpenAI style
{
"role": "assistant",
"content": [{"text": "Sure! Jason is 22."}],
}, # Bedrock style
],
}
result = _prepare_bedrock_converse_kwargs_internal(call_kwargs)
assert result["modelId"] == "anthropic.claude-3-haiku-20240307-v1:0"
assert result["system"] == [{"text": "You are a helpful assistant."}]
assert len(result["messages"]) == 2
# OpenAI-style user message converted
assert result["modelId"] == "anthropic.claude-3-haiku-20240307-v1:0"
assert result["messages"][0]["content"] == [
{"text": "Extract: Jason is 22 years old"}
]
# Bedrock-style assistant message preserved
assert result["messages"][1]["content"] == [{"text": "Sure! Jason is 22."}]
def test_bedrock_round_trip() -> None:
"""Bedrock input should be unchanged after round-trip through the function."""
call_kwargs = {
"modelId": "anthropic.claude-3-haiku-20240307-v1:0",
"system": [{"text": "Bedrock system."}],
"messages": [
{"role": "user", "content": [{"text": "Bedrock user message."}]},
],
}
import copy
original = copy.deepcopy(call_kwargs)
result = _prepare_bedrock_converse_kwargs_internal(call_kwargs)
assert result == original
def test_empty_and_missing_content() -> None:
"""Empty messages and missing content should be handled gracefully."""
# Empty messages
call_kwargs = {"messages": []}
result = _prepare_bedrock_converse_kwargs_internal(call_kwargs)
assert result["messages"] == []
# Message with no content
call_kwargs = {"messages": [{"role": "user"}]}
result = _prepare_bedrock_converse_kwargs_internal(call_kwargs)
assert result["messages"][0]["role"] == "user"
# Should not add a content key if not present
assert "content" not in result["messages"][0]
def test_bedrock_invalid_content_format() -> None:
"""Invalid content types should raise ValueError."""
call_kwargs = {
"messages": [{"role": "user", "content": 12345}] # Invalid content type
}
try:
_prepare_bedrock_converse_kwargs_internal(call_kwargs)
raise AssertionError("Should have raised ValueError")
except ValueError as e:
assert "Unsupported message content type for Bedrock" in str(e)