-
-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathtest_formatting.py
143 lines (109 loc) · 3.7 KB
/
test_formatting.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
import pytest
from jinja2.exceptions import SecurityError
from instructor.templating import handle_templating
def test_handle_insecure_template():
with pytest.raises(SecurityError):
kwargs = {
"messages": [
{
"role": "user",
"content": "{{ self.__init__.__globals__.__builtins__.__import__('os').system('ls') }} {{ variable }}",
}
]
}
context = {"variable": "test"}
handle_templating(kwargs, context)
def test_handle_templating_with_context():
kwargs = {"messages": [{"role": "user", "content": "Hello {{ name }}!"}]}
context = {"name": "Alice"}
result = handle_templating(kwargs, context)
assert result == {"messages": [{"role": "user", "content": "Hello Alice!"}]}
def test_handle_templating_without_context():
kwargs = {"messages": [{"role": "user", "content": "Hello {{ name }}!"}]}
result = handle_templating(kwargs)
assert result == kwargs
def test_handle_templating_with_anthropic_format():
kwargs = {
"messages": [
{"role": "user", "content": [{"type": "text", "text": "Hello {{ name }}!"}]}
]
}
context = {"name": "Bob"}
result = handle_templating(kwargs, context)
assert result == {
"messages": [
{"role": "user", "content": [{"type": "text", "text": "Hello Bob!"}]}
]
}
def test_handle_templating_with_mixed_content():
kwargs = {
"messages": [
{"role": "user", "content": "Hello {{ name }}!"},
{
"role": "assistant",
"content": [{"type": "text", "text": "Nice to meet you, {{ name }}!"}],
},
]
}
context = {"name": "Charlie"}
result = handle_templating(kwargs, context)
assert result == {
"messages": [
{"role": "user", "content": "Hello Charlie!"},
{
"role": "assistant",
"content": [{"type": "text", "text": "Nice to meet you, Charlie!"}],
},
]
}
def test_handle_templating_with_secret_context():
from pydantic import BaseModel, SecretStr
class UserContext(BaseModel):
name: str
address: SecretStr
kwargs = {
"messages": [
{
"role": "user",
"content": "{{ user.name }}'s address is '{{ user.address.get_secret_value() }}'",
}
]
}
context = {
"user": UserContext(
name="Jason", address=SecretStr("123 Secret St, Hidden City")
)
}
result = handle_templating(kwargs, context)
assert result == {
"messages": [
{
"role": "user",
"content": "Jason's address is '123 Secret St, Hidden City'",
}
]
}
# Ensure the original SecretStr is not exposed when rendered
assert str(context["user"].address) == "**********"
def test_handle_templating_with_cohere_format():
kwargs = {
"message": "Hello {{ name }}!",
"chat_history": [{"message": "Previous message to {{ name }}"}],
}
context = {"name": "David"}
result = handle_templating(kwargs, context)
assert result == {
"message": "Hello David!",
"chat_history": [{"message": "Previous message to David"}],
}
def test_handle_templating_with_gemini_format():
kwargs = {
"contents": [
{"role": "user", "parts": ["Hello {{ name }}!", "How are you {{ name }}?"]}
]
}
context = {"name": "Eve"}
result = handle_templating(kwargs, context)
assert result == {
"contents": [{"role": "user", "parts": ["Hello Eve!", "How are you Eve?"]}]
}