|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from typing import Any, Dict, List, Optional |
| 3 | + |
| 4 | +from labelbox.schema.ontology import Classification, Option |
| 5 | +from labelbox.schema.tool_building.tool_type import ToolType |
| 6 | + |
| 7 | + |
| 8 | +def _supported_classifications() -> List[Classification]: |
| 9 | + option_1_text = "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). If you simply do not have expertise to tackle this prompt, please skip the task; do not mark it as not rateable" |
| 10 | + option_2_text = 'This prompt contains a false, offensive, or controversial premise (eg. "why does 1+1=3"?)' |
| 11 | + option_3_text = "This prompt is not self-contained (i.e. the prompt cannot be understood without additional context about previous turns, account information or images)." |
| 12 | + options = [ |
| 13 | + Option(label=option_1_text, value="not_rateable"), |
| 14 | + Option(label=option_2_text, value="false_offensive_controversial"), |
| 15 | + Option(label=option_3_text, value="not_self_contained"), |
| 16 | + ] |
| 17 | + return [ |
| 18 | + Classification( |
| 19 | + class_type=Classification.Type.CHECKLIST, |
| 20 | + name="prompt_issue", |
| 21 | + options=options, |
| 22 | + ), |
| 23 | + ] |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class PromptIssueTool: |
| 28 | + """ |
| 29 | + Use this class in OntologyBuilder to create a tool for prompt rating |
| 30 | + It comes with a prebuild checklist of options, which a user can modify or override |
| 31 | + So essentially this is a tool with a prebuilt checklist classification |
| 32 | + """ |
| 33 | + |
| 34 | + name: str |
| 35 | + type: ToolType = field(default=ToolType.PROMPT_ISSUE, init=False) |
| 36 | + required: bool = False |
| 37 | + schema_id: Optional[str] = None |
| 38 | + feature_schema_id: Optional[str] = None |
| 39 | + color: Optional[str] = None |
| 40 | + classifications: List[Classification] = field( |
| 41 | + default_factory=_supported_classifications |
| 42 | + ) |
| 43 | + |
| 44 | + def __post_init__(self): |
| 45 | + if self.name.strip() == "": |
| 46 | + raise ValueError("Name cannot be empty") |
| 47 | + |
| 48 | + def asdict(self) -> Dict[str, Any]: |
| 49 | + return { |
| 50 | + "tool": self.type.value, |
| 51 | + "name": self.name, |
| 52 | + "required": self.required, |
| 53 | + "schemaNodeId": self.schema_id, |
| 54 | + "featureSchemaId": self.feature_schema_id, |
| 55 | + "classifications": [ |
| 56 | + classification.asdict() |
| 57 | + for classification in self.classifications |
| 58 | + ], |
| 59 | + "color": self.color, |
| 60 | + } |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def from_dict(cls, dictionary: Dict[str, Any]) -> "FactCheckingTool": |
| 64 | + return cls( |
| 65 | + name=dictionary["name"], |
| 66 | + schema_id=dictionary.get("schemaNodeId", None), |
| 67 | + feature_schema_id=dictionary.get("featureSchemaId", None), |
| 68 | + required=dictionary.get("required", False), |
| 69 | + classifications=[ |
| 70 | + Classification.from_dict(classification) |
| 71 | + for classification in dictionary["classifications"] |
| 72 | + ], |
| 73 | + color=dictionary.get("color", None), |
| 74 | + ) |
0 commit comments