Upload formats.py with huggingface_hub
Browse files11234567891041112131415891011121314151617181920212223242526272829161718192021222324252627282930313233343536373839404142434445464748493132333435363738395051525354555657585960616263646566676869707172737475767778798081824243444546478384858687498889909192935152535455565758596061626364656667686970717273747576777879808182949596979899100101102103104105106107108109110111from .artifact import Artifactfrom typing import ( Any, Dict, List, Optional,)
from .operator import StreamInstanceOperatorfrom .type_utils import isoftype
class Format(Artifact):
class Format(StreamInstanceOperator): pass
class SizeLimitingFormat(Format): size_limiter: Artifact = None
class ICLFormat(SizeLimitingFormat): prefix: str = "" input_prefix: str = "" output_prefix: str = "" target_prefix: str = " " instruction_prefix: str = "" input_output_separator: str = "\n" demo_separator: str = "\n\n" suffix: str = "" add_instruction_at_start: bool = True add_instruction_after_demos: bool = False
def single_source_str(self, source): return ( self.input_prefix + source + self.input_output_separator + self.output_prefixclass SystemFormat(Format): r"""Generates the whole input to the model, from constant strings that are given as args, and from values found in specified fields of the instance.
SystemFormat expects the input instance to contain: 1. A field named "source" whose value is a string verbalizing the original values in the instance (as read from the source dataset), in the context of the underlying task. 2. A field named "instruction" that contains a (non-None) string. 3. A field named with the value in arg 'demos_field', containing a list of dicts, each dict with fields "source" and "target", representing a single demo.
SystemFormat formats the above fields into a single string to be inputted to the model. This string overwrites field "source" of the instance. Formatting is driven by two args: 'demo_format' and 'model_input_format'. SystemFormat also pops field "instruction" and the field containing the demos out from the input instance.
Args: demos_field (str): the name of the field that contains the demos, being a list of dicts, each with "source" and "target" keys demo_format (str): formatting string for a single demo, combining fields "source" and "target" model_input_format (str) overall product format, combining instruction and source (as read from fields "instruction" and "source" of the input instance), together with demos (as formatted into one string)
Example: when input instance: { "source": "1+1", "target": "2", "instruction": "Solve the math exercises.", "demos": [{"source": "1+2", "target": "3"}, {"source": "4-2", "target": "2"}] } is process-ed by system_format = SystemFormat( demos_field="demos", demo_format="Input: {source}\nOutput: {target}\n\n", model_input_format="Instruction: {instruction}\n\n{demos}Input: {source}\nOutput: ", )
def single_source_str_with_instruction(self, source, instruction): return ( self.input_prefix + instruction + self.demo_separator + source + self.input_output_separator + self.output_prefix the resulting instance is: { "target": "2", "source": "Instruction: Solve the math exercises.\n\nInput: 1+2\nOutput: 3\n\nInput: 4-2\nOutput: 2\n\nInput: 1+1\nOutput: ", } """
demos_field: str = "demos" demo_format: str = ( "{source}\n{target}\n\n" # example: "User: {source}\nAgent: {target}\n\n" ) model_input_format: str = "{instruction}{demos}{source}\n"
@staticmethod def _retrieve_field_and_assert_not_none(instance, field_name) -> str: if field_name is not None and field_name in instance: field_value = instance[field_name] assert ( field_value is not None ), f"Value in field '{field_name}' should not be none. Received instance: {instance}" return field_value return ""
def process( self, instance: Dict[str, Any], stream_name: Optional[str] = None ) -> Dict[str, Any]: assert ( "source" in instance ), f"field 'source' is expected to be in the input instance. Received instance: {instance}" source = self._retrieve_field_and_assert_not_none( instance=instance, field_name="source" )
def format(self, instance, demos_instances=None): if demos_instances is None: demos_instances = [] source = self.prefix
instruction = "" instruction = self._retrieve_field_and_assert_not_none( instance=instance, field_name="instruction" ) # pop "instruction" from instance if "instruction" in instance: instruction = instance.pop("instruction") instance.pop("instruction")
demo_instances = [] if self.demos_field is not None and self.demos_field in instance: demos = instance[self.demos_field] assert ( "instruction" != None ), f"instruction field can not be none : {instance}"
if self.add_instruction_at_start and instruction != "": source += self.instruction_prefix + instruction + self.demo_separator
if self.add_instruction_after_demos and instruction != "": query_str = self.single_source_str_with_instruction( instance["source"], instruction ) else: query_str = self.single_source_str(instance["source"])
for demo_instance in demos_instances: demo_str = ( self.single_source_str(demo_instance["source"]) + self.target_prefix + demo_instance["target"] + self.demo_separator )
if self.size_limiter is not None: if not self.size_limiter.check( source + demo_str + query_str + instance["target"] ): continue
source += demo_str
source += query_str source += self.suffix return source demos is not None and isoftype(demos, List[Dict[str, Any]]) ), f"A list of dict-s is expected in field '{self.demos_field}'. Received instance: {instance}" demo_instances = demos # pop demos from instance instance.pop(self.demos_field)
demos_string = "" for demo_instance in demo_instances: demo_str = self.demo_format.format(**demo_instance) demos_string += demo_str
output = self.model_input_format.format( instruction=instruction, demos=demos_string, source=source, ) instance["source"] = output return instanceformats.py+101-72