Skip to content

Commit bd18ac1

Browse files
author
Val Brodsky
committed
Validate classifications
1 parent 6f3a526 commit bd18ac1

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

libs/labelbox/src/labelbox/schema/tool_building/prompt_issue_tool.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ def __post_init__(self):
4848
if self.name.strip() == "":
4949
raise ValueError("Name cannot be empty")
5050

51+
if not self._validate_classifications(self.classifications):
52+
raise ValueError("Only one checklist classification is supported")
53+
54+
def __setattr__(self, name, value):
55+
if name == "classifications" and not self._validate_classifications(
56+
value
57+
):
58+
raise ValueError("Classifications are immutable")
59+
object.__setattr__(self, name, value)
60+
61+
def _validate_classifications(
62+
self, classifications: List[Classification]
63+
) -> bool:
64+
if (
65+
len(classifications) != 1
66+
or classifications[0].class_type != Classification.Type.CHECKLIST
67+
):
68+
return False
69+
return True
70+
5171
def asdict(self) -> Dict[str, Any]:
5272
return {
5373
"tool": self.type.value,
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import pytest
2+
3+
from labelbox.schema.tool_building.classification import Classification
4+
from labelbox.schema.tool_building.prompt_issue_tool import PromptIssueTool
5+
6+
7+
def test_as_dict():
8+
tool = PromptIssueTool(name="Prompt Issue Tool")
9+
10+
# Get the dictionary representation
11+
tool_dict = tool.asdict()
12+
expected_dict = {
13+
"tool": "prompt-issue",
14+
"name": "Prompt Issue Tool",
15+
"required": False,
16+
"schemaNodeId": None,
17+
"featureSchemaId": None,
18+
"classifications": [
19+
{
20+
"type": "checklist",
21+
"instructions": "prompt_issue",
22+
"name": "prompt_issue",
23+
"required": False,
24+
"options": [
25+
{
26+
"schemaNodeId": None,
27+
"featureSchemaId": None,
28+
"label": "This prompt cannot be rated (eg. contains PII, a nonsense prompt, a foreign language, or other scenario that makes the responses impossible to assess reliably). However, if you simply do not have expertise to tackle this prompt, please skip the task; do not mark it as not rateable.",
29+
"value": "not_rateable",
30+
"options": [],
31+
},
32+
{
33+
"schemaNodeId": None,
34+
"featureSchemaId": None,
35+
"label": "This prompt contains a false, offensive, or controversial premise (eg. “why does 1+1=3”?)",
36+
"value": "false_offensive_controversial",
37+
"options": [],
38+
},
39+
{
40+
"schemaNodeId": None,
41+
"featureSchemaId": None,
42+
"label": "This prompt is not self-contained, i.e. the prompt cannot be understood without additional context about previous turns, account information or images.",
43+
"value": "not_self_contained",
44+
"options": [],
45+
},
46+
],
47+
"schemaNodeId": None,
48+
"featureSchemaId": None,
49+
"scope": "global",
50+
}
51+
],
52+
"color": None,
53+
}
54+
assert tool_dict == expected_dict
55+
56+
with pytest.raises(ValueError):
57+
tool.classifications = [
58+
Classification(Classification.Type.TEXT, "prompt_issue")
59+
]
60+
61+
with pytest.raises(ValueError):
62+
tool.classifications = "abcd"
63+
64+
# Test that we can modify the classification options
65+
classification = tool.classifications[0]
66+
classification.options.pop()
67+
classification.options[0].label = "test"
68+
classification.options[0].value == "test_value"
69+
tool.classifications = [classification]
70+
assert tool.classifications == [classification]

0 commit comments

Comments
 (0)