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
|
import pytest
import os
import pathlib
import base64
import json
from azure.ai.evaluation._common.utils import nltk_tokenize
from azure.ai.evaluation._common.utils import validate_conversation
from azure.ai.evaluation._exceptions import EvaluationException
@pytest.mark.unittest
class TestUtils:
def test_nltk_tokenize(self):
# Test with English text
text = "The capital of China is Beijing."
tokens = nltk_tokenize(text)
assert tokens == ["The", "capital", "of", "China", "is", "Beijing", "."]
# Test with Multi-language text
text = "The capital of China is 北京."
tokens = nltk_tokenize(text)
assert tokens == ["The", "capital", "of", "China", "is", "北京", "."]
def convert_json_list_to_jsonl(self, project_scope, azure_cred):
parent = pathlib.Path(__file__).parent.resolve()
path = os.path.join(parent, "data")
image_path = os.path.join(path, "image1.jpg")
with pathlib.Path(image_path).open("rb") as image_file:
encoded_image = base64.b64encode(image_file.read()).decode("utf-8")
conversation = [
{
"role": "system",
"content": [
{"type": "text", "text": "This is a nature boardwalk at the University of Wisconsin-Madison."}
],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Can you describe this image?"},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{encoded_image}"}},
],
},
]
messages = [{"messages": conversation}]
datafile_jsonl_path = os.path.join(path, "datafile.jsonl")
with open(datafile_jsonl_path, "w") as outfile:
for json_obj in messages:
json_line = json.dumps(json_obj)
outfile.write(json_line + "\n")
def test_messages_with_one_assistant_message(self):
conversation = {
"messages": [
{
"role": "system",
"content": [
{"type": "text", "text": "This is a nature boardwalk at the University of Wisconsin-Madison."}
],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Can you describe this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
},
},
],
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The image shows a man with short brown hair smiling, wearing a dark-colored shirt.",
}
],
},
]
}
validate_conversation(conversation=conversation)
def test_messages_with_missing_assistant_message(self):
conversation = {
"messages": [
{
"role": "system",
"content": [
{"type": "text", "text": "This is a nature boardwalk at the University of Wisconsin-Madison."}
],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Can you describe this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
},
},
],
},
]
}
try:
validate_conversation(conversation=conversation)
except EvaluationException as ex:
assert ex.message in "Assistant role required in one of the messages."
def test_messages_with_missing_user_message(self):
conversation = {
"messages": [
{
"role": "system",
"content": [
{"type": "text", "text": "This is a nature boardwalk at the University of Wisconsin-Madison."}
],
},
{
"role": "assistant",
"content": [
{"type": "text", "text": "Here is the picture you requested"},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
},
},
],
},
]
}
try:
validate_conversation(conversation=conversation)
except EvaluationException as ex:
assert ex.message in "User role required in one of the messages."
def test_messages_with_more_than_one_assistant_message(self):
conversation = {
"messages": [
{
"role": "system",
"content": [
{"type": "text", "text": "This is a nature boardwalk at the University of Wisconsin-Madison."}
],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Can you describe this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
},
},
],
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The image shows a man with short brown hair smiling, wearing a dark-colored shirt.",
}
],
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The image shows a man with short brown hair smiling, wearing a dark-colored shirt.",
}
],
},
]
}
try:
validate_conversation(conversation=conversation)
except EvaluationException as ex:
assert (
ex.message
in "Evaluators for multimodal conversations only support single turn. User and assistant role expected as the only role in each message."
)
def test_messages_multi_turn(self):
conversation = {
"messages": [
{
"role": "system",
"content": [
{"type": "text", "text": "This is a nature boardwalk at the University of Wisconsin-Madison."}
],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Can you describe this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
},
},
],
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The image shows a man with short brown hair smiling, wearing a dark-colored shirt.",
}
],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Okay, try again with this image"},
{
"type": "image_url",
"image_url": {
"url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
},
},
],
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The image shows a same man with short brown hair smiling, wearing a dark-colored shirt.",
}
],
},
]
}
try:
validate_conversation(conversation=conversation)
except EvaluationException as ex:
assert (
ex.message
in "Evaluators for multimodal conversations only support single turn. User and assistant role expected as the only role in each message."
)
|