Upload instructions.py with huggingface_hub
Browse files- instructions.py +5 -45
instructions.py
CHANGED
|
@@ -1,54 +1,14 @@
|
|
| 1 |
-
from
|
| 2 |
-
from typing import Any, Dict, Optional
|
| 3 |
|
| 4 |
-
from .collections import ListCollection
|
| 5 |
-
from .dataclass import NonPositionalField
|
| 6 |
-
from .operator import StreamInstanceOperator
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
"""The role of instruction is to add instruction to every instance.
|
| 11 |
|
| 12 |
-
Meaning the instruction is taking the instance and generating instruction field for it.
|
| 13 |
"""
|
| 14 |
|
| 15 |
-
skip_rendered_instance: bool = NonPositionalField(default=True)
|
| 16 |
-
|
| 17 |
-
def process(
|
| 18 |
-
self, instance: Dict[str, Any], stream_name: Optional[str] = None
|
| 19 |
-
) -> Dict[str, Any]:
|
| 20 |
-
if self.skip_rendered_instance:
|
| 21 |
-
if "instruction" in instance:
|
| 22 |
-
return instance
|
| 23 |
-
|
| 24 |
-
instance["instruction"] = self.get_instruction(instance)
|
| 25 |
-
|
| 26 |
-
return instance
|
| 27 |
-
|
| 28 |
-
@abstractmethod
|
| 29 |
-
def get_instruction(self, instance: Dict[str, object]) -> str:
|
| 30 |
-
pass
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
class TextualInstruction(Instruction):
|
| 34 |
text: str
|
| 35 |
|
| 36 |
-
def get_instruction(self
|
| 37 |
return self.text
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
class EmptyInstruction(Instruction):
|
| 41 |
-
def get_instruction(self, instance: Dict[str, object]) -> str:
|
| 42 |
-
return ""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
class InstructionsList(ListCollection):
|
| 46 |
-
def verify(self):
|
| 47 |
-
for instruction in self.items:
|
| 48 |
-
assert isinstance(instruction, Instruction)
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
class InstructionsDict(Dict):
|
| 52 |
-
def verify(self):
|
| 53 |
-
for _key, instruction in self.items():
|
| 54 |
-
assert isinstance(instruction, Instruction)
|
|
|
|
| 1 |
+
from .artifact import Artifact
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
class TextualInstruction(Artifact):
|
| 5 |
+
"""The role of TextualInstruction is to arrange potential instructions in the catalog, expressed as formatting strings.
|
| 6 |
|
| 7 |
+
The (formatted) instructions are added to the instances, in field named "instruction" via the Template Operator.
|
|
|
|
| 8 |
|
|
|
|
| 9 |
"""
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
text: str
|
| 12 |
|
| 13 |
+
def get_instruction(self) -> str:
|
| 14 |
return self.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|