Skip to content

Commit

Permalink
fix: DIA-1508: Fix sdk to support Prompts MultiSkill (#342)
Browse files Browse the repository at this point in the history
Co-authored-by: nik <nik@heartex.net>
Co-authored-by: hakan458 <hakan@heartex.com>
  • Loading branch information
3 people authored Oct 25, 2024
1 parent 0f4b7ae commit 238b0f4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/label_studio_sdk/label_interface/control_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,11 @@ def _resolve_to_name(self, to_name: Optional[str] = None) -> str:

return to_name
else:
if len(self.to_name) > 1:
raise Exception(
"Multiple to_name in control tag, specify to_name in function"
)
# TODO: "Pairwise" tag has multiple to_name
# if len(self.to_name) > 1:
# raise Exception(
# "Multiple to_name in control tag, specify to_name in function"
# )

return self.to_name[0]

Expand Down Expand Up @@ -494,13 +495,28 @@ class ChoicesTag(ControlTag):
_label_attr_name: str = "choices"
_value_class: Type[ChoicesValue] = ChoicesValue

@property
def is_multiple_choice(self):
return self.attr.get("choice") == "multiple"

def to_json_schema(self):
"""
Converts the current ChoicesTag instance into a JSON Schema.
Returns:
dict: A dictionary representing the JSON Schema compatible with OpenAPI 3.
"""
if self.is_multiple_choice:
return {
"type": "array",
"items": {
"type": "string",
"enum": self.labels,
},
"uniqueItems": True,
"description": f"Choices for {self.to_name[0]}",
}

return {
"type": "string",
"enum": self.labels,
Expand Down Expand Up @@ -813,9 +829,10 @@ class PairwiseTag(ControlTag):
_value_class: Type[PairwiseValue] = PairwiseValue
_label_attr_name: str = "selected"

def label(self, side):
def label(self, label):
""" """
value = PairwiseValue(selected=side)
value = PairwiseValue(selected=label)
# <Pairwise> tag has equal from_name and to_name, and string label that's not a list of strings
return Region(from_tag=self, to_tag=self, value=value)

def to_json_schema(self):
Expand Down
31 changes: 31 additions & 0 deletions tests/custom/test_interface/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,36 @@
{"sentiment": "Positive"},
{"sentiment": "Positive"}
),
# multiple choice
(
"""
<View>
<Choices name="sentiment" toName="doc" choice="multiple">
<Choice value="Positive" />
<Choice value="Negative" />
<Choice value="Neutral" />
</Choices>
<Text name="doc" value="$text" />
</View>
""",
{
"type": "object",
"properties": {
"sentiment": {
"description": "Choices for doc",
"type": "array",
"items": {
"type": "string",
"enum": ["Positive", "Negative", "Neutral"]
},
"uniqueItems": True,
}
},
"required": ["sentiment"]
},
{"sentiment": ["Positive", "Negative"]},
{"sentiment": ["Positive", "Negative"]}
),
# ner
(
"""
Expand Down Expand Up @@ -188,6 +218,7 @@
# ...
], ids=[
"simple_choices",
"multiple_choices",
"ner",
"classification_with_textarea",
"complex_interface"
Expand Down

0 comments on commit 238b0f4

Please sign in to comment.