|
| 1 | +import warnings |
| 2 | +from dataclasses import dataclass, field |
| 3 | +from typing import Any, Dict, List, Optional |
| 4 | + |
| 5 | +from labelbox.schema.tool_building.tool_type import ToolType |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class StepReasoningVariant: |
| 10 | + id: int |
| 11 | + name: str |
| 12 | + |
| 13 | + def asdict(self) -> Dict[str, Any]: |
| 14 | + return {"id": self.id, "name": self.name} |
| 15 | + |
| 16 | + |
| 17 | +@dataclass |
| 18 | +class IncorrectStepReasoningVariant: |
| 19 | + id: int |
| 20 | + name: str |
| 21 | + regenerate_conversations_after_incorrect_step: Optional[bool] = True |
| 22 | + rate_alternative_responses: Optional[bool] = False |
| 23 | + |
| 24 | + def asdict(self) -> Dict[str, Any]: |
| 25 | + actions = [] |
| 26 | + if self.regenerate_conversations_after_incorrect_step: |
| 27 | + actions.append("regenerateSteps") |
| 28 | + if self.rate_alternative_responses: |
| 29 | + actions.append("generateAndRateAlternativeSteps") |
| 30 | + return {"id": self.id, "name": self.name, "actions": actions} |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def from_dict( |
| 34 | + cls, dictionary: Dict[str, Any] |
| 35 | + ) -> "IncorrectStepReasoningVariant": |
| 36 | + return cls( |
| 37 | + id=dictionary["id"], |
| 38 | + name=dictionary["name"], |
| 39 | + regenerate_conversations_after_incorrect_step="regenerateSteps" |
| 40 | + in dictionary.get("actions", []), |
| 41 | + rate_alternative_responses="generateAndRateAlternativeSteps" |
| 42 | + in dictionary.get("actions", []), |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +def _create_correct_step() -> StepReasoningVariant: |
| 47 | + return StepReasoningVariant( |
| 48 | + id=StepReasoningVariants.CORRECT_STEP_ID, name="Correct" |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def _create_neutral_step() -> StepReasoningVariant: |
| 53 | + return StepReasoningVariant( |
| 54 | + id=StepReasoningVariants.NEUTRAL_STEP_ID, name="Neutral" |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +def _create_incorrect_step() -> IncorrectStepReasoningVariant: |
| 59 | + return IncorrectStepReasoningVariant( |
| 60 | + id=StepReasoningVariants.INCORRECT_STEP_ID, name="Incorrect" |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@dataclass |
| 65 | +class StepReasoningVariants: |
| 66 | + """ |
| 67 | + This class is used to define the possible options for evaluating a step |
| 68 | + Currently the options are correct, neutral, and incorrect |
| 69 | + """ |
| 70 | + |
| 71 | + CORRECT_STEP_ID = 0 |
| 72 | + NEUTRAL_STEP_ID = 1 |
| 73 | + INCORRECT_STEP_ID = 2 |
| 74 | + |
| 75 | + correct_step: StepReasoningVariant = field( |
| 76 | + default_factory=_create_correct_step |
| 77 | + ) |
| 78 | + neutral_step: StepReasoningVariant = field( |
| 79 | + default_factory=_create_neutral_step |
| 80 | + ) |
| 81 | + incorrect_step: IncorrectStepReasoningVariant = field( |
| 82 | + default_factory=_create_incorrect_step |
| 83 | + ) |
| 84 | + |
| 85 | + def asdict(self): |
| 86 | + return [ |
| 87 | + self.correct_step.asdict(), |
| 88 | + self.neutral_step.asdict(), |
| 89 | + self.incorrect_step.asdict(), |
| 90 | + ] |
| 91 | + |
| 92 | + @classmethod |
| 93 | + def from_dict(cls, dictionary: List[Dict[str, Any]]): |
| 94 | + correct_step = None |
| 95 | + neutral_step = None |
| 96 | + incorrect_step = None |
| 97 | + |
| 98 | + for variant in dictionary: |
| 99 | + if variant["id"] == cls.CORRECT_STEP_ID: |
| 100 | + correct_step = StepReasoningVariant(**variant) |
| 101 | + elif variant["id"] == cls.NEUTRAL_STEP_ID: |
| 102 | + neutral_step = StepReasoningVariant(**variant) |
| 103 | + elif variant["id"] == cls.INCORRECT_STEP_ID: |
| 104 | + incorrect_step = IncorrectStepReasoningVariant.from_dict( |
| 105 | + variant |
| 106 | + ) |
| 107 | + |
| 108 | + if not all([correct_step, neutral_step, incorrect_step]): |
| 109 | + raise ValueError("Invalid step reasoning variants") |
| 110 | + |
| 111 | + return cls( |
| 112 | + correct_step=correct_step, # type: ignore |
| 113 | + neutral_step=neutral_step, # type: ignore |
| 114 | + incorrect_step=incorrect_step, # type: ignore |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +@dataclass |
| 119 | +class StepReasoningDefinition: |
| 120 | + variants: StepReasoningVariants = field( |
| 121 | + default_factory=StepReasoningVariants |
| 122 | + ) |
| 123 | + version: int = field(default=1) |
| 124 | + title: Optional[str] = None |
| 125 | + value: Optional[str] = None |
| 126 | + |
| 127 | + def asdict(self) -> Dict[str, Any]: |
| 128 | + result = {"variants": self.variants.asdict(), "version": self.version} |
| 129 | + if self.title is not None: |
| 130 | + result["title"] = self.title |
| 131 | + if self.value is not None: |
| 132 | + result["value"] = self.value |
| 133 | + return result |
| 134 | + |
| 135 | + @classmethod |
| 136 | + def from_dict(cls, dictionary: Dict[str, Any]) -> "StepReasoningDefinition": |
| 137 | + variants = StepReasoningVariants.from_dict(dictionary["variants"]) |
| 138 | + title = dictionary.get("title", None) |
| 139 | + value = dictionary.get("value", None) |
| 140 | + return cls(variants=variants, title=title, value=value) |
| 141 | + |
| 142 | + |
| 143 | +@dataclass |
| 144 | +class StepReasoningTool: |
| 145 | + """ |
| 146 | + Use this class in OntologyBuilder to create a tool for step reasoning |
| 147 | + The definition field lists the possible options to evaulate a step |
| 148 | + """ |
| 149 | + |
| 150 | + name: str |
| 151 | + type: ToolType = field(default=ToolType.STEP_REASONING, init=False) |
| 152 | + required: bool = False |
| 153 | + schema_id: Optional[str] = None |
| 154 | + feature_schema_id: Optional[str] = None |
| 155 | + color: Optional[str] = None |
| 156 | + definition: StepReasoningDefinition = field( |
| 157 | + default_factory=StepReasoningDefinition |
| 158 | + ) |
| 159 | + |
| 160 | + def __post_init__(self): |
| 161 | + warnings.warn( |
| 162 | + "This feature is experimental and subject to change.", |
| 163 | + ) |
| 164 | + |
| 165 | + def reset_regenerate_conversations_after_incorrect_step(self): |
| 166 | + """ |
| 167 | + For live models, the default acation will invoke the model to generate alternatives if a step is marked as incorrect |
| 168 | + This method will reset the action to not regenerate the conversation |
| 169 | + """ |
| 170 | + self.definition.variants.incorrect_step.regenerate_conversations_after_incorrect_step = False |
| 171 | + |
| 172 | + def set_rate_alternative_responses(self): |
| 173 | + """ |
| 174 | + For live models, will require labelers to rate the alternatives generated by the model |
| 175 | + """ |
| 176 | + self.definition.variants.incorrect_step.rate_alternative_responses = ( |
| 177 | + True |
| 178 | + ) |
| 179 | + |
| 180 | + def asdict(self) -> Dict[str, Any]: |
| 181 | + return { |
| 182 | + "tool": self.type.value, |
| 183 | + "name": self.name, |
| 184 | + "required": self.required, |
| 185 | + "schemaNodeId": self.schema_id, |
| 186 | + "featureSchemaId": self.feature_schema_id, |
| 187 | + "definition": self.definition.asdict(), |
| 188 | + } |
| 189 | + |
| 190 | + @classmethod |
| 191 | + def from_dict(cls, dictionary: Dict[str, Any]) -> "StepReasoningTool": |
| 192 | + return cls( |
| 193 | + name=dictionary["name"], |
| 194 | + schema_id=dictionary.get("schemaNodeId", None), |
| 195 | + feature_schema_id=dictionary.get("featureSchemaId", None), |
| 196 | + required=dictionary.get("required", False), |
| 197 | + definition=StepReasoningDefinition.from_dict( |
| 198 | + dictionary["definition"] |
| 199 | + ), |
| 200 | + ) |
0 commit comments